diff options
author | Isidro Henoch <imhenoch@protonmail.com> | 2021-12-08 04:28:51 -0600 |
---|---|---|
committer | Isidro Henoch <imhenoch@protonmail.com> | 2021-12-08 04:28:51 -0600 |
commit | fabb451dbdbd28b0f09b91ee193d6d62ed027df7 (patch) | |
tree | 896334a1d8a0540b9d54c7390db2d78e61cadb1b /shared | |
parent | fce7acfe3581e5bb13bf15debb4289a1735d12ff (diff) | |
download | etbsa-trackermap-mobile-fabb451dbdbd28b0f09b91ee193d6d62ed027df7.tar.gz etbsa-trackermap-mobile-fabb451dbdbd28b0f09b91ee193d6d62ed027df7.tar.bz2 etbsa-trackermap-mobile-fabb451dbdbd28b0f09b91ee193d6d62ed027df7.zip |
WIP: Implements the basic structure for the units list/map
- Updates the Devices and Positions APIs to properly construct the Url's query when there are null values
- Adds a units controller to the shared module
- Adds a devices and map fragment that each print the fetched units on the console
- Adds a units view model to connect previously mentioned fragments with the units controller
Diffstat (limited to 'shared')
6 files changed, 116 insertions, 28 deletions
diff --git a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/apis/DevicesApi.kt b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/apis/DevicesApi.kt index bc87f78..331cc4e 100644 --- a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/apis/DevicesApi.kt +++ b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/apis/DevicesApi.kt @@ -29,7 +29,13 @@ class DevicesApi(basePath: kotlin.String = "https://demo.traccar.org/api") : Api */ @Suppress("UNCHECKED_CAST") suspend fun devicesGet(all: kotlin.Boolean? = null, userId: kotlin.Int? = null, id: kotlin.Int? = null, uniqueId: kotlin.String? = null): kotlin.Array<Device> { - val localVariableQuery: MultiValueMap = mapOf("all" to listOf("$all"), "userId" to listOf("$userId"), "id" to listOf("$id"), "uniqueId" to listOf("$uniqueId")) + val query: MutableMap<String, List<String>> = mutableMapOf() + all?.let { query["all"] = listOf("$it") } + userId?.let { query["userId"] = listOf("$it") } + id?.let { query["id"] = listOf("$it") } + uniqueId?.let { query["uniqueId"] = listOf("$it") } + val localVariableQuery: MultiValueMap = query + val localVariableConfig = RequestConfig( RequestMethod.GET, "/devices", query = localVariableQuery diff --git a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/apis/PositionsApi.kt b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/apis/PositionsApi.kt index 8c1b73e..278611f 100644 --- a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/apis/PositionsApi.kt +++ b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/apis/PositionsApi.kt @@ -27,8 +27,14 @@ class PositionsApi(basePath: kotlin.String = "https://demo.traccar.org/api") : A * @return kotlin.Array<Position> */ @Suppress("UNCHECKED_CAST") - suspend fun positionsGet(deviceId: kotlin.Int? = null, from: java.time.LocalDateTime? = null, to: java.time.LocalDateTime? = null, id: kotlin.Int? = null): kotlin.Array<Position> { - val localVariableQuery: MultiValueMap = mapOf("deviceId" to listOf("$deviceId"), "from" to listOf("$from"), "to" to listOf("$to"), "id" to listOf("$id")) + suspend fun positionsGet(deviceId: kotlin.Int? = null, from: LocalDateTimeAdapter? = null, to: LocalDateTimeAdapter? = null, id: kotlin.Int? = null): kotlin.Array<Position> { + val query: MutableMap<String, List<String>> = mutableMapOf() + deviceId?.let { query["deviceId"] = listOf("$it") } + from?.let { query["from"] = listOf("$it") } + to?.let { query["to"] = listOf("$it") } + id?.let { query["id"] = listOf("$it") } + val localVariableQuery: MultiValueMap = query + val localVariableConfig = RequestConfig( RequestMethod.GET, "/positions", query = localVariableQuery diff --git a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/models/Device.kt b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/models/Device.kt index 3729345..f8a1642 100644 --- a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/models/Device.kt +++ b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/models/Device.kt @@ -11,6 +11,9 @@ */ package mx.trackermap.TrackerMap.client.models +import kotlinx.datetime.LocalDateTime +import kotlinx.serialization.Serializable + /** * @@ -29,22 +32,22 @@ package mx.trackermap.TrackerMap.client.models * @param geofenceIds * @param attributes */ +@Serializable data class Device ( - val id: kotlin.Int? = null, - val name: kotlin.String? = null, - val uniqueId: kotlin.String? = null, - val status: kotlin.String? = null, - val disabled: kotlin.Boolean? = null, + val id: Int, + val name: String, + val uniqueId: String? = null, + val status: String? = null, + val disabled: Boolean? = null, /* in IS0 8601 format. eg. `1963-11-22T18:30:00Z` */ - val lastUpdate: java.time.LocalDateTime? = null, - val positionId: kotlin.Int? = null, - val groupId: kotlin.Int? = null, - val phone: kotlin.String? = null, - val model: kotlin.String? = null, - val contact: kotlin.String? = null, - val category: kotlin.String? = null, - val geofenceIds: kotlin.Array<kotlin.Int>? = null, - val attributes: kotlin.Any? = null +// val lastUpdate: LocalDateTime? = null, + val positionId: Int? = null, + val groupId: Int? = null, + val phone: String? = null, + val model: String? = null, + val contact: String? = null, + val category: String? = null, + val geofenceIds: Array<Int>? = null ) { }
\ No newline at end of file diff --git a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/models/Position.kt b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/models/Position.kt index 0ce4ed2..d765825 100644 --- a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/models/Position.kt +++ b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/models/Position.kt @@ -11,6 +11,9 @@ */ package mx.trackermap.TrackerMap.client.models +import kotlinx.datetime.LocalDateTime +import kotlinx.serialization.Serializable + /** * @@ -32,28 +35,27 @@ package mx.trackermap.TrackerMap.client.models * @param network * @param attributes */ +@Serializable data class Position ( val id: kotlin.Int? = null, val deviceId: kotlin.Int? = null, val protocol: kotlin.String? = null, /* in IS0 8601 format. eg. `1963-11-22T18:30:00Z` */ - val deviceTime: java.time.LocalDateTime? = null, +// val deviceTime: LocalDateTime? = null, /* in IS0 8601 format. eg. `1963-11-22T18:30:00Z` */ - val fixTime: java.time.LocalDateTime? = null, +// val fixTime: LocalDateTime? = null, /* in IS0 8601 format. eg. `1963-11-22T18:30:00Z` */ - val serverTime: java.time.LocalDateTime? = null, +// val serverTime: LocalDateTime? = null, val outdated: kotlin.Boolean? = null, val valid: kotlin.Boolean? = null, - val latitude: java.math.BigDecimal? = null, - val longitude: java.math.BigDecimal? = null, - val altitude: java.math.BigDecimal? = null, + val latitude: Double? = null, + val longitude: Double? = null, + val altitude: Double? = null, /* in knots */ - val speed: java.math.BigDecimal? = null, - val course: java.math.BigDecimal? = null, + val speed: Double? = null, + val course: Double? = null, val address: kotlin.String? = null, - val accuracy: java.math.BigDecimal? = null, - val network: kotlin.Any? = null, - val attributes: kotlin.Any? = null + val accuracy: Double? = null ) { }
\ No newline at end of file diff --git a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/models/UnitInformation.kt b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/models/UnitInformation.kt new file mode 100644 index 0000000..edebff0 --- /dev/null +++ b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/client/models/UnitInformation.kt @@ -0,0 +1,6 @@ +package mx.trackermap.TrackerMap.client.models + +data class UnitInformation( + val device: Device, + val position: Position? +)
\ No newline at end of file diff --git a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/UnitsController.kt b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/UnitsController.kt new file mode 100644 index 0000000..9c11b79 --- /dev/null +++ b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/UnitsController.kt @@ -0,0 +1,65 @@ +package mx.trackermap.TrackerMap.controllers + +import kotlinx.coroutines.DelicateCoroutinesApi +import kotlinx.coroutines.GlobalScope +import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.flow.collect +import kotlinx.coroutines.flow.combine +import kotlinx.coroutines.launch +import mx.trackermap.TrackerMap.client.apis.DevicesApi +import mx.trackermap.TrackerMap.client.apis.PositionsApi +import mx.trackermap.TrackerMap.client.models.Position +import mx.trackermap.TrackerMap.client.models.UnitInformation + +@DelicateCoroutinesApi +class UnitsController( + private val devicesApi: DevicesApi, + private val positionsApi: PositionsApi +) { + + val displayedUnitsFlow = MutableStateFlow<List<UnitInformation>>(emptyList()) + private val unitsFlow = MutableStateFlow<List<UnitInformation>>(emptyList()) + private val queryFlow = MutableStateFlow("") + + init { + GlobalScope.launch { + fetchUnits() + } + GlobalScope.launch { + setupFlows() + } + } + + suspend fun fetchUnits() { + val devices = devicesApi.devicesGet() + val positions = positionsApi.positionsGet() + val positionsMap: MutableMap<Int, Position> = mutableMapOf() + + positions.forEach { position -> + position.deviceId?.let { + positionsMap[it] = position + } + } + + val units = devices.map { + UnitInformation(it, positionsMap[it.id]) + } + unitsFlow.value = units + } + + fun search(query: String) { + queryFlow.value = query + } + + suspend fun setupFlows() { + unitsFlow.combine(queryFlow) { units, query -> + units.filter { unit -> + unit.device.name.contains(query) || + unit.device.contact?.contains(query) == true || + unit.position?.address?.contains(query) == true + } + }.collect { units -> + this.displayedUnitsFlow.value = units + } + } +}
\ No newline at end of file |