aboutsummaryrefslogtreecommitdiff
path: root/shared/src/commonMain/kotlin/mx/trackermap/TrackerMap/controllers/GeofencesController.kt
blob: fc336d1e40228793b53bd9f83424df225d0a564a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
    }
}