diff --git a/app/src/main/java/it/reyboz/bustorino/backend/gtfs/PolylineParser.java b/app/src/main/java/it/reyboz/bustorino/backend/gtfs/PolylineParser.java index cac9905..4bdc54f 100644 --- a/app/src/main/java/it/reyboz/bustorino/backend/gtfs/PolylineParser.java +++ b/app/src/main/java/it/reyboz/bustorino/backend/gtfs/PolylineParser.java @@ -1,48 +1,48 @@ package it.reyboz.bustorino.backend.gtfs; -import org.osmdroid.util.GeoPoint; +import org.maplibre.android.geometry.LatLng; import java.util.ArrayList; public abstract class PolylineParser { /** * Decode a Google polyline * Thanks to https://stackoverflow.com/questions/9341020/how-to-decode-googles-polyline-algorithm * @param encodedPolyline the encoded polyline in a string * @param initial_capacity for the list * @return the list of points correspoding to the polyline */ - public static ArrayList decodePolyline(String encodedPolyline, int initial_capacity) { - ArrayList points = new ArrayList<>(initial_capacity); + public static ArrayList decodePolyline(String encodedPolyline, int initial_capacity) { + ArrayList points = new ArrayList<>(initial_capacity); int truck = 0; int carriage_q = 0; int longit=0, latit=0; boolean is_lat=true; for (int x = 0, xx = encodedPolyline.length(); x < xx; ++x) { int i = encodedPolyline.charAt(x); i -= 63; int _5_bits = i << (32 - 5) >>> (32 - 5); truck |= _5_bits << carriage_q; carriage_q += 5; boolean is_last = (i & (1 << 5)) == 0; if (is_last) { boolean is_negative = (truck & 1) == 1; truck >>>= 1; if (is_negative) { truck = ~truck; } if (is_lat){ latit += truck; is_lat = false; } else{ longit += truck; - points.add(new GeoPoint((double)latit/1e5,(double)longit/1e5)); + points.add(new LatLng((double)latit/1e5,(double)longit/1e5)); is_lat=true; } carriage_q = 0; truck = 0; } } return points; } } diff --git a/app/src/main/java/it/reyboz/bustorino/fragments/LinesDetailFragment.kt b/app/src/main/java/it/reyboz/bustorino/fragments/LinesDetailFragment.kt index ae9d5d9..027df58 100644 --- a/app/src/main/java/it/reyboz/bustorino/fragments/LinesDetailFragment.kt +++ b/app/src/main/java/it/reyboz/bustorino/fragments/LinesDetailFragment.kt @@ -1,907 +1,1312 @@ /* BusTO - Fragments components Copyright (C) 2023 Fabio Mazza This program is free software: you can redistribute it and/or modify it under the terms of the GNU 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 General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ package it.reyboz.bustorino.fragments + +import android.Manifest import android.animation.ObjectAnimator +import android.animation.ValueAnimator import android.annotation.SuppressLint import android.content.Context import android.content.SharedPreferences -import android.graphics.Paint +import android.location.Location import android.os.Bundle import android.util.Log import android.view.LayoutInflater import android.view.View import android.view.ViewGroup +import android.view.animation.LinearInterpolator import android.widget.* +import androidx.activity.result.ActivityResultCallback import androidx.activity.result.contract.ActivityResultContracts import androidx.appcompat.content.res.AppCompatResources +import androidx.cardview.widget.CardView import androidx.core.content.ContextCompat import androidx.core.content.res.ResourcesCompat import androidx.fragment.app.viewModels -import androidx.lifecycle.lifecycleScope import androidx.preference.PreferenceManager import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView +import com.google.android.material.bottomsheet.BottomSheetBehavior +import com.google.gson.JsonObject import it.reyboz.bustorino.R import it.reyboz.bustorino.adapters.NameCapitalize import it.reyboz.bustorino.adapters.StopAdapterListener import it.reyboz.bustorino.adapters.StopRecyclerAdapter import it.reyboz.bustorino.backend.FiveTNormalizer import it.reyboz.bustorino.backend.Stop import it.reyboz.bustorino.backend.gtfs.GtfsUtils import it.reyboz.bustorino.backend.gtfs.LivePositionUpdate import it.reyboz.bustorino.backend.gtfs.PolylineParser import it.reyboz.bustorino.backend.utils import it.reyboz.bustorino.data.MatoTripsDownloadWorker import it.reyboz.bustorino.data.PreferencesHolder -import it.reyboz.bustorino.data.gtfs.MatoPattern import it.reyboz.bustorino.data.gtfs.MatoPatternWithStops import it.reyboz.bustorino.data.gtfs.TripAndPatternWithStops import it.reyboz.bustorino.map.* import it.reyboz.bustorino.map.CustomInfoWindow.TouchResponder import it.reyboz.bustorino.middleware.LocationUtils import it.reyboz.bustorino.util.Permissions import it.reyboz.bustorino.viewmodels.LinesViewModel import it.reyboz.bustorino.viewmodels.LivePositionsViewModel -import kotlinx.coroutines.delay -import kotlinx.coroutines.launch -import org.osmdroid.config.Configuration -import org.osmdroid.tileprovider.tilesource.TileSourceFactory -import org.osmdroid.util.BoundingBox -import org.osmdroid.util.GeoPoint -import org.osmdroid.views.MapView -import org.osmdroid.views.overlay.FolderOverlay -import org.osmdroid.views.overlay.Marker -import org.osmdroid.views.overlay.Polyline -import org.osmdroid.views.overlay.advancedpolyline.MonochromaticPaintList - - -class LinesDetailFragment() : ScreenBaseFragment() { +import org.maplibre.android.MapLibre +import org.maplibre.android.camera.CameraPosition +import org.maplibre.android.camera.CameraUpdateFactory +import org.maplibre.android.geometry.LatLng +import org.maplibre.android.geometry.LatLngBounds +import org.maplibre.android.location.LocationComponent +import org.maplibre.android.location.LocationComponentOptions +import org.maplibre.android.maps.MapLibreMap +import org.maplibre.android.maps.MapView +import org.maplibre.android.maps.OnMapReadyCallback +import org.maplibre.android.maps.Style +import org.maplibre.android.plugins.annotation.Symbol +import org.maplibre.android.plugins.annotation.SymbolManager +import org.maplibre.android.plugins.annotation.SymbolOptions +import org.maplibre.android.style.expressions.Expression +import org.maplibre.android.style.layers.LineLayer +import org.maplibre.android.style.layers.Property.ICON_ANCHOR_CENTER +import org.maplibre.android.style.layers.Property.ICON_ROTATION_ALIGNMENT_MAP +import org.maplibre.android.style.layers.PropertyFactory +import org.maplibre.android.style.layers.SymbolLayer +import org.maplibre.android.style.sources.GeoJsonSource +import org.maplibre.geojson.Feature +import org.maplibre.geojson.FeatureCollection +import org.maplibre.geojson.LineString +import org.maplibre.geojson.Point + + +class LinesDetailFragment() : ScreenBaseFragment(), OnMapReadyCallback { private var lineID = "" private lateinit var patternsSpinner: Spinner private var patternsAdapter: ArrayAdapter? = null + //Bottom sheet behavior + private lateinit var bottomSheetBehavior: BottomSheetBehavior + private var bottomLayout: RelativeLayout? = null + private lateinit var stopTitleTextView: TextView + private lateinit var stopNumberTextView: TextView + private lateinit var linesPassingTextView: TextView + private lateinit var arrivalsCard: CardView + private lateinit var directionsCard: CardView + + private var isBottomSheetShowing = false + + //private var patternsSpinnerState: Parcelable? = null private lateinit var currentPatterns: List - private lateinit var map: MapView + //private lateinit var map: MapView private var viewingPattern: MatoPatternWithStops? = null private val viewModel: LinesViewModel by viewModels() private val mapViewModel: MapViewModel by viewModels() private var firstInit = true private var pausedFragment = false private lateinit var switchButton: ImageButton private var favoritesButton: ImageButton? = null private var locationIcon: ImageButton? = null private var isLineInFavorite = false private var appContext: Context? = null private var isLocationPermissionOK = false private val lineSharedPrefMonitor = SharedPreferences.OnSharedPreferenceChangeListener { pref, keychanged -> if(keychanged!=PreferencesHolder.PREF_FAVORITE_LINES || lineID.isEmpty()) return@OnSharedPreferenceChangeListener val newFavorites = pref.getStringSet(PreferencesHolder.PREF_FAVORITE_LINES, HashSet()) newFavorites?.let {favorites-> isLineInFavorite = favorites.contains(lineID) //if the button has been intialized, change the icon accordingly favoritesButton?.let { button-> //avoid crashes if fragment not attached if(context==null) return@let if(isLineInFavorite) { button.setImageDrawable(ResourcesCompat.getDrawable(resources, R.drawable.ic_star_filled, null)) appContext?.let { Toast.makeText(it,R.string.favorites_line_add,Toast.LENGTH_SHORT).show()} } else { button.setImageDrawable(ResourcesCompat.getDrawable(resources, R.drawable.ic_star_outline, null)) appContext?.let {Toast.makeText(it,R.string.favorites_line_remove,Toast.LENGTH_SHORT).show()} } } } } private lateinit var stopsRecyclerView: RecyclerView private lateinit var descripTextView: TextView //adapter for recyclerView private val stopAdapterListener= object : StopAdapterListener { override fun onTappedStop(stop: Stop?) { if(viewModel.shouldShowMessage) { Toast.makeText(context, R.string.long_press_stop_4_options, Toast.LENGTH_SHORT).show() viewModel.shouldShowMessage=false } stop?.let { fragmentListener.requestArrivalsForStopID(it.ID) } if(stop == null){ Log.e(DEBUG_TAG,"Passed wrong stop") } if(fragmentListener == null){ Log.e(DEBUG_TAG, "Fragment listener is null") } } override fun onLongPressOnStop(stop: Stop?): Boolean { TODO("Not yet implemented") } } + private val patternsSorter = Comparator{ p1: MatoPatternWithStops, p2: MatoPatternWithStops -> + if(p1.pattern.directionId != p2.pattern.directionId) + return@Comparator p1.pattern.directionId - p2.pattern.directionId + else + return@Comparator -1*(p1.stopsIndices.size - p2.stopsIndices.size) + } - private var polyline: Polyline? = null + //map data + private lateinit var mapView: MapView + private lateinit var locationComponent: LocationComponent + private lateinit var mapStyle: Style + protected var map: MapLibreMap? = null + private var mapInitCompleted = false + private lateinit var stopsSource: GeoJsonSource + private lateinit var busesSource: GeoJsonSource + private lateinit var polylineSource: GeoJsonSource + private var stopsShowingInMap : List? = null + + private var stopsLayerStarted = false + private var lastStopsSizeShown = 0 + private var lastUpdateTime:Long = -2 + + //BUS POSITIONS + private val positionsByVehDict = HashMap(5) + private val animatorsByVeh = HashMap() + + private var lastLocation : Location? = null + private var enablingPositionFromClick = false + + private var polyline: LineString? = null + + private val showUserPositionRequestLauncher = + registerForActivityResult( + ActivityResultContracts.RequestMultiplePermissions(), + ActivityResultCallback { result -> + if (result == null) { + Log.w(DEBUG_TAG, "Got asked permission but request is null, doing nothing?") + } else if (java.lang.Boolean.TRUE == result[Manifest.permission.ACCESS_COARSE_LOCATION] + && java.lang.Boolean.TRUE == result[Manifest.permission.ACCESS_FINE_LOCATION]) { + // We can use the position, restart location overlay + if (context == null || requireContext().getSystemService(Context.LOCATION_SERVICE) == null) + return@ActivityResultCallback ///@registerForActivityResult + setMapUserLocationEnabled(true, true, enablingPositionFromClick) + } else Log.w(DEBUG_TAG, "No location permission") + }) //private var stopPosList = ArrayList() - private lateinit var stopsOverlay: FolderOverlay - private lateinit var locationOverlay: LocationOverlay - private val locationOverlayResponder = object : LocationOverlay.OverlayCallbacks{ - override fun onDisableFollowMyLocation() { - Log.d(DEBUG_TAG, "Follow location disabled") - } - - override fun onEnableFollowMyLocation() { - Log.d(DEBUG_TAG, "Follow location enabled") - } - } - //location request responder - private val locationRequestResLauncher = registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()){ res -> - //onActivityResult(res: map) - if(res[Permissions.LOCATION_PERMISSIONS[0]] ==true || res[Permissions.LOCATION_PERMISSIONS[1]] ==true) - locationIcon?.let { onPositionIconButtonClick(it) } - else{ - context?.let { Toast.makeText(it,R.string.location_permission_not_granted, Toast.LENGTH_SHORT).show() } - } - } //fragment actions private lateinit var fragmentListener: CommonFragmentListener private val stopTouchResponder = TouchResponder { stopID, stopName -> Log.d(DEBUG_TAG, "Asked to show arrivals for stop ID: $stopID") fragmentListener.requestArrivalsForStopID(stopID) } private var showOnTopOfLine = true private var recyclerInitDone = false private var useMQTTPositions = true + + //position of live markers - private val busPositionMarkersByTrip = HashMap() - private var busPositionsOverlay = FolderOverlay() + private val busPositionUpdateByVeh = HashMap() + //private var busPositionsOverlay = FolderOverlay() private val tripMarkersAnimators = HashMap() private val liveBusViewModel: LivePositionsViewModel by viewModels() + + //extra items to use the LibreMap + private lateinit var symbolManager : SymbolManager + private var stopActiveSymbol: Symbol? = null + private var shownStopInBottomSheet : Stop? = null + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + lineID = requireArguments().getString(LINEID_KEY,"") + + MapLibre.getInstance(requireContext()) + + } + @SuppressLint("SetTextI18n") override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View? { + //reset statuses + isBottomSheetShowing = false + mapInitCompleted = false + stopsLayerStarted = false + lastStopsSizeShown = 0 + val rootView = inflater.inflate(R.layout.fragment_lines_detail, container, false) - lineID = requireArguments().getString(LINEID_KEY, "") + //lineID = requireArguments().getString(LINEID_KEY, "") + arguments?.let { + lineID = it.getString(LINEID_KEY, "") + } switchButton = rootView.findViewById(R.id.switchImageButton) locationIcon = rootView.findViewById(R.id.locationEnableIcon) favoritesButton = rootView.findViewById(R.id.favoritesButton) stopsRecyclerView = rootView.findViewById(R.id.patternStopsRecyclerView) descripTextView = rootView.findViewById(R.id.lineDescripTextView) descripTextView.visibility = View.INVISIBLE + //map stuff + mapView = rootView.findViewById(R.id.lineMap) + mapView.getMapAsync(this) + + + //init bottom sheet + val bottomSheet = rootView.findViewById(R.id.bottom_sheet) + bottomLayout = bottomSheet + stopTitleTextView = bottomSheet.findViewById(R.id.stopTitleTextView) + stopNumberTextView = bottomSheet.findViewById(R.id.stopNumberTextView) + linesPassingTextView = bottomSheet.findViewById(R.id.linesPassingTextView) + arrivalsCard = bottomSheet.findViewById(R.id.arrivalsCardButton) + directionsCard = bottomSheet.findViewById(R.id.directionsCardButton) + // Setup close button + rootView.findViewById(R.id.btnClose).setOnClickListener { + hideStopBottomSheet() + } + val titleTextView = rootView.findViewById(R.id.titleTextView) titleTextView.text = getString(R.string.line)+" "+FiveTNormalizer.fixShortNameForDisplay( GtfsUtils.getLineNameFromGtfsID(lineID), true) favoritesButton?.isClickable = true favoritesButton?.setOnClickListener { if(lineID.isNotEmpty()) PreferencesHolder.addOrRemoveLineToFavorites(requireContext(),lineID,!isLineInFavorite) } val preferences = PreferencesHolder.getMainSharedPreferences(requireContext()) val favorites = preferences.getStringSet(PreferencesHolder.PREF_FAVORITE_LINES, HashSet()) if(favorites!=null && favorites.contains(lineID)){ favoritesButton?.setImageDrawable(ResourcesCompat.getDrawable(resources, R.drawable.ic_star_filled, null)) isLineInFavorite = true } appContext = requireContext().applicationContext preferences.registerOnSharedPreferenceChangeListener(lineSharedPrefMonitor) patternsSpinner = rootView.findViewById(R.id.patternsSpinner) patternsAdapter = ArrayAdapter(requireContext(), android.R.layout.simple_spinner_dropdown_item, ArrayList()) patternsSpinner.adapter = patternsAdapter - initializeMap(rootView) initializeRecyclerView() switchButton.setOnClickListener{ - if(map.visibility == View.VISIBLE){ - map.visibility = View.GONE - stopsRecyclerView.visibility = View.VISIBLE - locationIcon?.visibility = View.GONE - - viewModel.setMapShowing(false) - liveBusViewModel.stopMatoUpdates() - //map.overlayManager.remove(busPositionsOverlay) - - switchButton.setImageDrawable(AppCompatResources.getDrawable(requireContext(), R.drawable.ic_map_white_30)) + if(mapView.visibility == View.VISIBLE){ + hideMapAndShowStopList() } else{ - stopsRecyclerView.visibility = View.GONE - map.visibility = View.VISIBLE - locationIcon?.visibility = View.VISIBLE - viewModel.setMapShowing(true) - - //map.overlayManager.add(busPositionsOverlay) - //map. - if(useMQTTPositions) - liveBusViewModel.requestMatoPosUpdates(GtfsUtils.getLineNameFromGtfsID(lineID)) - else - liveBusViewModel.requestGTFSUpdates() - - switchButton.setImageDrawable(AppCompatResources.getDrawable(requireContext(), R.drawable.ic_list_30)) + hideStopListAndShowMap() } } locationIcon?.let {view -> if(!LocationUtils.isLocationEnabled(requireContext()) || !Permissions.anyLocationPermissionsGranted(requireContext())) setLocationIconEnabled(false) //set click Listener view.setOnClickListener(this::onPositionIconButtonClick) } //set - - + //INITIALIZE VIEW MODELS viewModel.setRouteIDQuery(lineID) + liveBusViewModel.setGtfsLineToFilterPos(lineID, null) val keySourcePositions = getString(R.string.pref_positions_source) useMQTTPositions = PreferenceManager.getDefaultSharedPreferences(requireContext()) .getString(keySourcePositions, "mqtt").contentEquals("mqtt") viewModel.patternsWithStopsByRouteLiveData.observe(viewLifecycleOwner){ patterns -> savePatternsToShow(patterns) } /* We have the pattern and the stops here, time to display them */ viewModel.stopsForPatternLiveData.observe(viewLifecycleOwner) { stops -> - if(map.visibility ==View.VISIBLE) - showPatternWithStopsOnMap(stops) + if(mapView.visibility ==View.VISIBLE) + viewingPattern?.let{ + showPatternWithStopsOnMap(it,stops) + } else{ if(stopsRecyclerView.visibility==View.VISIBLE) showStopsAsList(stops) } } viewModel.gtfsRoute.observe(viewLifecycleOwner){route-> if(route == null){ //need to close the fragment activity?.supportFragmentManager?.popBackStack() return@observe } descripTextView.text = route.longName descripTextView.visibility = View.VISIBLE } if(pausedFragment && viewModel.selectedPatternLiveData.value!=null){ val patt = viewModel.selectedPatternLiveData.value!! Log.d(DEBUG_TAG, "Recreating views on resume, setting pattern: ${patt.pattern.code}") showPattern(patt) pausedFragment = false } Log.d(DEBUG_TAG,"Data ${viewModel.stopsForPatternLiveData.value}") //listeners patternsSpinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener { override fun onItemSelected(p0: AdapterView<*>?, p1: View?, position: Int, p3: Long) { val patternWithStops = currentPatterns.get(position) //viewModel.setPatternToDisplay(patternWithStops) setPatternAndReqStops(patternWithStops) + liveBusViewModel.setGtfsLineToFilterPos(lineID, patternWithStops.pattern) Log.d(DEBUG_TAG, "item Selected, cleaning bus markers") - if(map?.visibility == View.VISIBLE) { - busPositionsOverlay.closeAllInfoWindows() - busPositionsOverlay.items.clear() - busPositionMarkersByTrip.clear() + if(mapView.visibility == View.VISIBLE) { + //TODO: REMOVE ACTIVE BUSES + busPositionUpdateByVeh.clear() stopAnimations() tripMarkersAnimators.clear() liveBusViewModel.retriggerPositionUpdate() } } override fun onNothingSelected(p0: AdapterView<*>?) { } } + return rootView + } + + // ------------- UI switch stuff --------- + + private fun hideMapAndShowStopList(){ + mapView.visibility = View.GONE + stopsRecyclerView.visibility = View.VISIBLE + locationIcon?.visibility = View.GONE + + viewModel.setMapShowing(false) + if(useMQTTPositions) liveBusViewModel.stopMatoUpdates() + //map.overlayManager.remove(busPositionsOverlay) + + switchButton.setImageDrawable(AppCompatResources.getDrawable(requireContext(), R.drawable.ic_map_white_30)) + } + + private fun hideStopListAndShowMap(){ + stopsRecyclerView.visibility = View.GONE + mapView.visibility = View.VISIBLE + locationIcon?.visibility = View.VISIBLE + viewModel.setMapShowing(true) + + //map.overlayManager.add(busPositionsOverlay) + //map. + if(useMQTTPositions) + liveBusViewModel.requestMatoPosUpdates(GtfsUtils.getLineNameFromGtfsID(lineID)) + else + liveBusViewModel.requestGTFSUpdates() + + switchButton.setImageDrawable(AppCompatResources.getDrawable(requireContext(), R.drawable.ic_list_30)) + } + + private fun setLocationIconEnabled(setTrue: Boolean){ + if(setTrue) + locationIcon?.setImageDrawable(ContextCompat.getDrawable(requireContext(), R.drawable.location_circlew_red)) + else + locationIcon?.setImageDrawable(ContextCompat.getDrawable(requireContext(), R.drawable.location_circlew_grey)) + } + + /** + * Handles logic of enabling the user location on the map + */ + @SuppressLint("MissingPermission") + private fun setMapUserLocationEnabled(enabled: Boolean, assumePermissions: Boolean, fromClick: Boolean) { + if (enabled) { + val permissionOk = assumePermissions || Permissions.bothLocationPermissionsGranted(requireContext()) + + if (permissionOk) { + Log.d(DEBUG_TAG, "Permission OK, starting location component, assumed: $assumePermissions") + locationComponent.isLocationComponentEnabled = true + //locationComponent.cameraMode = CameraMode.TRACKING //CameraMode.TRACKING + + setLocationIconEnabled(true) + if (fromClick) Toast.makeText(context, R.string.location_enabled, Toast.LENGTH_SHORT).show() + } else { + if (shouldShowRequestPermissionRationale(Manifest.permission.ACCESS_FINE_LOCATION)) { + //TODO: show dialog for permission rationale + Toast.makeText(activity, R.string.enable_position_message_map, Toast.LENGTH_SHORT).show() + } + Log.d(DEBUG_TAG, "Requesting permission to show user location") + enablingPositionFromClick = fromClick + showUserPositionRequestLauncher.launch(Permissions.LOCATION_PERMISSIONS) + } + } else{ + locationComponent.isLocationComponentEnabled = false + setLocationIconEnabled(false) + if (fromClick) { + Toast.makeText(requireContext(), R.string.location_disabled, Toast.LENGTH_SHORT).show() + //TODO: Cancel the request for the enablement of the position if needed + } + } + + } + + /** + * Switch position icon from activ + */ + private fun onPositionIconButtonClick(view: View){ + if(locationComponent.isLocationComponentEnabled) setMapUserLocationEnabled(false, false, true) + else{ + setMapUserLocationEnabled(true, false, true) + } + } + + // ------------- Map Code ------------------------- + /** + * This method sets up the map and the layers + */ + override fun onMapReady(mapReady: MapLibreMap) { + this.map = mapReady + + val mjson = Styles.getJsonStyleFromAsset(requireContext(), "map_style_good_noshops.json") + //ViewUtils.loadJsonFromAsset(requireContext(),"map_style_good.json") + + activity?.run { + val builder = Style.Builder().fromJson(mjson!!) + + mapReady.setStyle(builder) { style -> + + mapStyle = style + //setupLayers(style) + + symbolManager = SymbolManager(mapView,mapReady,style) + symbolManager.iconAllowOverlap = true + symbolManager.textAllowOverlap = false + + symbolManager.addClickListener{ _ -> + if (stopActiveSymbol!=null){ + hideStopBottomSheet() + + return@addClickListener true + } else + return@addClickListener false + } + + // Start observing data + initMapUserLocation(style, mapReady, requireContext()) + //initStopsLayer(style, FeatureCollection.fromFeatures(ArrayList())) + //init stop layer with this + //val stopsInCache = stopsViewModel.getAllStopsLoaded() + //if(stopsInCache.isEmpty()) + initStopsPolyLineLayers(style, FeatureCollection.fromFeatures(ArrayList()), null) + //else + // displayStops(stopsInCache) + setupBusLayer(style) + + } + + mapReady.addOnMapClickListener { point -> + val screenPoint = mapReady.projection.toScreenLocation(point) + val features = mapReady.queryRenderedFeatures(screenPoint, STOPS_LAYER_ID) + val busNearby = mapReady.queryRenderedFeatures(screenPoint, BUSES_LAYER_ID) + if (features.isNotEmpty()) { + val feature = features[0] + val id = feature.getStringProperty("id") + val name = feature.getStringProperty("name") + //Toast.makeText(requireContext(), "Clicked on $name ($id)", Toast.LENGTH_SHORT).show() + val stop = viewModel.getStopByID(id) + stop?.let { + if (isBottomSheetShowing){ + hideStopBottomSheet() + } + openStopInBottomSheet(it) + isBottomSheetShowing = true + //move camera + if(it.latitude!=null && it.longitude!=null) + //mapReady.cameraPosition = CameraPosition.Builder().target(LatLng(it.latitude!!, it.longitude!!)).build() + mapReady.animateCamera(CameraUpdateFactory.newLatLng(LatLng(it.latitude!!,it.longitude!!)),750) + } + return@addOnMapClickListener true + } else if (busNearby.isNotEmpty()){ + val feature = busNearby[0] + val vehid = feature.getStringProperty("veh") + val route = feature.getStringProperty("line") + + if(context!=null){ + Toast.makeText(context, "Veh $vehid on route $route", Toast.LENGTH_SHORT).show() + } + return@addOnMapClickListener true + } + false + } + + mapInitCompleted = true + // we start requesting the bus positions now + startRequestingBusPositions() + } + /*savedMapStateOnPause?.let{ + restoreMapStateFromBundle(it) + pendingLocationActivation = false + Log.d(DEBUG_TAG, "Restored map state from the saved bundle") + } + + */ + //reset saved State at the end + //if( savedMapStateOnPause == null) { + //set initial position + val zoom = 12.0 + //center position + //val latlngTarget = initialStopToShow?.let { + // LatLng(it.latitude!!, it.longitude!!) + //} ?: + val latlngTarget = LatLng(MapLibreFragment.DEFAULT_CENTER_LAT, MapLibreFragment.DEFAULT_CENTER_LON) + mapReady.cameraPosition = CameraPosition.Builder().target(latlngTarget).zoom(zoom).build() + //} + //reset saved state + //savedMapStateOnPause = null + } + + private fun startRequestingBusPositions(){ //live bus positions - liveBusViewModel.updatesWithTripAndPatterns.observe(viewLifecycleOwner){ - if(map.visibility == View.GONE || viewingPattern ==null){ + liveBusViewModel.filteredLocationUpdates.observe(viewLifecycleOwner){ updates -> + if(mapView.visibility == View.GONE || viewingPattern ==null){ //DO NOTHING return@observe } + /* val filtdLineID = GtfsUtils.stripGtfsPrefix(lineID) //filter buses with direction, show those only with the same direction - val outmap = HashMap>() + val updsForVeh = HashMap>() val currentPattern = viewingPattern!!.pattern - val numUpds = it.entries.size + val numUpds = updates.entries.size Log.d(DEBUG_TAG, "Got $numUpds updates, current pattern is: ${currentPattern.name}, directionID: ${currentPattern.directionId}") + // cannot understand where this is used val patternsDirections = HashMap() - for((tripId, pair) in it.entries){ + for((tripId, pair) in updates.entries){ //remove trips with wrong line ideas + val posUp = pair.first if(pair.first.routeID!=filtdLineID) continue if(pair.second!=null && pair.second?.pattern !=null){ val dir = pair.second?.pattern?.directionId if(dir !=null && dir == currentPattern.directionId){ - outmap[tripId] = pair + updsForVeh[posUp.vehicle] = pair } - patternsDirections.set(tripId,if (dir!=null) dir else -10) + patternsDirections[tripId] = dir ?: -10 } else{ - outmap[tripId] = pair + updsForVeh[posUp.vehicle] = pair //Log.d(DEBUG_TAG, "No pattern for tripID: $tripId") patternsDirections[tripId] = -10 } } - Log.d(DEBUG_TAG, " Filtered updates are ${outmap.keys.size}") // Original updates directs: $patternsDirections\n - updateBusPositionsInMap(outmap) + Log.d(DEBUG_TAG, " Filtered updates are ${updsForVeh.keys.size}") // Original updates directs: $patternsDirections\n + + */ + updateBusPositionsInMap(updates) //if not using MQTT positions if(!useMQTTPositions){ liveBusViewModel.requestDelayedGTFSUpdates(2000) } } //download missing tripIDs liveBusViewModel.tripsGtfsIDsToQuery.observe(viewLifecycleOwner){ //gtfsPosViewModel.downloadTripsFromMato(dat); MatoTripsDownloadWorker.requestMatoTripsDownload( it, requireContext().applicationContext, "BusTO-MatoTripDownload" ) } - - - return rootView } - private fun setLocationIconEnabled(setTrue: Boolean){ - if(setTrue) - locationIcon?.setImageDrawable(ContextCompat.getDrawable(requireContext(), R.drawable.location_circlew_red)) - else - locationIcon?.setImageDrawable(ContextCompat.getDrawable(requireContext(), R.drawable.location_circlew_grey)) + /** + * Initialize the map location, but do not enable the component + */ + @SuppressLint("MissingPermission") + private fun initMapUserLocation(style: Style, map: MapLibreMap, context: Context){ + locationComponent = map.locationComponent + val locationComponentOptions = + LocationComponentOptions.builder(context) + .pulseEnabled(false) + .build() + val locationComponentActivationOptions = + MapUtils.buildLocationComponentActivationOptions(style, locationComponentOptions, context) + locationComponent.activateLocationComponent(locationComponentActivationOptions) + locationComponent.isLocationComponentEnabled = false + + lastLocation?.let { + if (it.accuracy < 200) + locationComponent.forceLocationUpdate(it) + } } - /** - * Switch position icon from activ + * Update the bottom sheet with the stop information */ - private fun onPositionIconButtonClick(view: View){ - if(locationOverlay.isMyLocationEnabled){ - //switch off - locationOverlay.disableMyLocation() - //set image on respective button - setLocationIconEnabled(false) - if(context!=null) { - if (LocationUtils.isLocationEnabled(context)) { - //show message - Toast.makeText(context, R.string.location_disabled, Toast.LENGTH_SHORT).show() - } - } - } else{ - //switch on - locationOverlay.enableMyLocation() - if(context!=null) { - if(!Permissions.anyLocationPermissionsGranted(context)) { - locationRequestResLauncher.launch(Permissions.LOCATION_PERMISSIONS) - Toast.makeText(context, R.string.enable_position_message_map, Toast.LENGTH_SHORT).show() - } - else if (LocationUtils.isLocationEnabled(context)) { - //set image on button - setLocationIconEnabled(true) - //show message - Toast.makeText(context, R.string.location_enabled, Toast.LENGTH_SHORT).show() - } else{ - Toast.makeText(context, R.string.map_location_disabled_device, Toast.LENGTH_SHORT).show() - } + private fun openStopInBottomSheet(stop: Stop){ + bottomLayout?.let { + + //lay.findViewById(R.id.stopTitleTextView).text ="${stop.ID} - ${stop.stopDefaultName}" + stopTitleTextView.text = stop.stopDefaultName + stopNumberTextView.text = stop.ID + val string_show = if (stop.numRoutesStopping==0) "" + else if (stop.numRoutesStopping <= 1) + requireContext().getString(R.string.line_fill, stop.routesThatStopHereToString()) + else requireContext().getString(R.string.lines_fill, stop.routesThatStopHereToString()) + linesPassingTextView.text = string_show + + //SET ON CLICK LISTENER + arrivalsCard.setOnClickListener{ + fragmentListener?.requestArrivalsForStopID(stop.ID) } + + } + //add stop marker + if (stop.latitude!=null && stop.longitude!=null) { + stopActiveSymbol = symbolManager.create( + SymbolOptions() + .withLatLng(LatLng(stop.latitude!!, stop.longitude!!)) + .withIconImage(STOP_ACTIVE_IMG) + .withIconAnchor(ICON_ANCHOR_CENTER) + + ) + } + shownStopInBottomSheet = stop + bottomSheetBehavior.state = BottomSheetBehavior.STATE_EXPANDED } + // Hide the bottom sheet and remove extra symbol + private fun hideStopBottomSheet(){ + if (stopActiveSymbol!=null){ + symbolManager.delete(stopActiveSymbol) + stopActiveSymbol = null + } + bottomSheetBehavior.state = BottomSheetBehavior.STATE_HIDDEN + isBottomSheetShowing = false + //remove initial stop + //if(initialStopToShow!=null){ + // initialStopToShow = null + //} - private fun initializeMap(rootView : View){ - val ctx = requireContext().applicationContext - Configuration.getInstance().load(ctx, PreferenceManager.getDefaultSharedPreferences(ctx)) - - map = rootView.findViewById(R.id.lineMap) - map.let { - it.setTileSource(TileSourceFactory.MAPNIK) - - locationOverlay = LocationOverlay.createLocationOverlay(true, it, requireContext(), locationOverlayResponder) - locationOverlay.disableFollowLocation() - - stopsOverlay = FolderOverlay() - busPositionsOverlay = FolderOverlay() - - - //map.setTilesScaledToDpi(true); - //map.setTilesScaledToDpi(true); - it.setFlingEnabled(true) - it.setUseDataConnection(true) - - // add ability to zoom with 2 fingers - it.setMultiTouchControls(true) - it.minZoomLevel = 12.0 - - //map controller setup - val mapController = it.controller - var zoom = 12.0 - var centerMap = GeoPoint(DEFAULT_CENTER_LAT, DEFAULT_CENTER_LON) - if(mapViewModel.currentLat.value!=MapViewModel.INVALID) { - Log.d(DEBUG_TAG, "mapViewModel posi: ${mapViewModel.currentLat.value}, ${mapViewModel.currentLong.value}"+ - " zoom ${mapViewModel.currentZoom.value}") - zoom = mapViewModel.currentZoom.value!! - centerMap = GeoPoint(mapViewModel.currentLat.value!!, mapViewModel.currentLong.value!!) - /*viewLifecycleOwner.lifecycleScope.launch { - delay(100) - Log.d(DEBUG_TAG, "zooming back to point") - controller.animateTo(GeoPoint(mapViewModel.currentLat.value!!, mapViewModel.currentLong.value!!), - mapViewModel.currentZoom.value!!,null,null) - //controller.setCenter(GeoPoint(mapViewModel.currentLat.value!!, mapViewModel.currentLong.value!!)) - //controller.setZoom(mapViewModel.currentZoom.value!!) - */ - } - mapController.setZoom(zoom) - mapController.setCenter(centerMap) - Log.d(DEBUG_TAG, "Initializing map, first init $firstInit") - //map.invalidate() + shownStopInBottomSheet = null + } + + // ------- MAP LAYERS INITIALIZE ---- + /** + * Initialize the map layers for the stops + */ + private fun initStopsPolyLineLayers(style: Style, stopFeatures:FeatureCollection, lineFeature: Feature?){ + + stopsSource = GeoJsonSource(STOPS_SOURCE_ID,stopFeatures) + style.addSource(stopsSource) + //val context = requireContext() + //val stopIcon = ContextCompat.getDrawable(context, R.drawable.ball) + + val imgBus = ResourcesCompat.getDrawable(resources,R.drawable.bus_stop_new, activity?.theme)!! + //set the image tint + //DrawableCompat.setTint(imgBus,ContextCompat.getColor(context,R.color.line_drawn_poly)) + + // add icon + style.addImage(STOP_IMAGE_ID,imgBus) + + style.addImage(STOP_ACTIVE_IMG, ResourcesCompat.getDrawable(resources, R.drawable.bus_stop_new_highlight, activity?.theme)!!) + // Stops layer + val stopsLayer = SymbolLayer(STOPS_LAYER_ID, STOPS_SOURCE_ID) + stopsLayer.withProperties( + PropertyFactory.iconImage(STOP_IMAGE_ID), + PropertyFactory.iconAllowOverlap(true), + PropertyFactory.iconIgnorePlacement(true) + ) - it.overlayManager.add(stopsOverlay) - it.overlayManager.add(locationOverlay) - it.overlayManager.add(busPositionsOverlay) + polylineSource = lineFeature?.let { GeoJsonSource(POLYLINE_SOURCE, it) } ?: GeoJsonSource(POLYLINE_SOURCE) - zoomToCurrentPattern() - firstInit = false + val colorStr=ContextCompat.getString(requireContext(),R.color.line_drawn_poly) + //paint.style = Paint.Style.FILL_AND_STROKE + //paint.strokeJoin = Paint.Join.ROUND + //paint.strokeCap = Paint.Cap.ROUND + + val lineLayer = LineLayer(POLYLINE_LAYER, POLYLINE_SOURCE).withProperties( + PropertyFactory.lineColor(colorStr), + PropertyFactory.lineWidth(10f), //originally 13f + + ) + + style.addLayer(lineLayer) + style.addLayerAbove(stopsLayer, POLYLINE_LAYER) + + stopsLayerStarted = true + } + /** + * Add the stops to the layers + * Copied from MapLibreFragment + */ + private fun displayStops(stops: List?) { + if (stops.isNullOrEmpty()) return + + if (stops.size==lastStopsSizeShown){ + Log.d(DEBUG_TAG, "Not updating, have same number of stops. After 3 times") + return } } + /** + * Setup the Map Layers + */ + private fun setupBusLayer(style: Style) { + // Buses source + busesSource = GeoJsonSource(BUSES_SOURCE_ID) + style.addSource(busesSource) + style.addImage("bus_symbol",ResourcesCompat.getDrawable(resources, R.drawable.map_bus_position_icon, activity?.theme)!!) + + // Buses layer + val busesLayer = SymbolLayer(BUSES_LAYER_ID, BUSES_SOURCE_ID).apply { + withProperties( + PropertyFactory.iconImage("bus_symbol"), + PropertyFactory.iconSize(1.2f), + PropertyFactory.iconAllowOverlap(true), + PropertyFactory.iconIgnorePlacement(true), + PropertyFactory.iconRotate(Expression.get("bearing")), + PropertyFactory.iconRotationAlignment(ICON_ROTATION_ALIGNMENT_MAP) + + ) + } + style.addLayerAbove(busesLayer, STOPS_LAYER_ID) + + } + override fun onAttach(context: Context) { super.onAttach(context) if(context is CommonFragmentListener){ fragmentListener = context } else throw RuntimeException("$context must implement CommonFragmentListener") } private fun stopAnimations(){ - for(anim in tripMarkersAnimators.values){ + for(anim in animatorsByVeh.values){ anim.cancel() } } private fun savePatternsToShow(patterns: List){ - val patternsSorter = Comparator{ p1: MatoPatternWithStops, p2: MatoPatternWithStops -> - if(p1.pattern.directionId != p2.pattern.directionId) - return@Comparator p1.pattern.directionId - p2.pattern.directionId - else - return@Comparator -1*(p1.stopsIndices.size - p2.stopsIndices.size) - } currentPatterns = patterns.sortedWith(patternsSorter) patternsAdapter?.let { it.clear() it.addAll(currentPatterns.map { p->"${p.pattern.directionId} - ${p.pattern.headsign}" }) it.notifyDataSetChanged() } viewingPattern?.let { showPattern(it) } } /** * Called when the position of the spinner is updated */ private fun setPatternAndReqStops(patternWithStops: MatoPatternWithStops){ Log.d(DEBUG_TAG, "Requesting stops for pattern ${patternWithStops.pattern.code}") viewModel.selectedPatternLiveData.value = patternWithStops viewModel.currentPatternStops.value = patternWithStops.stopsIndices.sortedBy { i-> i.order } viewingPattern = patternWithStops viewModel.requestStopsForPatternWithStops(patternWithStops) } private fun showPattern(patternWs: MatoPatternWithStops){ Log.d(DEBUG_TAG, "Finding pattern to show: ${patternWs.pattern.code}") var pos = -2 val code = patternWs.pattern.code.trim() for(k in currentPatterns.indices){ if(currentPatterns[k].pattern.code.trim() == code){ pos = k break } } Log.d(DEBUG_TAG, "Found pattern $code in position: $pos") if(pos>=0) patternsSpinner.setSelection(pos) //set pattern setPatternAndReqStops(patternWs) } private fun zoomToCurrentPattern(){ - var pointsList: List - if(viewingPattern==null) { - Log.e(DEBUG_TAG, "asked to zoom to pattern but current viewing pattern is null") - if(polyline!=null) - pointsList = polyline!!.actualPoints - else { - Log.d(DEBUG_TAG, "The polyline is null") - return + if(polyline==null) return + val NULL_VALUE = -4000.0 + var maxLat = NULL_VALUE + var minLat = NULL_VALUE + var minLong = NULL_VALUE + var maxLong = NULL_VALUE + + polyline?.let { + for(p in it.coordinates()){ + val lat = p.latitude() + val lon = p.longitude() + // get max latitude + if(maxLat == NULL_VALUE) + maxLat =lat + else if (maxLat < lat) maxLat = lat + // find min latitude + if (minLat ==NULL_VALUE) + minLat = lat + else if (minLat > lat) minLat = lat + if(maxLong == NULL_VALUE || maxLong < lon ) + maxLong = lon + if (minLong == NULL_VALUE || minLong > lon) + minLong = lon } - }else{ - val pattern = viewingPattern!!.pattern - - pointsList = PolylineParser.decodePolyline(pattern.patternGeometryPoly, pattern.patternGeometryLength) - } - - var maxLat = -4000.0 - var minLat = -4000.0 - var minLong = -4000.0 - var maxLong = -4000.0 - for (p in pointsList){ - // get max latitude - if(maxLat == -4000.0) - maxLat = p.latitude - else if (maxLat < p.latitude) maxLat = p.latitude - // find min latitude - if (minLat == -4000.0) - minLat = p.latitude - else if (minLat > p.latitude) minLat = p.latitude - if(maxLong == -4000.0 || maxLong < p.longitude ) - maxLong = p.longitude - if (minLong == -4000.0 || minLong > p.longitude) - minLong = p.longitude - } - - val del = 0.008 - //map.controller.c - Log.d(DEBUG_TAG, "Setting limits of bounding box of line: $minLat -> $maxLat, $minLong -> $maxLong") - map.zoomToBoundingBox(BoundingBox(maxLat+del, maxLong+del, minLat-del, minLong-del), false) - } - - private fun showPatternWithStopsOnMap(stops: List){ - Log.d(DEBUG_TAG, "Got the stops: ${stops.map { s->s.gtfsID }}}") - if(viewingPattern==null || map == null) return + val padding = 100 // Pixel di padding intorno ai limiti + + Log.d(DEBUG_TAG, "Setting limits of bounding box of line: $minLat -> $maxLat, $minLong -> $maxLong") + val bbox = LatLngBounds.from(maxLat,maxLong, minLat, minLong) + //map.zoomToBoundingBox(BoundingBox(maxLat+del, maxLong+del, minLat-del, minLong-del), false) + map?.animateCamera(CameraUpdateFactory.newLatLngBounds(bbox, padding)) + } - val pattern = viewingPattern!!.pattern + } + + private fun showPatternWithStopsOnMap(patternWs: MatoPatternWithStops,stops: List){ + Log.d(DEBUG_TAG, "Got the stops: ${stops.map { s->s.gtfsID }}}") + //if(viewingPattern==null || map == null) return + if (map==null || !mapInitCompleted) return + viewingPattern = patternWs + + val pattern = patternWs.pattern val pointsList = PolylineParser.decodePolyline(pattern.patternGeometryPoly, pattern.patternGeometryLength) + val pointsToShow = pointsList.map { Point.fromLngLat(it.longitude, it.latitude) } + polyline = LineString.fromLngLats(pointsToShow) + val lineFeature = Feature.fromGeometry(polyline) + + // --- STOPS--- + val features = ArrayList()//stops.mapNotNull { stop -> + //stop.latitude?.let { lat -> + // stop.longitude?.let { lon -> + for (s in stops){ + if (s.latitude!=null && s.longitude!=null) { + val loc = if (showOnTopOfLine) findOptimalPosition(s, pointsList) + else LatLng(s.latitude!!, s.longitude!!) + features.add( + Feature.fromGeometry( + Point.fromLngLat(loc.longitude, loc.latitude), + JsonObject().apply { + addProperty("id", s.ID) + addProperty("name", s.stopDefaultName) + //addProperty("routes", s.routesThatStopHereToString()) // Add routes array to JSON object + } + ) + ) + } - var maxLat = -4000.0 - var minLat = -4000.0 - var minLong = -4000.0 - var maxLong = -4000.0 - for (p in pointsList){ - // get max latitude - if(maxLat == -4000.0) - maxLat = p.latitude - else if (maxLat < p.latitude) maxLat = p.latitude - // find min latitude - if (minLat == -4000.0) - minLat = p.latitude - else if (minLat > p.latitude) minLat = p.latitude - if(maxLong == -4000.0 || maxLong < p.longitude ) - maxLong = p.longitude - if (minLong == -4000.0 || minLong > p.longitude) - minLong = p.longitude - } - //val polyLine=Polyline(map) - //polyLine.setPoints(pointsList) - //save points - if(map.overlayManager.contains(polyline)){ - map.overlayManager.remove(polyline) - } - polyline = Polyline(map, false) - polyline!!.setPoints(pointsList) - //polyline.color = ContextCompat.getColor(context!!,R.color.brown_vd) - polyline!!.infoWindow = null - val paint = Paint() - paint.color = ContextCompat.getColor(requireContext(),R.color.line_drawn_poly) - paint.isAntiAlias = true - paint.strokeWidth = 13f - - paint.style = Paint.Style.FILL_AND_STROKE - paint.strokeJoin = Paint.Join.ROUND - paint.strokeCap = Paint.Cap.ROUND - polyline!!.outlinePaintLists.add(MonochromaticPaintList(paint)) - - map.overlayManager.add(0,polyline!!) - - stopsOverlay.closeAllInfoWindows() - stopsOverlay.items.clear() - val stopIcon = ContextCompat.getDrawable(requireContext(), R.drawable.ball) + } + Log.d(DEBUG_TAG,"Have put ${features.size} stops to display") + + // if the layer is already started, substitute the stops inside, otherwise start it + if (stopsLayerStarted) { + stopsSource.setGeoJson(FeatureCollection.fromFeatures(features)) + polylineSource.setGeoJson(FeatureCollection.fromFeature(lineFeature)) + lastStopsSizeShown = features.size + } else + map?.let { + Log.d(DEBUG_TAG, "Map stop layer is not started yet, init layer") + initStopsPolyLineLayers(mapStyle, FeatureCollection.fromFeatures(features),lineFeature) + Log.d(DEBUG_TAG,"Started stops layer on map") + lastStopsSizeShown = features.size + stopsLayerStarted = true + } + + /* OLD CODE for(s in stops){ - val gp = if (showOnTopOfLine) - findOptimalPosition(s,pointsList) - else GeoPoint(s.latitude!!,s.longitude!!) + val gp = val marker = MarkerUtils.makeMarker( gp, s.ID, s.stopDefaultName, s.routesThatStopHereToString(), map,stopTouchResponder, stopIcon, R.layout.linedetail_stop_infowindow, R.color.line_drawn_poly ) marker.setAnchor(Marker.ANCHOR_CENTER, Marker.ANCHOR_CENTER) stopsOverlay.add(marker) } + */ //POINTS LIST IS NOT IN ORDER ANY MORE //if(!map.overlayManager.contains(stopsOverlay)){ // map.overlayManager.add(stopsOverlay) //} - polyline!!.setOnClickListener(Polyline.OnClickListener { polyline, mapView, eventPos -> - Log.d(DEBUG_TAG, "clicked") - true - }) - - //map.controller.zoomToB//#animateTo(pointsList[0]) - val del = 0.008 - map.zoomToBoundingBox(BoundingBox(maxLat+del, maxLong+del, minLat-del, minLong-del), true) + zoomToCurrentPattern() //map.invalidate() } private fun initializeRecyclerView(){ val llManager = LinearLayoutManager(context) llManager.orientation = LinearLayoutManager.VERTICAL stopsRecyclerView.layoutManager = llManager } private fun showStopsAsList(stops: List){ Log.d(DEBUG_TAG, "Setting stops from: "+viewModel.currentPatternStops.value) val orderBy = viewModel.currentPatternStops.value!!.withIndex().associate{it.value.stopGtfsId to it.index} val stopsSorted = stops.sortedBy { s -> orderBy[s.gtfsID] } val numStops = stopsSorted.size Log.d(DEBUG_TAG, "RecyclerView adapter is: ${stopsRecyclerView.adapter}") val setNewAdapter = true if(setNewAdapter){ stopsRecyclerView.adapter = StopRecyclerAdapter( stopsSorted, stopAdapterListener, StopRecyclerAdapter.Use.LINES, NameCapitalize.FIRST ) } - - } - /** - * Remove bus marker from overlay associated with tripID - */ - private fun removeBusMarker(tripID: String){ - if(!busPositionMarkersByTrip.containsKey(tripID)){ - Log.e(DEBUG_TAG, "Asked to remove veh with tripID $tripID but it's supposedly not shown") - return - } - val marker = busPositionMarkersByTrip[tripID] - busPositionsOverlay.remove(marker) - busPositionMarkersByTrip.remove(tripID) - - val animator = tripMarkersAnimators[tripID] - animator?.let{ - it.cancel() - tripMarkersAnimators.remove(tripID) - } - - } - private fun showPatternWithStop(patternId: String){ + private fun displayPatternInSpinner(patternId: String){ //var index = 0 Log.d(DEBUG_TAG, "Showing pattern with code $patternId ") for (i in currentPatterns.indices){ val pattStop = currentPatterns[i] if(pattStop.pattern.code == patternId){ Log.d(DEBUG_TAG, "Pattern found in position $i") //setPatternAndReqStops(pattStop) patternsSpinner.setSelection(i) break } } } /** - * draw the position of the buses in the map. Copied from MapFragment + * Update function for the bus positions + * Takes the processed updates and saves them accordingly + * Copied from MapLibreFragment, removing the labels */ - private fun updateBusPositionsInMap(tripsPatterns: java.util.HashMap> - ) { - //Log.d(MapFragment.DEBUG_TAG, "Updating positions of the buses") - //if(busPositionsOverlay == null) busPositionsOverlay = new FolderOverlay(); - // cleanup the patterns - // at first run, the buses which have no direction are still displayed. If those become missing in the data, - // it becomes clear that they don't have the same direction - val currentBusesTripsIds = HashSet(busPositionMarkersByTrip.keys) - for (tripID in currentBusesTripsIds){ - if (!tripsPatterns.keys.contains(tripID)){ - //the tripId is not in the updates anymore, remove it - removeBusMarker(tripID) + private fun updateBusPositionsInMap(incomingData: HashMap>){ + val vehsNew = HashSet(incomingData.values.map { up -> up.first.vehicle }) + val vehsOld = HashSet(positionsByVehDict.keys) + + //val symbolsToUpdate = ArrayList() + for (upsWithTrp in incomingData.values){ + val pos = upsWithTrp.first + val vehID = pos.vehicle + var animate = false + if (vehsOld.contains(vehID)){ + //update position only if the starting or the stopping position of the animation are in the view + val oldPos = positionsByVehDict[vehID] + var avoidShowingUpdateBecauseIsImpossible = false + oldPos?.let{ + if(oldPos.routeID!=pos.routeID) { + val dist = LatLng(it.latitude, it.longitude).distanceTo(LatLng(pos.latitude, pos.longitude)) + val speed = dist*3.6 / (pos.timestamp - it.timestamp) //this should be in km/h + Log.w(DEBUG_TAG, "Vehicle $vehID changed route from ${oldPos.routeID} to ${pos.routeID}, distance: $dist, speed: $speed") + if (speed > 120 || speed < 0){ + avoidShowingUpdateBecauseIsImpossible = true + } + } + } + if (avoidShowingUpdateBecauseIsImpossible){ + // DO NOT SHOW THIS SHIT + Log.w(DEBUG_TAG, "Update for vehicle $vehID skipped") + continue + } + + val samePosition = oldPos?.let { (oldPos.latitude==pos.latitude)&&(oldPos.longitude == pos.longitude) }?:false + if(!samePosition) { + //val isPositionInBounds = isInsideVisibleRegion( + // pos.latitude, pos.longitude, true + //) || (oldPos?.let { isInsideVisibleRegion(it.latitude,it.longitude,true) } ?: false) + val skip = true + if (skip) { + //animate = true + //this moves both the icon and the label + moveVehicleToNewPosition(pos) + } else { + positionsByVehDict[vehID] = pos + /*busLabelSymbolsByVeh[vehID]?.let { + it.latLng = LatLng(pos.latitude, pos.longitude) + symbolsToUpdate.add(it) + }*/ + } + } + } + else{ + // update it simply + positionsByVehDict[vehID] = pos + //createLabelForVehicle(pos) } } + //symbolManager.update(symbolsToUpdate) + //remove old positions + vehsOld.removeAll(vehsNew) + //now vehsOld contains the vehicles id for those that have NOT been updated + val currentTimeStamp = System.currentTimeMillis() /1000 + for(vehID in vehsOld){ + //remove after 2 minutes of inactivity + if (positionsByVehDict[vehID]!!.timestamp - currentTimeStamp > 2*60){ + positionsByVehDict.remove(vehID) + //removeVehicleLabel(vehID) + } + } + //update UI + updatePositionsIcons() + } - val noPatternsTrips = ArrayList() - for (tripID in tripsPatterns.keys) { - val (update, tripWithPatternStops) = tripsPatterns[tripID] ?: continue - - var marker: Marker? = null - //check if Marker is already created - if (busPositionMarkersByTrip.containsKey(tripID)) { - - //check if the trip direction ID is the same, if not remove - if(tripWithPatternStops?.pattern != null && - tripWithPatternStops.pattern.directionId != viewingPattern?.pattern?.directionId){ - removeBusMarker(tripID) - - } else { - //need to change the position of the marker - marker = busPositionMarkersByTrip.get(tripID)!! - BusPositionUtils.updateBusPositionMarker(map, marker, update, tripMarkersAnimators, false) - // Set the pattern to add the info - if (marker.infoWindow != null && marker.infoWindow is BusInfoWindow) { - val window = marker.infoWindow as BusInfoWindow - if (window.pattern == null && tripWithPatternStops != null) { - //Log.d(DEBUG_TAG, "Update pattern for trip: "+tripID); - window.setPatternAndDraw(tripWithPatternStops.pattern) - } + /** + * This is the tricky part, animating the transitions + * Basically, we need to set the new positions with the data and redraw them all + */ + private fun moveVehicleToNewPosition(positionUpdate: LivePositionUpdate){ + if (positionUpdate.vehicle !in positionsByVehDict.keys) + return + val vehID = positionUpdate.vehicle + val currentUpdate = positionsByVehDict[positionUpdate.vehicle] + currentUpdate?.let { it -> + //cancel current animation on vehicle + animatorsByVeh[vehID]?.cancel() + + val currentPos = LatLng(it.latitude, it.longitude) + val newPos = LatLng(positionUpdate.latitude, positionUpdate.longitude) + val valueAnimator = ValueAnimator.ofObject(MapUtils.LatLngEvaluator(), currentPos, newPos) + valueAnimator.addUpdateListener(object : ValueAnimator.AnimatorUpdateListener { + private var latLng: LatLng? = null + override fun onAnimationUpdate(animation: ValueAnimator) { + latLng = animation.animatedValue as LatLng + //update position on animation + val update = positionsByVehDict[positionUpdate.vehicle]!! + latLng?.let { ll-> + update.latitude = ll.latitude + update.longitude = ll.longitude + updatePositionsIcons() } } - } else { - //marker is not there, need to make it - //if (mapView == null) Log.e(MapFragment.DEBUG_TAG, "Creating marker with null map, things will explode") - marker = Marker(map) - - //String route = GtfsUtils.getLineNameFromGtfsID(update.getRouteID()); - val mdraw = ResourcesCompat.getDrawable(getResources(), R.drawable.map_bus_position_icon, null)!! - //mdraw.setBounds(0,0,28,28); - - marker.icon = mdraw - var markerPattern: MatoPattern? = null - if (tripWithPatternStops != null) { - if (tripWithPatternStops.pattern != null) - markerPattern = tripWithPatternStops.pattern - } - marker.infoWindow = BusInfoWindow(map, update, markerPattern, true) { - // set pattern to show - if(it!=null) - showPatternWithStop(it.code) + }) + /*valueAnimator.addListener(object : AnimatorListenerAdapter() { + override fun onAnimationStart(animation: Animator) { + super.onAnimationStart(animation) + //val update = positionsByVehDict[positionUpdate.vehicle]!! + //remove the label at the start of the animation + //removeVehicleLabel(vehID) + val annot = busLabelSymbolsByVeh[vehID] + annot?.let { sym -> + sym.textOpacity = 0.0f + symbolsToUpdate.add(sym) + } } - //marker.infoWindow as BusInfoWindow - marker.setAnchor(Marker.ANCHOR_CENTER, Marker.ANCHOR_CENTER) - marker.setInfoWindowAnchor(Marker.ANCHOR_CENTER, Marker.ANCHOR_CENTER) - BusPositionUtils.updateBusPositionMarker(map,marker, update, tripMarkersAnimators,true) - // the overlay is null when it's not attached yet? - // cannot recreate it because it becomes null very soon - // if(busPositionsOverlay == null) busPositionsOverlay = new FolderOverlay(); - //save the marker - if (busPositionsOverlay != null) { - busPositionsOverlay.add(marker) - busPositionMarkersByTrip.put(tripID, marker) + + override fun onAnimationEnd(animation: Animator) { + super.onAnimationEnd(animation) + /*val annot = busLabelSymbolsByVeh[vehID] + annot?.let { sym -> + sym.textOpacity = 1.0f + sym.latLng = newPos //LatLng(newPos) + symbolsToUpdate.add(sym) + } + + */ } - } + }) + */ + animatorsByVeh[vehID]?.cancel() + //set the new position as the current one but with the old lat and lng + positionUpdate.latitude = currentUpdate.latitude + positionUpdate.longitude = currentUpdate.longitude + positionsByVehDict[vehID] = positionUpdate + valueAnimator.duration = 300 + valueAnimator.interpolator = LinearInterpolator() + valueAnimator.start() + + animatorsByVeh[vehID] = valueAnimator + + } ?: { + Log.e(DEBUG_TAG, "Have to run animation for veh ${positionUpdate.vehicle} but not in the dict, adding") + positionsByVehDict[positionUpdate.vehicle] = positionUpdate } + } + /** + * Update the bus positions displayed on the map, from the existing data + */ + private fun updatePositionsIcons(){ + //avoid frequent updates + val currentTime = System.currentTimeMillis() + if(currentTime - lastUpdateTime < 60){ + //DO NOT UPDATE THE MAP + return + } + val features = ArrayList()//stops.mapNotNull { stop -> + //stop.latitude?.let { lat -> + // stop.longitude?.let { lon -> + for (pos in positionsByVehDict.values){ + //if (s.latitude!=null && s.longitude!=null) + val point = Point.fromLngLat(pos.longitude, pos.latitude) + features.add( + Feature.fromGeometry( + point, + JsonObject().apply { + addProperty("veh", pos.vehicle) + addProperty("trip", pos.tripID) + addProperty("bearing", pos.bearing ?:0.0f) + addProperty("line", pos.routeID) + } + ) + ) + /*busLabelSymbolsByVeh[pos.vehicle]?.let { + it.latLng = LatLng(pos.latitude, pos.longitude) + symbolsToUpdate.add(it) + } - if (noPatternsTrips.size > 0) { - Log.i(DEBUG_TAG, "These trips have no matching pattern: $noPatternsTrips") + */ } + busesSource.setGeoJson(FeatureCollection.fromFeatures(features)) + //update labels, clear cache to be used + //symbolManager.update(symbolsToUpdate) + //symbolsToUpdate.clear() + lastUpdateTime = System.currentTimeMillis() } + override fun onResume() { super.onResume() Log.d(DEBUG_TAG, "Resetting paused from onResume") pausedFragment = false val keySourcePositions = getString(R.string.pref_positions_source) useMQTTPositions = PreferenceManager.getDefaultSharedPreferences(requireContext()) .getString(keySourcePositions, "mqtt").contentEquals("mqtt") //separate paths if(useMQTTPositions) liveBusViewModel.requestMatoPosUpdates(GtfsUtils.getLineNameFromGtfsID(lineID)) else liveBusViewModel.requestGTFSUpdates() if(mapViewModel.currentLat.value!=MapViewModel.INVALID) { Log.d(DEBUG_TAG, "mapViewModel posi: ${mapViewModel.currentLat.value}, ${mapViewModel.currentLong.value}"+ " zoom ${mapViewModel.currentZoom.value}") - val controller = map.controller + //THIS WAS A FIX FOR THE OLD OSMDROID MAP + /*val controller = map.controller viewLifecycleOwner.lifecycleScope.launch { delay(100) Log.d(DEBUG_TAG, "zooming back to point") controller.animateTo(GeoPoint(mapViewModel.currentLat.value!!, mapViewModel.currentLong.value!!), mapViewModel.currentZoom.value!!,null,null) //controller.setCenter(GeoPoint(mapViewModel.currentLat.value!!, mapViewModel.currentLong.value!!)) //controller.setZoom(mapViewModel.currentZoom.value!!) } - - //controller.setZoom() + */ } //initialize GUI here fragmentListener.readyGUIfor(FragmentKind.LINES) } override fun onPause() { super.onPause() - liveBusViewModel.stopMatoUpdates() + if(useMQTTPositions) liveBusViewModel.stopMatoUpdates() pausedFragment = true //save map - val center = map.mapCenter - mapViewModel.currentLat.value = center.latitude - mapViewModel.currentLong.value = center.longitude - mapViewModel.currentZoom.value = map.zoomLevel.toDouble() + val camera = map?.cameraPosition + camera?.let {cam-> + mapViewModel.currentLat.value = cam.target?.latitude ?: -400.0 + mapViewModel.currentLong.value = cam.target?.longitude ?: -400.0 + mapViewModel.currentZoom.value = cam.zoom + } + } override fun getBaseViewForSnackBar(): View? { return null } companion object { private const val LINEID_KEY="lineID" - fun newInstance() = LinesDetailFragment() - const val DEBUG_TAG="LinesDetailFragment" + private const val STOPS_SOURCE_ID = "stops-source" + private const val STOPS_LAYER_ID = "stops-layer" + private const val BUSES_SOURCE_ID = "buses-source" + private const val BUSES_LAYER_ID = "buses-layer" + private const val STOP_ACTIVE_IMG = "stop_active_img" + private const val STOP_IMAGE_ID = "stop-img" + private const val POLYLINE_LAYER = "polyline-layer" + private const val POLYLINE_SOURCE = "polyline-source" + + private const val DEBUG_TAG="LinesDetailFragment" fun makeArgs(lineID: String): Bundle{ val b = Bundle() b.putString(LINEID_KEY, lineID) return b } + fun newInstance(lineID: String?) = LinesDetailFragment().apply { + lineID?.let { arguments = makeArgs(it) } + } @JvmStatic - private fun findOptimalPosition(stop: Stop, pointsList: MutableList): GeoPoint{ + private fun findOptimalPosition(stop: Stop, pointsList: MutableList): LatLng{ if(stop.latitude==null || stop.longitude ==null|| pointsList.isEmpty()) throw IllegalArgumentException() val sLat = stop.latitude!! val sLong = stop.longitude!! if(pointsList.size < 2) return pointsList[0] pointsList.sortBy { utils.measuredistanceBetween(sLat, sLong, it.latitude, it.longitude) } val p1 = pointsList[0] val p2 = pointsList[1] if (p1.longitude == p2.longitude){ //Log.e(DEBUG_TAG, "Same longitude") - return GeoPoint(sLat, p1.longitude) + return LatLng(sLat, p1.longitude) } else if (p1.latitude == p2.latitude){ //Log.d(DEBUG_TAG, "Same latitude") - return GeoPoint(p2.latitude,sLong) + return LatLng(p2.latitude,sLong) } val m = (p1.latitude - p2.latitude) / (p1.longitude - p2.longitude) val minv = (p1.longitude-p2.longitude)/(p1.latitude - p2.latitude) val cR = p1.latitude - p1.longitude * m val longNew = (minv * sLong + sLat -cR ) / (m+minv) val latNew = (m*longNew + cR) //Log.d(DEBUG_TAG,"Stop ${stop.ID} old pos: ($sLat, $sLong), new pos ($latNew,$longNew)") - return GeoPoint(latNew,longNew) + return LatLng(latNew,longNew) } private const val DEFAULT_CENTER_LAT = 45.12 private const val DEFAULT_CENTER_LON = 7.6858 } } \ No newline at end of file diff --git a/app/src/main/java/it/reyboz/bustorino/fragments/MapLibreFragment.kt b/app/src/main/java/it/reyboz/bustorino/fragments/MapLibreFragment.kt index 0b7f634..d1e1143 100644 --- a/app/src/main/java/it/reyboz/bustorino/fragments/MapLibreFragment.kt +++ b/app/src/main/java/it/reyboz/bustorino/fragments/MapLibreFragment.kt @@ -1,1151 +1,1136 @@ package it.reyboz.bustorino.fragments import android.Manifest import android.animation.Animator import android.animation.AnimatorListenerAdapter import android.animation.ValueAnimator import android.annotation.SuppressLint import android.content.Context import android.content.pm.PackageManager import android.location.Location import android.location.LocationListener import android.location.LocationManager import android.os.Bundle import android.util.Log import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.view.animation.LinearInterpolator import android.widget.ImageButton import android.widget.RelativeLayout import android.widget.TextView import android.widget.Toast import androidx.activity.result.ActivityResultCallback import androidx.activity.result.contract.ActivityResultContracts import androidx.cardview.widget.CardView import androidx.core.app.ActivityCompat import androidx.core.content.ContextCompat import androidx.core.content.res.ResourcesCompat import androidx.fragment.app.Fragment import androidx.fragment.app.viewModels import androidx.preference.PreferenceManager import com.google.android.material.bottomsheet.BottomSheetBehavior import com.google.gson.Gson import com.google.gson.JsonObject import it.reyboz.bustorino.R import it.reyboz.bustorino.backend.Stop import it.reyboz.bustorino.backend.gtfs.LivePositionUpdate import it.reyboz.bustorino.backend.mato.MQTTMatoClient import it.reyboz.bustorino.data.gtfs.TripAndPatternWithStops import it.reyboz.bustorino.fragments.SettingsFragment.LIVE_POSITIONS_PREF_MQTT_VALUE import it.reyboz.bustorino.map.MapUtils import it.reyboz.bustorino.map.Styles import it.reyboz.bustorino.util.Permissions import it.reyboz.bustorino.viewmodels.LivePositionsViewModel import it.reyboz.bustorino.viewmodels.StopsMapViewModel import org.maplibre.android.MapLibre import org.maplibre.android.camera.CameraPosition import org.maplibre.android.camera.CameraUpdateFactory import org.maplibre.android.geometry.LatLng import org.maplibre.android.geometry.LatLngBounds import org.maplibre.android.location.LocationComponent import org.maplibre.android.location.LocationComponentActivationOptions import org.maplibre.android.location.LocationComponentOptions import org.maplibre.android.location.engine.LocationEngineRequest import org.maplibre.android.location.modes.CameraMode import org.maplibre.android.maps.MapLibreMap import org.maplibre.android.maps.MapView import org.maplibre.android.maps.OnMapReadyCallback import org.maplibre.android.maps.Style import org.maplibre.android.plugins.annotation.Symbol import org.maplibre.android.plugins.annotation.SymbolManager import org.maplibre.android.plugins.annotation.SymbolOptions import org.maplibre.android.style.expressions.Expression import org.maplibre.android.style.layers.Property.* import org.maplibre.android.style.layers.PropertyFactory import org.maplibre.android.style.layers.SymbolLayer import org.maplibre.android.style.sources.GeoJsonSource import org.maplibre.geojson.Feature import org.maplibre.geojson.FeatureCollection import org.maplibre.geojson.Point // TODO: Rename parameter arguments, choose names that match // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER private const val STOP_TO_SHOW = "stoptoshow" /** * A simple [Fragment] subclass. * Use the [MapLibreFragment.newInstance] factory method to * create an instance of this fragment. */ class MapLibreFragment : ScreenBaseFragment(), OnMapReadyCallback { - protected var listenerMain: FragmentListenerMain? = null + protected var fragmentListener: CommonFragmentListener? = null //private var param1: String? = null //private var param2: String? = null // Declare a variable for MapView private lateinit var mapView: MapView private lateinit var locationComponent: LocationComponent private var lastLocation: Location? = null private val stopsViewModel: StopsMapViewModel by viewModels() private val gson = Gson() private var stopsShowing = ArrayList(0) private var isBottomSheetShowing = false private lateinit var symbolManager: SymbolManager protected var map: MapLibreMap? = null // Sources for stops and buses private lateinit var stopsSource: GeoJsonSource private lateinit var busesSource: GeoJsonSource private var stopsLayerStarted = false private var lastStopsSizeShown = 0 private var lastBBox = LatLngBounds.from(2.0, 2.0, 1.0,1.0) private lateinit var mapStyle: Style private var mapInitCompleted =false private var stopsRedrawnTimes = 0 //bottom Sheet behavior private lateinit var bottomSheetBehavior: BottomSheetBehavior private var bottomLayout: RelativeLayout? = null private lateinit var stopTitleTextView: TextView private lateinit var stopNumberTextView: TextView private lateinit var linesPassingTextView: TextView private lateinit var arrivalsCard: CardView private lateinit var directionsCard: CardView private var stopActiveSymbol: Symbol? = null // Location stuff private lateinit var locationManager: LocationManager private lateinit var showUserPositionButton: ImageButton private lateinit var centerUserButton: ImageButton private lateinit var followUserButton: ImageButton private var followingUserLocation = false private var pendingLocationActivation = false private var ignoreCameraMovementForFollowing = true private var enablingPositionFromClick = false private val positionRequestLauncher = registerForActivityResult, Map>( ActivityResultContracts.RequestMultiplePermissions(), ActivityResultCallback { result -> if (result == null) { Log.w(DEBUG_TAG, "Got asked permission but request is null, doing nothing?") } else if (java.lang.Boolean.TRUE == result[Manifest.permission.ACCESS_COARSE_LOCATION] && java.lang.Boolean.TRUE == result[Manifest.permission.ACCESS_FINE_LOCATION]) { // We can use the position, restart location overlay Log.d(DEBUG_TAG, "HAVE THE PERMISSIONS") if (context == null || requireContext().getSystemService(Context.LOCATION_SERVICE) == null) return@ActivityResultCallback ///@registerForActivityResult val locationManager = requireContext().getSystemService(Context.LOCATION_SERVICE) as LocationManager @SuppressLint("MissingPermission") val userLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER) if (userLocation != null) { if(LatLng(userLocation.latitude, userLocation.longitude).distanceTo(DEFAULT_LATLNG) >= MAX_DIST_KM*1000){ setMapLocationEnabled(true, true, false) } } else requestInitialUserLocation() } else{ Toast.makeText(requireContext(),"User location disabled", Toast.LENGTH_SHORT).show() Log.w(DEBUG_TAG, "No location permission") } }) private val showUserPositionRequestLauncher = registerForActivityResult, Map>( ActivityResultContracts.RequestMultiplePermissions(), ActivityResultCallback { result -> if (result == null) { Log.w(DEBUG_TAG, "Got asked permission but request is null, doing nothing?") } else if (java.lang.Boolean.TRUE == result[Manifest.permission.ACCESS_COARSE_LOCATION] && java.lang.Boolean.TRUE == result[Manifest.permission.ACCESS_FINE_LOCATION]) { // We can use the position, restart location overlay if (context == null || requireContext().getSystemService(Context.LOCATION_SERVICE) == null) return@ActivityResultCallback ///@registerForActivityResult setMapLocationEnabled(true, true, enablingPositionFromClick) } else Log.w(DEBUG_TAG, "No location permission") }) //BUS POSITIONS private var useMQTTViewModel = true private val livePositionsViewModel : LivePositionsViewModel by viewModels() private val positionsByVehDict = HashMap(5) private val animatorsByVeh = HashMap() private var lastUpdateTime : Long = -1 private var busLabelSymbolsByVeh = HashMap() private val symbolsToUpdate = ArrayList() private var initialStopToShow : Stop? = null private var initialStopShown = false //shown stuff private var savedStateOnStop : Bundle? = null private var savedMapStateOnPause : Bundle? = null private var shownStopInBottomSheet : Stop? = null private val showBusLayer = true override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) arguments?.let { initialStopToShow = Stop.fromBundle(arguments) } MapLibre.getInstance(requireContext()) } override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View? { // Inflate the layout for this fragment val rootView = inflater.inflate(R.layout.fragment_map_libre, container, false) //reset the counter lastStopsSizeShown = 0 stopsRedrawnTimes = 0 stopsLayerStarted = false symbolsToUpdate.clear() // Init layout view // Init the MapView mapView = rootView.findViewById(R.id.libreMapView) if(savedStateOnStop!=null){ mapView.onCreate(savedStateOnStop) } else mapView.onCreate(savedInstanceState) mapView.getMapAsync(this) //{ //map -> //map.setStyle("https://demotiles.maplibre.org/style.json") } //init bottom sheet val bottomSheet = rootView.findViewById(R.id.bottom_sheet) bottomLayout = bottomSheet stopTitleTextView = bottomSheet.findViewById(R.id.stopTitleTextView) stopNumberTextView = bottomSheet.findViewById(R.id.stopNumberTextView) linesPassingTextView = bottomSheet.findViewById(R.id.linesPassingTextView) arrivalsCard = bottomSheet.findViewById(R.id.arrivalsCardButton) directionsCard = bottomSheet.findViewById(R.id.directionsCardButton) + showUserPositionButton = rootView.findViewById(R.id.locationEnableIcon) showUserPositionButton.setOnClickListener(this::switchUserLocationStatus) followUserButton = rootView.findViewById(R.id.followUserImageButton) centerUserButton = rootView.findViewById(R.id.centerMapImageButton) bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet) bottomSheetBehavior.state = BottomSheetBehavior.STATE_HIDDEN arrivalsCard.setOnClickListener { if(context!=null){ Toast.makeText(context,"ARRIVALS", Toast.LENGTH_SHORT).show() } } centerUserButton.setOnClickListener { if(context!=null && locationComponent.isLocationComponentEnabled) { val location = locationComponent.lastKnownLocation location?.let { mapView.getMapAsync { map -> map.animateCamera(CameraUpdateFactory.newCameraPosition( CameraPosition.Builder().target(LatLng(location.latitude, location.longitude)).build()), 500) } } } } followUserButton.setOnClickListener { // onClick user following button if(context!=null && locationComponent.isLocationComponentEnabled){ if(followingUserLocation) locationComponent.cameraMode = CameraMode.NONE else locationComponent.cameraMode = CameraMode.TRACKING // CameraMode.TRACKING makes the camera move and jump to the location setFollowingUser(!followingUserLocation) } } locationManager = requireActivity().getSystemService(Context.LOCATION_SERVICE) as LocationManager - if (haveLocationPermissions()) { + if (Permissions.bothLocationPermissionsGranted(requireContext())) { requestInitialUserLocation() } else{ if (shouldShowRequestPermissionRationale(Manifest.permission.ACCESS_FINE_LOCATION)) { //TODO: show dialog for permission rationale Toast.makeText(activity, R.string.enable_position_message_map, Toast.LENGTH_SHORT) .show() } positionRequestLauncher.launch(Permissions.LOCATION_PERMISSIONS) } // Setup close button rootView.findViewById(R.id.btnClose).setOnClickListener { hideStopBottomSheet() } Log.d(DEBUG_TAG, "Fragment View Created!") //TODO: Reshow last open stop when switching back to the map fragment return rootView } /** * This method sets up the map and the layers */ override fun onMapReady(mapReady: MapLibreMap) { this.map = mapReady val mjson = Styles.getJsonStyleFromAsset(requireContext(), "map_style_good_noshops.json") //ViewUtils.loadJsonFromAsset(requireContext(),"map_style_good.json") activity?.run { val builder = Style.Builder().fromJson(mjson!!) mapReady.setStyle(builder) { style -> mapStyle = style //setupLayers(style) symbolManager = SymbolManager(mapView,mapReady,style) symbolManager.iconAllowOverlap = true symbolManager.textAllowOverlap = false symbolManager.addClickListener{ _ -> if (stopActiveSymbol!=null){ hideStopBottomSheet() return@addClickListener true } else return@addClickListener false } // Start observing data observeStops() initMapLocation(style, mapReady, requireContext()) - //initStopsLayer(style, FeatureCollection.fromFeatures(ArrayList())) //init stop layer with this val stopsInCache = stopsViewModel.getAllStopsLoaded() if(stopsInCache.isEmpty()) initStopsLayer(style, FeatureCollection.fromFeatures(ArrayList())) else displayStops(stopsInCache) if(showBusLayer) setupBusLayer(style) } mapReady.addOnCameraIdleListener { map?.let { val newBbox = it.projection.visibleRegion.latLngBounds if ((newBbox.center==lastBBox.center) && (newBbox.latitudeSpan==lastBBox.latitudeSpan) && (newBbox.longitudeSpan==lastBBox.latitudeSpan)){ //do nothing } else { stopsViewModel.loadStopsInLatLngBounds(newBbox) lastBBox = newBbox } } } mapReady.addOnCameraMoveStartedListener { map?.let { setFollowingUser(it.locationComponent.cameraMode == CameraMode.TRACKING) } //setFollowingUser() } mapReady.addOnMapClickListener { point -> val screenPoint = mapReady.projection.toScreenLocation(point) val features = mapReady.queryRenderedFeatures(screenPoint, STOPS_LAYER_ID) val busNearby = mapReady.queryRenderedFeatures(screenPoint, BUSES_LAYER_ID) if (features.isNotEmpty()) { val feature = features[0] val id = feature.getStringProperty("id") val name = feature.getStringProperty("name") //Toast.makeText(requireContext(), "Clicked on $name ($id)", Toast.LENGTH_SHORT).show() val stop = stopsViewModel.getStopByID(id) stop?.let { if (isBottomSheetShowing){ hideStopBottomSheet() } openStopInBottomSheet(it) isBottomSheetShowing = true //move camera if(it.latitude!=null && it.longitude!=null) //mapReady.cameraPosition = CameraPosition.Builder().target(LatLng(it.latitude!!, it.longitude!!)).build() mapReady.animateCamera(CameraUpdateFactory.newLatLng(LatLng(it.latitude!!,it.longitude!!)),750) } return@addOnMapClickListener true } else if (busNearby.isNotEmpty()){ val feature = busNearby[0] val vehid = feature.getStringProperty("veh") val route = feature.getStringProperty("line") if(context!=null){ Toast.makeText(context, "Veh $vehid on route $route", Toast.LENGTH_SHORT).show() } return@addOnMapClickListener true } false } mapInitCompleted = true // we start requesting the bus positions now startRequestingPositions() } savedMapStateOnPause?.let{ restoreMapStateFromBundle(it) pendingLocationActivation = false Log.d(DEBUG_TAG, "Restored map state from the saved bundle") } //reset saved State at the end if( savedMapStateOnPause == null) { //set initial position val zoom = 15.0 //center position val latlngTarget = initialStopToShow?.let { LatLng(it.latitude!!, it.longitude!!) } ?: LatLng(DEFAULT_CENTER_LAT, DEFAULT_CENTER_LON) mapReady.cameraPosition = CameraPosition.Builder().target(latlngTarget).zoom(zoom).build() } //reset saved state savedMapStateOnPause = null } private fun initStopsLayer(style: Style, features:FeatureCollection){ stopsSource = GeoJsonSource(STOPS_SOURCE_ID,features) style.addSource(stopsSource) // add icon style.addImage(STOP_IMAGE_ID, ResourcesCompat.getDrawable(resources,R.drawable.bus_stop_new, activity?.theme)!!) style.addImage(STOP_ACTIVE_IMG, ResourcesCompat.getDrawable(resources, R.drawable.bus_stop_new_highlight, activity?.theme)!!) // Stops layer val stopsLayer = SymbolLayer(STOPS_LAYER_ID, STOPS_SOURCE_ID) stopsLayer.withProperties( PropertyFactory.iconImage(STOP_IMAGE_ID), PropertyFactory.iconAllowOverlap(true), PropertyFactory.iconIgnorePlacement(true) ) style.addLayerBelow(stopsLayer, "label_country_1") stopsLayerStarted = true } /** * Setup the Map Layers */ private fun setupBusLayer(style: Style) { // Buses source busesSource = GeoJsonSource(BUSES_SOURCE_ID) style.addSource(busesSource) style.addImage("bus_symbol",ResourcesCompat.getDrawable(resources, R.drawable.map_bus_position_icon, activity?.theme)!!) // Buses layer val busesLayer = SymbolLayer(BUSES_LAYER_ID, BUSES_SOURCE_ID).apply { withProperties( PropertyFactory.iconImage("bus_symbol"), PropertyFactory.iconSize(1.2f), PropertyFactory.iconAllowOverlap(true), PropertyFactory.iconIgnorePlacement(true), PropertyFactory.iconRotate(Expression.get("bearing")), PropertyFactory.iconRotationAlignment(ICON_ROTATION_ALIGNMENT_MAP) ) } style.addLayerAbove(busesLayer, STOPS_LAYER_ID) //Line names layer /*vehiclesLabelsSource = GeoJsonSource(LABELS_SOURCE) style.addSource(vehiclesLabelsSource) val textLayer = SymbolLayer(LABELS_LAYER_ID, LABELS_SOURCE).apply { withProperties( PropertyFactory.textField("label"), PropertyFactory.textSize(30f), PropertyFactory.textColor(Color.BLACK), //PropertyFactory.textHaloColor(Color.BLACK), //PropertyFactory.textHaloWidth(1f), PropertyFactory.textAnchor(TEXT_ANCHOR_CENTER), PropertyFactory.textAllowOverlap(true), PropertyFactory.textRotationAlignment(TEXT_ROTATION_ALIGNMENT_VIEWPORT) ) } style.addLayerAbove(textLayer, BUSES_LAYER_ID) */ } /** * Update the bottom sheet with the stop information */ private fun openStopInBottomSheet(stop: Stop){ bottomLayout?.let { //lay.findViewById(R.id.stopTitleTextView).text ="${stop.ID} - ${stop.stopDefaultName}" stopTitleTextView.text = stop.stopDefaultName stopNumberTextView.text = stop.ID val string_show = if (stop.numRoutesStopping==0) "" else if (stop.numRoutesStopping <= 1) requireContext().getString(R.string.line_fill, stop.routesThatStopHereToString()) else requireContext().getString(R.string.lines_fill, stop.routesThatStopHereToString()) linesPassingTextView.text = string_show //SET ON CLICK LISTENER arrivalsCard.setOnClickListener{ - listenerMain?.requestArrivalsForStopID(stop.ID) + fragmentListener?.requestArrivalsForStopID(stop.ID) } } //add stop marker if (stop.latitude!=null && stop.longitude!=null) { stopActiveSymbol = symbolManager.create( SymbolOptions() .withLatLng(LatLng(stop.latitude!!, stop.longitude!!)) .withIconImage(STOP_ACTIVE_IMG) .withIconAnchor(ICON_ANCHOR_CENTER) ) } shownStopInBottomSheet = stop bottomSheetBehavior.state = BottomSheetBehavior.STATE_EXPANDED - } override fun onAttach(context: Context) { super.onAttach(context) - listenerMain = if (context is FragmentListenerMain) { + fragmentListener = if (context is CommonFragmentListener) { context } else { throw RuntimeException( context.toString() + " must implement FragmentListenerMain" ) } } override fun onDetach() { super.onDetach() - listenerMain = null + fragmentListener = null } override fun onStart() { super.onStart() mapView.onStart() } override fun onResume() { super.onResume() mapView.onResume() val keySourcePositions = getString(R.string.pref_positions_source) if(showBusLayer) { useMQTTViewModel = PreferenceManager.getDefaultSharedPreferences(requireContext()) .getString(keySourcePositions, LIVE_POSITIONS_PREF_MQTT_VALUE) .contentEquals(LIVE_POSITIONS_PREF_MQTT_VALUE) if (useMQTTViewModel) livePositionsViewModel.requestMatoPosUpdates(MQTTMatoClient.LINES_ALL) else livePositionsViewModel.requestGTFSUpdates() //mapViewModel.testCascade(); livePositionsViewModel.isLastWorkResultGood.observe(this) { d: Boolean -> Log.d( DEBUG_TAG, "Last trip download result is $d" ) } livePositionsViewModel.tripsGtfsIDsToQuery.observe(this) { dat: List -> Log.i(DEBUG_TAG, "Have these trips IDs missing from the DB, to be queried: $dat") livePositionsViewModel.downloadTripsFromMato(dat) } } - listenerMain?.readyGUIfor(FragmentKind.MAP) + fragmentListener?.readyGUIfor(FragmentKind.MAP) } override fun onPause() { super.onPause() mapView.onPause() Log.d(DEBUG_TAG, "Fragment paused") savedMapStateOnPause = saveMapStateInBundle() + if (useMQTTViewModel) livePositionsViewModel.stopMatoUpdates() + } override fun onStop() { super.onStop() mapView.onStop() Log.d(DEBUG_TAG, "Fragment stopped!") savedStateOnStop = Bundle().let { mapView.onSaveInstanceState(it) it } } override fun onLowMemory() { super.onLowMemory() mapView.onLowMemory() } override fun onDestroy() { super.onDestroy() mapView.onDestroy() Log.d(DEBUG_TAG, "Destroyed map Fragment!!") } override fun getBaseViewForSnackBar(): View? { return mapView } override fun onSaveInstanceState(outState: Bundle) { super.onSaveInstanceState(outState) mapView.onSaveInstanceState(outState) Log.d(DEBUG_TAG, "Saved instanceState") } private fun observeStops() { // Observe stops stopsViewModel.stopsToShow.observe(viewLifecycleOwner) { stops -> stopsShowing = ArrayList(stops) displayStops(stopsShowing) initialStopToShow?.let{ s-> //show the stop in the bottom sheet if(!initialStopShown) { openStopInBottomSheet(s) initialStopShown = true } } } } /** * Add the stops to the layers */ private fun displayStops(stops: List?) { if (stops.isNullOrEmpty()) return if (stops.size==lastStopsSizeShown){ Log.d(DEBUG_TAG, "Not updating, have same number of stops. After 3 times") return } /*if(stops.size> lastStopsSizeShown){ stopsRedrawnTimes = 0 } else{ stopsRedrawnTimes++ } */ val features = ArrayList()//stops.mapNotNull { stop -> //stop.latitude?.let { lat -> // stop.longitude?.let { lon -> for (s in stops){ if (s.latitude!=null && s.longitude!=null) features.add( Feature.fromGeometry( Point.fromLngLat(s.longitude!!, s.latitude!!), JsonObject().apply { addProperty("id", s.ID) addProperty("name", s.stopDefaultName) addProperty("routes", s.routesThatStopHereToString()) // Add routes array to JSON object } ) ) } Log.d(DEBUG_TAG,"Have put ${features.size} stops to display") // if the layer is already started, substitute the stops inside, otherwise start it if (stopsLayerStarted) { stopsSource.setGeoJson(FeatureCollection.fromFeatures(features)) lastStopsSizeShown = features.size } else map?.let { Log.d(DEBUG_TAG, "Map stop layer is not started yet, init layer") initStopsLayer(mapStyle, FeatureCollection.fromFeatures(features)) Log.d(DEBUG_TAG,"Started stops layer on map") lastStopsSizeShown = features.size stopsLayerStarted = true } } // Hide the bottom sheet and remove extra symbol private fun hideStopBottomSheet(){ if (stopActiveSymbol!=null){ symbolManager.delete(stopActiveSymbol) stopActiveSymbol = null } bottomSheetBehavior.state = BottomSheetBehavior.STATE_HIDDEN isBottomSheetShowing = false //remove initial stop if(initialStopToShow!=null){ initialStopToShow = null } shownStopInBottomSheet = null } // --------------- BUS LOCATIONS STUFF -------------------------- /** * Start requesting position updates */ private fun startRequestingPositions() { livePositionsViewModel.updatesWithTripAndPatterns.observe(viewLifecycleOwner) { data: HashMap> -> Log.d( DEBUG_TAG, "Have " + data.size + " trip updates, has Map start finished: " + mapInitCompleted ) if (mapInitCompleted) updateBusPositionsInMap(data) if (!isDetached && !useMQTTViewModel) livePositionsViewModel.requestDelayedGTFSUpdates( 3000 ) } } private fun isInsideVisibleRegion(latitude: Double, longitude: Double, nullValue: Boolean): Boolean{ var isInside = nullValue val visibleRegion = map?.projection?.visibleRegion visibleRegion?.let { val bounds = it.latLngBounds isInside = bounds.contains(LatLng(latitude, longitude)) } return isInside } private fun createLabelForVehicle(positionUpdate: LivePositionUpdate){ val newSymbol = symbolManager.create(SymbolOptions() .withLatLng(LatLng(positionUpdate.latitude, positionUpdate.longitude)) .withTextColor("#ffffff") .withTextField(positionUpdate.routeID.substringBeforeLast('U')) .withTextSize(13f) .withTextAnchor(TEXT_ANCHOR_CENTER) ) busLabelSymbolsByVeh[positionUpdate.vehicle] = newSymbol } private fun removeVehicleLabel(vehicle: String){ busLabelSymbolsByVeh[vehicle]?.let { symbolManager.delete(it) busLabelSymbolsByVeh.remove(vehicle) } } + /** + * Update function for the bus positions + * Takes the processed updates and saves them accordingly + */ private fun updateBusPositionsInMap(incomingData: HashMap>){ val vehsNew = HashSet(incomingData.values.map { up -> up.first.vehicle }) val vehsOld = HashSet(positionsByVehDict.keys) val symbolsToUpdate = ArrayList() for (upsWithTrp in incomingData.values){ val pos = upsWithTrp.first val vehID = pos.vehicle var animate = false if (vehsOld.contains(vehID)){ //update position only if the starting or the stopping position of the animation are in the view val oldPos = positionsByVehDict[vehID] var avoidShowingUpdateBecauseIsImpossible = false oldPos?.let{ if(oldPos.routeID!=pos.routeID) { val dist = LatLng(it.latitude, it.longitude).distanceTo(LatLng(pos.latitude, pos.longitude)) val speed = dist*3.6 / (pos.timestamp - it.timestamp) //this should be in km/h Log.w(DEBUG_TAG, "Vehicle $vehID changed route from ${oldPos.routeID} to ${pos.routeID}, distance: $dist, speed: $speed") if (speed > 120 || speed < 0){ avoidShowingUpdateBecauseIsImpossible = true } } } if (avoidShowingUpdateBecauseIsImpossible){ // DO NOT SHOW THIS SHIT Log.w(DEBUG_TAG, "Update for vehicle $vehID skipped") continue } val samePosition = oldPos?.let { (oldPos.latitude==pos.latitude)&&(oldPos.longitude == pos.longitude) }?:false if(!samePosition) { val isPositionInBounds = isInsideVisibleRegion( pos.latitude, pos.longitude, true ) || (oldPos?.let { isInsideVisibleRegion(it.latitude,it.longitude,true) } ?: false) if (isPositionInBounds) { //animate = true //this moves both the icon and the label moveVehicleToNewPosition(pos) } else { positionsByVehDict[vehID] = pos busLabelSymbolsByVeh[vehID]?.let { it.latLng = LatLng(pos.latitude, pos.longitude) symbolsToUpdate.add(it) } } } } else{ // update it simply positionsByVehDict[vehID] = pos createLabelForVehicle(pos) } } symbolManager.update(symbolsToUpdate) //remove old positions vehsOld.removeAll(vehsNew) //now vehsOld contains the vehicles id for those that have NOT been updated val currentTimeStamp = System.currentTimeMillis() /1000 for(vehID in vehsOld){ //remove after 2 minutes of inactivity if (positionsByVehDict[vehID]!!.timestamp - currentTimeStamp > 2*60){ positionsByVehDict.remove(vehID) removeVehicleLabel(vehID) } } //update UI updatePositionsIcons() } /** * This is the tricky part, animating the transitions * Basically, we need to set the new positions with the data and redraw them all */ private fun moveVehicleToNewPosition(positionUpdate: LivePositionUpdate){ if (positionUpdate.vehicle !in positionsByVehDict.keys) return val vehID = positionUpdate.vehicle val currentUpdate = positionsByVehDict[positionUpdate.vehicle] currentUpdate?.let { it -> //cancel current animation on vehicle animatorsByVeh[vehID]?.cancel() val currentPos = LatLng(it.latitude, it.longitude) val newPos = LatLng(positionUpdate.latitude, positionUpdate.longitude) val valueAnimator = ValueAnimator.ofObject(MapUtils.LatLngEvaluator(), currentPos, newPos) valueAnimator.addUpdateListener(object : ValueAnimator.AnimatorUpdateListener { private var latLng: LatLng? = null override fun onAnimationUpdate(animation: ValueAnimator) { latLng = animation.animatedValue as LatLng //update position on animation val update = positionsByVehDict[positionUpdate.vehicle]!! latLng?.let { ll-> update.latitude = ll.latitude update.longitude = ll.longitude updatePositionsIcons() } } }) valueAnimator.addListener(object : AnimatorListenerAdapter() { override fun onAnimationStart(animation: Animator) { super.onAnimationStart(animation) //val update = positionsByVehDict[positionUpdate.vehicle]!! //remove the label at the start of the animation //removeVehicleLabel(vehID) val annot = busLabelSymbolsByVeh[vehID] annot?.let { sym -> sym.textOpacity = 0.0f symbolsToUpdate.add(sym) } } override fun onAnimationEnd(animation: Animator) { super.onAnimationEnd(animation) //recreate the label at the end of the animation //createLabelForVehicle(positionUpdate) val annot = busLabelSymbolsByVeh[vehID] annot?.let { sym -> sym.textOpacity = 1.0f sym.latLng = newPos //LatLng(newPos) symbolsToUpdate.add(sym) } } }) //set the new position as the current one but with the old lat and lng positionUpdate.latitude = currentUpdate.latitude positionUpdate.longitude = currentUpdate.longitude positionsByVehDict[vehID] = positionUpdate valueAnimator.duration = 300 valueAnimator.interpolator = LinearInterpolator() valueAnimator.start() animatorsByVeh[vehID] = valueAnimator } ?: { Log.e(DEBUG_TAG, "Have to run animation for veh ${positionUpdate.vehicle} but not in the dict, adding") positionsByVehDict[positionUpdate.vehicle] = positionUpdate } } /** * Update the bus positions displayed on the map, from the existing data */ private fun updatePositionsIcons(){ //avoid frequent updates val currentTime = System.currentTimeMillis() if(currentTime - lastUpdateTime < 60){ //DO NOT UPDATE THE MAP return } val features = ArrayList()//stops.mapNotNull { stop -> //stop.latitude?.let { lat -> // stop.longitude?.let { lon -> for (pos in positionsByVehDict.values){ //if (s.latitude!=null && s.longitude!=null) val point = Point.fromLngLat(pos.longitude, pos.latitude) features.add( Feature.fromGeometry( point, JsonObject().apply { addProperty("veh", pos.vehicle) addProperty("trip", pos.tripID) addProperty("bearing", pos.bearing ?:0.0f) addProperty("line", pos.routeID) } ) ) /*busLabelSymbolsByVeh[pos.vehicle]?.let { it.latLng = LatLng(pos.latitude, pos.longitude) symbolsToUpdate.add(it) } */ } busesSource.setGeoJson(FeatureCollection.fromFeatures(features)) //update labels, clear cache to be used symbolManager.update(symbolsToUpdate) symbolsToUpdate.clear() lastUpdateTime = System.currentTimeMillis() } // ------ LOCATION STUFF ----- @SuppressLint("MissingPermission") private fun requestInitialUserLocation() { val provider : String = LocationManager.GPS_PROVIDER//getBestLocationProvider() //provider.let { setLocationIconEnabled(true) Toast.makeText(requireContext(), R.string.position_searching_message, Toast.LENGTH_SHORT).show() pendingLocationActivation = true locationManager.requestSingleUpdate(provider, object : LocationListener { override fun onLocationChanged(location: Location) { val userLatLng = LatLng(location.latitude, location.longitude) val distanceToTarget = userLatLng.distanceTo(DEFAULT_LATLNG) if (distanceToTarget <= MAX_DIST_KM*1000.0) { map?.let{ // if we are still waiting for the position to enable if(pendingLocationActivation) setMapLocationEnabled(true, true, false) } } else { Toast.makeText(context, "You are too far, not showing the position", Toast.LENGTH_SHORT).show() } } override fun onProviderDisabled(provider: String) {} override fun onProviderEnabled(provider: String) {} @Deprecated("Deprecated in Java") override fun onStatusChanged(provider: String?, status: Int, extras: Bundle?) {} }, null) } /** * Initialize the map location, but do not enable the component */ @SuppressLint("MissingPermission") private fun initMapLocation(style: Style, map: MapLibreMap, context: Context){ locationComponent = map.locationComponent val locationComponentOptions = LocationComponentOptions.builder(context) .pulseEnabled(true) .build() val locationComponentActivationOptions = - buildLocationComponentActivationOptions(style, locationComponentOptions, context) + MapUtils.buildLocationComponentActivationOptions(style, locationComponentOptions, context) locationComponent.activateLocationComponent(locationComponentActivationOptions) locationComponent.isLocationComponentEnabled = false lastLocation?.let { if (it.accuracy < 200) locationComponent.forceLocationUpdate(it) } } - private fun buildLocationComponentActivationOptions( - style: Style, - locationComponentOptions: LocationComponentOptions, - context: Context - ): LocationComponentActivationOptions { - return LocationComponentActivationOptions - .builder(context, style) - .locationComponentOptions(locationComponentOptions) - .useDefaultLocationEngine(true) - .locationEngineRequest( - LocationEngineRequest.Builder(750) - .setFastestInterval(750) - .setPriority(LocationEngineRequest.PRIORITY_HIGH_ACCURACY) - .build() - ) - .build() - } - private fun haveLocationPermissions(): Boolean{ - return !(ActivityCompat.checkSelfPermission( - requireContext(),Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && - ActivityCompat.checkSelfPermission(requireContext(),Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) - } /** * Handles logic of enabling the user location on the map */ @SuppressLint("MissingPermission") private fun setMapLocationEnabled(enabled: Boolean, assumePermissions: Boolean, fromClick: Boolean) { if (enabled) { - val permissionOk = assumePermissions || haveLocationPermissions() + val permissionOk = assumePermissions || Permissions.bothLocationPermissionsGranted(requireContext()) if (permissionOk) { Log.d(DEBUG_TAG, "Permission OK, starting location component, assumed: $assumePermissions") locationComponent.isLocationComponentEnabled = true if (initialStopToShow==null) { locationComponent.cameraMode = CameraMode.TRACKING //CameraMode.TRACKING setFollowingUser(true) } setLocationIconEnabled(true) if (fromClick) Toast.makeText(context, R.string.location_enabled, Toast.LENGTH_SHORT).show() } else { if (shouldShowRequestPermissionRationale(Manifest.permission.ACCESS_FINE_LOCATION)) { //TODO: show dialog for permission rationale Toast.makeText(activity, R.string.enable_position_message_map, Toast.LENGTH_SHORT).show() } Log.d(DEBUG_TAG, "Requesting permission to show user location") enablingPositionFromClick = fromClick showUserPositionRequestLauncher.launch(Permissions.LOCATION_PERMISSIONS) } } else{ locationComponent.isLocationComponentEnabled = false setFollowingUser(false) setLocationIconEnabled(false) if (fromClick) { Toast.makeText(requireContext(), R.string.location_disabled, Toast.LENGTH_SHORT).show() if(pendingLocationActivation) pendingLocationActivation=false //Cancel the request for the enablement of the position } } } + + private fun setLocationIconEnabled(enabled: Boolean){ if (enabled) showUserPositionButton.setImageDrawable(ContextCompat.getDrawable(requireContext(), R.drawable.location_circlew_red)) else showUserPositionButton.setImageDrawable(ContextCompat.getDrawable(requireContext(), R.drawable.location_circlew_grey)) } /** * Helper method for GUI */ private fun updateFollowingIcon(enabled: Boolean){ if(enabled) followUserButton.setImageDrawable(ContextCompat.getDrawable(requireContext(), R.drawable.ic_follow_me_on)) else followUserButton.setImageDrawable(ContextCompat.getDrawable(requireContext(), R.drawable.ic_follow_me)) } private fun setFollowingUser(following: Boolean){ updateFollowingIcon(following) followingUserLocation = following if(following) ignoreCameraMovementForFollowing = true } private fun switchUserLocationStatus(view: View?){ if(pendingLocationActivation || locationComponent.isLocationComponentEnabled) setMapLocationEnabled(false, false, true) else{ Log.d(DEBUG_TAG, "Request enable location") setMapLocationEnabled(true, false, true) } } private fun saveMapStateBeforePause(bundle: Bundle){ map?.let { val newBbox = it.projection.visibleRegion.latLngBounds bundle.putDouble("center_map_lat", newBbox.center.latitude) bundle.putDouble("center_map_lon", newBbox.center.longitude) it.cameraPosition.zoom.let { z-> bundle.putDouble("map_zoom",z) } } shownStopInBottomSheet?.let { bundle.putBundle("shown_stop", it.toBundle()) } } private fun saveMapStateInBundle(): Bundle { val b = Bundle() saveMapStateBeforePause(b) return b } private fun restoreMapStateFromBundle(bundle: Bundle){ val latCenter = bundle.getDouble("center_map_lat", -10.0) val lonCenter = bundle.getDouble("center_map_lon",-10.0) val zoom = bundle.getDouble("map_zoom", -10.0) if(lonCenter>=0 &&latCenter>=0) map?.let { val newPos = CameraPosition.Builder().target(LatLng(latCenter,lonCenter)) if(zoom>0) newPos.zoom(zoom) it.cameraPosition=newPos.build() } val mStop = bundle.getBundle("shown_stop")?.let { Stop.fromBundle(it) } mStop?.let { openStopInBottomSheet(it) } } companion object { private const val STOPS_SOURCE_ID = "stops-source" private const val STOPS_LAYER_ID = "stops-layer" private const val STOPS_LAYER_SEL_ID ="stops-layer-selected" private const val BUSES_SOURCE_ID = "buses-source" private const val BUSES_LAYER_ID = "buses-layer" private const val LABELS_LAYER_ID = "bus-labels-layer" private const val LABELS_SOURCE = "labels-source" private const val STOP_IMAGE_ID ="bus-stop-icon" - private const val DEFAULT_CENTER_LAT = 45.0708 - private const val DEFAULT_CENTER_LON = 7.6858 + const val DEFAULT_CENTER_LAT = 45.0708 + const val DEFAULT_CENTER_LON = 7.6858 private val DEFAULT_LATLNG = LatLng(DEFAULT_CENTER_LAT, DEFAULT_CENTER_LON) private const val POSITION_FOUND_ZOOM = 16.5 private const val NO_POSITION_ZOOM = 17.1 private const val MAX_DIST_KM = 90.0 //private const val ACCESS_TOKEN="KxO8lF4U3kiO63m0c7lzqDCDrMUVg1OA2JVzRXxxmYSyjugr1xpe4W4Db5rFNvbQ" //private const val MAPLIBRE_URL = "https://api.jawg.io/styles/" private const val DEBUG_TAG = "BusTO-MapLibreFrag" private const val STOP_ACTIVE_IMG = "Stop-active" private const val LOCATION_PERMISSION_REQUEST_CODE = 981202 /** * Use this factory method to create a new instance of * this fragment using the provided parameters. * * @param stop Eventual stop to center the map into * @return A new instance of fragment MapLibreFragment. */ @JvmStatic fun newInstance(stop: Stop?) = MapLibreFragment().apply { arguments = Bundle().let { // Cannot use Parcelable as it requires higher version of Android //stop?.let{putParcelable(STOP_TO_SHOW, it)} stop?.toBundle(it) } } //private fun makeStyleUrl(style: String = "jawg-streets") = // "${MAPLIBRE_URL+ style}.json?access-token=${ACCESS_TOKEN}" private fun makeStyleMapBoxUrl(dark: Boolean) = if(dark) "https://basemaps.cartocdn.com/gl/dark-matter-gl-style/style.json" else //"https://basemaps.cartocdn.com/gl/positron-gl-style/style.json" "https://basemaps.cartocdn.com/gl/voyager-gl-style/style.json" const val OPENFREEMAP_LIBERY = "https://tiles.openfreemap.org/styles/liberty" const val OPENFREEMAP_BRIGHT = "https://tiles.openfreemap.org/styles/bright" } } \ No newline at end of file diff --git a/app/src/main/java/it/reyboz/bustorino/map/MapUtils.kt b/app/src/main/java/it/reyboz/bustorino/map/MapUtils.kt index 87f9080..0bd3b47 100644 --- a/app/src/main/java/it/reyboz/bustorino/map/MapUtils.kt +++ b/app/src/main/java/it/reyboz/bustorino/map/MapUtils.kt @@ -1,27 +1,50 @@ package it.reyboz.bustorino.map import android.animation.TypeEvaluator +import android.content.Context import org.maplibre.android.geometry.LatLng +import org.maplibre.android.location.LocationComponentActivationOptions +import org.maplibre.android.location.LocationComponentOptions +import org.maplibre.android.location.engine.LocationEngineRequest +import org.maplibre.android.maps.Style class MapUtils { companion object{ @JvmStatic fun shortestRotation(from: Float, to: Float): Float { var delta = (to - from) % 360 if (delta > 180) delta -= 360 if (delta < -180) delta += 360 return from + delta } + @JvmStatic + fun buildLocationComponentActivationOptions( + style: Style, + locationComponentOptions: LocationComponentOptions, + context: Context + ): LocationComponentActivationOptions { + return LocationComponentActivationOptions + .builder(context, style) + .locationComponentOptions(locationComponentOptions) + .useDefaultLocationEngine(true) + .locationEngineRequest( + LocationEngineRequest.Builder(750) + .setFastestInterval(750) + .setPriority(LocationEngineRequest.PRIORITY_HIGH_ACCURACY) + .build() + ) + .build() + } } //TODO: Do the same for LatLng and bearing, if possible class LatLngEvaluator : TypeEvaluator { private val latLng = LatLng() override fun evaluate(fraction: Float, startValue: LatLng, endValue: LatLng): LatLng { latLng.latitude = startValue.latitude + (endValue.latitude - startValue.latitude) * fraction latLng.longitude = startValue.longitude + (endValue.longitude - startValue.longitude) * fraction return latLng } } } \ No newline at end of file diff --git a/app/src/main/java/it/reyboz/bustorino/viewmodels/LinesViewModel.kt b/app/src/main/java/it/reyboz/bustorino/viewmodels/LinesViewModel.kt index 96d16d9..5b17a87 100644 --- a/app/src/main/java/it/reyboz/bustorino/viewmodels/LinesViewModel.kt +++ b/app/src/main/java/it/reyboz/bustorino/viewmodels/LinesViewModel.kt @@ -1,111 +1,123 @@ package it.reyboz.bustorino.viewmodels import android.app.Application import android.util.Log import androidx.lifecycle.* import it.reyboz.bustorino.backend.Result import it.reyboz.bustorino.backend.Stop import it.reyboz.bustorino.data.GtfsRepository import it.reyboz.bustorino.data.NextGenDB import it.reyboz.bustorino.data.OldDataRepository import it.reyboz.bustorino.data.gtfs.GtfsDatabase import it.reyboz.bustorino.data.gtfs.GtfsRoute import it.reyboz.bustorino.data.gtfs.MatoPatternWithStops import it.reyboz.bustorino.data.gtfs.PatternStop import java.util.concurrent.Executors class LinesViewModel(application: Application) : AndroidViewModel(application) { private val gtfsRepo: GtfsRepository private val oldRepo: OldDataRepository //val patternsByRouteLiveData: LiveData> private val routeIDToSearch = MutableLiveData() private var lastShownPatternStops = ArrayList() val currentPatternStops = MutableLiveData>() val selectedPatternLiveData = MutableLiveData() val stopsForPatternLiveData = MutableLiveData>() private val executor = Executors.newFixedThreadPool(2) val mapShowing = MutableLiveData(true) fun setMapShowing(yes: Boolean){ mapShowing.value = yes //retrigger redraw stopsForPatternLiveData.postValue(stopsForPatternLiveData.value) } init { val gtfsDao = GtfsDatabase.getGtfsDatabase(application).gtfsDao() gtfsRepo = GtfsRepository(gtfsDao) oldRepo = OldDataRepository(executor, NextGenDB.getInstance(application)) } val routesGTTLiveData: LiveData> by lazy{ gtfsRepo.getLinesLiveDataForFeed("gtt") } val patternsWithStopsByRouteLiveData = routeIDToSearch.switchMap { gtfsRepo.getPatternsWithStopsForRouteID(it) } val gtfsRoute = routeIDToSearch.switchMap { gtfsRepo.getRouteFromGtfsId(it) } fun setRouteIDQuery(routeID: String){ routeIDToSearch.value = routeID } fun getRouteIDQueried(): String?{ return routeIDToSearch.value } var shouldShowMessage = true fun setPatternToDisplay(patternStops: MatoPatternWithStops){ selectedPatternLiveData.value = patternStops } /** * Find the */ private fun requestStopsForGTFSIDs(gtfsIDs: List){ if (gtfsIDs.equals(lastShownPatternStops)){ //nothing to do return } oldRepo.requestStopsWithGtfsIDs(gtfsIDs) { if (it.isSuccess) { stopsForPatternLiveData.postValue(it.result) } else { Log.e("BusTO-LinesVM", "Got error on callback with stops for gtfsID") it.exception?.printStackTrace() } } lastShownPatternStops.clear() for(id in gtfsIDs) lastShownPatternStops.add(id) } fun requestStopsForPatternWithStops(patternStops: MatoPatternWithStops){ val gtfsIDs = ArrayList() for(pat in patternStops.stopsIndices){ gtfsIDs.add(pat.stopGtfsId) } requestStopsForGTFSIDs(gtfsIDs) } + fun getStopByID(id:String) : Stop?{ + //var stop : Stop? = null + val stop = stopsForPatternLiveData.value?.let { stops -> + for (s in stops){ + if(s.ID == id) + return@let s + } + return@let null + } + return stop + } + /*fun getLinesGTT(): MutableLiveData> { val routesData = MutableLiveData>() viewModelScope.launch { val routes=gtfsRepo.getLinesForFeed("gtt") routesData.postValue(routes) } return routesData }*/ } \ No newline at end of file diff --git a/app/src/main/java/it/reyboz/bustorino/viewmodels/LivePositionsViewModel.kt b/app/src/main/java/it/reyboz/bustorino/viewmodels/LivePositionsViewModel.kt index af29f77..024f874 100644 --- a/app/src/main/java/it/reyboz/bustorino/viewmodels/LivePositionsViewModel.kt +++ b/app/src/main/java/it/reyboz/bustorino/viewmodels/LivePositionsViewModel.kt @@ -1,288 +1,349 @@ /* BusTO - ViewModel components Copyright (C) 2023 Fabio Mazza This program is free software: you can redistribute it and/or modify it under the terms of the GNU 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 General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ package it.reyboz.bustorino.viewmodels import android.app.Application import android.util.Log import androidx.lifecycle.* import androidx.work.WorkInfo import androidx.work.WorkManager import com.android.volley.DefaultRetryPolicy import com.android.volley.Response import it.reyboz.bustorino.backend.NetworkVolleyManager import it.reyboz.bustorino.backend.gtfs.GtfsRtPositionsRequest +import it.reyboz.bustorino.backend.gtfs.GtfsUtils import it.reyboz.bustorino.backend.gtfs.LivePositionUpdate import it.reyboz.bustorino.backend.mato.MQTTMatoClient import it.reyboz.bustorino.data.GtfsRepository import it.reyboz.bustorino.data.MatoPatternsDownloadWorker import it.reyboz.bustorino.data.MatoTripsDownloadWorker +import it.reyboz.bustorino.data.gtfs.MatoPattern import it.reyboz.bustorino.data.gtfs.TripAndPatternWithStops import kotlinx.coroutines.delay import kotlinx.coroutines.launch import java.util.* import kotlin.collections.ArrayList import kotlin.collections.HashMap import kotlin.collections.HashSet +typealias FullPositionUpdatesMap = HashMap> +typealias FullPositionUpdate = Pair class LivePositionsViewModel(application: Application): AndroidViewModel(application) { private val gtfsRepo = GtfsRepository(application) //private val updates = UpdatesMap() private val positionsToBeMatchedLiveData = MutableLiveData>() private val netVolleyManager = NetworkVolleyManager.getInstance(application) private var mqttClient = MQTTMatoClient() private var lineListening = "" private var lastTimeReceived: Long = 0 private val gtfsRtRequestRunning = MutableLiveData(false) private val lastFailedTripsRequest = HashMap() private val workManager = WorkManager.getInstance(application) private var lastRequestedDownloadTrips = MutableLiveData>() + //INPUT FILTER FOR LINE + private var gtfsLineToFilterPos = MutableLiveData>() + + fun setGtfsLineToFilterPos(line: String, pattern: MatoPattern?){ + gtfsLineToFilterPos.value = Pair(line, pattern) + } + var isLastWorkResultGood = workManager .getWorkInfosForUniqueWorkLiveData(MatoTripsDownloadWorker.TAG_TRIPS).map { it -> if (it.isEmpty()) return@map false var res = true if(it[0].state == WorkInfo.State.FAILED){ val currDate = Date() res = false lastRequestedDownloadTrips.value?.let { trips-> for(tr in trips){ lastFailedTripsRequest[tr] = currDate } } } return@map res } /** * Responder to the MQTT Client */ private val matoPositionListener = MQTTMatoClient.Companion.MQTTMatoListener{ val mupds = ArrayList() if(lineListening==MQTTMatoClient.LINES_ALL){ for(sdic in it.values){ for(update in sdic.values){ mupds.add(update) } } } else{ //we're listening to one if (it.containsKey(lineListening.trim()) ){ for(up in it[lineListening]?.values!!){ mupds.add(up) } } } val time = System.currentTimeMillis() if(lastTimeReceived == (0.toLong()) || (time-lastTimeReceived)>500){ positionsToBeMatchedLiveData.postValue(mupds) lastTimeReceived = time } } //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 } } // get the trip IDs in the DB private val gtfsTripsPatternsInDB = tripsIDsInUpdates.switchMap { //Log.i(DEBUG_TI, "tripsIds in updates: ${it.size}") gtfsRepo.gtfsDao.getTripPatternStops(it) } //trip IDs to query, which are not present in the DB //REMEMBER TO OBSERVE THIS IN THE MAP val tripsGtfsIDsToQuery: LiveData> = gtfsTripsPatternsInDB.map { tripswithPatterns -> 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"))} else { Log.e(DEBUG_TI,"Got results for gtfsTripsInDB but not tripsIDsInUpdates??") return@map ArrayList() } } // unify trips with updates // TODO: Unify trip data inside the LivePositionUpdate val updatesWithTripAndPatterns = gtfsTripsPatternsInDB.map { tripPatterns-> Log.i(DEBUG_TI, "Mapping trips and patterns") val mdict = HashMap>() //missing patterns val routesToDownload = HashSet() if(positionsToBeMatchedLiveData.value!=null) for(update in positionsToBeMatchedLiveData.value!!){ val trID:String = update.tripID var found = false for(trip in tripPatterns){ if (trip.pattern == null){ //pattern is null, which means we have to download // the pattern data from MaTO routesToDownload.add(trip.trip.routeID) } if (trip.trip.tripID == "gtt:$trID"){ found = true //insert directly mdict[trID] = Pair(update,trip) break } } if (!found){ //Log.d(DEBUG_TI, "Cannot find pattern ${tr}") //give the update anyway mdict[trID] = Pair(update,null) } } //have to request download of missing Patterns if (routesToDownload.size > 0){ Log.d(DEBUG_TI, "Have ${routesToDownload.size} missing patterns from the DB: $routesToDownload") //downloadMissingPatterns (ArrayList(routesToDownload)) MatoPatternsDownloadWorker.downloadPatternsForRoutes(routesToDownload.toList(), getApplication()) } return@map mdict } + //OBSERVE THIS TO GET THE LOCATION UPDATES FILTERED + val filteredLocationUpdates = MediatorLiveData() + init { + filteredLocationUpdates.addSource(updatesWithTripAndPatterns){ + filterUpdatesForGtfsLine(it, gtfsLineToFilterPos.value!!) + } + + filteredLocationUpdates.addSource(gtfsLineToFilterPos){ + filterUpdatesForGtfsLine(updatesWithTripAndPatterns.value!!, it) + } + } + + private fun filterUpdatesForGtfsLine(updates: FullPositionUpdatesMap, + linePatt: Pair): + HashMap{ + val gtfsLineId = linePatt.first + val pattern = linePatt.second + + + val filtdLineID = GtfsUtils.stripGtfsPrefix(gtfsLineId) + //filter buses with direction, show those only with the same direction + val updsForTripId = HashMap>() + val directionId = pattern?.directionId ?: -100 + val numUpds = updates.entries.size + Log.d(DEBUG_TI, "Got $numUpds updates, current pattern is: ${pattern?.name}, directionID: ${pattern?.directionId}") + // cannot understand where this is used + val patternsDirections = HashMap() + for((tripId, pair) in updates.entries){ + //remove trips with wrong line ideas + val posUp = pair.first + if(pair.first.routeID!=filtdLineID) + continue + + if(directionId!=-100 && pair.second!=null && pair.second?.pattern !=null){ + val dir = pair.second?.pattern?.directionId + if(dir !=null && dir == directionId){ + updsForTripId[tripId] = pair + } + patternsDirections[tripId] = dir ?: -10 + } else{ + updsForTripId[tripId] = pair + //Log.d(DEBUG_TAG, "No pattern for tripID: $tripId") + patternsDirections[tripId] = -10 + } + } + Log.d(DEBUG_TI, " Filtered updates are ${updsForTripId.keys.size}") // Original updates directs: $patternsDirections\n + + return updsForTripId + } + fun requestMatoPosUpdates(line: String){ lineListening = line viewModelScope.launch { mqttClient.startAndSubscribe(line,matoPositionListener, getApplication()) } //updatePositions(1000) } fun stopMatoUpdates(){ viewModelScope.launch { val tt = System.currentTimeMillis() mqttClient.stopMatoRequests(matoPositionListener) val time = System.currentTimeMillis() -tt Log.d(DEBUG_TI, "Took $time ms to unsubscribe") } } fun retriggerPositionUpdate(){ if(positionsToBeMatchedLiveData.value!=null){ positionsToBeMatchedLiveData.postValue(positionsToBeMatchedLiveData.value) } } //Gtfs Real time private val gtfsPositionsReqListener = object: GtfsRtPositionsRequest.Companion.RequestListener{ override fun onResponse(response: ArrayList?) { Log.i(DEBUG_TI,"Got response from the GTFS RT server") response?.let {it:ArrayList -> if (it.size == 0) { Log.w(DEBUG_TI,"No position updates from the GTFS RT server") return } else { //Log.i(DEBUG_TI, "Posting value to positionsLiveData") viewModelScope.launch { positionsToBeMatchedLiveData.postValue(it) } } } gtfsRtRequestRunning.postValue(false) } } private val positionRequestErrorListener = Response.ErrorListener { Log.e(DEBUG_TI, "Could not download the update", it) gtfsRtRequestRunning.postValue(false) } fun requestGTFSUpdates(){ if(gtfsRtRequestRunning.value == null || !gtfsRtRequestRunning.value!!) { val request = GtfsRtPositionsRequest(positionRequestErrorListener, gtfsPositionsReqListener) request.setRetryPolicy( DefaultRetryPolicy(1000,10,DefaultRetryPolicy.DEFAULT_BACKOFF_MULT) ) netVolleyManager.requestQueue.add(request) Log.i(DEBUG_TI, "Requested GTFS realtime position updates") gtfsRtRequestRunning.value = true } } fun requestDelayedGTFSUpdates(timems: Long){ viewModelScope.launch { delay(timems) requestGTFSUpdates() } } override fun onCleared() { //stop the MQTT Service Log.d(DEBUG_TI, "Clearing the live positions view model, stopping the mqttClient") mqttClient.disconnect() super.onCleared() } //Request trips download fun downloadTripsFromMato(trips: List): Boolean{ if(trips.isEmpty()) return false var shouldContinue = false val currentDateTime = Date().time for (tr in trips){ if (!lastFailedTripsRequest.containsKey(tr)){ shouldContinue = true break } else{ //Log.i(DEBUG_TI, "Last time the trip has failed is ${lastFailedTripsRequest[tr]}") if ((lastFailedTripsRequest[tr]!!.time - currentDateTime) > MAX_TIME_RETRY){ shouldContinue =true break } } } if (shouldContinue) { //if one trip val workRequ =MatoTripsDownloadWorker.requestMatoTripsDownload(trips, getApplication(), "BusTO-MatoTripsDown") workRequ?.let { req -> Log.d(DEBUG_TI, "Enqueueing new work, saving work info") lastRequestedDownloadTrips.postValue(trips) //isLastWorkResultGood = } } else{ Log.w(DEBUG_TI, "Requested to fetch data for ${trips.size} trips but they all have failed before in the last $MAX_MINUTES_RETRY mins") } return shouldContinue } companion object{ private const val DEBUG_TI = "BusTO-LivePosViewModel" private const val MAX_MINUTES_RETRY = 3 private const val MAX_TIME_RETRY = MAX_MINUTES_RETRY*60*1000 //3 minutes (in milliseconds) } } \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_lines_detail.xml b/app/src/main/res/layout/fragment_lines_detail.xml index 27ad761..0cc9ef2 100644 --- a/app/src/main/res/layout/fragment_lines_detail.xml +++ b/app/src/main/res/layout/fragment_lines_detail.xml @@ -1,155 +1,278 @@ - - + + + + + + + - \ No newline at end of file + + + + + + + + + + + + + + + + + \ No newline at end of file