Travis Reitter
04/17/2023, 4:14 PMState
directly from SwiftUI (ie, avoiding the State -> Model
translation and avoiding creating a class that implements BaseMviView
)? My project's equivalent of MainController in the sample exposes a Flow<State>
that I consume directly from Compose with Flow<T>.collectAsStateWithLifecycle()
and I'd like to follow as closely as possible with my SwiftUITravis Reitter
04/17/2023, 4:15 PMFlow
to a Publisher
that seems OK but requires new boilerplate code for every Flow
instance but the article is 2 years old so I don't know if there's a simpler option available now (or, especially, a solution geared toward MVIKotlin usage)Arkadii Ivanov
04/17/2023, 4:16 PMArkadii Ivanov
04/17/2023, 4:17 PMArkadii Ivanov
04/17/2023, 5:20 PMclass WrappedStateFlow<T : Any>(
source: Flow<T>,
private val context: CoroutineContext = Dispatchers.Main.immediate,
) : StateFlow<T> by source {
fun subscribe(onNext: (T) -> Unit): Cancellation {
val scope = CoroutineScope(context)
scope.launch { collect(onNext) }
return Cancellation { scope.cancel() }
}
fun interface Cancellation {
fun cancel()
}
}
Arkadii Ivanov
04/17/2023, 5:21 PMfun <T : Any> StateFlow<T>.wrap(): WrappedStateFlow<T> = WrappedStateFlow(this)
And just expose WrappedStateFlow
instead of StateFlow
.Travis Reitter
04/17/2023, 5:46 PMTravis Reitter
04/17/2023, 5:54 PMsource
param should be defined as source: StateFlow<T>
, right?Arkadii Ivanov
04/17/2023, 5:55 PMArkadii Ivanov
04/17/2023, 5:56 PMTravis Reitter
04/17/2023, 5:56 PMTravis Reitter
04/17/2023, 5:58 PMfun interface Cancellation
. I guess it's a work-around for the fact that interfaces can't be instantiated? I'm just surprised that works :)Arkadii Ivanov
04/17/2023, 5:59 PMTravis Reitter
04/17/2023, 5:59 PMobject :
since I didn't know about this shortcutArkadii Ivanov
04/17/2023, 6:02 PM