Nick
10/01/2020, 2:22 PMstreetsofboston
10/01/2020, 2:32 PMView
dependent on a ViewModel…
Instead, think about how Fragments and Activities handle this.
A Fragment or Activity receive a ViewModel and a View as input/dependency and this Fragment/Activity then observes properties on the ViewModel and manipulates the View accordingly.
View ------+
+--> Fragment/Activity
ViewModel -+
Make a similar type of class (a ‘controller’?) that will take (or get injected) a custom View and your custom-view related ViewModel like a Fragment or Activity.
MyCustomView --+
+--> MyCustomController
MyCustomViewModel --+
allan.conda
10/01/2020, 2:36 PMThis doesn’t feel right.What makes you feel so?
streetsofboston
10/01/2020, 2:39 PMclass SomeActivity : AppCompatActivity() {
...
val myCustomController: MyCustomController by lazy {
MyCustomController(findViewById(R.id.my_custom_view), MyCustomViewModel())
}
...
}
You could finagle the creation of the MyCustomController and its dependencies (MyCustomView and MyCustomViewModel) through dependency injection (dagger/koin/etc), if you want to remove some boiler plate.allan.conda
10/01/2020, 2:49 PMstreetsofboston
10/01/2020, 2:57 PMclass MyCustomView(private val viewModel: MyCustomViewModel): View() {...}
it would be
class MyCustomView : View() {…}
and a class MyCustomController(private val view: MyCustomView, private val viewModel: MyCustomViewModel)
where the MyCustomController plays the same role as a Fragment or Activity with respect to hooking up a ViewModel to a View.
You could use dagger/koin to reduce the boilerplate instantiations of MyCustomControllers.gildor
10/01/2020, 3:06 PMJoost Klitsie
10/01/2020, 3:17 PMNick
10/01/2020, 3:20 PMstreetsofboston
10/01/2020, 3:36 PMNick
10/01/2020, 3:36 PMgildor
10/01/2020, 3:39 PMNick
10/01/2020, 3:47 PMallan.conda
10/01/2020, 3:48 PMbut it requires quite a lot of custom logic and essentially own framework,I know 🙂 , that’s why I mentioned using something like Conductor to facilitate this, because this wasn’t how the Android Views were originally designed for. Additionally, Jetpack Compose is just around the corner. It doesn’t need Fragments or multiple Activities, and you can provide the viewModel from the top level Composable function
You put the ViewModel in a controllerThat could work also, but that’s no longer simple MVVM. It’s MVVM+Controller.
gildor
10/01/2020, 3:58 PMThe majority of our crashes and problems comes from lifecycle issues related to fragments. I want to avoid them if I can.Really? what kind crashes? i
Nick
10/01/2020, 3:58 PMgildor
10/01/2020, 3:59 PMthat’s why I mentioned using something like ConductorI’m not a big fan of conductor, for me it looks that they took all bad things from fragments, but yeah, made lifecycle more simple
allan.conda
10/01/2020, 4:00 PMgildor
10/01/2020, 4:00 PMallan.conda
10/01/2020, 4:00 PMI’m not a big fan of conductor, for me it looks that they take all bad things from fragments, but yeah, make lifecycle more simpleme neither 😄 I don’t like to depend on 3rd party libraries for something so important unless it’s really mainstream
Fatih
10/01/2020, 5:03 PM