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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
|
/**
* TrackerMap
* Copyright (C) 2021-2022 Iván Ávalos <avalos@disroot.org>, Henoch Ojeda <imhenoch@protonmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package mx.trackermap.TrackerMap.android.units
import android.util.Log
import androidx.lifecycle.*
import com.mousebird.maply.Point2d
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.MapLayer
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
import kotlin.time.ExperimentalTime
@DelicateCoroutinesApi
@ExperimentalTime
class UnitsViewModel(
savedStateHandle: SavedStateHandle
) : ViewModel(), KoinComponent {
enum class UnitsDisplayMode {
MAP, LIST
}
data class Camera (
val point: Point2d,
val animated: Boolean,
)
private val unitsController: UnitsController by inject()
private val geofenceController: GeofencesController by inject()
private var _searchQuery = savedStateHandle.getLiveData("searchQuery", "")
private var _unitsDisplayMode = MutableLiveData(UnitsDisplayMode.MAP)
private var _units = MutableLiveData<List<UnitInformation>>()
private var _oldSelectedUnit = MutableLiveData<UnitInformation?>()
private var _selectedUnit = MutableLiveData<UnitInformation?>()
private var _mapLayerType = MutableLiveData<MapLayer.Type>()
private var _geofences = MutableLiveData<Map<Int, Geofence>>()
private val _camera = MutableLiveData<Camera?>()
val searchQuery: LiveData<String> get() = _searchQuery
val unitsDisplayMode: LiveData<UnitsDisplayMode> get() = _unitsDisplayMode
val units: LiveData<List<UnitInformation>> get() = _units
val oldSelectedUnit: LiveData<UnitInformation?> get() = _oldSelectedUnit
val selectedUnit: LiveData<UnitInformation?> get() = _selectedUnit
val mapLayerType: LiveData<MapLayer.Type> get() = _mapLayerType
val geofences: LiveData<Map<Int, Geofence>> get() = _geofences
val camera: LiveData<Camera?> get() = _camera
init {
Log.d("UnitsViewModel", "Initializing Units View Model")
unitsController.fetchUnits(viewModelScope)
viewModelScope.launch {
setupObservers()
}
viewModelScope.launch {
setupGeofenceObserver()
}
}
private suspend fun setupObservers() {
Log.d("UnitsViewModel", "Setup observers")
unitsController.displayedUnitsFlow.collect { units ->
this._units.value = units
updateSelectedUnit()
}
}
private suspend fun setupGeofenceObserver() {
geofenceController.geofencesFlow.collect {
this._geofences.postValue(it)
}
}
fun selectUnit(unit: UnitInformation?, switchToMap: Boolean = true) {
Log.d("UnitsViewModel", "Selecting unit ${unit?.device?.name}")
_oldSelectedUnit.postValue(_selectedUnit.value)
_selectedUnit.postValue(unit)
if (unit != null && switchToMap) {
setDisplayMode(UnitsDisplayMode.MAP)
}
}
fun selectUnitWithPositionId(id: Int?, switchToMap: Boolean = true) {
if (id == null) {
Log.d("UnitsViewModel", "Deselecting unit")
selectUnit(null, switchToMap = switchToMap)
return
}
Log.d("UnitsViewModel", "Selecting unit with position id: $id")
val unit = _units.value?.find { it.position?.id == id }
selectUnit(unit, switchToMap = switchToMap)
}
private fun selectUnitWithUnitId(id: Int?, switchToMap: Boolean = true) {
if (id == null) {
Log.d("UnitsViewModel", "Deselecting unit")
selectUnit(null, switchToMap = switchToMap)
return
}
Log.d("UnitsViewModel", "Selecting unit with device id: $id")
val unit = _units.value?.find { it.device.id == id }
selectUnit(unit, switchToMap = switchToMap)
}
private fun updateSelectedUnit() {
selectedUnit.value?.let { selected ->
Log.d("UnitsViewModel", "Updating selected unit with id: ${selected.device.id}")
selectUnitWithUnitId(selected.device.id, switchToMap = false)
}
}
fun setDisplayMode(displayMode: UnitsDisplayMode) {
Log.d("UnitsViewModel", "Setting Display mode to $displayMode")
_unitsDisplayMode.postValue(displayMode)
}
fun setMapLayerType(layer: MapLayer.Type) {
_mapLayerType.postValue(layer)
}
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)
}
fun moveCamera(point: Camera) {
_camera.postValue(point)
}
fun clearCamera() {
_camera.postValue(null)
}
}
|