https://kotlinlang.org logo
#android-architecture
Title
# android-architecture
g

gts13

10/23/2023, 8:37 AM
I am trying to slice the work of the viewmodel into delegates. And I want to do a similar approach to this but without the library which is used there. I want to achieve this with Kotlin + Hilt. The point that I fail is how to pass the state of the viewmodel to the delegate. Or should I create a new state for this delegate and observe it from the viewmodel?
👀 1
c

Christian Ricardo

10/24/2023, 2:15 AM
I don't think you need a library for that... You can make an interface with the attributes and functions you want to separate, it's much simpler. The ViewModel can implement that interface by delegating the implementation something like:
Copy code
@HiltViewModel
class SomeViewModel @Inject constructor(
    someInterface: SomeInterface
) : ViewModel(),
    SomeInterface by someInterface
👍 2
I call that interfaces as presenters because are in the presentation layer
Copy code
interface SomePresenter {
    val someData: State<String>
    val anotherData: State<String>
    fun onEvent(event: YourEvent)
}
with the events you don't need that state machine, is the same but easier
👍 1
5 Views