Changeset View
Changeset View
Standalone View
Standalone View
Copy file pathapp/src/main/java/it/reyboz/bustorino/fragments/NearbyStopsFragment.kt
| Show All 11 Lines | BusTO - Fragments components | ||||
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
| GNU General Public License for more details. | GNU General Public License for more details. | ||||
| You should have received a copy of the GNU General Public License | You should have received a copy of the GNU General Public License | ||||
| along with this program. If not, see <http://www.gnu.org/licenses/>. | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||||
| */ | */ | ||||
| package it.reyboz.bustorino.fragments | package it.reyboz.bustorino.fragments | ||||
| import android.annotation.SuppressLint | |||||
| import android.content.Context | import android.content.Context | ||||
| import android.location.Location | import android.location.Location | ||||
| import android.os.Bundle | import android.os.Bundle | ||||
| import android.util.Log | import android.util.Log | ||||
| import android.view.LayoutInflater | import android.view.LayoutInflater | ||||
| import android.view.View | import android.view.View | ||||
| import android.view.ViewGroup | import android.view.ViewGroup | ||||
| 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 | |||||
| class NearbyStopsFragment : ScreenBaseFragment() { | class NearbyStopsFragment : ScreenBaseFragment() { | ||||
| override fun getBaseViewForSnackBar(): View? { | override fun getBaseViewForSnackBar(): View? { | ||||
| return null | return null | ||||
| } | } | ||||
| enum class FragType(val num: Int) { | enum class FragType(val num: Int) { | ||||
| STOPS(1), ARRIVALS(2); | STOPS(1), ARRIVALS(2); | ||||
| companion object { | companion object { | ||||
| @JvmStatic | @JvmStatic | ||||
| fun fromNum(i: Int): FragType { | fun fromNum(i: Int): FragType { | ||||
| when (i) { | when (i) { | ||||
| 1 -> return STOPS | 1 -> return STOPS | ||||
| 2 -> return ARRIVALS | 2 -> return ARRIVALS | ||||
| else -> throw IllegalArgumentException("type not recognized") | else -> throw IllegalArgumentException("type not recognized") | ||||
| } | } | ||||
| } | } | ||||
| } | } | ||||
| } | } | ||||
| private enum class LocationShowingStatus { | private enum class LocationShowingStatus { | ||||
| SEARCHING, FIRST_FIX, DISABLED, NO_PERMISSION | SEARCHING, POSITION_FOUND, DISABLED, NO_PERMISSION | ||||
| } | } | ||||
| private var mListener: FragmentListenerMain? = null | private var mListener: FragmentListenerMain? = null | ||||
| private var fragment_type = FragType.STOPS | private var fragment_type = FragType.STOPS | ||||
| private lateinit var gridRecyclerView: RecyclerView | private lateinit var gridRecyclerView: RecyclerView | ||||
| private var dataAdapter: SquareStopAdapter? = null | private var dataAdapter: SquareStopAdapter? = null | ||||
| 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 -> | ||||
| Log.d(DEBUG_TAG, "Received " + stops.size + " stops nearby") | Log.d(DEBUG_TAG, "Received " + stops.size + " stops nearby") | ||||
| var distance = viewModel.distanceMtLiveData.getValue() | var distance = viewModel.distanceMtLiveData.getValue() | ||||
| if (distance == null) { | if (distance == null) { | ||||
| distance = 40 | distance = 40 | ||||
| } | } | ||||
| if ((stops.size < stopsMinNumber && distance <= stopsMaxDistance)) { | if ((stops.size < stopsMinNumber && distance <= stopsMaxDistance)) { | ||||
| viewModel.setDistance(distance + 40) | viewModel.setDistance(distance + 40) | ||||
| //viewModel.requestStopsAtDistance(distance, true); | //viewModel.requestStopsAtDistance(distance, true); | ||||
| //Log.d(DEBUG_TAG, "Doubling distance now!"); | //Log.d(DEBUG_TAG, "Doubling distance now!"); | ||||
| return@observe // THIS WORKS AS AN `else` | return@observe // THIS WORKS AS AN `else` | ||||
| } | } | ||||
| if (!stops.isEmpty()) { | if (!stops.isEmpty()) { | ||||
| currentNearbyStops = stops | currentNearbyStops = stops | ||||
| setShowingStatus(LocationShowingStatus.POSITION_FOUND) | |||||
| showStopsInViews(currentNearbyStops, lastPosition) | showStopsInViews(currentNearbyStops, lastPosition) | ||||
| } | } | ||||
| } | } | ||||
| viewModel.downloadingArrivals.observe(viewLifecycleOwner){ running -> | viewModel.downloadingArrivals.observe(viewLifecycleOwner){ running -> | ||||
| if(!running) flatProgressBar.visibility = View.GONE | if(!running) flatProgressBar.visibility = View.GONE | ||||
| else flatProgressBar.visibility = View.VISIBLE | else flatProgressBar.visibility = View.VISIBLE | ||||
| } | } | ||||
| viewModel.progressPerc.observe(viewLifecycleOwner){ progress -> | viewModel.progressPerc.observe(viewLifecycleOwner){ progress -> | ||||
| flatProgressBar.isIndeterminate = false | flatProgressBar.isIndeterminate = false | ||||
| flatProgressBar.progress = progress | flatProgressBar.progress = progress | ||||
| flatProgressBar.max = 100 | flatProgressBar.max = 100 | ||||
| if (progress<100){ | if (progress<100){ | ||||
| flatProgressBar.visibility = View.VISIBLE | flatProgressBar.visibility = View.VISIBLE | ||||
| } | } | ||||
| } | } | ||||
| 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(); | ||||
| setShowingStatus(LocationShowingStatus.POSITION_FOUND) | |||||
| 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 All 35 Lines | private fun updateLocationViewModel(location: Location, accuracy: Float = 150f) { | ||||
| lastPosition = GPSPoint(location.getLatitude(), location.getLongitude()) | lastPosition = GPSPoint(location.getLatitude(), location.getLongitude()) | ||||
| //viewModel.requestStopsAtDistance(location.getLatitude(), location.getLongitude(), distance, true); | //viewModel.requestStopsAtDistance(location.getLatitude(), location.getLongitude(), distance, true); | ||||
| viewModel.setLastLocation(location) | viewModel.setLastLocation(location) | ||||
| } | } | ||||
| } | } | ||||
| private fun setShowingStatus(newStatus: LocationShowingStatus) { | private fun setShowingStatus(newStatus: LocationShowingStatus) { | ||||
| var newStatus = newStatus | var newStatus = newStatus | ||||
| if (BuildConfig.DEBUG) Log.d(DEBUG_TAG, "Asked to set showing status : $newStatus") | |||||
| if (newStatus == showingStatus) { | if (newStatus == showingStatus) { | ||||
| return | return | ||||
| } | } | ||||
| if (BuildConfig.DEBUG) Log.d(DEBUG_TAG, "Changing showing status from $showingStatus to $newStatus") | |||||
| if (!isLocationEnabled && newStatus != LocationShowingStatus.NO_PERMISSION) { | if (!isLocationEnabled && newStatus != LocationShowingStatus.NO_PERMISSION) { | ||||
| Log.d(DEBUG_TAG, "asked to show status: $newStatus but the position is disabled") | Log.d(DEBUG_TAG, "asked to show status: $newStatus but the position is disabled") | ||||
| newStatus = LocationShowingStatus.DISABLED | newStatus = LocationShowingStatus.DISABLED | ||||
| } | } | ||||
| when (newStatus) { | when (newStatus) { | ||||
| LocationShowingStatus.FIRST_FIX -> { | LocationShowingStatus.POSITION_FOUND -> { | ||||
| 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 56 Lines • ▼ Show 20 Lines | class NearbyStopsFragment : ScreenBaseFragment() { | ||||
| /** | /** | ||||
| * Prepare the views for the set fragment type | * Prepare the views for the set fragment type | ||||
| */ | */ | ||||
| private fun prepareForFragmentType() { | private fun prepareForFragmentType() { | ||||
| if (fragment_type == FragType.STOPS) { | if (fragment_type == FragType.STOPS) { | ||||
| switchButton!!.text = getString(R.string.show_arrivals) | switchButton!!.text = getString(R.string.show_arrivals) | ||||
| titleTextView!!.text = getString(R.string.nearby_stops_message) | titleTextView!!.text = getString(R.string.nearby_stops_message) | ||||
| dataAdapter?.let{ gridRecyclerView.adapter = it} | dataAdapter?.let{ gridRecyclerView.adapter = it | ||||
| } | |||||
| 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 | |||||