prefill api key on login screen
if set of course
This commit is contained in:
parent
8dab1d7691
commit
b0d86f0a67
2 changed files with 11 additions and 0 deletions
|
@ -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) {
|
||||
|
|
|
@ -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()
|
||||
|
||||
|
|
Loading…
Reference in a new issue