aboutsummaryrefslogtreecommitdiff
path: root/shared
diff options
context:
space:
mode:
Diffstat (limited to 'shared')
-rw-r--r--shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/UnitsController.kt45
-rw-r--r--shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/utils/Coroutines.kt20
2 files changed, 51 insertions, 14 deletions
diff --git a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/UnitsController.kt b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/UnitsController.kt
index 584f656..21692e7 100644
--- a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/UnitsController.kt
+++ b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/UnitsController.kt
@@ -1,17 +1,23 @@
package mx.trackermap.TrackerMap.controllers
+import kotlinx.coroutines.CoroutineScope
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.flow.*
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
+import mx.trackermap.TrackerMap.utils.Coroutines
+import kotlin.time.Duration.Companion.seconds
+import kotlin.time.ExperimentalTime
@DelicateCoroutinesApi
+@ExperimentalTime
class UnitsController(
private val devicesApi: DevicesApi,
private val positionsApi: PositionsApi
@@ -23,28 +29,39 @@ class UnitsController(
init {
GlobalScope.launch {
- fetchUnits()
+ fetchUnits(this)
}
GlobalScope.launch {
setupFlows()
}
}
- private 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
+ private suspend fun fetchUnits(scope: CoroutineScope) {
+ Coroutines.tickerFlow(10.seconds)
+ .map {
+ print("Fetching new positions...\n")
+ positionsApi.positionsGet()
}
- }
+ .distinctUntilChanged { old, new ->
+ old.contentEquals(new)
+ }
+ .onEach { positions ->
+ print("Position(s) changed! Reloading data...\n")
+ val devices = devicesApi.devicesGet()
+ val positionsMap: MutableMap<Int, Position> = mutableMapOf()
- val units = devices.map {
- UnitInformation(it, positionsMap[it.id])
- }
- unitsFlow.value = units
+ positions.forEach { position ->
+ position.deviceId?.let {
+ positionsMap[it] = position
+ }
+ }
+
+ val units = devices.map {
+ UnitInformation(it, positionsMap[it.id])
+ }
+ unitsFlow.value = units
+ }
+ .launchIn(scope)
}
fun search(query: String) {
diff --git a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/utils/Coroutines.kt b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/utils/Coroutines.kt
new file mode 100644
index 0000000..c953887
--- /dev/null
+++ b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/utils/Coroutines.kt
@@ -0,0 +1,20 @@
+package mx.trackermap.TrackerMap.utils
+
+import kotlinx.coroutines.delay
+import kotlinx.coroutines.flow.flow
+import kotlin.time.Duration
+import kotlin.time.ExperimentalTime
+
+class Coroutines {
+ companion object {
+ // Source: https://stackoverflow.com/questions/54827455/how-to-implement-timer-with-kotlin-coroutines/54828055#54828055
+ @ExperimentalTime
+ fun tickerFlow(period: Duration, initialDelay: Duration = Duration.ZERO) = flow {
+ delay(initialDelay)
+ while (true) {
+ emit(Unit)
+ delay(period)
+ }
+ }
+ }
+} \ No newline at end of file