This post has been de-listed
It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.
Hi, our project is using the VIPER architecture, with each View having its own BUILDER and PRESENTER. The builder is created with a viewmodel and we inject all our dependencies there. The builder then creates the presenter with those dependency.
We initially started off using KOIN as our dependency solution which worked relatively well, but decided to test migrate to dagger/hilt. The migration progress for the APP went relatively smooth as we were able to take advantage of the @ ViewModelInject that Hilt provided. However, I am currently stuck trying to migrate our dynamic feature model to the same structure as the rest of the app.
I am able to follow Google's documentation and create a dagger component to be injected into DFM but i am having extreme difficulty getting the dependencies inject into the viewmodel without creating factory boilerplates for each view's Builder.
Does anyone have any suggestions how i can dynamically create the viewmodels with the injection constructor without having to manually specify in each view?
Here is our base screenview where we initialize our Builder/Presenter:
abstract class ScreenView<BUILDER : ScreenBuilderBase, PRESENTER : ScreenPresenter<out RouterBase>>(u/LayoutRes protected val fragment: Int,private val builderClass: KClass<BUILDER>) : Fragment() {
lateinit var presenter: PRESENTERprotected lateinit var builder: BUILDERvar savedStateViewModelFactory: SavedStateViewModelFactory? = nulloverride fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {return inflater.inflate(fragment, container, false)}
@Suppress("UNCHECKED_CAST")override fun onViewCreated(view: View, savedInstanceState: Bundle?) {super.onViewCreated(view, savedInstanceState)val viewModelProvider: ViewModelProvider =savedStateViewModelFactory?.let { factory: SavedStateViewModelFactory ->ViewModelProvider(this, factory)} ?: ViewModelProvider(this)val builder: BUILDER = logDuration("ViewModelProvider.get(${builderClass.simpleName}}") {viewModelProvider.get(builderClass.java)}builder.setNavController(findNavController())presenter = builder.presenter as PRESENTER}
Subreddit
Post Details
- Posted
- 3 years ago
- Reddit URL
- View post on reddit.com
- External URL
- reddit.com/r/androiddev/...