diff options
author | Iván Ávalos <avalos@disroot.org> | 2022-01-08 02:31:33 -0600 |
---|---|---|
committer | Iván Ávalos <avalos@disroot.org> | 2022-01-08 02:31:33 -0600 |
commit | 4475382b482e9b20cb34dfd2e6896546bf86c485 (patch) | |
tree | c3cc590e273e63030db33b79de7cd33a80b451ee /shared/src/commonMain | |
parent | e4f78f4af356215650df9df32a13ceff8067e137 (diff) | |
download | etbsa-trackermap-mobile-4475382b482e9b20cb34dfd2e6896546bf86c485.tar.gz etbsa-trackermap-mobile-4475382b482e9b20cb34dfd2e6896546bf86c485.tar.bz2 etbsa-trackermap-mobile-4475382b482e9b20cb34dfd2e6896546bf86c485.zip |
Finished events report, implemented GeofenceController, fixed geofencesGet() and many fixes
Diffstat (limited to 'shared/src/commonMain')
5 files changed, 64 insertions, 33 deletions
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<Geofence> */ @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<Geofence> { - 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<Geofence> { + val query: MutableMap<String, List<String>> = 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<kotlin.Array<Geofence>>( + val response = request<Array<Geofence>>( localVariableConfig ) return when (response.responseType) { - ResponseType.Success -> (response as Success<*>).data as kotlin.Array<Geofence> + ResponseType.Success -> (response as Success<*>).data as Array<Geofence> 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<Geofence>() - - 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<Map<Int,Geofence>>(emptyMap()) + + init { + GlobalScope.launch { + fetchGeofences() + } + } + + private suspend fun fetchGeofences() { + val geofences = geofencesApi.geofencesGet(all = true) + val geofencesMap = mutableMapOf<Int, Geofence>() + geofences.forEach { + geofencesMap[it.id!!] = it + } + geofencesFlow.value = geofencesMap + } +}
\ No newline at end of file |