aboutsummaryrefslogtreecommitdiff
path: root/shared/src/commonMain
diff options
context:
space:
mode:
authorIván Ávalos <avalos@disroot.org>2023-09-17 21:56:55 -0600
committerIván Ávalos <avalos@disroot.org>2023-09-17 23:51:33 -0600
commitedbd2c7713a0ba4e7e7a3ba6d59d16861ea4eb23 (patch)
tree885ca095c993c7a661303d215d9be0a6271ba3ea /shared/src/commonMain
parent7aec305729b872d668df45eae4821b106c1a20cb (diff)
downloadetbsa-trackermap-mobile-edbd2c7713a0ba4e7e7a3ba6d59d16861ea4eb23.tar.gz
etbsa-trackermap-mobile-edbd2c7713a0ba4e7e7a3ba6d59d16861ea4eb23.tar.bz2
etbsa-trackermap-mobile-edbd2c7713a0ba4e7e7a3ba6d59d16861ea4eb23.zip
- [shared] Implement network state monitoring
- [android] UI reacts to network state - [ios] UI reacts to network state
Diffstat (limited to 'shared/src/commonMain')
-rw-r--r--shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/infrastructure/ApiClient.kt2
-rw-r--r--shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/infrastructure/SessionManager.kt2
-rw-r--r--shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/NetworkController.kt8
-rw-r--r--shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/SessionController.kt18
4 files changed, 15 insertions, 15 deletions
diff --git a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/infrastructure/ApiClient.kt b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/infrastructure/ApiClient.kt
index 937b2dd..8238f7e 100644
--- a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/infrastructure/ApiClient.kt
+++ b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/infrastructure/ApiClient.kt
@@ -168,7 +168,7 @@ open class ApiClient(
}
}
- if (sessionManager.token.isNotEmpty()) {
+ if (sessionManager.hasSession) {
request.headers["Cookie"] = sessionManager.token
}
val response: HttpResponse = client.request(request)
diff --git a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/infrastructure/SessionManager.kt b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/infrastructure/SessionManager.kt
index caf2da1..71ae5d0 100644
--- a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/infrastructure/SessionManager.kt
+++ b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/infrastructure/SessionManager.kt
@@ -28,6 +28,8 @@ class SessionManager(
settings[ACCESS_TOKEN_KEY] = token
}
+ val hasSession: Boolean get() = settings.hasKey(ACCESS_TOKEN_KEY)
+
fun clearSession() {
settings.remove(ACCESS_TOKEN_KEY)
settings.remove(SERVER_URL_KEY)
diff --git a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/NetworkController.kt b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/NetworkController.kt
new file mode 100644
index 0000000..08dcc87
--- /dev/null
+++ b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/NetworkController.kt
@@ -0,0 +1,8 @@
+package mx.trackermap.TrackerMap.controllers
+
+import kotlinx.coroutines.flow.StateFlow
+import mx.trackermap.TrackerMap.Injectable
+
+expect class NetworkController: Injectable {
+ val networkAvailable: StateFlow<Boolean?>
+} \ No newline at end of file
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 a63bba2..5cfdf96 100644
--- a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/SessionController.kt
+++ b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/SessionController.kt
@@ -25,13 +25,15 @@ import kotlinx.serialization.json.JsonPrimitive
import mx.trackermap.TrackerMap.Injectable
import mx.trackermap.TrackerMap.client.apis.SessionApi
import mx.trackermap.TrackerMap.client.apis.UsersApi
+import mx.trackermap.TrackerMap.client.infrastructure.SessionManager
import mx.trackermap.TrackerMap.client.models.SessionBody
import mx.trackermap.TrackerMap.client.models.User
@DelicateCoroutinesApi
class SessionController(
+ private val sessionManager: SessionManager,
private val sessionApi: SessionApi,
- private val usersApi: UsersApi
+ private val usersApi: UsersApi,
): Injectable {
sealed class LoginState {
object Nothing: LoginState()
@@ -46,6 +48,7 @@ class SessionController(
val loginStateFlow = MutableStateFlow<LoginState?>(null)
val userFlow = MutableStateFlow<User?>(null)
+ val hasSession: Boolean get() = sessionManager.hasSession
fun getSession() {
loginStateFlow.value = LoginState.Loading
@@ -59,19 +62,6 @@ class SessionController(
}
}
- fun restoreSession() {
- loginStateFlow.value = LoginState.Loading
- GlobalScope.launch {
- try {
- userFlow.value = sessionApi.sessionGet()
- loginStateFlow.value = LoginState.Success
- } catch (e: Exception) {
- e.printStackTrace()
- loginStateFlow.value = LoginState.Nothing
- }
- }
- }
-
fun login(body: SessionBody) {
val url = body.url.trim()
val email = body.email.trim()