Changeset View
Changeset View
Standalone View
Standalone View
Copy file pathapp/src/main/java/it/reyboz/bustorino/fragments/NearbyStopsFragment.kt
| Show All 27 Lines | |||||
| import android.widget.ProgressBar | import android.widget.ProgressBar | ||||
| import android.widget.TextView | import android.widget.TextView | ||||
| import androidx.appcompat.widget.AppCompatButton | import androidx.appcompat.widget.AppCompatButton | ||||
| import androidx.core.util.Pair | import androidx.core.util.Pair | ||||
| import androidx.fragment.app.viewModels | import androidx.fragment.app.viewModels | ||||
| import androidx.preference.PreferenceManager | import androidx.preference.PreferenceManager | ||||
| import androidx.recyclerview.widget.RecyclerView | import androidx.recyclerview.widget.RecyclerView | ||||
| import androidx.work.WorkInfo | import androidx.work.WorkInfo | ||||
| import com.google.android.material.button.MaterialButton | |||||
| import it.reyboz.bustorino.BuildConfig | import it.reyboz.bustorino.BuildConfig | ||||
| import it.reyboz.bustorino.R | import it.reyboz.bustorino.R | ||||
| import it.reyboz.bustorino.adapters.ArrivalsStopAdapter | import it.reyboz.bustorino.adapters.ArrivalsStopAdapter | ||||
| import it.reyboz.bustorino.adapters.SquareStopAdapter | import it.reyboz.bustorino.adapters.SquareStopAdapter | ||||
| import it.reyboz.bustorino.backend.* | import it.reyboz.bustorino.backend.* | ||||
| import it.reyboz.bustorino.data.DatabaseUpdate | import it.reyboz.bustorino.data.DatabaseUpdate | ||||
| import it.reyboz.bustorino.middleware.AutoFitGridLayoutManager | import it.reyboz.bustorino.middleware.AutoFitGridLayoutManager | ||||
| import it.reyboz.bustorino.middleware.FusedNativeLocationProvider | import it.reyboz.bustorino.middleware.FusedNativeLocationProvider | ||||
| import it.reyboz.bustorino.middleware.FusedNativeLocationProvider.LocationUpdateListener | import it.reyboz.bustorino.middleware.FusedNativeLocationProvider.LocationUpdateListener | ||||
| import it.reyboz.bustorino.util.Permissions.Companion.anyLocationPermissionsGranted | import it.reyboz.bustorino.util.Permissions | ||||
| import it.reyboz.bustorino.util.Permissions.Companion.bothLocationPermissionsGranted | import it.reyboz.bustorino.util.Permissions.Companion.bothLocationPermissionsGranted | ||||
| import it.reyboz.bustorino.util.StopSorterByDistance | import it.reyboz.bustorino.util.StopSorterByDistance | ||||
| import it.reyboz.bustorino.viewmodels.NearbyStopsViewModel | import it.reyboz.bustorino.viewmodels.NearbyStopsViewModel | ||||
| import java.util.* | import java.util.* | ||||
| import kotlin.math.min | import kotlin.math.min | ||||
| class NearbyStopsFragment : ScreenBaseFragment() { | class NearbyStopsFragment : ScreenBaseFragment() { | ||||
| override fun getBaseViewForSnackBar(): View? { | override fun getBaseViewForSnackBar(): View? { | ||||
| Show All 29 Lines | class NearbyStopsFragment : ScreenBaseFragment() { | ||||
| private var gridLayoutManager: AutoFitGridLayoutManager? = null | private var gridLayoutManager: AutoFitGridLayoutManager? = null | ||||
| private var lastPosition: GPSPoint? = null | private var lastPosition: GPSPoint? = null | ||||
| private var circlingProgressBar: ProgressBar? = null | private var circlingProgressBar: ProgressBar? = null | ||||
| private lateinit var flatProgressBar: ProgressBar | private lateinit var flatProgressBar: ProgressBar | ||||
| //protected SharedPreferences globalSharedPref; | //protected SharedPreferences globalSharedPref; | ||||
| //private SharedPreferences.OnSharedPreferenceChangeListener preferenceChangeListener; | //private SharedPreferences.OnSharedPreferenceChangeListener preferenceChangeListener; | ||||
| private var messageTextView: TextView? = null | private var messageTextView: TextView? = null | ||||
| private lateinit var enableLocationButton : MaterialButton | |||||
| private var titleTextView: TextView? = null | private var titleTextView: TextView? = null | ||||
| private var loadingTextView: TextView? = null | private var loadingTextView: TextView? = null | ||||
| private var scrollListener: CommonScrollListener? = null | private var scrollListener: CommonScrollListener? = null | ||||
| private var switchButton: AppCompatButton? = null | private var switchButton: AppCompatButton? = null | ||||
| private var firstLocForStops = true | private var firstLocForStops = true | ||||
| private var firstLocForArrivals = true | private var firstLocForArrivals = true | ||||
| private var stopsMaxDistance = -3 | private var stopsMaxDistance = -3 | ||||
| private var stopsMinNumber = -1 | private var stopsMinNumber = -1 | ||||
| Show All 16 Lines | private val locationUpdateListener: LocationUpdateListener = object : LocationUpdateListener { | ||||
| isLocationEnabled = isEnabled | isLocationEnabled = isEnabled | ||||
| if (isEnabled) { | if (isEnabled) { | ||||
| setShowingStatus(LocationShowingStatus.SEARCHING) | setShowingStatus(LocationShowingStatus.SEARCHING) | ||||
| } else { | } else { | ||||
| setShowingStatus(LocationShowingStatus.DISABLED) | setShowingStatus(LocationShowingStatus.DISABLED) | ||||
| } | } | ||||
| } | } | ||||
| } | } | ||||
| private val locationPermissionLauncher = getPositionRequestLauncher(){ granted -> | |||||
| startLocationUpdatesByType() | |||||
| setShowingStatus(LocationShowingStatus.SEARCHING) | |||||
| } | |||||
| // Two different settings for the location provider | // Two different settings for the location provider | ||||
| private val locationOptionsArrivals = FusedNativeLocationProvider.Options(5 * 1000L, 25f) | private val locationOptionsArrivals = FusedNativeLocationProvider.Options(5 * 1000L, 25f) | ||||
| private val locationOptionsStops = FusedNativeLocationProvider.Options(1000L, 5f) | private val locationOptionsStops = FusedNativeLocationProvider.Options(1000L, 5f) | ||||
| private var locationProvider: FusedNativeLocationProvider? = null | private var locationProvider: FusedNativeLocationProvider? = null | ||||
| /*private val arrivalsListener: ArrivalsListener = object : ArrivalsListener { | /*private val arrivalsListener: ArrivalsListener = object : ArrivalsListener { | ||||
| Show All 40 Lines | override fun onCreate(savedInstanceState: Bundle?) { | ||||
| locationProvider = FusedNativeLocationProvider(requireContext()) | locationProvider = FusedNativeLocationProvider(requireContext()) | ||||
| } | } | ||||
| override fun onCreateView( | override fun onCreateView( | ||||
| inflater: LayoutInflater, container: ViewGroup?, | inflater: LayoutInflater, container: ViewGroup?, | ||||
| savedInstanceState: Bundle? | savedInstanceState: Bundle? | ||||
| ): View { | ): View { | ||||
| // Inflate the layout for this fragment | // Inflate the layout for this fragment | ||||
| if (getContext() == null) throw RuntimeException() | val context = requireContext() | ||||
| val root = inflater.inflate(R.layout.fragment_nearby_stops, container, false) | val root = inflater.inflate(R.layout.fragment_nearby_stops, container, false) | ||||
| gridRecyclerView = root.findViewById<RecyclerView>(R.id.stopGridRecyclerView) | gridRecyclerView = root.findViewById<RecyclerView>(R.id.stopGridRecyclerView) | ||||
| gridLayoutManager = AutoFitGridLayoutManager( | gridLayoutManager = AutoFitGridLayoutManager( | ||||
| requireContext().getApplicationContext(), | context.getApplicationContext(), | ||||
| utils.convertDipToPixels(getContext(), COLUMN_WIDTH_DP.toFloat()).toInt() | utils.convertDipToPixels(context, COLUMN_WIDTH_DP.toFloat()).toInt() | ||||
| ) | ) | ||||
| gridRecyclerView.setLayoutManager(gridLayoutManager) | gridRecyclerView.setLayoutManager(gridLayoutManager) | ||||
| gridRecyclerView.setHasFixedSize(false) | gridRecyclerView.setHasFixedSize(false) | ||||
| circlingProgressBar = root.findViewById<ProgressBar>(R.id.circularProgressBar) | circlingProgressBar = root.findViewById<ProgressBar>(R.id.circularProgressBar) | ||||
| flatProgressBar = root.findViewById(R.id.horizontalProgressBar) | flatProgressBar = root.findViewById(R.id.horizontalProgressBar) | ||||
| messageTextView = root.findViewById<TextView>(R.id.messageTextView) | messageTextView = root.findViewById<TextView>(R.id.messageTextView) | ||||
| enableLocationButton = root.findViewById(R.id.grantLocationButton) | |||||
| titleTextView = root.findViewById<TextView>(R.id.titleTextView) | titleTextView = root.findViewById<TextView>(R.id.titleTextView) | ||||
| loadingTextView = root.findViewById<TextView>(R.id.positionLoadingTextView) | loadingTextView = root.findViewById<TextView>(R.id.positionLoadingTextView) | ||||
| switchButton = root.findViewById<AppCompatButton>(R.id.switchButton) | switchButton = root.findViewById<AppCompatButton>(R.id.switchButton) | ||||
| scrollListener = CommonScrollListener(mListener, false) | scrollListener = CommonScrollListener(mListener, false) | ||||
| switchButton!!.setOnClickListener(View.OnClickListener { v: View? -> switchFragmentType() }) | switchButton!!.setOnClickListener(View.OnClickListener { v: View? -> switchFragmentType() }) | ||||
| if (BuildConfig.DEBUG) Log.d(DEBUG_TAG, "onCreateView") | if (BuildConfig.DEBUG) Log.d(DEBUG_TAG, "onCreateView") | ||||
| val appContext = requireContext().applicationContext | val appContext = requireContext().applicationContext | ||||
| DatabaseUpdate.watchUpdateWorkStatus(context, this){ workInfos -> | DatabaseUpdate.watchUpdateWorkStatus(context, this){ workInfos -> | ||||
| if (workInfos.isEmpty()) { | if (workInfos.isEmpty()) { | ||||
| viewModel.setDBUpdateRunning(false) | viewModel.setDBUpdateRunning(false) | ||||
| return@watchUpdateWorkStatus | return@watchUpdateWorkStatus | ||||
| } | } | ||||
| val wi = workInfos.get(0) | val wi = workInfos.get(0) | ||||
| if (wi.state == WorkInfo.State.RUNNING && locationProvider!!.isRunning()) { | if (wi.state == WorkInfo.State.RUNNING && locationProvider!!.isRunning()) { | ||||
| locationProvider!!.stopUpdates() | locationProvider!!.stopUpdates() | ||||
| viewModel.setDBUpdateRunning(true) | viewModel.setDBUpdateRunning(true) | ||||
| } else { | } else { | ||||
| //start the request | //start the request | ||||
| if (bothLocationPermissionsGranted(requireContext())) { | checkPermissionLocationStart() | ||||
| if (!locationProvider!!.isRunning()) { | viewModel.setDBUpdateRunning(false) | ||||
| startLocationUpdatesByType() | //actually restart request | ||||
| } | } | ||||
| } else { | |||||
| setShowingStatus(LocationShowingStatus.NO_PERMISSION) | |||||
| } | } | ||||
| viewModel.setDBUpdateRunning(false) | //add location listener | ||||
| //actually restart request | locationProvider!!.addListener(locationUpdateListener) | ||||
| enableLocationButton.setOnClickListener { | |||||
| locationPermissionLauncher.launch(Permissions.LOCATION_PERMISSIONS) | |||||
| } | } | ||||
| return root | |||||
| } | } | ||||
| if (anyLocationPermissionsGranted(appContext)) { | private fun checkPermissionLocationStart(){ | ||||
| Log.d(DEBUG_TAG, "Check permission and start location updates") | |||||
| if (bothLocationPermissionsGranted(requireContext())) { | |||||
| if (!locationProvider!!.isRunning()) { | |||||
| startLocationUpdatesByType() | |||||
| setShowingStatus(LocationShowingStatus.SEARCHING) | setShowingStatus(LocationShowingStatus.SEARCHING) | ||||
| } | |||||
| } else { | } else { | ||||
| setShowingStatus(LocationShowingStatus.NO_PERMISSION) | setShowingStatus(LocationShowingStatus.NO_PERMISSION) | ||||
| } | } | ||||
| //add location listener | |||||
| locationProvider!!.addListener(locationUpdateListener) | |||||
| return root | |||||
| } | } | ||||
| override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | ||||
| super.onViewCreated(view, savedInstanceState) | super.onViewCreated(view, savedInstanceState) | ||||
| gridRecyclerView.setVisibility(View.INVISIBLE) | gridRecyclerView.setVisibility(View.INVISIBLE) | ||||
| gridRecyclerView.addOnScrollListener(scrollListener!!) | gridRecyclerView.addOnScrollListener(scrollListener!!) | ||||
| //mListener?.readyGUIfor(FragmentKind.NEARBY_STOPS) | //mListener?.readyGUIfor(FragmentKind.NEARBY_STOPS) | ||||
| //observe the livedata | //observe the livedata | ||||
| viewModel.stopsAtDistance.observe(getViewLifecycleOwner()) {stops -> | viewModel.stopsAtDistance.observe(getViewLifecycleOwner()) {stops -> | ||||
| Show All 29 Lines | override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | ||||
| } | } | ||||
| viewModel.arrivalsDecoupled.observe(viewLifecycleOwner){ stoprouteList -> | viewModel.arrivalsDecoupled.observe(viewLifecycleOwner){ stoprouteList -> | ||||
| if (getContext() == null) { | if (getContext() == null) { | ||||
| Log.e(DEBUG_TAG, "Trying to show arrivals in Recycler but we're not attached") | Log.e(DEBUG_TAG, "Trying to show arrivals in Recycler but we're not attached") | ||||
| return@observe | return@observe | ||||
| } | } | ||||
| val context = requireContext() | |||||
| if (firstLocForArrivals) { | if (firstLocForArrivals) { | ||||
| arrivalsStopAdapter = ArrivalsStopAdapter(stoprouteList, mListener, context, lastPosition!!) | mListener?.let{ | ||||
| arrivalsStopAdapter = ArrivalsStopAdapter(stoprouteList, it, context, lastPosition!!) | |||||
| gridRecyclerView.setAdapter(arrivalsStopAdapter) | gridRecyclerView.setAdapter(arrivalsStopAdapter) | ||||
| firstLocForArrivals = false | firstLocForArrivals = false | ||||
| } | |||||
| } else { | } else { | ||||
| arrivalsStopAdapter!!.setRoutesPairListAndPosition(stoprouteList, lastPosition) | arrivalsStopAdapter!!.setRoutesPairListAndPosition(stoprouteList, lastPosition) | ||||
| } | } | ||||
| //arrivalsStopAdapter.notifyDataSetChanged(); | //arrivalsStopAdapter.notifyDataSetChanged(); | ||||
| showRecyclerHidingLoadMessage() | showRecyclerHidingLoadMessage() | ||||
| //if (mListener != null) mListener!!.readyGUIfor(FragmentKind.NEARBY_ARRIVALS) | //if (mListener != null) mListener!!.readyGUIfor(FragmentKind.NEARBY_ARRIVALS) | ||||
| } | } | ||||
| //added | |||||
| checkPermissionLocationStart() | |||||
| } | } | ||||
| /** | /** | ||||
| * Internal bit used to start location updates | * Internal bit used to start location updates | ||||
| */ | */ | ||||
| private fun startLocationUpdatesByType() { | private fun startLocationUpdatesByType() { | ||||
| when (fragment_type) { | when (fragment_type) { | ||||
| ▲ Show 20 Lines • Show All 50 Lines • ▼ Show 20 Lines | private fun setShowingStatus(newStatus: LocationShowingStatus) { | ||||
| } | } | ||||
| when (newStatus) { | when (newStatus) { | ||||
| LocationShowingStatus.FIRST_FIX -> { | LocationShowingStatus.FIRST_FIX -> { | ||||
| circlingProgressBar!!.setVisibility(View.GONE) | circlingProgressBar!!.setVisibility(View.GONE) | ||||
| loadingTextView!!.setVisibility(View.GONE) | loadingTextView!!.setVisibility(View.GONE) | ||||
| gridRecyclerView.setVisibility(View.VISIBLE) | gridRecyclerView.setVisibility(View.VISIBLE) | ||||
| messageTextView!!.setVisibility(View.GONE) | messageTextView!!.setVisibility(View.GONE) | ||||
| enableLocationButton.setVisibility(View.GONE) | |||||
| } | } | ||||
| LocationShowingStatus.NO_PERMISSION -> { | LocationShowingStatus.NO_PERMISSION -> { | ||||
| circlingProgressBar!!.setVisibility(View.GONE) | circlingProgressBar?.setVisibility(View.GONE) | ||||
| loadingTextView!!.setVisibility(View.GONE) | loadingTextView?.setVisibility(View.GONE) | ||||
| messageTextView!!.setText(R.string.enable_position_message_nearby) | messageTextView?.setText(R.string.enable_position_message_nearby) | ||||
| messageTextView!!.setVisibility(View.VISIBLE) | messageTextView?.setVisibility(View.VISIBLE) | ||||
| enableLocationButton.setVisibility(View.VISIBLE) | |||||
| } | } | ||||
| LocationShowingStatus.DISABLED -> { | LocationShowingStatus.DISABLED -> { | ||||
| //if (showingStatus== LocationShowingStatus.SEARCHING){ | //if (showingStatus== LocationShowingStatus.SEARCHING){ | ||||
| circlingProgressBar!!.setVisibility(View.GONE) | circlingProgressBar!!.setVisibility(View.GONE) | ||||
| loadingTextView!!.setVisibility(View.GONE) | loadingTextView!!.setVisibility(View.GONE) | ||||
| //} | //} | ||||
| messageTextView!!.setText(R.string.enable_location_message) | messageTextView!!.setText(R.string.enable_location_message) | ||||
| messageTextView!!.setVisibility(View.VISIBLE) | messageTextView!!.setVisibility(View.VISIBLE) | ||||
| enableLocationButton.setVisibility(View.GONE) | |||||
| } | } | ||||
| LocationShowingStatus.SEARCHING -> { | LocationShowingStatus.SEARCHING -> { | ||||
| circlingProgressBar!!.setVisibility(View.VISIBLE) | circlingProgressBar!!.setVisibility(View.VISIBLE) | ||||
| loadingTextView!!.setVisibility(View.VISIBLE) | loadingTextView!!.setVisibility(View.VISIBLE) | ||||
| gridRecyclerView.setVisibility(View.GONE) | gridRecyclerView.setVisibility(View.GONE) | ||||
| messageTextView!!.setVisibility(View.GONE) | messageTextView!!.setVisibility(View.GONE) | ||||
| enableLocationButton.setVisibility(View.GONE) | |||||
| } | } | ||||
| } | } | ||||
| showingStatus = newStatus | showingStatus = newStatus | ||||
| } | } | ||||
| override fun onAttach(context: Context) { | override fun onAttach(context: Context) { | ||||
| super.onAttach(context) | super.onAttach(context) | ||||
| ▲ Show 20 Lines • Show All 59 Lines • ▼ Show 20 Lines | override fun onResume() { | ||||
| } catch (ex: NumberFormatException) { | } catch (ex: NumberFormatException) { | ||||
| stopsMinNumber = 5 | stopsMinNumber = 5 | ||||
| } | } | ||||
| if (BuildConfig.DEBUG) Log.d( | if (BuildConfig.DEBUG) Log.d( | ||||
| DEBUG_TAG, | DEBUG_TAG, | ||||
| "Max distance for stops: $stopsMaxDistance, Min number of stops: $stopsMinNumber" | "Max distance for stops: $stopsMaxDistance, Min number of stops: $stopsMinNumber" | ||||
| ) | ) | ||||
| if (!locationProvider!!.isRunning()) { | //checkPermissionLocationStart() | ||||
| startLocationUpdatesByType() | |||||
| } | |||||
| } | } | ||||
| override fun onDetach() { | override fun onDetach() { | ||||
| super.onDetach() | super.onDetach() | ||||
| mListener = null | mListener = null | ||||
| //if (arrivalsManager != null) arrivalsManager!!.cancelAllRequests() | //if (arrivalsManager != null) arrivalsManager!!.cancelAllRequests() | ||||
| } | } | ||||
| override fun onStart() { | |||||
| super.onStart() | |||||
| if(BuildConfig.DEBUG) Log.d(DEBUG_TAG, "onStart called") | |||||
| //checkPermissionLocationStart() | |||||
| } | |||||
| /** | /** | ||||
| * Display the stops, or run new set of requests for arrivals | * Display the stops, or run new set of requests for arrivals | ||||
| */ | */ | ||||
| private fun showStopsInViews(stops: ArrayList<Stop>, location: GPSPoint?) { | private fun showStopsInViews(stops: ArrayList<Stop>, location: GPSPoint?) { | ||||
| if (stops.isEmpty()) { | if (stops.isEmpty()) { | ||||
| setNoStopsLayout() | setNoStopsLayout() | ||||
| return | return | ||||
| } | } | ||||
| ▲ Show 20 Lines • Show All 66 Lines • ▼ Show 20 Lines | private fun prepareForFragmentType() { | ||||
| mListener?.readyGUIfor(FragmentKind.NEARBY_STOPS) | mListener?.readyGUIfor(FragmentKind.NEARBY_STOPS) | ||||
| } else if (fragment_type == FragType.ARRIVALS) { | } else if (fragment_type == FragType.ARRIVALS) { | ||||
| titleTextView!!.text = getString(R.string.nearby_arrivals_message) | titleTextView!!.text = getString(R.string.nearby_arrivals_message) | ||||
| switchButton!!.text = getString(R.string.show_stops) | switchButton!!.text = getString(R.string.show_stops) | ||||
| val arrivalsSorted = viewModel.arrivalsDecoupled.value | val arrivalsSorted = viewModel.arrivalsDecoupled.value | ||||
| arrivalsSorted?.let{ | arrivalsSorted?.let{ | ||||
| lastPosition?.let{pos -> | lastPosition?.let{pos -> | ||||
| arrivalsStopAdapter = ArrivalsStopAdapter(it,mListener,requireContext(), pos ) | arrivalsStopAdapter = ArrivalsStopAdapter(it,mListener!!,requireContext(), pos ) | ||||
| } | } | ||||
| } | } | ||||
| arrivalsStopAdapter?.let{ | arrivalsStopAdapter?.let{ | ||||
| gridRecyclerView.setAdapter(it) | gridRecyclerView.setAdapter(it) | ||||
| } | } | ||||
| mListener?.readyGUIfor(FragmentKind.NEARBY_ARRIVALS) | mListener?.readyGUIfor(FragmentKind.NEARBY_ARRIVALS) | ||||
| } | } | ||||
| ▲ Show 20 Lines • Show All 119 Lines • Show Last 20 Lines | |||||