From b0d86f0a6735c40759e923a326bef77a04967c8b Mon Sep 17 00:00:00 2001 From: Minecon724 Date: Thu, 8 Aug 2024 12:06:02 +0200 Subject: [PATCH] prefill api key on login screen if set of course --- .../java/eu/m724/vastapp/activity/login/LoginActivity.kt | 6 ++++++ .../java/eu/m724/vastapp/activity/login/LoginViewModel.kt | 5 +++++ 2 files changed, 11 insertions(+) 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 fa7a0cf..0e3055e 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 @@ -77,17 +77,23 @@ class LoginActivity : ComponentActivity() { val cronetEngine = CronetEngine.Builder(baseContext).build() val loginViewModel = LoginViewModel(application, cronetEngine, executor) + // load api key if saved + loginViewModel.init() + + // handle login errors loginViewModel.error.observe(this) { errorMessage -> if (errorMessage != null) { Toast.makeText(baseContext, errorMessage, Toast.LENGTH_SHORT).show() } } + // handle loading error val loadingError = intent.getStringExtra("error") if (loadingError != null) { Toast.makeText(baseContext, loadingError, Toast.LENGTH_SHORT).show() } + // load app if it's time lifecycleScope.launch { // TODO I was suggested not to launch an activity from a lifecycle scope loginViewModel.uiState.collect { state -> if (state is LoginUiState.Success) { 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 0d274e3..2d46fdb 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 @@ -5,6 +5,7 @@ import androidx.lifecycle.AndroidViewModel import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData import androidx.lifecycle.viewModelScope +import eu.m724.vastapp.BuildConfig import eu.m724.vastapp.VastApplication import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.StateFlow @@ -32,6 +33,10 @@ class LoginViewModel( private val application = getApplication() + fun init() { + _apiKey.value = application.loadKey() ?: BuildConfig.VASTAI_KEY + } + fun tryLogin() { val userDeferred = application.vastApi.getUser()