Differential D236 Diff 781 app/src/main/java/it/reyboz/bustorino/viewmodels/ServiceAlertsViewModel.kt
Changeset View
Changeset View
Standalone View
Standalone View
app/src/main/java/it/reyboz/bustorino/viewmodels/ServiceAlertsViewModel.kt
| Show All 12 Lines | BusTO - View Model components | ||||
| GNU General Public License for more details. | GNU General Public License for more details. | ||||
| You should have received a copy of the GNU General Public License | You should have received a copy of the GNU General Public License | ||||
| along with this program. If not, see <http://www.gnu.org/licenses/>. | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||||
| */ | */ | ||||
| package it.reyboz.bustorino.viewmodels | package it.reyboz.bustorino.viewmodels | ||||
| import android.app.Application | import android.app.Application | ||||
| import android.content.Context | |||||
| import android.util.Log | import android.util.Log | ||||
| import androidx.lifecycle.AndroidViewModel | import androidx.lifecycle.AndroidViewModel | ||||
| import androidx.lifecycle.LiveData | |||||
| import androidx.lifecycle.MutableLiveData | import androidx.lifecycle.MutableLiveData | ||||
| import androidx.lifecycle.map | import androidx.lifecycle.map | ||||
| import androidx.lifecycle.switchMap | import androidx.lifecycle.switchMap | ||||
| import androidx.lifecycle.viewModelScope | import androidx.lifecycle.viewModelScope | ||||
| import androidx.room.concurrent.AtomicBoolean | import androidx.room.concurrent.AtomicBoolean | ||||
| import androidx.work.ExistingWorkPolicy | import androidx.work.ExistingWorkPolicy | ||||
| import androidx.work.WorkInfo | |||||
| import androidx.work.WorkManager | import androidx.work.WorkManager | ||||
| import com.google.transit.realtime.GtfsRealtime.Alert | import com.google.transit.realtime.GtfsRealtime.Alert | ||||
| import it.reyboz.bustorino.backend.NetworkVolleyManager | import it.reyboz.bustorino.backend.NetworkVolleyManager | ||||
| import it.reyboz.bustorino.backend.Stop | |||||
| import it.reyboz.bustorino.data.GtfsAlertDBDownloadWorker | import it.reyboz.bustorino.data.GtfsAlertDBDownloadWorker | ||||
| import it.reyboz.bustorino.data.GtfsRepository | import it.reyboz.bustorino.data.GtfsRepository | ||||
| import it.reyboz.bustorino.data.gtfs.GtfsDatabase | import it.reyboz.bustorino.data.gtfs.GtfsDatabase | ||||
| import kotlinx.coroutines.Dispatchers | import kotlinx.coroutines.Dispatchers | ||||
| import kotlinx.coroutines.delay | import kotlinx.coroutines.delay | ||||
| import kotlinx.coroutines.launch | import kotlinx.coroutines.launch | ||||
| import kotlin.time.Duration.Companion.seconds | import kotlin.time.Duration.Companion.seconds | ||||
| class ServiceAlertsViewModel(app: Application) : AndroidViewModel(app) { | class ServiceAlertsViewModel(app: Application) : AndroidViewModel(app) { | ||||
| private val gtfsRepo = GtfsRepository(app) | private val gtfsRepo = GtfsRepository(app) | ||||
| private val volleyManager = NetworkVolleyManager.getInstance(app) | private val volleyManager = NetworkVolleyManager.getInstance(app) | ||||
| private val alertsDao = GtfsDatabase.getGtfsDatabase(app).alertsDao() | private val alertsDao = GtfsDatabase.getGtfsDatabase(app).alertsDao() | ||||
| private val workManager = WorkManager.getInstance(app) | private val workManager = WorkManager.getInstance(app) | ||||
| //val alertsLiveData = MutableLiveData<ArrayList<Alert>>(ArrayList()) | //val alertsLiveData = MutableLiveData<ArrayList<Alert>>(ArrayList()) | ||||
| private val stopToFilter = MutableLiveData("") | private val stopGtfsIdToFilter = MutableLiveData<Stop>() | ||||
| private val routeToFilter = MutableLiveData("") | private val routeToFilter = MutableLiveData("") | ||||
| val lastTimeRunningDownload = MutableLiveData(0L) | val lastTimeRunningDownload = MutableLiveData(0L) | ||||
| private val keepRunning = AtomicBoolean(false) | private val keepRunning = AtomicBoolean(false) | ||||
| private val waitingToRerun = AtomicBoolean(false) | private val waitingToRerun = AtomicBoolean(false) | ||||
| fun setRunningDownloadRequests(value: Boolean) { | fun setRunningDownloadRequests(value: Boolean) { | ||||
| Log.d(DEBUG_TAG, "setRunningDownloadRequests: $value") | Log.d(DEBUG_TAG, "setRunningDownloadRequests: $value") | ||||
| keepRunning.set(value) | keepRunning.set(value) | ||||
| } | } | ||||
| val alertsByRouteLiveData = routeToFilter.switchMap { | val alertsByRouteLiveData = routeToFilter.switchMap { | ||||
| val unixTimestamp = (System.currentTimeMillis()/1000) | val unixTimestamp = (System.currentTimeMillis()/1000) | ||||
| gtfsRepo.getAlertsByRouteID(it).map{ l -> l.filter { al->al.isActive(unixTimestamp) }} | gtfsRepo.getAlertsByRouteID(it).map{ l -> l.filter { al->al.isActive(unixTimestamp) }} | ||||
| } | } | ||||
| val alertsByStopLiveData = stopToFilter.switchMap { | val alertsByStopLiveData = stopGtfsIdToFilter.switchMap { | ||||
| gtfsRepo.alertsDao.getAlertsForStop(it) | if(it.gtfsID!=null) gtfsRepo.alertsDao.getAlertsForStopGtfsId(it.gtfsID!!) else MutableLiveData() | ||||
| } | } | ||||
| val allAlertsLiveData = gtfsRepo.alertsDao.getAllAlertsLiveData() | val allAlertsLiveData = gtfsRepo.alertsDao.getAllAlertsLiveData() | ||||
| /* | /* | ||||
| private val volleyErrorListener = Response.ErrorListener { err -> | private val volleyErrorListener = Response.ErrorListener { err -> | ||||
| Log.e(DEBUG_TAG, "Error getting alerts: ${err.message}", err) | Log.e(DEBUG_TAG, "Error getting alerts: ${err.message}", err) | ||||
| } | } | ||||
| private var numTries = 0 | private var numTries = 0 | ||||
| Show All 12 Lines | class ServiceAlertsViewModel(app: Application) : AndroidViewModel(app) { | ||||
| private fun requestAlerts(){ | private fun requestAlerts(){ | ||||
| val req = GtfsRtAlertsRequest(volleyErrorListener, responseListener) | val req = GtfsRtAlertsRequest(volleyErrorListener, responseListener) | ||||
| volleyManager.requestQueue.add(req) | volleyManager.requestQueue.add(req) | ||||
| } | } | ||||
| */ | */ | ||||
| fun setStopFilter(stopId: String) { | /// WE DO NOT KNOW HOW TO GET THE GTFS STOP ID, the one given by MaTO is the same as the stop CODE | ||||
| stopToFilter.value = stopId | /// but we need the ID from the stops.txt table of GTT GTFS data | ||||
| /// DISABLING THIS FUNCTION | |||||
| /*fun setStopFilter(stop: Stop) { | |||||
| Log.d(DEBUG_TAG, "Setting stop to filter: ${stop.ID} - ${stop.stopDisplayName}, gtfsID: ${stop.gtfsID}") | |||||
| stopGtfsIdToFilter.value = stop | |||||
| } | } | ||||
| */ | |||||
| fun setGtfsLineFilter(routeId: String) { | fun setGtfsLineFilter(routeId: String) { | ||||
| routeToFilter.value = routeId | routeToFilter.value = routeId | ||||
| } | } | ||||
| private fun downloadWorkIfTimePassed(){ | private fun downloadWorkIfTimePassed(){ | ||||
| val currentTime = System.currentTimeMillis() | val currentTime = System.currentTimeMillis() | ||||
| waitingToRerun.set(false) | waitingToRerun.set(false) | ||||
| val diff = currentTime - lastTimeRunningDownload.value!! | val diff = currentTime - lastTimeRunningDownload.value!! | ||||
| Log.d(DEBUG_TAG, "diff : ${diff/1000} s") | Log.d(DEBUG_TAG, "diff : ${diff/1000} s") | ||||
| val MINUTES_CHECK = 3 | val MINUTES_CHECK = 3 | ||||
| if (lastTimeRunningDownload.value == 0L || | if (lastTimeRunningDownload.value == 0L || | ||||
| currentTime > lastTimeRunningDownload.value!! + MINUTES_CHECK*60*1000){ | currentTime > lastTimeRunningDownload.value!! + MINUTES_CHECK*60*1000){ | ||||
| //actually enqueue request | //actually enqueue request | ||||
| Log.d(DEBUG_TAG, "Launching request to download alerts") | Log.d(DEBUG_TAG, "Launching request to download alerts") | ||||
| val req = GtfsAlertDBDownloadWorker.makeOneTimeRequest("alertsrn") | val req = GtfsAlertDBDownloadWorker.makeOneTimeRequest(WORK_TAG) | ||||
| workManager.enqueueUniqueWork("AlertsDownloadsRun", ExistingWorkPolicy.KEEP, req) | workManager.enqueueUniqueWork("AlertsDownloadsRun", ExistingWorkPolicy.KEEP, req) | ||||
| lastTimeRunningDownload.postValue(System.currentTimeMillis()) | lastTimeRunningDownload.postValue(System.currentTimeMillis()) | ||||
| } | } | ||||
| viewModelScope.launch(Dispatchers.IO) { | viewModelScope.launch(Dispatchers.IO) { | ||||
| waitingToRerun.set(true) | waitingToRerun.set(true) | ||||
| delay((61).seconds) | delay((61).seconds) | ||||
| if(keepRunning.get()) downloadWorkIfTimePassed() | if(keepRunning.get()) downloadWorkIfTimePassed() | ||||
| } | } | ||||
| } | } | ||||
| fun launchAlertsPeriodCheck(){ | fun launchAlertsPeriodCheck(){ | ||||
| setRunningDownloadRequests(true) | setRunningDownloadRequests(true) | ||||
| if(!waitingToRerun.get()) | if(!waitingToRerun.get()) | ||||
| downloadWorkIfTimePassed() | downloadWorkIfTimePassed() | ||||
| } | } | ||||
| fun getDownloadStatusLiveData(context: Context): LiveData<List<WorkInfo>>{ | |||||
| val workManager = WorkManager.getInstance(context) | |||||
| return workManager.getWorkInfosByTagLiveData(WORK_TAG) | |||||
| } | |||||
| private fun filterAlertsForStop(stopId: String, alerts: ArrayList<Alert>) : ArrayList<Alert>{ | private fun filterAlertsForStop(stopId: String, alerts: ArrayList<Alert>) : ArrayList<Alert>{ | ||||
| val filteredAlerts = ArrayList<Alert>() | val filteredAlerts = ArrayList<Alert>() | ||||
| for (al in alerts) { | for (al in alerts) { | ||||
| for (ie in al.informedEntityList) { | for (ie in al.informedEntityList) { | ||||
| if (ie.stopId == stopId) { | if (ie.stopId == stopId) { | ||||
| filteredAlerts.add(al) | filteredAlerts.add(al) | ||||
| ▲ Show 20 Lines • Show All 45 Lines • ▼ Show 20 Lines | init{ | ||||
| } | } | ||||
| */ | */ | ||||
| } | } | ||||
| companion object{ | companion object{ | ||||
| private const val DEBUG_TAG = "BusTO-GTFSRTAlerts" | private const val DEBUG_TAG = "BusTO-GTFSRTAlerts" | ||||
| public const val WORK_TAG = "AlertsDownloadWorker" | |||||
| } | } | ||||
| } | } | ||||
| 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