package mx.trackermap.TrackerMap.android.units import android.util.Log import androidx.lifecycle.* import com.mousebird.maply.RemoteTileInfoNew import com.mousebird.maply.TileInfoNew import kotlinx.coroutines.DelicateCoroutinesApi import kotlinx.coroutines.flow.collect import kotlinx.coroutines.launch import mx.trackermap.TrackerMap.client.models.Geofence import mx.trackermap.TrackerMap.client.models.UnitInformation import mx.trackermap.TrackerMap.controllers.GeofencesController import mx.trackermap.TrackerMap.controllers.UnitsController import org.koin.core.component.KoinComponent import org.koin.core.component.inject @DelicateCoroutinesApi class UnitsViewModel( savedStateHandle: SavedStateHandle ) : ViewModel(), KoinComponent { enum class UnitsDisplayMode { MAP, LIST } private val unitsController: UnitsController by inject() private val geofenceController: GeofencesController by inject() private var _searchQuery = savedStateHandle.getLiveData("searchQuery", "") private var _unitsDisplayMode = MutableLiveData(UnitsDisplayMode.LIST) private var _units = MutableLiveData>() private var _selectedUnit = MutableLiveData() private var _mapTileInfo = MutableLiveData() private var _geofences = MutableLiveData>() val searchQuery: LiveData get() = _searchQuery val unitsDisplayMode: LiveData get() = _unitsDisplayMode val units: LiveData> get() = _units val selectedUnit: LiveData get() = _selectedUnit val mapTileInfo: LiveData get() = _mapTileInfo val geofences: LiveData> get() = _geofences init { Log.d("UnitsViewModel", "Initializing Units View Model") viewModelScope.launch { setupObservers() } viewModelScope.launch { setupGeofenceObserver() } } private suspend fun setupObservers() { Log.d("UnitsViewModel", "Setup observers") unitsController.displayedUnitsFlow.collect { units -> this._units.value = units } } private suspend fun setupGeofenceObserver() { geofenceController.geofencesFlow.collect { this._geofences.postValue(it) } } fun selectUnit(unit: UnitInformation) { Log.d("UnitsViewModel", "Selecting unit ${unit.device.name}") _selectedUnit.postValue(unit) setDisplayMode(UnitsDisplayMode.MAP) } fun selectUnitWith(positionId: Int?) { if (positionId == null) { Log.d("UnitsViewModel", "Deselecting unit") _selectedUnit.postValue(null) return } Log.d("UnitsViewModel", "Selecting unit with position id: $positionId") val unit = _units.value?.find { it.position?.id == positionId } _selectedUnit.postValue(unit) } fun setDisplayMode(displayMode: UnitsDisplayMode) { Log.d("UnitsViewModel", "Setting Display mode to $displayMode") _unitsDisplayMode.postValue(displayMode) } fun setMapLayer(url: String, minZoom: Int = 0, maxZoom: Int = 21) { val tileInfo = RemoteTileInfoNew(url, minZoom, maxZoom) _mapTileInfo.postValue(tileInfo) } fun toggleDisplayMode() { Log.d("UnitsViewModel", "Toggling Display mode") val newDisplayMode = if (unitsDisplayMode.value == UnitsDisplayMode.MAP) { _selectedUnit.postValue(null) UnitsDisplayMode.LIST } else { UnitsDisplayMode.MAP } _unitsDisplayMode.postValue(newDisplayMode) } fun search(query: String) { unitsController.search(query) } }