diff --git a/app/src/main/java/it/reyboz/bustorino/backend/VehicleUtils.kt b/app/src/main/java/it/reyboz/bustorino/backend/VehicleUtils.kt --- a/app/src/main/java/it/reyboz/bustorino/backend/VehicleUtils.kt +++ b/app/src/main/java/it/reyboz/bustorino/backend/VehicleUtils.kt @@ -46,8 +46,8 @@ VehicleClassInfo(9000, "BYD K9", "E-Bus", 9120, 9121), VehicleClassInfo(9200, "Citymood", "Bus", 9200, 9251), VehicleClassInfo(9200, "Citymood", "Bus", 9252, 9261), - VehicleClassInfo(9400, "E-Way", "E-Bus", 9400, 9599), - VehicleClassInfo(9600, "E-Way 18m", "E-Bus", 9600, 9699) + VehicleClassInfo(9400, "Iveco E-Way", "E-Bus", 9400, 9599), + VehicleClassInfo(9600, "Iveco E-Way 18m", "E-Bus", 9600, 9699) ) fun getTypeForLabel(label: String): VehicleClassInfo? { diff --git a/app/src/main/java/it/reyboz/bustorino/data/MatoTripsDownloadWorker.kt b/app/src/main/java/it/reyboz/bustorino/data/MatoTripsDownloadWorker.kt --- a/app/src/main/java/it/reyboz/bustorino/data/MatoTripsDownloadWorker.kt +++ b/app/src/main/java/it/reyboz/bustorino/data/MatoTripsDownloadWorker.kt @@ -28,6 +28,7 @@ class MatoTripsDownloadWorker(appContext: Context, workerParams: WorkerParameters) : CoroutineWorker(appContext, workerParams) { + private val DOWNLOAD_STEP = 50 override suspend fun doWork(): Result { @@ -41,7 +42,7 @@ var i = 0 var totDown = 0 while (i protected var locationEngine: MapLibreLocationEngine? = null - protected lateinit var locationProvider: FusedNativeLocationProvider + protected var locationProvider: FusedNativeLocationProvider? = null protected var shownToastNoPosition = false protected var locationEnabledOnDevice = true @@ -185,8 +186,9 @@ protected val animatorsByVeh = HashMap() protected var vehShowing = "" protected var lastUpdateTime:Long = -2 + protected var jobUpdate: Job? = null - private val lifecycleOwnerLiveData = getViewLifecycleOwnerLiveData() + //private val lifecycleOwnerLiveData = viewLifecycleOwnerLiveData //extra items to use the LibreMap @@ -253,6 +255,7 @@ } override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { + //TODO: Re-create map when this preference changes lastMapStyle = PreferencesHolder.getMapLibreStyleFile(requireContext()) Log.d(DEBUG_TAG, "onCreateView lastMapStyle: $lastMapStyle") return super.onCreateView(inflater, container, savedInstanceState) @@ -311,6 +314,7 @@ } override fun onStop() { + locationProvider?.removeListener(deviceLocationStatusListener) mapView?.onStop() super.onStop() } @@ -323,7 +327,6 @@ override fun onDestroyView() { bottomLayout = null - locationProvider.removeListener(deviceLocationStatusListener) mapInitialized = false locationInitialized = false super.onDestroyView() @@ -478,9 +481,10 @@ mStyle?.let{ style -> locationComponent = map.locationComponent - locationProvider = FusedNativeLocationProvider(context) - locationProvider.addListener(deviceLocationStatusListener) - locationEngine = MapLibreLocationEngine(locationProvider) + val locProvider = FusedNativeLocationProvider(context) + locProvider.addListener(deviceLocationStatusListener) + locationEngine = MapLibreLocationEngine(locProvider) + locationProvider = locProvider val options = LocationComponentActivationOptions.builder(context, style) .useDefaultLocationEngine(false) .locationEngine(locationEngine) @@ -709,20 +713,30 @@ /** * Update the bus positions displayed on the map, from the existing data * - * @param forced If true, forces immediate update ignoring the 60ms throttle + * @param forced If true, forces immediate update ignoring the 100ms throttle */ protected fun updatePositionsIcons(forced: Boolean) { // Avoid frequent updates - throttle to max once per 60ms val currentTime = System.currentTimeMillis() - if (!forced && currentTime - lastUpdateTime < 60) { + val isStarted = (lifecycle.currentState.isAtLeast(Lifecycle.State.STARTED)) + if(forced){ + // if we're running a forced update, cancel the pending one + jobUpdate?.cancel() + } + else if (currentTime - lastUpdateTime < 100) { // Schedule delayed update - if(lifecycleOwnerLiveData.value != null) - viewLifecycleOwner.lifecycleScope.launch { - delay(200.milliseconds) - updatePositionsIcons(forced) + if(viewLifecycleOwnerLiveData.value != null) { + jobUpdate?.cancel() + jobUpdate = viewLifecycleOwner.lifecycleScope.launch { + delay(100.milliseconds) + updatePositionsIcons(false) } + } return } + if(!isStarted){ + Log.w(DEBUG_TAG, "fragment is not started, ") + } val busFeatures = ArrayList() val selectedBusFeatures = ArrayList() diff --git a/app/src/main/java/it/reyboz/bustorino/viewmodels/LivePositionsViewModel.kt b/app/src/main/java/it/reyboz/bustorino/viewmodels/LivePositionsViewModel.kt --- a/app/src/main/java/it/reyboz/bustorino/viewmodels/LivePositionsViewModel.kt +++ b/app/src/main/java/it/reyboz/bustorino/viewmodels/LivePositionsViewModel.kt @@ -46,6 +46,7 @@ import kotlin.collections.HashSet import androidx.core.content.edit import androidx.lifecycle.MutableLiveData +import kotlin.text.contains typealias FullPositionUpdatesMap = HashMap> typealias FullPositionUpdate = Pair @@ -177,7 +178,9 @@ //find the trip IDs in the updates private val tripsIDsInUpdates = positionsToBeMatchedLiveData.map { it -> //Log.d(DEBUG_TI, "Updates map has keys ${upMap.keys}") - it.map { pos -> "gtt:"+pos.tripID } + it.map { pos -> "gtt:"+pos.tripID }.filter{ s-> + !(s.contains("null") || s.trim() =="gtt:") + } } // get the trip IDs in the DB @@ -191,7 +194,9 @@ val tripNames=tripswithPatterns.map { twp-> twp.trip.tripID } Log.i(DEBUG_TI, "Have ${tripswithPatterns.size} trips in the DB") if (tripsIDsInUpdates.value!=null) - return@map tripsIDsInUpdates.value!!.filter { !(tripNames.contains(it) || it.contains("null"))} + return@map tripsIDsInUpdates.value!!.filter { !( + tripNames.contains(it) || it.contains("null") || it =="gtt:" + )}.distinct() else { Log.e(DEBUG_TI,"Got results for gtfsTripsInDB but not tripsIDsInUpdates??") return@map ArrayList()