Differential D236 Diff 781 app/src/main/java/it/reyboz/bustorino/fragments/GeneralMapLibreFragment.kt
Changeset View
Changeset View
Standalone View
Standalone View
app/src/main/java/it/reyboz/bustorino/fragments/GeneralMapLibreFragment.kt
| Show All 35 Lines | |||||
| import android.widget.ImageButton | import android.widget.ImageButton | ||||
| import android.widget.ImageView | import android.widget.ImageView | ||||
| import android.widget.RelativeLayout | import android.widget.RelativeLayout | ||||
| import android.widget.TextView | import android.widget.TextView | ||||
| import android.widget.Toast | import android.widget.Toast | ||||
| import androidx.activity.result.ActivityResultCallback | import androidx.activity.result.ActivityResultCallback | ||||
| import androidx.activity.result.contract.ActivityResultContracts | import androidx.activity.result.contract.ActivityResultContracts | ||||
| import androidx.cardview.widget.CardView | import androidx.cardview.widget.CardView | ||||
| import androidx.constraintlayout.widget.ConstraintLayout | |||||
| import androidx.core.content.ContextCompat | import androidx.core.content.ContextCompat | ||||
| import androidx.core.content.res.ResourcesCompat | import androidx.core.content.res.ResourcesCompat | ||||
| import androidx.core.view.ViewCompat | import androidx.core.view.ViewCompat | ||||
| import androidx.fragment.app.activityViewModels | import androidx.fragment.app.activityViewModels | ||||
| import androidx.fragment.app.viewModels | import androidx.fragment.app.viewModels | ||||
| import androidx.lifecycle.lifecycleScope | import androidx.lifecycle.lifecycleScope | ||||
| import com.google.android.material.bottomsheet.BottomSheetBehavior | import com.google.android.material.bottomsheet.BottomSheetBehavior | ||||
| import com.google.gson.JsonObject | import com.google.gson.JsonObject | ||||
| import it.reyboz.bustorino.BuildConfig | import it.reyboz.bustorino.BuildConfig | ||||
| import it.reyboz.bustorino.R | import it.reyboz.bustorino.R | ||||
| import it.reyboz.bustorino.backend.FiveTNormalizer | import it.reyboz.bustorino.backend.FiveTNormalizer | ||||
| import it.reyboz.bustorino.backend.LivePositionTripPattern | import it.reyboz.bustorino.backend.LivePositionTripPattern | ||||
| import it.reyboz.bustorino.backend.LivePositionsServiceStatus | import it.reyboz.bustorino.backend.LivePositionsServiceStatus | ||||
| import it.reyboz.bustorino.backend.Stop | import it.reyboz.bustorino.backend.Stop | ||||
| import it.reyboz.bustorino.backend.VehicleUtils | |||||
| import it.reyboz.bustorino.backend.gtfs.GtfsUtils | import it.reyboz.bustorino.backend.gtfs.GtfsUtils | ||||
| import it.reyboz.bustorino.backend.gtfs.LivePositionUpdate | import it.reyboz.bustorino.backend.gtfs.LivePositionUpdate | ||||
| import it.reyboz.bustorino.backend.utils | import it.reyboz.bustorino.backend.utils | ||||
| import it.reyboz.bustorino.data.PreferencesHolder | import it.reyboz.bustorino.data.PreferencesHolder | ||||
| import it.reyboz.bustorino.data.gtfs.TripAndPatternWithStops | import it.reyboz.bustorino.data.gtfs.TripAndPatternWithStops | ||||
| import it.reyboz.bustorino.map.MapLibreLocationEngine | import it.reyboz.bustorino.map.MapLibreLocationEngine | ||||
| import it.reyboz.bustorino.map.MapLibreUtils | import it.reyboz.bustorino.map.MapLibreUtils | ||||
| import it.reyboz.bustorino.middleware.FusedNativeLocationProvider | import it.reyboz.bustorino.middleware.FusedNativeLocationProvider | ||||
| ▲ Show 20 Lines • Show All 43 Lines • ▼ Show 20 Lines | abstract class GeneralMapLibreFragment: ScreenBaseFragment(), OnMapReadyCallback { | ||||
| protected var mapView: MapView? = null | protected var mapView: MapView? = null | ||||
| protected lateinit var mapStyle: Style | protected lateinit var mapStyle: Style | ||||
| protected lateinit var stopsSource: GeoJsonSource | protected lateinit var stopsSource: GeoJsonSource | ||||
| protected lateinit var busesSource: GeoJsonSource | protected lateinit var busesSource: GeoJsonSource | ||||
| protected lateinit var selectedStopSource: GeoJsonSource | protected lateinit var selectedStopSource: GeoJsonSource | ||||
| protected lateinit var selectedBusSource: GeoJsonSource //= GeoJsonSource(SEL_BUS_SOURCE) | protected lateinit var selectedBusSource: GeoJsonSource //= GeoJsonSource(SEL_BUS_SOURCE) | ||||
| protected lateinit var sharedPreferences: SharedPreferences | protected lateinit var sharedPreferences: SharedPreferences | ||||
| protected lateinit var bottomSheetBehavior: BottomSheetBehavior<RelativeLayout> | protected lateinit var bottomSheetBehavior: BottomSheetBehavior<ConstraintLayout> | ||||
| protected var locationEngine: MapLibreLocationEngine? = null | protected var locationEngine: MapLibreLocationEngine? = null | ||||
| protected lateinit var locationProvider: FusedNativeLocationProvider | protected lateinit var locationProvider: FusedNativeLocationProvider | ||||
| protected var shownToastNoPosition = false | protected var shownToastNoPosition = false | ||||
| protected var locationEnabledOnDevice = true | protected var locationEnabledOnDevice = true | ||||
| //TODO ACTIVATE THIS | //TODO ACTIVATE THIS | ||||
| Show All 30 Lines | protected val positionRequestResponder = registerForActivityResult( | ||||
| // TODO find better way to show the necessity of the permission | // TODO find better way to show the necessity of the permission | ||||
| if(shouldShowRequestPermissionRationale(PERM_LOC_FINE)) | if(shouldShowRequestPermissionRationale(PERM_LOC_FINE)) | ||||
| Toast.makeText(requireContext(), | Toast.makeText(requireContext(), | ||||
| R.string.enable_position_message_map, Toast.LENGTH_SHORT).show() | R.string.enable_position_message_map, Toast.LENGTH_SHORT).show() | ||||
| } | } | ||||
| } | } | ||||
| ) | ) | ||||
| //Bottom sheet behavior in GeneralMapLibreFragment | //Bottom sheet behavior in GeneralMapLibreFragment | ||||
| protected var bottomLayout: RelativeLayout? = null | protected var bottomLayout: ConstraintLayout? = null | ||||
| protected lateinit var stopTitleTextView: TextView | protected lateinit var stopTitleTextView: TextView | ||||
| protected lateinit var stopNumberTextView: TextView | protected lateinit var stopNumberTextView: TextView | ||||
| protected lateinit var linesPassingTextView: TextView | protected lateinit var linesPassingTextView: TextView | ||||
| protected lateinit var extraBottomTextView: TextView | protected lateinit var extraBottomTextView: TextView | ||||
| protected lateinit var arrivalsCard: CardView | protected lateinit var arrivalsCard: CardView | ||||
| protected lateinit var directionsCard: CardView | protected lateinit var directionsCard: CardView | ||||
| protected lateinit var bottomrightImage: ImageView | protected lateinit var bottomrightImage: ImageView | ||||
| protected lateinit var locationComponent: LocationComponent | protected lateinit var locationComponent: LocationComponent | ||||
| protected lateinit var busPositionsIconButton: ImageButton | protected lateinit var busPositionsIconButton: ImageButton | ||||
| protected lateinit var vehicleIcon: ImageView | |||||
| protected var lastLocation : Location? = null | protected var lastLocation : Location? = null | ||||
| private var lastMapStyle ="" | private var lastMapStyle ="" | ||||
| //BUS POSITIONS | //BUS POSITIONS | ||||
| protected val updatesByVehDict = HashMap<String, LivePositionTripPattern>(5) | protected val updatesByVehDict = HashMap<String, LivePositionTripPattern>(5) | ||||
| ▲ Show 20 Lines • Show All 68 Lines • ▼ Show 20 Lines | abstract class GeneralMapLibreFragment: ScreenBaseFragment(), OnMapReadyCallback { | ||||
| } | } | ||||
| override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { | override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { | ||||
| lastMapStyle = PreferencesHolder.getMapLibreStyleFile(requireContext()) | lastMapStyle = PreferencesHolder.getMapLibreStyleFile(requireContext()) | ||||
| Log.d(DEBUG_TAG, "onCreateView lastMapStyle: $lastMapStyle") | Log.d(DEBUG_TAG, "onCreateView lastMapStyle: $lastMapStyle") | ||||
| return super.onCreateView(inflater, container, savedInstanceState) | return super.onCreateView(inflater, container, savedInstanceState) | ||||
| } | } | ||||
| override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | protected fun initBottomSheet(view: View){ | ||||
| super.onViewCreated(view, savedInstanceState) | val bottomSheet = view.findViewById<ConstraintLayout>(R.id.bottom_sheet) | ||||
| //init bottom sheet | |||||
| val bottomSheet = view.findViewById<RelativeLayout>(R.id.bottom_sheet) | |||||
| bottomLayout = bottomSheet | bottomLayout = bottomSheet | ||||
| stopTitleTextView = view.findViewById(R.id.stopTitleTextView) | stopTitleTextView = view.findViewById(R.id.stopTitleTextView) | ||||
| stopNumberTextView = view.findViewById(R.id.stopNumberTextView) | stopNumberTextView = view.findViewById(R.id.stopNumberTextView) | ||||
| linesPassingTextView = view.findViewById(R.id.linesPassingTextView) | linesPassingTextView = view.findViewById(R.id.descriptionTextView) | ||||
| arrivalsCard = view.findViewById(R.id.arrivalsCardButton) | arrivalsCard = view.findViewById(R.id.arrivalsCardButton) | ||||
| directionsCard = view.findViewById(R.id.directionsCardButton) | directionsCard = view.findViewById(R.id.directionsCardButton) | ||||
| bottomrightImage = view.findViewById(R.id.rightmostImageView) | vehicleIcon = view.findViewById(R.id.vehicleIcon) | ||||
| bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet) | bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet) | ||||
| bottomSheetBehavior.state = BottomSheetBehavior.STATE_HIDDEN | |||||
| } | |||||
| override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | |||||
| super.onViewCreated(view, savedInstanceState) | |||||
| //init bottom sheet | |||||
| initBottomSheet(view) | |||||
| bottomrightImage = view.findViewById(R.id.rightmostImageView) | |||||
| extraBottomTextView = view.findViewById(R.id.extraBottomTextView) | extraBottomTextView = view.findViewById(R.id.extraBottomTextView) | ||||
| bottomSheetBehavior.state = BottomSheetBehavior.STATE_HIDDEN | |||||
| } | } | ||||
| override fun onResume() { | override fun onResume() { | ||||
| super.onResume() | super.onResume() | ||||
| mapView?.onResume() | mapView?.onResume() | ||||
| val newMapStyle = PreferencesHolder.getMapLibreStyleFile(requireContext()) | val newMapStyle = PreferencesHolder.getMapLibreStyleFile(requireContext()) | ||||
| Log.d(DEBUG_TAG, "onResume newMapStyle: $newMapStyle, lastMapStyle: $lastMapStyle") | Log.d(DEBUG_TAG, "onResume newMapStyle: $newMapStyle, lastMapStyle: $lastMapStyle") | ||||
| // TODO: reload style if user changed preferences | // TODO: reload style if user changed preferences | ||||
| ▲ Show 20 Lines • Show All 375 Lines • ▼ Show 20 Lines | ) { | ||||
| } | } | ||||
| directionsCard.setOnClickListener { | directionsCard.setOnClickListener { | ||||
| onDirectionsClick(pat?.code ?: "", veh) | onDirectionsClick(pat?.code ?: "", veh) | ||||
| } | } | ||||
| directionsCard.visibility = View.VISIBLE | directionsCard.visibility = View.VISIBLE | ||||
| bottomrightImage.setImageDrawable( | bottomrightImage.setImageDrawable( | ||||
| ResourcesCompat.getDrawable(resources, R.drawable.ic_magnifying_glass, activity?.theme) | ResourcesCompat.getDrawable(resources, R.drawable.ic_magnifying_glass, activity?.theme) | ||||
| ) | ) | ||||
| // if you change this, remember to change the color of the vehicleIcon | |||||
| val colorBlue = ResourcesCompat.getColor(resources, R.color.blue_500, activity?.theme) | val colorBlue = ResourcesCompat.getColor(resources, R.color.blue_500, activity?.theme) | ||||
| ViewCompat.setBackgroundTintList(directionsCard, ColorStateList.valueOf(colorBlue)) | ViewCompat.setBackgroundTintList(directionsCard, ColorStateList.valueOf(colorBlue)) | ||||
| linesPassingTextView.text = getString(R.string.vehicle_fill, data.posUpdate.vehicle) | linesPassingTextView.text = getString(R.string.vehicle_fill, data.posUpdate.vehicle) | ||||
| arrivalsCard.visibility = View.GONE | arrivalsCard.visibility = View.GONE | ||||
| extraBottomTextView.text = getString(R.string.updated_fill, utils.unixTimestampToLocalTime(data.posUpdate.timestamp)) | extraBottomTextView.text = getString(R.string.updated_fill, utils.unixTimestampToLocalTime(data.posUpdate.timestamp)) | ||||
| extraBottomTextView.visibility = View.VISIBLE | extraBottomTextView.visibility = View.VISIBLE | ||||
| val update = data.posUpdate | |||||
| val vehInfo = VehicleUtils.getTypeForLabel(update.vehicle) | |||||
| if(vehInfo == null){ | |||||
| vehicleIcon.visibility = View.GONE | |||||
| } else{ | |||||
| val ico = when(vehInfo.type){ | |||||
| VehicleUtils.VehicleType.BUS -> R.drawable.ic_bus_small | |||||
| VehicleUtils.VehicleType.ELECTRIC_BUS -> R.drawable.ic_bus_electric_small | |||||
| VehicleUtils.VehicleType.TRAM -> R.drawable.ic_tram_24 | |||||
| } | |||||
| vehicleIcon.setImageDrawable(ResourcesCompat.getDrawable(resources, ico, activity?.theme)) | |||||
| vehicleIcon.visibility = View.VISIBLE | |||||
| vehicleIcon.setOnClickListener { | |||||
| val print = "${vehInfo.type.getName()} ${vehInfo.name}" | |||||
| makeToast(print) | |||||
| } | |||||
| } | |||||
| } | } | ||||
| vehShowing = veh | vehShowing = veh | ||||
| bottomSheetBehavior.state = BottomSheetBehavior.STATE_EXPANDED | bottomSheetBehavior.state = BottomSheetBehavior.STATE_EXPANDED | ||||
| updatePositionsIcons(true) | updatePositionsIcons(true) | ||||
| Log.d(DEBUG_TAG, "Shown vehicle $veh in bottom sheet") | Log.d(DEBUG_TAG, "Shown vehicle $veh in bottom sheet") | ||||
| } | } | ||||
| /** | /** | ||||
| ▲ Show 20 Lines • Show All 137 Lines • ▼ Show 20 Lines | protected fun openStopInBottomSheet(stop: Stop){ | ||||
| } | } | ||||
| context?.let { | context?.let { | ||||
| val colorIcon = ViewUtils.getColorFromTheme(it, android.R.attr.colorAccent)//ResourcesCompat.getColor(resources,R.attr.colorAccent,activity?.theme) | val colorIcon = ViewUtils.getColorFromTheme(it, android.R.attr.colorAccent)//ResourcesCompat.getColor(resources,R.attr.colorAccent,activity?.theme) | ||||
| ViewCompat.setBackgroundTintList(directionsCard, ColorStateList.valueOf(colorIcon)) | ViewCompat.setBackgroundTintList(directionsCard, ColorStateList.valueOf(colorIcon)) | ||||
| } | } | ||||
| bottomrightImage.setImageDrawable(ResourcesCompat.getDrawable(resources, R.drawable.navigation_right, activity?.theme)) | bottomrightImage.setImageDrawable(ResourcesCompat.getDrawable(resources, R.drawable.navigation_right, activity?.theme)) | ||||
| vehicleIcon.visibility = View.GONE | |||||
| } | } | ||||
| //add stop marker | //add stop marker | ||||
| if (stop.latitude!=null && stop.longitude!=null) { | if (stop.latitude!=null && stop.longitude!=null) { | ||||
| Log.d(DEBUG_TAG, "Showing stop: ${stop.ID}") | Log.d(DEBUG_TAG, "Showing stop: ${stop.ID}") | ||||
| if (showOpenStopWithSymbolLayer()) { | if (showOpenStopWithSymbolLayer()) { | ||||
| stopActiveSymbol = symbolManager?.create( | stopActiveSymbol = symbolManager?.create( | ||||
| SymbolOptions() | SymbolOptions() | ||||
| ▲ Show 20 Lines • Show All 319 Lines • ▼ Show 20 Lines | companion object{ | ||||
| private const val PERM_LOC_COARSE = Manifest.permission.ACCESS_COARSE_LOCATION | private const val PERM_LOC_COARSE = Manifest.permission.ACCESS_COARSE_LOCATION | ||||
| private const val PERM_LOC_FINE = Manifest.permission.ACCESS_FINE_LOCATION | private const val PERM_LOC_FINE = Manifest.permission.ACCESS_FINE_LOCATION | ||||
| //TODO: this is hardcoded, make it modifiable by the user | //TODO: this is hardcoded, make it modifiable by the user | ||||
| protected const val MAX_DIST_KM = 90.0 | protected const val MAX_DIST_KM = 90.0 | ||||
| } | } | ||||
| } | } | ||||
| No newline at end of file | No newline at end of file | ||||
Public contents are in Creative Commons Attribution-ShareAlike 4.0 (CC-BY-SA) or GNU Free Documentation License (at your option) unless otherwise noted. · Contact / Register