-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
62 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 0 additions & 5 deletions
5
shared/src/androidMain/kotlin/ru/alex009/moko/mvvm/declarativeui/Platform.kt
This file was deleted.
Oops, something went wrong.
7 changes: 0 additions & 7 deletions
7
shared/src/commonMain/kotlin/ru/alex009/moko/mvvm/declarativeui/Greeting.kt
This file was deleted.
Oops, something went wrong.
42 changes: 42 additions & 0 deletions
42
shared/src/commonMain/kotlin/ru/alex009/moko/mvvm/declarativeui/LoginViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package ru.alex009.moko.mvvm.declarativeui | ||
|
||
import dev.icerock.moko.mvvm.viewmodel.ViewModel | ||
import kotlinx.coroutines.channels.Channel | ||
import kotlinx.coroutines.delay | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.SharingStarted | ||
import kotlinx.coroutines.flow.StateFlow | ||
import kotlinx.coroutines.flow.combine | ||
import kotlinx.coroutines.flow.receiveAsFlow | ||
import kotlinx.coroutines.flow.stateIn | ||
import kotlinx.coroutines.launch | ||
|
||
class LoginViewModel : ViewModel() { | ||
val login: MutableStateFlow<String> = MutableStateFlow("") | ||
val password: MutableStateFlow<String> = MutableStateFlow("") | ||
|
||
private val _isLoading: MutableStateFlow<Boolean> = MutableStateFlow(false) | ||
val isLoading: StateFlow<Boolean> = _isLoading | ||
|
||
val isButtonEnabled: StateFlow<Boolean> = | ||
combine(isLoading, login, password) { isLoading, login, password -> | ||
isLoading.not() && login.isNotBlank() && password.isNotBlank() | ||
}.stateIn(viewModelScope, SharingStarted.Eagerly, false) | ||
|
||
private val _actions = Channel<Action>() | ||
val actions: Flow<Action> get() = _actions.receiveAsFlow() | ||
|
||
fun onLoginPressed() { | ||
_isLoading.value = true | ||
viewModelScope.launch { | ||
delay(1000) | ||
_isLoading.value = false | ||
_actions.send(Action.LoginSuccess) | ||
} | ||
} | ||
|
||
sealed interface Action { | ||
object LoginSuccess : Action | ||
} | ||
} |
5 changes: 0 additions & 5 deletions
5
shared/src/commonMain/kotlin/ru/alex009/moko/mvvm/declarativeui/Platform.kt
This file was deleted.
Oops, something went wrong.
7 changes: 0 additions & 7 deletions
7
shared/src/iosMain/kotlin/ru/alex009/moko/mvvm/declarativeui/Platform.kt
This file was deleted.
Oops, something went wrong.