https://kotlinlang.org logo
#android
Title
# android
h

Hrafn Thorvaldsson

07/01/2022, 2:21 PM
I'm in the process of deep diving into setting up an MVI architecture with coroutines and flows, but am coming up short in finding good source material explaining some things in regards to MVI (not the basics, those seem to be reproduced ad nauseam online). Specifically I am looking for examples on how best to handle triggered side effects (for instance long running network requests) in the context of coroutines and flows as well as how to merge hot flows (for instance a hot flow from a database connection) with any UI initiated MVI actions. Would anyone here know of a good OSS repository that has a MVI architecture using coroutines and flows, that specifically answers those questions I mentioned? Even though my use case is Android, I am not looking specifically for Android examples, rather any Kotlin project with MVI using coroutines and flows.
a

Adam Powell

07/01/2022, 2:26 PM
@Manuel Vivo and @Tunji Dahunsi might have some good references
👍 1
t

Tunji Dahunsi

07/01/2022, 2:38 PM
👋 The way I’ve handled side effects as a result of an MVI
Intent
is to model it in the stream. That is I never actually treat it as a side effect as thats one of the tenets of functional programming. This is because if you’re going to go through the trouble of using sealed classes to pass events to your VM instead of just calling a method, you might as well go all the way and use a pure functional approach. As far as examples go, I’ve linked one below this post. Each
Action
(or
Intent
) is mapped to a stream that modifies the state. Since the stream is backed by a
Flow
I can
map { }
and call
suspend
functions in any of them allowing me to mutate the state inline without launching a side effect. https://github.com/tunjid/me/blob/main/common/feature-archive-edit/src/commonMain/kotlin/com/tunjid/me/archiveedit/ArchiveEditMutator.kt#L87
🙌 1
That said, MVI has a relatively high cognitive overload. There are benefits to it, but unless your app really needs it, you may end up losing more than you gain.
The example linked collects a flow from the database, and allows the user to write to the db as well as perform other editor actions. No side effects are launched, all state changes are written inline and reduced into an initial state.
h

Hrafn Thorvaldsson

07/01/2022, 2:51 PM
Thank you for the info and link @Tunji Dahunsi, this does look like it showcases the behavior I have been searching for. On the subject of high cognitive overload, there is a reason I'm so late in looking into it. It is however overdue on the to-do list 😅
👍 2
3 Views