Created
September 2, 2022 02:25
-
-
Save PompaDonpa/478720334357f17dee73f145fd61612f to your computer and use it in GitHub Desktop.
Login Page Android State
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
sealed class UiState { | |
object SignedOut : UiState() | |
object InProgress : UiState() | |
object Error : UiState() | |
object SignIn : UiState() | |
} | |
class MyViewModel : ViewModel() { | |
private val _uiState = mutableStateOf<UiState>(SignedOut) | |
val uiState: State<UiState> | |
get() = _uiState | |
// ... | |
} | |
// In addition to the mutableStateOf API, Compose provides extensions for LiveData, Flow, and Observable | |
// to register as a listener and represent the value as a state. | |
class MyViewModel : ViewModel() { | |
private val _uiState = MutableLiveData<UiState>(SignedOut) | |
val uiState: LiveData<UiState> | |
get() = _uiState | |
// ... | |
} | |
@Composable | |
fun MyComposable(viewModel: MyViewModel) { | |
val uiState = viewModel.uiState.observeAsState() | |
// ... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment