Josh
05/04/2021, 5:18 PMVitor Prado
05/04/2021, 6:02 PMVitor Prado
05/04/2021, 6:05 PMclass LoginViewModel(
private val login: Login = Login()
) {
private val _state = MutableStateFlow<ComponentState>(ComponentState.Idle)
val state = _state.asStateFlow()
private val _effects: Channel<LoginEffect> = Channel()
val effects = _effects.receiveAsFlow()
fun sendLogin(email: String, password: String) {
MainScope().launch {
_state.emit(login(LoginCredentials(email, password, "password"))))
}
}
}
Josh
05/04/2021, 6:06 PMVitor Prado
05/04/2021, 6:07 PMstate.asState()
… for swift you need to create a watch()
method and pass a simple lambda to listen flow changesJosh
05/04/2021, 6:08 PMVitor Prado
05/04/2021, 6:09 PMVitor Prado
05/04/2021, 6:09 PMJosh
05/04/2021, 6:12 PMJosh
05/04/2021, 6:12 PMVitor Prado
05/04/2021, 6:12 PMVitor Prado
05/04/2021, 6:14 PMVitor Prado
05/04/2021, 6:14 PMVitor Prado
05/04/2021, 6:15 PMfun <T> Flow<T>.watch(block: (T) -> Unit): Closeable {
val scope = MainScope()
onEach {
block(it)
}.launchIn(scope)
return object : Closeable {
override fun close() {
try {
scope.cancel()
} catch (e: Exception) {}
}
}
}
Josh
05/04/2021, 6:15 PMJosh
05/04/2021, 6:16 PMVitor Prado
05/04/2021, 6:17 PMJosh
05/04/2021, 6:21 PM