From 18dea0beb1285f793ea1ca024b4cf1c48c302e59 Mon Sep 17 00:00:00 2001 From: Minecon724 Date: Sun, 4 Aug 2024 09:36:07 +0200 Subject: [PATCH] make loading seamless --- .../vastapp/activity/login/LoginActivity.kt | 8 +++- .../vastapp/activity/login/LoginViewModel.kt | 39 ++++++++++--------- 2 files changed, 27 insertions(+), 20 deletions(-) diff --git a/app/src/main/java/eu/m724/vastapp/activity/login/LoginActivity.kt b/app/src/main/java/eu/m724/vastapp/activity/login/LoginActivity.kt index eb706fd..1045384 100644 --- a/app/src/main/java/eu/m724/vastapp/activity/login/LoginActivity.kt +++ b/app/src/main/java/eu/m724/vastapp/activity/login/LoginActivity.kt @@ -107,7 +107,13 @@ class LoginActivity : ComponentActivity() { verticalArrangement = Arrangement.Center, horizontalAlignment = Alignment.CenterHorizontally ) { - LoginApp(loginViewModel) + val loading by loginViewModel.fullscreenLoading.collectAsState() + + if (loading) { + CircularProgressIndicator() + } else { + LoginApp(loginViewModel) + } } } } diff --git a/app/src/main/java/eu/m724/vastapp/activity/login/LoginViewModel.kt b/app/src/main/java/eu/m724/vastapp/activity/login/LoginViewModel.kt index c703080..a079008 100644 --- a/app/src/main/java/eu/m724/vastapp/activity/login/LoginViewModel.kt +++ b/app/src/main/java/eu/m724/vastapp/activity/login/LoginViewModel.kt @@ -31,25 +31,12 @@ class LoginViewModel( private val _apiKey = MutableStateFlow("") var apiKey: StateFlow = _apiKey.asStateFlow() + private val _fullscreenLoading = MutableStateFlow(false) + var fullscreenLoading: StateFlow = _fullscreenLoading.asStateFlow() + private val applicationContext = getApplication().applicationContext private val sharedPreferences = applicationContext.getSharedPreferences("login", Context.MODE_PRIVATE) - fun loadKey() { - val apiKey = sharedPreferences.getString("apiKey", null) - - if (apiKey != null) { - _apiKey.value = apiKey - tryLogin(LoginUiState.FullLoading) - } - } - - fun tryLogin() { - tryLogin(LoginUiState.Loading) - } - - fun onApiKeyChange(apiKey: String) { - _apiKey.update { apiKey } - } private fun saveKey() { with (sharedPreferences.edit()) { @@ -58,7 +45,17 @@ class LoginViewModel( } } - private fun tryLogin(initialState: LoginUiState) { + fun loadKey() { + val apiKey = sharedPreferences.getString("apiKey", null) + + if (apiKey != null) { + _apiKey.value = apiKey + _fullscreenLoading.value = true + tryLogin() + } + } + + fun tryLogin() { val apiKey = apiKey.value val vastApi = VastApi(apiKey, cronetEngine, executor) @@ -69,12 +66,16 @@ class LoginViewModel( _uiState.value = LoginUiState.Success(user) }, { apiFailure -> _uiState.value = LoginUiState.Idle + _fullscreenLoading.value = false _error.postValue(apiFailure.errorMessage) }) ) - - _uiState.value = initialState + _uiState.value = LoginUiState.Loading request.start() } + + fun onApiKeyChange(apiKey: String) { + _apiKey.update { apiKey } + } } \ No newline at end of file