badoo / MVICore

MVI framework with events, time-travel, and more
https://badoo.github.io/MVICore/
Other
1.27k stars 90 forks source link

Best practices for using ModelWatcher with sealed class ViewModel #111

Closed kyhule closed 5 years ago

kyhule commented 5 years ago

We represent our view model as sealed classes a lot of times and was looking to leverage ModelWatcher but I'm struggling to come up with a good way to handle this. Take for example the following view model:

sealed class TransportControlsViewModel {
    data class Show(
        val startTime: Long,
        val position: Long,
        val duration: Int
    ) : TransportControlsViewModel()
    object Hide : TransportControlsViewModel()
}

My initial approach was to set to watch the entire model but that turned out not to be very helpful because, when shown, there is a new model emitted every second with the only property usually changing is position.

Have you had to handle any similar scenario in your since implementing this?

zsoltk commented 5 years ago

@kyhule I guess you could do a when expression in accept(vm: TransportControlsViewModel), and pass only instances of Show to your ModelWatcher.

For the general case, when you have multiple data classes inside your ViewModel sealed class, you could have a ModelWatcher instance tailored for each different type respectively.

ShikaSD commented 5 years ago

Considering I have seen similar cases before, probably we can improve it from the library perspective as well. For now, please use the suggestion above :)

kyhule commented 5 years ago

Thanks for the suggestions; I had not even considered multiple model watchers.

ShikaSD commented 5 years ago

@kyhule Included in 1.2.4 :)

kyhule commented 5 years ago

@shikasd Nice! I checked it out when you submitted a PR. I'll have to update it now and give it a try. Thanks again and awesome work as always 👍