aboutsummaryrefslogtreecommitdiff
path: root/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers
diff options
context:
space:
mode:
Diffstat (limited to 'shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers')
-rw-r--r--shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/AttributesController.kt52
-rw-r--r--shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/DeviceController.kt34
-rw-r--r--shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/SessionController.kt63
-rw-r--r--shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/UserController.kt10
4 files changed, 54 insertions, 105 deletions
diff --git a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/AttributesController.kt b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/AttributesController.kt
deleted file mode 100644
index 6262ec8..0000000
--- a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/AttributesController.kt
+++ /dev/null
@@ -1,52 +0,0 @@
-package mx.trackermap.TrackerMap.controllers
-
-import mx.trackermap.TrackerMap.client.apis.AttributesApi
-import mx.trackermap.TrackerMap.client.models.Attribute
-import mx.trackermap.TrackerMap.client.models.Device
-
-class AttributesController(
- private val attributesApi: AttributesApi,
- private val sessionController: SessionController)
-{
- var userAttributes = emptyArray<Attribute>()
- val deviceAttributes = mutableMapOf<Int, Array<Attribute>>()
-
- /** userAttributes **/
-
- suspend fun getUserAttributes() {
- userAttributes = attributesApi.attributesComputedGet(userId = sessionController.user?.id)
- }
-
- fun getUserAttribute(attribute: String) = userAttributes.find { it.attribute == attribute }
-
- suspend fun createUserAttribute(attribute: Attribute) {
- userAttributes += attributesApi.attributesComputedPost(attribute)
- }
-
- suspend fun updateUserAttribute(attribute: Attribute) {
- userAttributes = userAttributes.map {
- if (attribute.id != null && it.id == attribute.id) {
- attributesApi.attributesComputedIdPut(attribute, attribute.id)
- } else it
- }.toTypedArray()
- }
-
- suspend fun deleteUserAttribute(attribute: String) {
- userAttributes.find { it.attribute == attribute }?.id?.let { id ->
- attributesApi.attributesComputedIdDelete(id)
- userAttributes = userAttributes.filter { it.id != id }.toTypedArray()
- }
- }
-
- /** deviceAttributes **/
-
- suspend fun getDeviceAttributes(device: Device) {
- device.id?.let {
- deviceAttributes[it] = attributesApi.attributesComputedGet(deviceId = device.id)
- }
- }
-
- fun getDeviceAttribute(device: Device, attribute: String) =
- deviceAttributes[device.id]?.find { it.attribute == attribute }
-
-} \ No newline at end of file
diff --git a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/DeviceController.kt b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/DeviceController.kt
deleted file mode 100644
index 2e0d924..0000000
--- a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/DeviceController.kt
+++ /dev/null
@@ -1,34 +0,0 @@
-package mx.trackermap.TrackerMap.controllers
-
-import mx.trackermap.TrackerMap.client.apis.DevicesApi
-import mx.trackermap.TrackerMap.client.models.Device
-
-class DeviceController(
- private val devicesApi: DevicesApi,
- private val sessionController: SessionController)
-{
- var devices = emptyArray<Device>()
-
- suspend fun getDevices() {
- devices = devicesApi.devicesGet(userId = sessionController.user?.id)
- }
-
- fun getDevice(id: Int) = devices.find { it.id == id }
-
- suspend fun createDevice(device: Device) {
- devices += devicesApi.devicesPost(device)
- }
-
- suspend fun updateDevice(id: Int, device: Device) {
- devices = devices.map {
- if (it.id == id)
- devicesApi.devicesIdPut(id = id, body = device)
- else it
- }.toTypedArray()
- }
-
- suspend fun deleteDevice(id: Int) {
- devicesApi.devicesIdDelete(id)
- devices = devices.filter { it.id != id }.toTypedArray()
- }
-} \ 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 ba892f6..8f5ac56 100644
--- a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/SessionController.kt
+++ b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/SessionController.kt
@@ -1,20 +1,65 @@
package mx.trackermap.TrackerMap.controllers
+import android.util.Log
+import kotlinx.coroutines.DelicateCoroutinesApi
+import kotlinx.coroutines.GlobalScope
+import kotlinx.coroutines.flow.MutableStateFlow
+import kotlinx.coroutines.launch
import mx.trackermap.TrackerMap.client.apis.SessionApi
+import mx.trackermap.TrackerMap.client.models.SessionBody
import mx.trackermap.TrackerMap.client.models.User
-class SessionController(private val sessionApi: SessionApi) {
- var user: User? = null
-
- suspend fun createSession (email: String, password: String) {
- user = sessionApi.sessionPost(email, password)
+@DelicateCoroutinesApi
+class SessionController(
+ private val sessionApi: SessionApi
+) {
+ sealed class LoginState {
+ object Loading: LoginState()
+ object EmailMissing : LoginState()
+ object PasswordMissing : LoginState()
+ object Failure : LoginState()
+ object Success : LoginState()
}
- suspend fun getSession () {
- user = sessionApi.sessionGet()
+ val loginStateFlow = MutableStateFlow<LoginState?>(null)
+ val userFlow = MutableStateFlow<User?>(null)
+
+ fun restoreSession() {
+ loginStateFlow.value = LoginState.Loading
+ GlobalScope.launch {
+ try {
+ userFlow.value = sessionApi.sessionGet()
+ Log.d("LoginViewModel", userFlow.value.toString())
+ loginStateFlow.value = LoginState.Success
+ } catch (e: Exception) {
+ Log.d("LoginViewModel", "No session")
+ }
+ }
}
- suspend fun deleteSession () {
- sessionApi.sessionDelete()
+ fun login(body: SessionBody) {
+ val email = body.email.trim()
+ val password = body.password.trim()
+
+ if (email.isEmpty()) {
+ loginStateFlow.value = LoginState.EmailMissing
+ return
+ }
+
+ if (password.isEmpty()) {
+ loginStateFlow.value = LoginState.PasswordMissing
+ return
+ }
+
+ loginStateFlow.value = LoginState.Loading
+ GlobalScope.launch {
+ try {
+ userFlow.value = sessionApi.sessionPost(email, password)
+ Log.d("LoginViewModel", userFlow.value.toString())
+ loginStateFlow.value = LoginState.Success
+ } catch (e: Exception) {
+ loginStateFlow.value = LoginState.Failure
+ }
+ }
}
} \ No newline at end of file
diff --git a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/UserController.kt b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/UserController.kt
deleted file mode 100644
index a398185..0000000
--- a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/UserController.kt
+++ /dev/null
@@ -1,10 +0,0 @@
-package mx.trackermap.TrackerMap.controllers
-
-import mx.trackermap.TrackerMap.client.apis.UsersApi
-import mx.trackermap.TrackerMap.client.models.User
-
-class UserController(private val usersApi: UsersApi) {
- suspend fun updateUser(user: User) {
- usersApi.usersPost(user)
- }
-} \ No newline at end of file