diff options
Diffstat (limited to 'shared')
-rw-r--r-- | shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/UnitsController.kt | 9 |
1 files changed, 8 insertions, 1 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 b282a34..e74c7c4 100644 --- a/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/UnitsController.kt +++ b/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/UnitsController.kt @@ -32,6 +32,7 @@ import mx.trackermap.TrackerMap.client.models.UnitInformation import mx.trackermap.TrackerMap.utils.Coroutines import kotlin.time.Duration.Companion.seconds import kotlin.time.ExperimentalTime +import kotlinx.coroutines.Job @DelicateCoroutinesApi @ExperimentalTime @@ -47,6 +48,8 @@ class UnitsController( private val unitsFlow = MutableStateFlow<List<UnitInformation>>(emptyList()) private val queryFlow = MutableStateFlow("") + private var unitsUpdateJob: Job? = null + init { GlobalScope.launch { fetchUnits(this) @@ -57,7 +60,7 @@ class UnitsController( } private suspend fun fetchUnits(scope: CoroutineScope) { - Coroutines.tickerFlow(UPDATE_TIME.seconds) + unitsUpdateJob = Coroutines.tickerFlow(UPDATE_TIME.seconds) .map { print("Fetching new positions...\n") positionsApi.positionsGet() @@ -87,6 +90,10 @@ class UnitsController( .launchIn(scope) } + fun stopUpdates() { + unitsUpdateJob?.cancel() + } + fun search(query: String) { queryFlow.value = query } |