From e1360bc0fad1838a8c431d5142ce13cc81c10480 Mon Sep 17 00:00:00 2001 From: Iván Ávalos Date: Fri, 14 Jan 2022 23:01:39 -0600 Subject: Refactored even more session management code into SessionController --- .../TrackerMap/controllers/SessionController.kt | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'shared/src/commonMain/kotlin/mx/trackermap/TrackerMap') diff --git a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/SessionController.kt b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/SessionController.kt index 8f5ac56..ed65824 100644 --- a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/SessionController.kt +++ b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/SessionController.kt @@ -19,11 +19,25 @@ class SessionController( object PasswordMissing : LoginState() object Failure : LoginState() object Success : LoginState() + object SignOut : LoginState() } val loginStateFlow = MutableStateFlow(null) val userFlow = MutableStateFlow(null) + fun getSession() { + loginStateFlow.value = LoginState.Loading + GlobalScope.launch { + try { + userFlow.value = sessionApi.sessionGet() + Log.d("LoginViewModel", userFlow.value.toString()) + loginStateFlow.value = LoginState.Success + } catch (e: Exception) { + loginStateFlow.value = LoginState.Failure + } + } + } + fun restoreSession() { loginStateFlow.value = LoginState.Loading GlobalScope.launch { @@ -62,4 +76,12 @@ class SessionController( } } } + + fun logout() { + GlobalScope.launch { + loginStateFlow.value = LoginState.Loading + sessionApi.sessionDelete() + loginStateFlow.value = LoginState.SignOut + } + } } \ No newline at end of file -- cgit v1.2.3