Changeset View
Changeset View
Standalone View
Standalone View
app/src/main/java/it/reyboz/bustorino/fragments/AlertsDialogFragment.kt
| Show All 38 Lines | |||||
| import it.reyboz.bustorino.data.gtfs.AlertWithDetails | import it.reyboz.bustorino.data.gtfs.AlertWithDetails | ||||
| import it.reyboz.bustorino.data.gtfs.GtfsAlertsTranslation | import it.reyboz.bustorino.data.gtfs.GtfsAlertsTranslation | ||||
| import it.reyboz.bustorino.viewmodels.ServiceAlertsViewModel | import it.reyboz.bustorino.viewmodels.ServiceAlertsViewModel | ||||
| import java.util.Locale | import java.util.Locale | ||||
| import kotlin.getValue | import kotlin.getValue | ||||
| import kotlin.collections.HashMap | import kotlin.collections.HashMap | ||||
| class AlertsDialogFragment(private val gtfsLineShow: String) : DialogFragment() { | class AlertsDialogFragment(private val gtfsLineShow: String, private val stopToShow: String) : DialogFragment() { | ||||
| private lateinit var titleTextView: TextView | private lateinit var titleTextView: TextView | ||||
| private lateinit var messageTextView: TextView | private lateinit var messageTextView: TextView | ||||
| private lateinit var statusCardView: CardView | private lateinit var statusCardView: CardView | ||||
| private lateinit var recyclerView: RecyclerView | private lateinit var recyclerView: RecyclerView | ||||
| private val alertsViewModel: ServiceAlertsViewModel by activityViewModels() | private val alertsViewModel: ServiceAlertsViewModel by activityViewModels() | ||||
| override fun onCreate(savedInstanceState: Bundle?) { | override fun onCreate(savedInstanceState: Bundle?) { | ||||
| super.onCreate(savedInstanceState) | super.onCreate(savedInstanceState) | ||||
| Log.d(DEBUG_TAG, "created DialogFragment for line ${gtfsLineShow}") | Log.d(DEBUG_TAG, "created DialogFragment for line ${gtfsLineShow} and/or stop ${stopToShow}") | ||||
| } | } | ||||
| 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 | ||||
| val root = inflater.inflate(R.layout.fragment_dialog_alerts_line, container, false) | val root = inflater.inflate(R.layout.fragment_dialog_alerts_line, container, false) | ||||
| titleTextView = root.findViewById<TextView>(R.id.titleTextView) | titleTextView = root.findViewById<TextView>(R.id.titleTextView) | ||||
| titleTextView.setText(getString(R.string.alert_line_fill,GtfsUtils.lineNameDisplayFromGtfsID(gtfsLineShow))) | val text = if (gtfsLineShow.isNotEmpty()) | ||||
| getString(R.string.alert_line_fill,GtfsUtils.lineNameDisplayFromGtfsID(gtfsLineShow)) | |||||
| else if(stopToShow.isNotEmpty()){ | |||||
| getString(R.string.alert_stop_fill,stopToShow) | |||||
| } else{ | |||||
| throw Exception("Either text or line has to be filled") | |||||
| } | |||||
| titleTextView.setText(text) | |||||
| recyclerView = root.findViewById(R.id.alertsRecyclerView) | recyclerView = root.findViewById(R.id.alertsRecyclerView) | ||||
| recyclerView.layoutManager = LinearLayoutManager(context, RecyclerView.VERTICAL, false) | recyclerView.layoutManager = LinearLayoutManager(context, RecyclerView.VERTICAL, false) | ||||
| messageTextView = root.findViewById(R.id.alertMessageTextView) | messageTextView = root.findViewById(R.id.alertMessageTextView) | ||||
| statusCardView = root.findViewById(R.id.statusCard) | statusCardView = root.findViewById(R.id.statusCard) | ||||
| if(gtfsLineShow.isNotEmpty()) | |||||
| alertsViewModel.alertsByRouteLiveData.observe(viewLifecycleOwner){ alerts -> | alertsViewModel.alertsByRouteLiveData.observe(viewLifecycleOwner){ alerts -> | ||||
| showAlerts(alerts) | showAlerts(alerts) | ||||
| } | } | ||||
| else if(stopToShow.isNotEmpty()){ | |||||
| alertsViewModel.alertsByStopLiveData.observe(viewLifecycleOwner){ alerts -> showAlerts(alerts) } | |||||
| } | |||||
| val btnClose = root.findViewById<ImageButton>(R.id.btnClose) | val btnClose = root.findViewById<ImageButton>(R.id.btnClose) | ||||
| btnClose.setOnClickListener { | btnClose.setOnClickListener { | ||||
| dismiss() | dismiss() | ||||
| } | } | ||||
| val btnRefresh = root.findViewById<ImageButton>(R.id.btnRefresh) | val btnRefresh = root.findViewById<ImageButton>(R.id.btnRefresh) | ||||
| btnRefresh.setOnClickListener { | btnRefresh.setOnClickListener { | ||||
| ▲ Show 20 Lines • Show All 60 Lines • ▼ Show 20 Lines | companion object { | ||||
| /** | /** | ||||
| * Use this factory method to create a new instance of | * Use this factory method to create a new instance of | ||||
| * this fragment using the provided parameters. | * this fragment using the provided parameters. | ||||
| * | * | ||||
| * @param gtfsLine Line To show. | * @param gtfsLine Line To show. | ||||
| * @return A new instance of fragment LineAlertsDialogFragment. | * @return A new instance of fragment LineAlertsDialogFragment. | ||||
| */ | */ | ||||
| @JvmStatic | @JvmStatic | ||||
| fun newInstance(gtfsLine: String) = | fun newInstanceForLine(gtfsLine: String) = | ||||
| AlertsDialogFragment(gtfsLine) | AlertsDialogFragment(gtfsLine, "") | ||||
| @JvmStatic | |||||
| fun newInstanceForStop(stop: String) = | |||||
| AlertsDialogFragment("", stop) | |||||
| private const val GTFS_LINE_ARG = "gtfsLine" | private const val GTFS_LINE_ARG = "gtfsLine" | ||||
| private const val DEBUG_TAG = "BusTO-AlertsDialog" | private const val DEBUG_TAG = "BusTO-AlertsDialog" | ||||
| } | } | ||||
| } | } | ||||
| 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