kyleg
01/27/2020, 7:56 PMkyleg
01/27/2020, 8:01 PMViewModel
, in MVI you have a state class wrapped in LiveData and your View observes the LiveData and on changes it re-renders itself using the data wrapped in LiveData. Your view issues actions/intents to the ViewModel, which then updates the state (triggering the re-rendering of View via the observer).
Essentially, right now my contracts are (note that State
is not the State
monad from Arrow)
interface ViewModelContract {
val state: LiveData<MyState>
fun instruct(action: Action): IO<Unit>
}
interface View {
val viewModel: ViewModelContract
fun render(MyState)
}
Right now I have my ViewModel function that receives the intents returning an IO<Unit> so the view can run unsafeRunAsync
on the result of the viewmodel’s action receiver.
The observer is set up in the view’s onCreate or something. Depends on whether it’s an Activity or Fragment.
Wherever the view has to issue an action to the viewmodel, I have the call appended with .unsafeRunAsync()
.
Maybe this is a question better suited for another forum. Just thought I’d ask here, as I think someone from the Arrow team has described an MVP (or MVC, I forget) Android application using Reader
.kyleg
01/27/2020, 8:04 PMunsafeRun…
inside my init block because it’s
class MyViewModel … {
init {
IO.effect { someVal = repoCall() }.unsafeRunAsync {}
}
// ...
}
Instead of pushing it to the “edge” in my Activity/Fragment.stojan
01/27/2020, 9:11 PMkyleg
01/28/2020, 12:38 AMkyleg
01/28/2020, 12:40 AMunsafeRunAsync
inside my VM’s init block? (Because if it were done in the view like myViewModel.init().unsafeRunAsync {}
then I would run the risk of cancelling the call with a rotation?kyleg
01/28/2020, 12:43 AMkyleg
01/28/2020, 1:03 AMaballano
01/28/2020, 12:28 PMaballano
01/28/2020, 12:31 PMaballano
01/28/2020, 12:32 PMaballano
01/28/2020, 12:33 PMaballano
01/28/2020, 2:27 PMkyleg
01/28/2020, 5:38 PMonDestroy
stuff to save state on rotation.
I can’t say I’m advanced enough to be using the State monad, but it seems like my approach largely recreates the concept by having an immutable state that can only be changed by the VM that owns it, and in practice is only changed by a single function (that admittedly calls a few other VM-owned, private, pure functions just for readability’s sake)
I’m looking forward to seeing the Android module. I don’t suppose it’s being developed “in public” on the repo? If not, no big deal, just thought I’d ask. It would be interesting to see what’s being considered.
I saw yesterday that Jetpack has some experimental Compose module that was announced at Google ’19. I haven’t fiddled with it, but that plus Arrow looks like one could write some really functional apps.
As for the pragmatic approach re unsafeRun, I agree. But since I’m new to this stuff, it’s easy for me to confuse pragmatism with ignorance: am I doing it this way because it’s actually better, or because my ignorance makes me think it’s better. 🙂