add remembering me

This commit is contained in:
Minecon724 2024-08-03 17:42:11 +02:00
parent 9d0401c0f6
commit ac3900d3d4
Signed by: Minecon724
GPG key ID: 3CCC4D267742C8E8
2 changed files with 26 additions and 9 deletions

View file

@ -63,7 +63,6 @@ import androidx.compose.ui.unit.dp
import androidx.lifecycle.lifecycleScope
import eu.m724.vastapp.BuildConfig
import eu.m724.vastapp.R
import eu.m724.vastapp.activity.PermissionChecker
import eu.m724.vastapp.activity.dashboard.DashboardActivity
import eu.m724.vastapp.ui.theme.VastappTheme
import eu.m724.vastapp.vastai.data.User
@ -84,7 +83,7 @@ class LoginActivity : ComponentActivity() {
val executor = Executors.newSingleThreadExecutor()
val cronetEngine = CronetEngine.Builder(baseContext).build()
val loginViewModel = LoginViewModel(cronetEngine, executor)
val loginViewModel = LoginViewModel(application, cronetEngine, executor)
loginViewModel.error.observe(this) { errorMessage ->
if (errorMessage != null) {
@ -100,6 +99,8 @@ class LoginActivity : ComponentActivity() {
}
}
loginViewModel.loadKey()
enableEdgeToEdge()
setContent {
VastappTheme {

View file

@ -1,23 +1,24 @@
package eu.m724.vastapp.activity.login
import android.widget.Toast
import android.app.Application
import android.content.Context
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import eu.m724.vastapp.vastai.ApiFailure
import eu.m724.vastapp.vastai.ApiRoute
import eu.m724.vastapp.vastai.VastApi
import eu.m724.vastapp.vastai.api.UserUrlRequestCallback
import eu.m724.vastapp.vastai.data.User
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
import org.chromium.net.CronetEngine
import java.util.concurrent.Executor
class LoginViewModel(val cronetEngine: CronetEngine, val executor: Executor) : ViewModel() {
class LoginViewModel(
application: Application,
val cronetEngine: CronetEngine,
val executor: Executor
) : AndroidViewModel(application) {
private val _uiState: MutableStateFlow<LoginUiState> =
MutableStateFlow(LoginUiState.Idle)
val uiState: StateFlow<LoginUiState> =
@ -26,11 +27,26 @@ class LoginViewModel(val cronetEngine: CronetEngine, val executor: Executor) : V
private val _error = MutableLiveData<String?>(null)
val error: LiveData<String?> = _error // TODO put this in uistate
private val applicationContext = getApplication<Application>().applicationContext
private val sharedPreferences = applicationContext.getSharedPreferences("login", Context.MODE_PRIVATE)
fun loadKey() {
val apiKey = sharedPreferences.getString("apiKey", null)
if (apiKey != null) {
tryLogin(apiKey)
}
}
fun tryLogin(apiKey: String) {
val vastApi = VastApi(apiKey, cronetEngine, executor)
val request = vastApi.buildRequest(
ApiRoute.SHOW_USER,
UserUrlRequestCallback({ user ->
with (sharedPreferences.edit()) {
putString("apiKey", apiKey) // TODO encrypt
apply()
} // TODO toggle for this
_uiState.value = LoginUiState.Success(user)
}, { apiFailure ->
_uiState.value = LoginUiState.Idle