Changeset View
Changeset View
Standalone View
Standalone View
app/src/main/java/it/reyboz/bustorino/fragments/ArrivalsFragment.kt
| Show All 19 Lines | |||||
| import android.content.Context | import android.content.Context | ||||
| import android.database.Cursor | import android.database.Cursor | ||||
| 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.* | import android.widget.* | ||||
| import androidx.cardview.widget.CardView | |||||
| import androidx.fragment.app.activityViewModels | |||||
| import androidx.fragment.app.viewModels | import androidx.fragment.app.viewModels | ||||
| import androidx.loader.app.LoaderManager | import androidx.loader.app.LoaderManager | ||||
| import androidx.loader.content.CursorLoader | import androidx.loader.content.CursorLoader | ||||
| import androidx.loader.content.Loader | import androidx.loader.content.Loader | ||||
| import androidx.recyclerview.widget.DividerItemDecoration | import androidx.recyclerview.widget.DividerItemDecoration | ||||
| import androidx.recyclerview.widget.GridLayoutManager | import androidx.recyclerview.widget.GridLayoutManager | ||||
| import androidx.recyclerview.widget.GridLayoutManager.SpanSizeLookup | import androidx.recyclerview.widget.GridLayoutManager.SpanSizeLookup | ||||
| import androidx.recyclerview.widget.LinearLayoutManager | import androidx.recyclerview.widget.LinearLayoutManager | ||||
| import androidx.recyclerview.widget.RecyclerView | import androidx.recyclerview.widget.RecyclerView | ||||
| import it.reyboz.bustorino.R | import it.reyboz.bustorino.R | ||||
| import it.reyboz.bustorino.adapters.PalinaAdapter | import it.reyboz.bustorino.adapters.PalinaAdapter | ||||
| import it.reyboz.bustorino.adapters.PalinaAdapter.PalinaClickListener | import it.reyboz.bustorino.adapters.PalinaAdapter.PalinaClickListener | ||||
| import it.reyboz.bustorino.adapters.RouteAdapter | import it.reyboz.bustorino.adapters.RouteAdapter | ||||
| import it.reyboz.bustorino.adapters.RouteOnlyLineAdapter | import it.reyboz.bustorino.adapters.RouteOnlyLineAdapter | ||||
| import it.reyboz.bustorino.backend.* | import it.reyboz.bustorino.backend.* | ||||
| import it.reyboz.bustorino.backend.DBStatusManager.OnDBUpdateStatusChangeListener | import it.reyboz.bustorino.backend.DBStatusManager.OnDBUpdateStatusChangeListener | ||||
| import it.reyboz.bustorino.backend.Passaggio.Source | import it.reyboz.bustorino.backend.Passaggio.Source | ||||
| import it.reyboz.bustorino.data.AppDataProvider | import it.reyboz.bustorino.data.AppDataProvider | ||||
| import it.reyboz.bustorino.data.NextGenDB | import it.reyboz.bustorino.data.NextGenDB | ||||
| import it.reyboz.bustorino.data.UserDB | import it.reyboz.bustorino.data.UserDB | ||||
| import it.reyboz.bustorino.middleware.CoroutineFavoriteAction | import it.reyboz.bustorino.middleware.CoroutineFavoriteAction | ||||
| import it.reyboz.bustorino.util.LinesNameSorter | import it.reyboz.bustorino.util.LinesNameSorter | ||||
| import it.reyboz.bustorino.viewmodels.ArrivalsViewModel | import it.reyboz.bustorino.viewmodels.ArrivalsViewModel | ||||
| import it.reyboz.bustorino.viewmodels.ServiceAlertsViewModel | |||||
| import java.util.* | import java.util.* | ||||
| class ArrivalsFragment : ResultBaseFragment(), LoaderManager.LoaderCallbacks<Cursor> { | class ArrivalsFragment : ResultBaseFragment(), LoaderManager.LoaderCallbacks<Cursor> { | ||||
| private var DEBUG_TAG = DEBUG_TAG_ALL | private var DEBUG_TAG = DEBUG_TAG_ALL | ||||
| private lateinit var stopID: String | private lateinit var stopID: String | ||||
| //private set | //private set | ||||
| private var stopName: String? = null | private var stopName: String? = null | ||||
| Show All 15 Lines | class ArrivalsFragment : ResultBaseFragment(), LoaderManager.LoaderCallbacks<Cursor> { | ||||
| private lateinit var resultsLayout : LinearLayout | private lateinit var resultsLayout : LinearLayout | ||||
| private lateinit var loadingMessageTextView: TextView | private lateinit var loadingMessageTextView: TextView | ||||
| private lateinit var progressBar: ProgressBar | private lateinit var progressBar: ProgressBar | ||||
| private lateinit var howDoesItWorkTextView: TextView | private lateinit var howDoesItWorkTextView: TextView | ||||
| private lateinit var hideHintButton: Button | private lateinit var hideHintButton: Button | ||||
| private lateinit var alertsCardView : CardView | |||||
| //private NestedScrollView theScrollView; | //private NestedScrollView theScrollView; | ||||
| protected lateinit var noArrivalsRecyclerView: RecyclerView | protected lateinit var noArrivalsRecyclerView: RecyclerView | ||||
| private var noArrivalsAdapter: RouteOnlyLineAdapter? = null | private var noArrivalsAdapter: RouteOnlyLineAdapter? = null | ||||
| private var noArrivalsTitleView: TextView? = null | private var noArrivalsTitleView: TextView? = null | ||||
| private var layoutManager: GridLayoutManager? = null | private var layoutManager: GridLayoutManager? = null | ||||
| //private View canaryEndView; | //private View canaryEndView; | ||||
| private var fetchers: List<ArrivalsFetcher?> = ArrayList() | private var fetchers: List<ArrivalsFetcher?> = ArrayList() | ||||
| private val arrivalsViewModel : ArrivalsViewModel by viewModels() | private val arrivalsViewModel : ArrivalsViewModel by viewModels() | ||||
| private val alertsViewModel: ServiceAlertsViewModel by activityViewModels() | |||||
| private var reloadOnResume = true | private var reloadOnResume = true | ||||
| private var routesNoPassages = listOf<Route>() | private var routesNoPassages = listOf<Route>() | ||||
| fun getStopID() = stopID | fun getStopID() = stopID | ||||
| private val palinaClickListener: PalinaClickListener = object : PalinaClickListener { | private val palinaClickListener: PalinaClickListener = object : PalinaClickListener { | ||||
| override fun showRouteFullDirection(route: Route) { | override fun showRouteFullDirection(route: Route) { | ||||
| var routeName = route.routeLongDisplayName | var routeName = route.routeLongDisplayName | ||||
| ▲ Show 20 Lines • Show All 73 Lines • ▼ Show 20 Lines | ): View? { | ||||
| openInMapButton = root.findViewById(R.id.openInMapButton) | openInMapButton = root.findViewById(R.id.openInMapButton) | ||||
| // "How does it work part" | // "How does it work part" | ||||
| howDoesItWorkTextView = root.findViewById(R.id.howDoesItWorkTextView) | howDoesItWorkTextView = root.findViewById(R.id.howDoesItWorkTextView) | ||||
| hideHintButton = root.findViewById(R.id.hideHintButton) | hideHintButton = root.findViewById(R.id.hideHintButton) | ||||
| //TODO: Hide this layout at the beginning, show it later | //TODO: Hide this layout at the beginning, show it later | ||||
| resultsLayout = root.findViewById(R.id.resultsLayout) | resultsLayout = root.findViewById(R.id.resultsLayout) | ||||
| loadingMessageTextView = root.findViewById(R.id.loadingMessageTextView) | loadingMessageTextView = root.findViewById(R.id.loadingMessageTextView) | ||||
| progressBar = root.findViewById(R.id.circularProgressBar) | progressBar = root.findViewById(R.id.circularProgressBar) | ||||
| alertsCardView = root.findViewById(R.id.alertsCardView) | |||||
| hideHintButton.setOnClickListener { v: View? -> this.onHideHint(v) } | hideHintButton.setOnClickListener { v: View? -> this.onHideHint(v) } | ||||
| //theScrollView = root.findViewById(R.id.arrivalsScrollView); | //theScrollView = root.findViewById(R.id.arrivalsScrollView); | ||||
| // recyclerview holding the arrival times | // recyclerview holding the arrival times | ||||
| arrivalsRecyclerView = root.findViewById(R.id.arrivalsRecyclerView) | arrivalsRecyclerView = root.findViewById(R.id.arrivalsRecyclerView) | ||||
| val manager = LinearLayoutManager(context) | val manager = LinearLayoutManager(context) | ||||
| arrivalsRecyclerView.setLayoutManager(manager) | arrivalsRecyclerView.setLayoutManager(manager) | ||||
| ▲ Show 20 Lines • Show All 140 Lines • ▼ Show 20 Lines | override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | ||||
| Fetcher.Result.NOT_FOUND -> showFetcherMessage(R.string.no_bus_stop_have_this_name, src) | Fetcher.Result.NOT_FOUND -> showFetcherMessage(R.string.no_bus_stop_have_this_name, src) | ||||
| else -> showFetcherMessage(R.string.internal_error, src) | else -> showFetcherMessage(R.string.internal_error, src) | ||||
| } | } | ||||
| } | } | ||||
| arrivalsViewModel.stopInFavorites.observe(viewLifecycleOwner, { isFavorite -> | arrivalsViewModel.stopInFavorites.observe(viewLifecycleOwner, { isFavorite -> | ||||
| updateStarIcon(isFavorite) | updateStarIcon(isFavorite) | ||||
| }) | }) | ||||
| alertsViewModel.alertsByStopLiveData.observe(viewLifecycleOwner) { alerts -> | |||||
| if(alerts!=null && alerts.isNotEmpty()){ | |||||
| alertsCardView.visibility = View.VISIBLE | |||||
| alertsCardView.setOnClickListener { | |||||
| AlertsDialogFragment.newInstanceForStop(stopID).show(parentFragmentManager, "AlertsDialogStop$stopID") | |||||
| } | |||||
| } else{ | |||||
| alertsCardView.visibility = View.GONE | |||||
| } | |||||
| } | |||||
| } | } | ||||
| private fun showShortToast(id: Int) = showToastMessage(id,true) | private fun showShortToast(id: Int) = showToastMessage(id,true) | ||||
| private fun showFetcherMessage(id: Int, source: Source?){ | private fun showFetcherMessage(id: Int, source: Source?){ | ||||
| val srcString = source?.let{ getDisplayArrivalsSource(it,requireContext())} | val srcString = source?.let{ getDisplayArrivalsSource(it,requireContext())} | ||||
| if (srcString!=null){ | if (srcString!=null){ | ||||
| Toast.makeText(requireContext(), id, Toast.LENGTH_SHORT).show() | Toast.makeText(requireContext(), id, Toast.LENGTH_SHORT).show() | ||||
| ▲ Show 20 Lines • Show All 81 Lines • ▼ Show 20 Lines | class ArrivalsFragment : ResultBaseFragment(), LoaderManager.LoaderCallbacks<Cursor> { | ||||
| } | } | ||||
| override fun onStart() { | override fun onStart() { | ||||
| super.onStart() | super.onStart() | ||||
| if (needUpdateOnAttach) { | if (needUpdateOnAttach) { | ||||
| updateFragmentData(null) | updateFragmentData(null) | ||||
| needUpdateOnAttach = false | needUpdateOnAttach = false | ||||
| } else{ | |||||
| updateFragmentData(lastUpdatedPalina ?: Palina(stopID)) | |||||
| } | } | ||||
| } | } | ||||
| override fun onPause() { | override fun onPause() { | ||||
| if (listener != null) prefs!!.unregisterListener() | if (listener != null) prefs!!.unregisterListener() | ||||
| super.onPause() | super.onPause() | ||||
| val loaderManager = loaderManager | val loaderManager = loaderManager | ||||
| Log.d(DEBUG_TAG, "onPause, have running loaders: " + loaderManager.hasRunningLoaders()) | Log.d(DEBUG_TAG, "onPause, have running loaders: " + loaderManager.hasRunningLoaders()) | ||||
| ▲ Show 20 Lines • Show All 46 Lines • ▼ Show 20 Lines | class ArrivalsFragment : ResultBaseFragment(), LoaderManager.LoaderCallbacks<Cursor> { | ||||
| } | } | ||||
| /** | /** | ||||
| * Update the UI with the new data | * Update the UI with the new data | ||||
| * @param p the full Palina | * @param p the full Palina | ||||
| */ | */ | ||||
| fun updateFragmentData(p: Palina?) { | fun updateFragmentData(p: Palina?) { | ||||
| if (p != null) lastUpdatedPalina = p | if (p != null) { | ||||
| lastUpdatedPalina = p | |||||
| //set the gtfsID for the alerts | |||||
| if(isAdded){ | |||||
| //alertsViewModel.setStopFilter(p) | |||||
| } else{ | |||||
| Log.w(DEBUG_TAG, "Cannot filter alerts for palina $p, the fragment is not added") | |||||
| } | |||||
| } | |||||
| if (!isAdded) { | if (!isAdded) { | ||||
| //defer update at next show | //defer update at next show | ||||
| if (p == null) Log.w(DEBUG_TAG, "Asked to update the data, but we're not attached and the data is null") | if (p == null) Log.w(DEBUG_TAG, "Asked to update the data, but we're not attached and the data is null") | ||||
| else needUpdateOnAttach = true | else needUpdateOnAttach = true | ||||
| } else { | } else { | ||||
| //set title | //set title | ||||
| if(stopName==null && p?.stopDisplayName != null){ | if(stopName==null && p?.stopDisplayName != null){ | ||||
| ▲ Show 20 Lines • Show All 353 Lines • Show Last 20 Lines | |||||
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