jefbit
05/09/2024, 3:58 PMval _event = MutableSharedFlow<UiEvent>()
val event: SharedFlow<UiEvent>
get() = _event
sealed class UiEvent {
data object DisplayToastEvent : UiEvent()
data object NavigateToDetailEvent : UiEvent()
}
from my composable I handle my event below
LaunchedEffect(Unit){
viewModel.event.collect { event ->
is UiEvent.NavigateToDetailEvent -> //navigate to detail
is UiEvent.DisplayErrorMsgEvent -> //display toast msg
}
}
What/Where is the best way to handle these Events from SwiftUi? Is there an equivalent in SwiftUi? I am having no problem observing my StateFlow and updating the view when necessary but cant seem to find any information regarding one off events. Thanks for the help.jefbit
05/09/2024, 3:59 PMcoachroebuck
05/10/2024, 12:45 AMcoachroebuck
05/10/2024, 12:47 AMcoachroebuck
05/10/2024, 12:47 AMcoachroebuck
05/10/2024, 12:47 AMcoachroebuck
05/10/2024, 12:47 AMinterface CFlow<T> {
fun tryEmit(state: T?)
suspend fun collect(function: (T?) -> Unit)
suspend fun unCollect()
val value: T?
}
expect fun <T> getCFlow(value: T?): CFlow<T>
With the keyword being "expect"coachroebuck
05/10/2024, 12:48 AMclass AndroidCFlow<T>(value: T?) : CFlow<T> {
private val _stateFlow: MutableStateFlow<T?> =
MutableStateFlow(value)
val stateFlow: Flow<T?> = _stateFlow
override fun tryEmit(state: T?) {
_stateFlow.tryEmit(state)
}
override suspend fun collect(function: (T?) -> Unit) {
_stateFlow.collect { value -> function(value) }
}
override suspend fun unCollect() { }
override val value: T?
get() = _stateFlow.value
}
actual fun <T> getCFlow(value: T?): CFlow<T> = AndroidCFlow(value)
jefbit
05/10/2024, 12:48 AMcoachroebuck
05/10/2024, 12:49 AMcoachroebuck
05/10/2024, 12:49 AMcoachroebuck
05/10/2024, 12:50 AMcoachroebuck
05/10/2024, 12:50 AMjefbit
05/10/2024, 12:51 AMcoachroebuck
05/10/2024, 12:52 AMcoachroebuck
05/10/2024, 12:53 AMcoachroebuck
05/10/2024, 12:54 AMcoachroebuck
05/10/2024, 12:55 AMjefbit
05/10/2024, 12:58 AMcoachroebuck
05/10/2024, 1:01 AMlaunched effect code iOS side to handle one off events.
I'll share my source code and go backwards. I hope this will help.coachroebuck
05/10/2024, 1:02 AMjefbit
05/10/2024, 1:02 AMcoachroebuck
05/10/2024, 1:04 AMviewModel.viewModel.cFlow.collect(function: { latestState in onStateReceived(latestState: latestState as! LoginViewModelStore.State)
}, completionHandler: { error in
print("LoginView: error=[\(String(describing: error))]")
})
coachroebuck
05/10/2024, 1:06 AMprivate func onStateReceived(latestState: LoginViewModelStore.State) {
if let _ = latestState as? LoginViewModelStore.StateCurrentState {
onCurrentState(latestState: latestState as! LoginViewModelStore.StateCurrentState)
} else if let _ = latestState as? LoginViewModelStore.StateIdle {
onIdle()
}
}
coachroebuck
05/10/2024, 1:07 AMcoachroebuck
05/10/2024, 1:08 AMcoachroebuck
05/10/2024, 1:08 AMcoachroebuck
05/10/2024, 1:08 AMclass SelectionStore: ObservableObject {
@Published var selection: String?
}
coachroebuck
05/10/2024, 1:09 AMcoachroebuck
05/10/2024, 1:11 AMcoachroebuck
05/10/2024, 1:13 AMcoachroebuck
05/10/2024, 1:13 AMcoachroebuck
05/10/2024, 1:15 AM@Published var yourProperty: String?
coachroebuck
05/10/2024, 1:15 AMcoachroebuck
05/10/2024, 1:17 AMcoachroebuck
05/10/2024, 1:19 AMcoachroebuck
05/10/2024, 1:19 AMcoachroebuck
05/10/2024, 1:20 AMcoachroebuck
05/10/2024, 1:21 AMcoachroebuck
05/10/2024, 1:21 AMcoachroebuck
05/10/2024, 1:21 AMjefbit
05/10/2024, 1:21 AMcoachroebuck
05/10/2024, 1:23 AM