prefill api key on login screen

if set of course
This commit is contained in:
Minecon724 2024-08-08 12:06:02 +02:00
parent 8dab1d7691
commit b0d86f0a67
Signed by: Minecon724
GPG key ID: 3CCC4D267742C8E8
2 changed files with 11 additions and 0 deletions

View file

@ -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) {

View file

@ -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<VastApplication>()
fun init() {
_apiKey.value = application.loadKey() ?: BuildConfig.VASTAI_KEY
}
fun tryLogin() {
val userDeferred = application.vastApi.getUser()