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 0af0821..fe9bd65 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 @@ -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 { 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 01f7d8a..8c1aa5c 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 @@ -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 = MutableStateFlow(LoginUiState.Idle) val uiState: StateFlow = @@ -26,11 +27,26 @@ class LoginViewModel(val cronetEngine: CronetEngine, val executor: Executor) : V private val _error = MutableLiveData(null) val error: LiveData = _error // TODO put this in uistate + 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) { + 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