Page Menu
Home
GitPull.it
Search
Configure Global Search
Log In
Files
F15187282
LinesDetailFragment.kt
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Authored By
Unknown
Size
47 KB
Referenced Files
None
Subscribers
None
LinesDetailFragment.kt
View Options
/*
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 <http://www.gnu.org/licenses/>.
*/
package it.reyboz.bustorino.fragments
import android.animation.ObjectAnimator
import android.annotation.SuppressLint
import android.content.Context
import android.content.SharedPreferences
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.widget.*
import androidx.appcompat.content.res.AppCompatResources
import androidx.core.content.ContextCompat
import androidx.core.content.res.ResourcesCompat
import androidx.fragment.app.activityViewModels
import androidx.fragment.app.viewModels
import androidx.preference.PreferenceManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
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.Stop
import it.reyboz.bustorino.backend.gtfs.GtfsUtils
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.MatoPatternWithStops
import it.reyboz.bustorino.map.*
import it.reyboz.bustorino.util.Permissions
import it.reyboz.bustorino.viewmodels.LinesViewModel
import it.reyboz.bustorino.viewmodels.MapStateViewModel
import it.reyboz.bustorino.viewmodels.ServiceAlertsViewModel
import kotlinx.coroutines.Runnable
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.maps.MapLibreMap
import org.maplibre.android.maps.Style
import org.maplibre.android.style.expressions.Expression
import org.maplibre.android.style.layers.LineLayer
import org.maplibre.android.style.layers.Property
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() : GeneralMapLibreFragment() {
private var lineID = "" // the GTFS line ID (e.g. "gtt:10U")
private lateinit var patternsSpinner: Spinner
private var patternsAdapter: ArrayAdapter<String>? = null
//private var isBottomSheetShowing = false
private var shouldMapLocationBeReactivated = true
private var toRunWhenMapReady : Runnable? = null
//private var mapInitialized = AtomicBoolean(false)
//private var patternsSpinnerState: Parcelable? = null
private lateinit var currentPatterns: List<MatoPatternWithStops>
//private lateinit var map: MapView
private var patternShown: MatoPatternWithStops? = null
private val viewModel: LinesViewModel by viewModels()
private val alertsViewModel: ServiceAlertsViewModel by activityViewModels()
//private var firstInit = true
private var pausedFragment = false
private lateinit var switchButton: ImageButton
private lateinit var lineInfoButton: 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
private var stopIDFromToShow = ""
private var patternIdToShow = ""
//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)
}
//map data
//style and sources are in GeneralMapLibreFragment
private lateinit var polylineSource: GeoJsonSource
private lateinit var polyArrowSource: GeoJsonSource
private var savedCameraPosition: CameraPosition? = null
private var lastStopsSizeShown = 0
//BUS POSITIONS
private var enablingPositionFromClick = false
private var polyline: LineString? = null
//private var stopPosList = ArrayList<GeoPoint>()
//fragment actions
private var showOnTopOfLine = false
private var recyclerInitDone = false
private var usingMQTTPositions = true
private var restoredCameraInMap = false
//position of live markers
private val tripMarkersAnimators = HashMap<String, ObjectAnimator>()
//extra items to use the LibreMap
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val args = requireArguments()
lineID = args.getString(LINEID_KEY,"")
stopIDFromToShow = args.getString(STOPID_FROM_KEY, "") //can be null
patternIdToShow = args.getString(PATTERN_SHOW_KEY, "")
}
@SuppressLint("SetTextI18n")
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
//reset statuses
//isBottomSheetShowing = false
//stopsLayerStarted = false
lastStopsSizeShown = 0
mapInitialized = false
val rootView = inflater.inflate(R.layout.fragment_lines_detail, container, false)
//lineID = requireArguments().getString(LINEID_KEY, "")
arguments?.let {
lineID = it.getString(LINEID_KEY, "")
stopIDFromToShow = it.getString(STOPID_FROM_KEY, "") //can be null
patternIdToShow = it.getString(PATTERN_SHOW_KEY, "")
Log.d(DEBUG_TAG, "LineID selected: $lineID, stopIDFromToShow: $stopIDFromToShow, patternIdToShow: $patternIdToShow")
}
switchButton = rootView.findViewById(R.id.switchImageButton)
locationIcon = rootView.findViewById(R.id.locationEnableIcon)
busPositionsIconButton = rootView.findViewById(R.id.busPositionsImageButton)
lineInfoButton = rootView.findViewById(R.id.lineInfoWarningButton)
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)
// Setup close button
rootView.findViewById<View>(R.id.btnClose).setOnClickListener {
hideStopOrBusBottomSheet()
}
val titleTextView = rootView.findViewById<TextView>(R.id.titleTextView)
titleTextView.text = getString(R.string.line)+" "+ GtfsUtils.lineNameDisplayFromGtfsID(lineID)
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<String>())
patternsSpinner.adapter = patternsAdapter
initializeRecyclerView()
switchButton.setOnClickListener{
if(mapView?.visibility == View.VISIBLE){
hideMapAndShowStopList()
} else{
hideStopListAndShowMap()
}
}
locationIcon?.let {view ->
//set click Listener
view.setOnClickListener(this::switchUserLocationStatus)
}
busPositionsIconButton.setOnClickListener {
LivePositionsDialogFragment().show(parentFragmentManager, "LivePositionsDialog")
}
//set
lineInfoButton.setOnClickListener {
AlertsDialogFragment.newInstanceForLine(lineID).show(parentFragmentManager, "Alerts-Line$lineID")
}
/*
*/
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 currentShownPattern = patternShown?.pattern
val patternWithStops = currentPatterns[position]
Log.d(DEBUG_TAG, "request stops for pattern ${patternWithStops.pattern.code}")
setPatternAndReqStops(patternWithStops)
if(mapView?.visibility == View.VISIBLE) {
//Clear buses if we are changing direction
currentShownPattern?.let { patt ->
if(patt.directionId != patternWithStops.pattern.directionId){
stopAnimations()
updatesByVehDict.clear()
updatePositionsIcons(true)
livePositionsViewModel.retriggerPositionUpdate()
}
if (shownStopInBottomSheet!=null){
//check if the stop is inside the new pattern
/*val s = shownStopInBottomSheet!!
val newPatternStops = patternWithStops.stopsIndices
val filterPStops = newPatternStops.filter { ps -> ps.stopGtfsId == "gtt:${s.ID}" }
if (filterPStops.isEmpty()){
hideStopOrBusBottomSheet()
}
*/
// do another thing, just close the stop when the pattern is changed
if (patt.code != patternWithStops.pattern.code){
hideStopOrBusBottomSheet()
}
}
}
}
livePositionsViewModel.setGtfsLineToFilterPos(lineID, patternWithStops.pattern)
}
override fun onNothingSelected(p0: AdapterView<*>?) {
}
}
Log.d(DEBUG_TAG, "Views created!")
observeStatusLivePositions()
return rootView
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
//reflect UI
//INITIALIZE VIEW MODELS
viewModel.setRouteIDQuery(lineID)
livePositionsViewModel.setGtfsLineToFilterPos(lineID, null)
//observe the change, clear buses when switching position
livePositionsViewModel.useMQTTPositionsLiveData.observe(viewLifecycleOwner){ useMQTT->
//Log.d(DEBUG_TAG, "Changed MQTT positions, now have to use MQTT: $useMQTT")
if (isResumed) {
//Log.d(DEBUG_TAG, "Deciding to switch, the current source is using MQTT: $usingMQTTPositions")
if(useMQTT!=usingMQTTPositions){
// we have to switch
val clearPos = PreferenceManager.getDefaultSharedPreferences(requireContext()).getBoolean("positions_clear_on_switch_pref", true)
livePositionsViewModel.clearOldPositionsUpdates()
if(useMQTT){
//switching to MQTT, the GTFS positions are disabled automatically
livePositionsViewModel.requestMatoPosUpdates(GtfsUtils.getLineNameFromGtfsID(lineID))
} else{
//switching to GTFS RT: stop Mato, launch first request
livePositionsViewModel.stopMatoUpdates()
livePositionsViewModel.requestGTFSUpdates()
}
Log.d(DEBUG_TAG, "Should clear positions: $clearPos")
if (clearPos) {
livePositionsViewModel.clearAllPositions()
//force clear of the viewed data
if(!vehShowing.isNullOrEmpty()) hideStopOrBusBottomSheet()
clearAllBusPositionsInMap()
}
}
}
usingMQTTPositions = useMQTT
}
val keySourcePositions = getString(R.string.pref_positions_source)
usingMQTTPositions = PreferenceManager.getDefaultSharedPreferences(requireContext())
.getString(keySourcePositions, "mqtt").contentEquals("mqtt")
viewModel.patternsWithStopsByRouteLiveData.observe(viewLifecycleOwner, this::savePatternsToShow)
/*
*/
viewModel.stopsForPatternLiveData.observe(viewLifecycleOwner) { stops ->
val pattern = viewModel.selectedPatternLiveData.value
if (pattern == null) {
Log.w(DEBUG_TAG, "The selectedPattern is null!")
return@observe
}
if(mapView?.visibility ==View.VISIBLE) {
// We have the pattern and the stops here, time to display them
//TODO: Decide if we should follow the camera view given by the previous screen (probably the map fragment)
// use !restoredCameraInMap to do so
// val shouldZoom = (shownStopInBottomSheet == null) //use this if we want to avoid zoom when we're keeping the stop open
displayPatternWithStopsOnMap(pattern, stops, true)
} else {
if(stopsRecyclerView.visibility==View.VISIBLE) {
patternShown = pattern
showStopsInRecyclerView(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
}
mapStateViewModel.locationUserActive.observe(viewLifecycleOwner) {
setLocationIconEnabled(it)
}
// enable info button if there are alerts on the line
alertsViewModel.setGtfsLineFilter(lineID)
alertsViewModel.alertsByRouteLiveData.observe(viewLifecycleOwner){ list ->
Log.d(DEBUG_TAG, "alerts for line $lineID: ${list.size}")
if(list.isNotEmpty()){
lineInfoButton.visibility = View.VISIBLE
//Log.d(DEBUG_TAG, "First alert is:\n ${list[0].longPrint()}")
} else
lineInfoButton.visibility = View.GONE
}
lineInfoButton.setOnClickListener {
AlertsDialogFragment.newInstanceForLine(lineID).show(parentFragmentManager, "Alerts-Line$lineID")
}
}
// ------------- UI switch stuff ---------
private fun hideMapAndShowStopList(){
mapView?.visibility = View.GONE
stopsRecyclerView.visibility = View.VISIBLE
locationIcon?.visibility = View.GONE
busPositionsIconButton?.visibility = View.GONE
viewModel.setMapShowing(false)
if(usingMQTTPositions) livePositionsViewModel.stopMatoUpdates()
//map.overlayManager.remove(busPositionsOverlay)
switchButton.setImageDrawable(AppCompatResources.getDrawable(requireContext(), R.drawable.ic_map_white_30))
hideStopOrBusBottomSheet()
if(locationComponent.isLocationComponentEnabled){
setLocationComponentEnabled(false)
shouldMapLocationBeReactivated = true
} else
shouldMapLocationBeReactivated = false
}
private fun hideStopListAndShowMap(){
stopsRecyclerView.visibility = View.GONE
mapView?.visibility = View.VISIBLE
locationIcon?.visibility = View.VISIBLE
busPositionsIconButton.visibility = View.VISIBLE
viewModel.setMapShowing(true)
//map.overlayManager.add(busPositionsOverlay)
//map.
if(usingMQTTPositions)
livePositionsViewModel.requestMatoPosUpdates(GtfsUtils.getLineNameFromGtfsID(lineID))
else
livePositionsViewModel.requestGTFSUpdates()
switchButton.setImageDrawable(AppCompatResources.getDrawable(requireContext(), R.drawable.ic_list_30))
if(shouldMapLocationBeReactivated){
setLocationComponentEnabled(Permissions.bothLocationPermissionsGranted(requireContext()))
}
}
override fun setLocationIconEnabled(enabled: Boolean){
if(enabled) {
locationIcon?.setImageDrawable(ContextCompat.getDrawable(requireContext(), R.drawable.location_circlew_red))
}
else {
locationIcon?.setImageDrawable(ContextCompat.getDrawable(
requireContext(),
R.drawable.location_circlew_grey
)
)
}
}
override fun onMapLocationEnabled(active: Boolean) {
//extra thing: show the toast
showToastLocation(active)
}
override fun onMapLocationComponentInitialized() {
//enable the position after the first fix
//onMapLocationEnabled(true)
}
@SuppressLint("MissingPermission")
override fun onFirstReceivedLocation(location: Location) {
if(mapInitialized){
val center = map!!.cameraPosition.target
val newPos = LatLng(location.latitude, location.longitude)
Log.d(DEBUG_TAG, "Center of the map : $center")
val newStatus = if(center==null || newPos.distanceTo(center) > 20*1000){
Log.d(DEBUG_TAG, "Distance from center of map to location: "+center?.distanceTo(newPos))
if(!shownToastNoPosition) context?.let{ c->
Toast.makeText(c, R.string.too_far_not_showing_location, Toast.LENGTH_LONG).show()
shownToastNoPosition = true
}
false
} else{
true
}
if(!newStatus) setLocationComponentEnabled(newStatus)
mapStateViewModel.locationUserActive.value = newStatus
}
}
// ------------- Map Code -------------------------
/**
* This method sets up the map and the layers
*/
override fun onMapReady(mapReady: MapLibreMap) {
this.map = mapReady
var setViewAlready = false
val context = requireContext()
val mjson = MapLibreStyles.getJsonStyleFromAsset(context, PreferencesHolder.getMapLibreStyleFile(context)) //ViewUtils.loadJsonFromAsset(requireContext(),"map_style_good.json")
activity?.run {
val builder = Style.Builder().fromJson(mjson!!)
mapReady.setStyle(builder) { style ->
addImagesStyle(style)
mapStyle = style
//setupLayers(style)
//checkInitMapLocation(mapReady, style,requireContext())
//if(!stopsLayerStarted)
initPolylineStopsLayers(style, null)
setupBusLayer(style)
initSymbolManager(mapReady, style)
toRunWhenMapReady?.run()
toRunWhenMapReady = null
mapInitialized = true
if(patternShown!=null){
viewModel.stopsForPatternLiveData.value?.let {
Log.d(DEBUG_TAG, "Show stops from the cache")
displayPatternWithStopsOnMap(patternShown!!, it, true)
//Show stop from cache
mapStateViewModel.lastOpenStopID.value?.let{ sID->
val s= it.filter { stop -> stop.ID==sID }
if (s.isEmpty()) {
if(sID.isNotEmpty())
Log.w(DEBUG_TAG,"Wanted to open stop $sID in map but it was not loaded!")
}
else openStopInBottomSheet(s[0])
}
}
}
var restoredMapState = mapStateViewModel.restoreMapState(mapReady)
arguments?.let { args ->
// if there is a Camera State in the arguments, set it for the new camera (doesn't work yet!)
if (!restoredMapState && MapCameraState.checkInBundle(args)) {
val initCamState = MapCameraState.fromBundle(args)
//map?.let{
MapStateViewModel.restoreMapState(mapReady, initCamState)
setViewAlready = true
restoredMapState = true
}
}
restoredCameraInMap = restoredMapState
}
mapReady.addOnMapClickListener { point ->
val screenPoint = mapReady.projection.toScreenLocation(point)
val stopsNearby = mapReady.queryRenderedFeatures(screenPoint, STOPS_LAYER_ID)
val busNearby = mapReady.queryRenderedFeatures(screenPoint, BUSES_LAYER_ID)
//Log.d(DEBUG_TAG, "onMapClick, stopsNearby: $stopsNearby \nstopShown: $shownStopInBottomSheet \nbusNearby: $busNearby,")
if (stopsNearby.isNotEmpty()) {
val feature = stopsNearby[0]
val id = feature.getStringProperty("id")
val stop = viewModel.getStopByID(id)
stop?.let {
if (isBottomSheetShowing()) {
hideStopOrBusBottomSheet()
}
openStopInBottomSheet(it)
//move camera
if(it.latitude!=null && it.longitude!=null)
mapReady.animateCamera(CameraUpdateFactory.newLatLng(LatLng(it.latitude!!,it.longitude!!)),750)
}
return@addOnMapClickListener true
} else if (busNearby.isNotEmpty()){
val feature = busNearby[0]
openBusFromMapClick(feature)
return@addOnMapClickListener true
}
false
}
// we start requesting the bus positions now
observeBusPositionUpdates()
}
val zoom = 12.0
val latlngTarget = LatLng(MapLibreFragment.DEFAULT_CENTER_LAT, MapLibreFragment.DEFAULT_CENTER_LON)
if(!setViewAlready)
mapReady.cameraPosition = savedCameraPosition ?:CameraPosition.Builder().target(latlngTarget).zoom(zoom).build()
savedCameraPosition = null
if(shouldMapLocationBeReactivated)
mapReady.style?.let{ checkInitMapLocation(mapReady,it, context)}
}
override fun showOpenStopWithSymbolLayer(): Boolean {
return true
}
/**
* Separate function to find the vehicle associated with a feature and display it
*/
private fun openBusFromMapClick(feature: Feature){
val vehid = feature.getStringProperty("veh")
if(isBottomSheetShowing())
hideStopOrBusBottomSheet()
showVehicleTripInBottomSheet(vehid)
updatesByVehDict[vehid]?.let {
map?.animateCamera(
CameraUpdateFactory.newLatLng(LatLng(it.posUpdate.latitude, it.posUpdate.longitude)),
750
)
}
}
private fun observeBusPositionUpdates(){
//live bus positions
livePositionsViewModel.filteredLocationUpdates.observe(viewLifecycleOwner){ pair ->
//Log.d(DEBUG_TAG, "Received ${updates.size} updates for the positions")
val updates = pair.first
val vehiclesNotOnCorrectDir = pair.second
if(mapView?.visibility == View.GONE || patternShown ==null){
//DO NOTHING
Log.w(DEBUG_TAG, "not doing anything because map is not visible")
return@observe
}
//remove vehicles not on this direction
removeVehiclesData(vehiclesNotOnCorrectDir)
updateBusPositionsInMap(updates, hasVehicleTracking = true) { veh->
showVehicleTripInBottomSheet(veh)
}
//if not using MQTT positions
if(!usingMQTTPositions){
livePositionsViewModel.requestDelayedGTFSUpdates(2000)
}
}
//download missing tripIDs
livePositionsViewModel.tripsGtfsIDsToQuery.observe(viewLifecycleOwner){
//gtfsPosViewModel.downloadTripsFromMato(dat);
MatoTripsDownloadWorker.requestMatoTripsDownload(
it, requireContext().applicationContext,
"BusTO-MatoTripDownload"
)
}
}
private fun showVehicleTripInBottomSheet(veh: String) {
super.showVehicleTripInBottomSheet(veh) { patternCode, veh ->
//this is checked in @GeneralMapLibreFragment
//val data = updatesByVehDict[veh] ?: return@showVehicleTripInBottomSheet
if (patternCode.isEmpty()) return@showVehicleTripInBottomSheet
if (patternShown?.pattern?.code == patternCode) {
//center view on vehicle
updatesByVehDict[veh]?.let { up->
map?.let{
/*
val c = it.cameraPosition
it.moveCamera(CameraUpdateFactory.CameraPositionUpdate(c.bearing,
LatLng(up.posUpdate.latitude, up.posUpdate.longitude),
c.tilt,c.zoom, c.padding)
)
*/
it.animateCamera(CameraUpdateFactory.newLatLng(LatLng(up.posUpdate.latitude, up.posUpdate.longitude)))
}
} ?: {
Toast.makeText(context, R.string.showing_same_direction, Toast.LENGTH_SHORT).show()
}
} else {
showPatternWithCode(patternCode)
}
}
}
// ------- MAP LAYERS INITIALIZE ----
/**
* Initialize the map layers for the stops
*/
private fun initPolylineStopsLayers(style: Style, arrowFeatures: FeatureCollection?){
Log.d(DEBUG_TAG, "INIT STOPS CALLED")
stopsSource = GeoJsonSource(STOPS_SOURCE_ID)
//val context = requireContext()
val stopIcon = ResourcesCompat.getDrawable(resources,R.drawable.ball, activity?.theme)!!
val imgStop = ResourcesCompat.getDrawable(resources,R.drawable.bus_stop_new, activity?.theme)!!
val polyIconArrow = ResourcesCompat.getDrawable(resources, R.drawable.arrow_up_box_fill, activity?.theme)!!
//set the image tint
//DrawableCompat.setTint(imgBus,ContextCompat.getColor(context,R.color.line_drawn_poly))
// add icons
style.addImage(STOP_IMAGE_ID,stopIcon)
style.addImage(POLY_ARROW, polyIconArrow)
style.addImage(STOP_ACTIVE_IMG, ResourcesCompat.getDrawable(resources, R.drawable.bus_stop_new_highlight, activity?.theme)!!)
polylineSource = GeoJsonSource(POLYLINE_SOURCE) //lineFeature?.let { GeoJsonSource(POLYLINE_SOURCE, it) } ?: GeoJsonSource(POLYLINE_SOURCE)
style.addSource(polylineSource)
val color=ContextCompat.getColor(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(color),
PropertyFactory.lineWidth(5.0f), //originally 13f
PropertyFactory.lineOpacity(1.0f),
PropertyFactory.lineJoin(Property.LINE_JOIN_ROUND),
PropertyFactory.lineCap(Property.LINE_CAP_ROUND)
)
polyArrowSource = GeoJsonSource(POLY_ARROWS_SOURCE, arrowFeatures)
style.addSource(polyArrowSource)
val arrowsLayer = SymbolLayer(POLY_ARROWS_LAYER, POLY_ARROWS_SOURCE).withProperties(
PropertyFactory.iconImage(POLY_ARROW),
PropertyFactory.iconRotate(Expression.get("bearing")),
PropertyFactory.iconRotationAlignment(ICON_ROTATION_ALIGNMENT_MAP)
)
val layers = style.layers
val lastLayers = layers.filter { l-> l.id.contains("city") }
//Log.d(DEBUG_TAG,"Layers:\n ${style.layers.map { l -> l.id }}")
Log.d(DEBUG_TAG, "City layers: ${lastLayers.map { l-> l.id }}")
if(lastLayers.isNotEmpty())
style.addLayerAbove(lineLayer,lastLayers[0].id)
else
style.addLayerBelow(lineLayer,"label_country_1")
//style.addLayerAbove(stopsLayer, POLYLINE_LAYER)
style.addLayerAbove(arrowsLayer, POLYLINE_LAYER)
stopsLayerStarted = true
initStopsLayer(style, null, POLY_ARROWS_LAYER)
}
private fun filterPatternFromArgs(patterns: List<MatoPatternWithStops>): MatoPatternWithStops?{
var p: MatoPatternWithStops? = null
if (patternIdToShow.isNotEmpty()){
for (patt in patterns) {
if (patt.pattern.code == patternIdToShow){
p = patt
}
}
if(p==null)
Log.w(DEBUG_TAG, "We had to show the pattern with code $patternIdToShow, but we didn't find it")
else
Log.d(DEBUG_TAG, "Requesting to show pattern with code $patternIdToShow, found pattern ${p.pattern.code}")
}
// if we are loading from a stop, find it
else if(stopIDFromToShow.isNotEmpty()) {
val stopGtfsID = "gtt:$stopIDFromToShow"
var pLength = 0
for (patt in patterns) {
for (pstop in patt.stopsIndices) {
if (pstop.stopGtfsId == stopGtfsID) {
//found
if (patt.stopsIndices.size > pLength) {
p = patt
pLength = patt.stopsIndices.size
}
//break here, we have determined this pattern has the stop we're looking for
break
}
}
}
if(p==null)
Log.w(DEBUG_TAG, "We had to show the pattern from stop $stopIDFromToShow, but we didn't find it")
else
Log.d(DEBUG_TAG, "Requesting to show pattern from stop $stopIDFromToShow, found pattern ${p.pattern.code}")
}
// the flag of showing pattern is not necessary anymore, we have set the pattern
patternIdToShow = ""
// the flag of selecting from stop needs to be used again when displaying the pattern
return p
}
/**
* Save the loaded pattern data, without the stops!
*/
private fun savePatternsToShow(patterns: List<MatoPatternWithStops>){
currentPatterns = patterns.sortedWith(patternsSorter)
patternsAdapter?.let {
it.clear()
it.addAll(currentPatterns.map { p->"${p.pattern.directionId} - ${p.pattern.headsign}" })
it.notifyDataSetChanged()
}
val patternToShow = filterPatternFromArgs(currentPatterns)
if(patternToShow!=null) {
//showPattern(patternToShow)
patternShown = patternToShow
}
patternShown?.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 }
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, "Requesting stops fro pattern $code in position: $pos")
// this triggers the showing on the map / recyclerview
if (pos !=-2)
patternsSpinner.setSelection(pos)
else
Log.e(DEBUG_TAG, "Pattern with code $code not found!!")
}
/**
* Zoom on the map to get the pattern
*/
private fun zoomToCurrentPattern(){
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
}
val padding = 50 // 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))
}
}
private fun displayPatternWithStopsOnMap(patternWs: MatoPatternWithStops, stopsToSort: List<Stop>, zoomToPattern: Boolean){
if(!mapInitialized){
//set the runnable and do nothing else
Log.d(DEBUG_TAG, "Delaying pattern display to when map is Ready: ${patternWs.pattern.code}")
toRunWhenMapReady = Runnable {
displayPatternWithStopsOnMap(patternWs, stopsToSort, zoomToPattern)
}
return
}
Log.d(DEBUG_TAG, "Got the stops: ${stopsToSort.map { s->s.gtfsID }}}")
patternShown = patternWs
//Problem: stops are not sorted
val stopOrderD = patternWs.stopsIndices.withIndex().associate{it.value.stopGtfsId to it.index}
val stopsSorted = stopsToSort.sortedBy { s-> stopOrderD[s.gtfsID] }
val pattern = patternWs.pattern
val pointsList = PolylineParser.decodePolyline(pattern.patternGeometryPoly, pattern.patternGeometryLength)
val pointsToShow = pointsList.map { Point.fromLngLat(it.longitude, it.latitude) }
Log.d(DEBUG_TAG, "The polyline has ${pointsToShow.size} points to display")
polyline = LineString.fromLngLats(pointsToShow)
val lineFeature = Feature.fromGeometry(polyline)
//Log.d(DEBUG_TAG, "Polyline in JSON is: ${lineFeature.toJson()}")
// --- STOPS---
val features = ArrayList<Feature>()
for (s in stopsSorted){
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
}
)
)
}
}
// -- ARROWS --
//val splitPolyline = MapLibreUtils.splitPolyWhenDistanceTooBig(pointsList, 200.0)
val arrowFeatures = ArrayList<Feature>()
val pointsIndexToShowIcon = MapLibreUtils.findPointsToPutDirectionMarkers(pointsList, stopsSorted, 750.0)
for (idx in pointsIndexToShowIcon){
val pnow = pointsList[idx]
val otherp = if(idx>1) pointsList[idx-1] else pointsList[idx+1]
val bearing = if (idx>1) MapLibreUtils.getBearing(pointsList[idx-1], pnow) else MapLibreUtils.getBearing(pnow, pointsList[idx+1])
arrowFeatures.add(Feature.fromGeometry(
Point.fromLngLat((pnow.longitude+otherp.longitude)/2, (pnow.latitude+otherp.latitude)/2 ), //average
JsonObject().apply {
addProperty("bearing", bearing)
}
))
}
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(lineFeature)
polyArrowSource.setGeoJson(FeatureCollection.fromFeatures(arrowFeatures))
lastStopsSizeShown = features.size
} else
map?.let {
Log.d(DEBUG_TAG, "Map stop layer is not started yet, init layer")
initPolylineStopsLayers(mapStyle, FeatureCollection.fromFeatures(arrowFeatures))
Log.d(DEBUG_TAG,"Started stops layer on map")
lastStopsSizeShown = features.size
stopsLayerStarted = true
} ?:{
Log.e(DEBUG_TAG, "Stops layer is not started!!")
}
var reallyZoomToPattern = zoomToPattern
if(stopIDFromToShow.isNotEmpty()){
//open the stop
val stopfilt = stopsSorted.filter { s -> s.ID == stopIDFromToShow }
if (stopfilt.isEmpty()){
Log.e(DEBUG_TAG, "Tried to show stop but it's not in the selected pattern")
} else{
val stop = stopfilt[0]
openStopInBottomSheet(stop)
if(stop.hasCoords()) {
reallyZoomToPattern = false
setCameraPosition(stop.latitude!!, stop.longitude!!, 13.5)
}
}
// Reset this to avoid checking again when showing
stopIDFromToShow = ""
//camera set
}
if(reallyZoomToPattern) zoomToCurrentPattern()
}
private fun initializeRecyclerView(){
val llManager = LinearLayoutManager(context)
llManager.orientation = LinearLayoutManager.VERTICAL
stopsRecyclerView.layoutManager = llManager
}
private fun showStopsInRecyclerView(stops: List<Stop>){
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
)
}
}
/**
* This method fixes the display of the pattern, to be used when clicking on a bus
*/
private fun showPatternWithCode(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
}
}
}
override fun onResume() {
super.onResume()
Log.d(DEBUG_TAG, "Resetting paused from onResume")
pausedFragment = false
val keySourcePositions = getString(R.string.pref_positions_source)
usingMQTTPositions = PreferenceManager.getDefaultSharedPreferences(requireContext())
.getString(keySourcePositions, "mqtt").contentEquals("mqtt")
//separate paths
if(usingMQTTPositions)
livePositionsViewModel.requestMatoPosUpdates(GtfsUtils.getLineNameFromGtfsID(lineID))
else
livePositionsViewModel.requestGTFSUpdates()
//initialize GUI here
fragmentListener?.readyGUIfor(FragmentKind.LINES)
}
override fun onPause() {
super.onPause()
if(usingMQTTPositions) livePositionsViewModel.stopMatoUpdates()
pausedFragment = true
//save map
map?.let{
//if map is initialized
mapStateViewModel.saveMapState(it)
}
mapStateViewModel.lastOpenStopID.postValue(shownStopInBottomSheet?.ID)
}
override fun onStop() {
super.onStop()
if(locationInitialized)
shouldMapLocationBeReactivated = locationComponent.isLocationComponentEnabled
else
shouldMapLocationBeReactivated = false
}
override fun onDestroyView() {
map?.run {
Log.d(DEBUG_TAG, "Saving camera position")
savedCameraPosition = cameraPosition
}
super.onDestroyView()
Log.d(DEBUG_TAG, "Destroying the views")
/*mapStyle.removeLayer(STOPS_LAYER_ID)
mapStyle?.removeSource(STOPS_SOURCE_ID)
mapStyle.removeLayer(POLYLINE_LAYER)
mapStyle.removeSource(POLYLINE_SOURCE)
*/
//stopsLayerStarted = false
}
override fun onMapDestroy() {
mapStyle.removeLayer(STOPS_LAYER_ID)
mapStyle.removeSource(STOPS_SOURCE_ID)
mapStyle.removeLayer(POLYLINE_LAYER)
mapStyle.removeSource(POLYLINE_SOURCE)
mapStyle.removeLayer(BUSES_LAYER_ID)
mapStyle.removeSource(BUSES_SOURCE_ID)
//map?.locationComponent?.isLocationComponentEnabled = false
setLocationComponentEnabled(false)
}
override fun getBaseViewForSnackBar(): View? {
return null
}
companion object {
private const val LINEID_KEY="lineID"
private const val STOPID_FROM_KEY="stopID"
private const val PATTERN_SHOW_KEY ="patternIDShow"
private const val DEBUG_TAG="BusTO-LineDetalFragment"
fun makeArgs(lineID: String, stopIDFrom: String?): Bundle{
val b = Bundle()
b.putString(LINEID_KEY, lineID)
b.putString(STOPID_FROM_KEY, stopIDFrom)
return b
}
fun makeArgsPattern(lineID: String, patternShow: String?, extraArgs: Bundle?): Bundle {
val b= extraArgs ?: Bundle()
b.putString(LINEID_KEY, lineID)
b.putString(PATTERN_SHOW_KEY, patternShow)
return b
}
fun newInstance(lineID: String?, stopIDFrom: String?) = LinesDetailFragment().apply {
lineID?.let { arguments = makeArgs(it, stopIDFrom) }
}
@JvmStatic
private fun findOptimalPosition(stop: Stop, pointsList: MutableList<LatLng>): 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 LatLng(sLat, p1.longitude)
} else if (p1.latitude == p2.latitude){
//Log.d(DEBUG_TAG, "Same latitude")
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 LatLng(latNew,longNew)
}
private const val DEFAULT_CENTER_LAT = 45.12
private const val DEFAULT_CENTER_LON = 7.6858
}
}
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Thu, Nov 19, 09:47 (3 m, 55 s)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2265689
Default Alt Text
LinesDetailFragment.kt (47 KB)
Attached To
Mode
R4 Libre BusTO
Attached
Detach File
Event Timeline