Changeset View
Changeset View
Standalone View
Standalone View
src/it/reyboz/bustorino/fragments/LinesViewModel.kt
Show All 13 Lines | |||||
class LinesViewModel(application: Application) : AndroidViewModel(application) { | class LinesViewModel(application: Application) : AndroidViewModel(application) { | ||||
private val gtfsRepo: GtfsRepository | private val gtfsRepo: GtfsRepository | ||||
private val oldRepo: OldDataRepository | private val oldRepo: OldDataRepository | ||||
//val patternsByRouteLiveData: LiveData<List<MatoPattern>> | //val patternsByRouteLiveData: LiveData<List<MatoPattern>> | ||||
private val routeIDToSearch = MutableLiveData<String>() | private val routeIDToSearch = MutableLiveData<String>() | ||||
private var lastShownPatternStops = ArrayList<String>() | |||||
val stopsForPatternLiveData = MutableLiveData<List<Stop>>() | val stopsForPatternLiveData = MutableLiveData<List<Stop>>() | ||||
val executor = Executors.newFixedThreadPool(2) | val executor = Executors.newFixedThreadPool(2) | ||||
init { | init { | ||||
val gtfsDao = GtfsDatabase.getGtfsDatabase(application).gtfsDao() | val gtfsDao = GtfsDatabase.getGtfsDatabase(application).gtfsDao() | ||||
gtfsRepo = GtfsRepository(gtfsDao) | gtfsRepo = GtfsRepository(gtfsDao) | ||||
oldRepo = OldDataRepository(executor, NextGenDB.getInstance(application)) | oldRepo = OldDataRepository(executor, NextGenDB.getInstance(application)) | ||||
} | } | ||||
val routesGTTLiveData: LiveData<List<GtfsRoute>> by lazy{ | val routesGTTLiveData: LiveData<List<GtfsRoute>> by lazy{ | ||||
gtfsRepo.getLinesLiveDataForFeed("gtt") | gtfsRepo.getLinesLiveDataForFeed("gtt") | ||||
} | } | ||||
val patternsWithStopsByRouteLiveData = routeIDToSearch.switchMap { | val patternsWithStopsByRouteLiveData = routeIDToSearch.switchMap { | ||||
gtfsRepo.getPatternsWithStopsForRouteID(it) | gtfsRepo.getPatternsWithStopsForRouteID(it) | ||||
} | } | ||||
val routesName: LiveData<List<String>> = Transformations.map(routesGTTLiveData) { | |||||
it.map { route -> route.longName } | |||||
} | |||||
fun setRouteIDQuery(routeID: String){ | fun setRouteIDQuery(routeID: String){ | ||||
routeIDToSearch.value = routeID | routeIDToSearch.value = routeID | ||||
} | } | ||||
fun getRouteIDQueried(): String?{ | fun getRouteIDQueried(): String?{ | ||||
return routeIDToSearch.value | return routeIDToSearch.value | ||||
} | } | ||||
var shouldShowMessage = true; | var shouldShowMessage = true | ||||
fun requestStopsForGTFSIDs(gtfsIDs: List<String>){ | private fun requestStopsForGTFSIDs(gtfsIDs: List<String>){ | ||||
if (gtfsIDs.equals(lastShownPatternStops)){ | |||||
//nothing to do | |||||
return | |||||
} | |||||
oldRepo.requestStopsWithGtfsIDs(gtfsIDs) { | oldRepo.requestStopsWithGtfsIDs(gtfsIDs) { | ||||
if (it.isSuccess) { | if (it.isSuccess) { | ||||
stopsForPatternLiveData.postValue(it.result) | stopsForPatternLiveData.postValue(it.result) | ||||
} else { | } else { | ||||
Log.e("BusTO-LinesVM", "Got error on callback with stops for gtfsID") | Log.e("BusTO-LinesVM", "Got error on callback with stops for gtfsID") | ||||
it.exception?.printStackTrace() | it.exception?.printStackTrace() | ||||
} | } | ||||
} | } | ||||
lastShownPatternStops.clear() | |||||
for(id in gtfsIDs) | |||||
lastShownPatternStops.add(id) | |||||
} | } | ||||
fun requestStopsForPatternWithStops(patternStops: MatoPatternWithStops){ | fun requestStopsForPatternWithStops(patternStops: MatoPatternWithStops){ | ||||
val gtfsIDs = ArrayList<String>() | val gtfsIDs = ArrayList<String>() | ||||
for(pat in patternStops.stopsIndices){ | for(pat in patternStops.stopsIndices){ | ||||
gtfsIDs.add(pat.stopGtfsId) | gtfsIDs.add(pat.stopGtfsId) | ||||
} | } | ||||
requestStopsForGTFSIDs(gtfsIDs) | requestStopsForGTFSIDs(gtfsIDs) | ||||
} | } | ||||
/*fun getLinesGTT(): MutableLiveData<List<GtfsRoute>> { | /*fun getLinesGTT(): MutableLiveData<List<GtfsRoute>> { | ||||
val routesData = MutableLiveData<List<GtfsRoute>>() | val routesData = MutableLiveData<List<GtfsRoute>>() | ||||
viewModelScope.launch { | viewModelScope.launch { | ||||
val routes=gtfsRepo.getLinesForFeed("gtt") | val routes=gtfsRepo.getLinesForFeed("gtt") | ||||
routesData.postValue(routes) | routesData.postValue(routes) | ||||
} | } | ||||
return routesData | return routesData | ||||
}*/ | }*/ | ||||
} | } | ||||
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