Differential D156 Diff 489 app/src/main/java/it/reyboz/bustorino/viewmodels/LivePositionsViewModel.kt
Changeset View
Changeset View
Standalone View
Standalone View
app/src/main/java/it/reyboz/bustorino/viewmodels/LivePositionsViewModel.kt
Show All 36 Lines | class LivePositionsViewModel(application: Application): AndroidViewModel(application) { | ||||
private val gtfsRepo = GtfsRepository(application) | private val gtfsRepo = GtfsRepository(application) | ||||
//private val updates = UpdatesMap() | //private val updates = UpdatesMap() | ||||
private val updatesLiveData = MutableLiveData<ArrayList<LivePositionUpdate>>() | private val updatesLiveData = MutableLiveData<ArrayList<LivePositionUpdate>>() | ||||
private val netVolleyManager = NetworkVolleyManager.getInstance(application) | private val netVolleyManager = NetworkVolleyManager.getInstance(application) | ||||
private var mqttClient = MQTTMatoClient.getInstance() | private var mqttClient = MQTTMatoClient() | ||||
private var lineListening = "" | private var lineListening = "" | ||||
private var lastTimeReceived: Long = 0 | private var lastTimeReceived: Long = 0 | ||||
private val gtfsRtRequestRunning = MutableLiveData<Boolean>(false) | private val gtfsRtRequestRunning = MutableLiveData<Boolean>(false) | ||||
/** | /** | ||||
* Responder to the MQTT Client | * Responder to the MQTT Client | ||||
▲ Show 20 Lines • Show All 96 Lines • ▼ Show 20 Lines | fun requestMatoPosUpdates(line: String){ | ||||
//updatePositions(1000) | //updatePositions(1000) | ||||
} | } | ||||
fun stopMatoUpdates(){ | fun stopMatoUpdates(){ | ||||
viewModelScope.launch { | viewModelScope.launch { | ||||
val tt = System.currentTimeMillis() | val tt = System.currentTimeMillis() | ||||
mqttClient.desubscribe(matoPositionListener) | mqttClient.stopMatoRequests(matoPositionListener) | ||||
val time = System.currentTimeMillis() -tt | val time = System.currentTimeMillis() -tt | ||||
Log.d(DEBUG_TI, "Took $time ms to unsubscribe") | Log.d(DEBUG_TI, "Took $time ms to unsubscribe") | ||||
} | } | ||||
} | } | ||||
fun retriggerPositionUpdate(){ | fun retriggerPositionUpdate(){ | ||||
if(updatesLiveData.value!=null){ | if(updatesLiveData.value!=null){ | ||||
Show All 40 Lines | class LivePositionsViewModel(application: Application): AndroidViewModel(application) { | ||||
fun requestDelayedGTFSUpdates(timems: Long){ | fun requestDelayedGTFSUpdates(timems: Long){ | ||||
viewModelScope.launch { | viewModelScope.launch { | ||||
delay(timems) | delay(timems) | ||||
requestGTFSUpdates() | requestGTFSUpdates() | ||||
} | } | ||||
} | } | ||||
override fun onCleared() { | |||||
//stop the MQTT Service | |||||
Log.d(DEBUG_TI, "Clearing the live positions view model, stopping the mqttClient") | |||||
mqttClient.closeClientForever() | |||||
super.onCleared() | |||||
} | |||||
companion object{ | companion object{ | ||||
private const val DEBUG_TI = "BusTO-LivePosViewModel" | private const val DEBUG_TI = "BusTO-LivePosViewModel" | ||||
} | } | ||||
} | } | ||||
No newline at end of file | No newline at end of file |
Public contents are in Creative Commons Attribution-ShareAlike 4.0 (CC-BY-SA) or GNU Free Documentation License (at your option) unless otherwise noted. · Contact / Register