aboutsummaryrefslogtreecommitdiff
path: root/shared/src
diff options
context:
space:
mode:
authorIván Ávalos <avalos@disroot.org>2022-01-17 02:12:26 -0600
committerIván Ávalos <avalos@disroot.org>2022-01-17 02:12:26 -0600
commit9739f50ed78a74bdc689a575e0a3297da4d30ddf (patch)
treedba1228de5d586dbe3afbac319ffef5b3176e599 /shared/src
parentc8d423b0933b21c9e16475f9bf89160fc8861825 (diff)
parente38960ff845bddbd463c43801ad01834f77474f2 (diff)
downloadetbsa-trackermap-mobile-9739f50ed78a74bdc689a575e0a3297da4d30ddf.tar.gz
etbsa-trackermap-mobile-9739f50ed78a74bdc689a575e0a3297da4d30ddf.tar.bz2
etbsa-trackermap-mobile-9739f50ed78a74bdc689a575e0a3297da4d30ddf.zip
Merge branch 'main' of https://git.sr.ht/~avalos/trackermap-mobile
Diffstat (limited to 'shared/src')
-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