From 4475382b482e9b20cb34dfd2e6896546bf86c485 Mon Sep 17 00:00:00 2001 From: Iván Ávalos Date: Sat, 8 Jan 2022 02:31:33 -0600 Subject: Finished events report, implemented GeofenceController, fixed geofencesGet() and many fixes --- .../TrackerMap/client/apis/GeofencesApi.kt | 45 +++++++++++++--------- .../TrackerMap/client/models/EventInformation.kt | 6 +++ .../TrackerMap/client/models/Geofence.kt | 2 + .../TrackerMap/controllers/GeofenceController.kt | 14 ------- .../TrackerMap/controllers/GeofencesController.kt | 30 +++++++++++++++ 5 files changed, 64 insertions(+), 33 deletions(-) create mode 100644 shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/models/EventInformation.kt delete mode 100644 shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/GeofenceController.kt create mode 100644 shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/GeofencesController.kt (limited to 'shared') diff --git a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/apis/GeofencesApi.kt b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/apis/GeofencesApi.kt index ede7586..dd9c89d 100644 --- a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/apis/GeofencesApi.kt +++ b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/apis/GeofencesApi.kt @@ -15,7 +15,7 @@ import mx.trackermap.TrackerMap.client.models.Geofence import mx.trackermap.TrackerMap.client.infrastructure.* -class GeofencesApi(basePath: kotlin.String = "https://demo.traccar.org/api") : ApiClient(basePath) { +class GeofencesApi(basePath: String = "https://demo.traccar.org/api") : ApiClient(basePath) { /** * Fetch a list of Geofences @@ -28,18 +28,25 @@ class GeofencesApi(basePath: kotlin.String = "https://demo.traccar.org/api") : A * @return kotlin.Array */ @Suppress("UNCHECKED_CAST") - suspend fun geofencesGet(all: kotlin.Boolean? = null, userId: kotlin.Int? = null, deviceId: kotlin.Int? = null, groupId: kotlin.Int? = null, refresh: kotlin.Boolean? = null): kotlin.Array { - val localVariableQuery: MultiValueMap = mapOf("all" to listOf("$all"), "userId" to listOf("$userId"), "deviceId" to listOf("$deviceId"), "groupId" to listOf("$groupId"), "refresh" to listOf("$refresh")) + suspend fun geofencesGet(all: Boolean? = null, userId: Int? = null, deviceId: Int? = null, groupId: Int? = null, refresh: Boolean? = null): Array { + val query: MutableMap> = mutableMapOf() + all?.let { query["all"] = listOf("$it") } + userId?.let { query["userId"] = listOf("$it") } + deviceId?.let { query["userId"] = listOf("$it") } + groupId?.let { query["groupId"] = listOf("$it") } + refresh?.let { query["refresh"] = listOf("$it") } + val localVariableQuery: MultiValueMap = query + val localVariableConfig = RequestConfig( RequestMethod.GET, "/geofences", query = localVariableQuery ) - val response = request>( + val response = request>( localVariableConfig ) return when (response.responseType) { - ResponseType.Success -> (response as Success<*>).data as kotlin.Array + ResponseType.Success -> (response as Success<*>).data as Array ResponseType.Informational -> TODO() ResponseType.Redirection -> TODO() ResponseType.ClientError -> throw ClientException((response as ClientError<*>).body as? String ?: "Client error") @@ -48,12 +55,12 @@ class GeofencesApi(basePath: kotlin.String = "https://demo.traccar.org/api") : A } /** * Delete a Geofence - * - * @param id + * + * @param id * @return void */ - suspend fun geofencesIdDelete(id: kotlin.Int): Unit { - + suspend fun geofencesIdDelete(id: Int) { + val localVariableConfig = RequestConfig( RequestMethod.DELETE, "/geofences/{id}".replace("{" + "id" + "}", "$id") @@ -72,15 +79,15 @@ class GeofencesApi(basePath: kotlin.String = "https://demo.traccar.org/api") : A } /** * Update a Geofence - * - * @param body - * @param id + * + * @param body + * @param id * @return Geofence */ @Suppress("UNCHECKED_CAST") - suspend fun geofencesIdPut(body: Geofence, id: kotlin.Int): Geofence { - val localVariableBody: kotlin.Any? = body - + suspend fun geofencesIdPut(body: Geofence, id: Int): Geofence { + val localVariableBody: Any = body + val localVariableConfig = RequestConfig( RequestMethod.PUT, "/geofences/{id}".replace("{" + "id" + "}", "$id") @@ -99,14 +106,14 @@ class GeofencesApi(basePath: kotlin.String = "https://demo.traccar.org/api") : A } /** * Create a Geofence - * - * @param body + * + * @param body * @return Geofence */ @Suppress("UNCHECKED_CAST") suspend fun geofencesPost(body: Geofence): Geofence { - val localVariableBody: kotlin.Any? = body - + val localVariableBody: Any = body + val localVariableConfig = RequestConfig( RequestMethod.POST, "/geofences" diff --git a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/models/EventInformation.kt b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/models/EventInformation.kt new file mode 100644 index 0000000..1256b8c --- /dev/null +++ b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/models/EventInformation.kt @@ -0,0 +1,6 @@ +package mx.trackermap.TrackerMap.client.models + +data class EventInformation( + val event: Event, + val position: Position? +) diff --git a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/models/Geofence.kt b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/models/Geofence.kt index f82ca07..3b4ee9b 100644 --- a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/models/Geofence.kt +++ b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/models/Geofence.kt @@ -11,6 +11,7 @@ */ package mx.trackermap.TrackerMap.client.models +import kotlinx.serialization.Serializable import kotlinx.serialization.json.JsonPrimitive @@ -23,6 +24,7 @@ import kotlinx.serialization.json.JsonPrimitive * @param calendarId * @param attributes */ +@Serializable data class Geofence ( val id: Int? = null, diff --git a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/GeofenceController.kt b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/GeofenceController.kt deleted file mode 100644 index 011d094..0000000 --- a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/GeofenceController.kt +++ /dev/null @@ -1,14 +0,0 @@ -package mx.trackermap.TrackerMap.controllers - -import mx.trackermap.TrackerMap.client.apis.GeofencesApi -import mx.trackermap.TrackerMap.client.models.Geofence - -class GeofenceController(val geofencesApi: GeofencesApi) { - var geofences = emptyArray() - - suspend fun getGeofences () { - geofences = geofencesApi.geofencesGet() - } - - fun getGeofence (id: Int) = geofences.find { it.id == id } -} \ No newline at end of file diff --git a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/GeofencesController.kt b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/GeofencesController.kt new file mode 100644 index 0000000..fc336d1 --- /dev/null +++ b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/GeofencesController.kt @@ -0,0 +1,30 @@ +package mx.trackermap.TrackerMap.controllers + +import kotlinx.coroutines.DelicateCoroutinesApi +import kotlinx.coroutines.GlobalScope +import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.launch +import mx.trackermap.TrackerMap.client.apis.GeofencesApi +import mx.trackermap.TrackerMap.client.models.Geofence + +@DelicateCoroutinesApi +class GeofencesController( + private val geofencesApi: GeofencesApi +) { + val geofencesFlow = MutableStateFlow>(emptyMap()) + + init { + GlobalScope.launch { + fetchGeofences() + } + } + + private suspend fun fetchGeofences() { + val geofences = geofencesApi.geofencesGet(all = true) + val geofencesMap = mutableMapOf() + geofences.forEach { + geofencesMap[it.id!!] = it + } + geofencesFlow.value = geofencesMap + } +} \ No newline at end of file -- cgit v1.2.3