Hi All! Are there any samples of using MVIKotlin3...
# mvikotlin
i
Hi All! Are there any samples of using MVIKotlin3.0Ax/Decompose and Coroutines? I’ve been working on updating my MVIKotlin app to 3.0A3 and Decompose and loving the compose-jb sample but it uses reaktive.
a
Personally I'm not aware of any samples with coroutines, perhaps you can search GitHub by package name "com.arkivanov.decompose". But it should be quite similar to Reaktive, except you will need a scope. You can add the following extension function:
Copy code
fun LifecycleOwner.coroutineScope(context: CoroutineContext): CoroutineScope {
    val scope = CoroutineScope(context)
    lifecycle.doOnDestroy(scope::cancel)

    return scope
}
And then use it in a component as follows:
Copy code
class SomeComponent(componentContext: ComponentContext): ComponentContext by componentContext {
    private val scope = coroutineScope(Dispatchers.Main)
}
Also the sample Todo app in the MVIKotlin repository supports both Reaktive and coroutines. But it doesn't use Decompose. Also you may find this sample a bit messy, because it supports both async libraries and so has some additional abstractions.
i
Thanks @Arkadii Ivanov! I was also figuring out the best way to handle the outputs. It seems that the outputs in the example are used by the router and set in the components constructor as
private val output: Consumer<Output>
. But of course
Consumer
is a reaktive class. Is there a coroutines equivalent? `
a
You can just use normal functions:
Copy code
private val output: (Output) -> Unit
Please also keep in mind another way. Instead of having
sealed interface Output
and a single function/consumer, you define multiple separate functions:
Copy code
private val onFinished: () -> Unit,
private val onError: () -> Unit,
// ...
The former option may work better with reactive streams (Flow/Observable), and you can map/combine the streams. But the latter option may be less boilerplate sometimes. Just FYI.