Bimba.git

commit 825ae7c9bb82f43bf399d27c886d8d6ad8ee23c2

Author: Adam Evyčędo <git@apiote.xyz>

prepare terminus arrivals for elizabeth

%!v(PANIC=String method: strings: negative Repeat count)


diff --git a/app/src/main/java/xyz/apiote/bimba/czwek/departures/Departures.kt b/app/src/main/java/xyz/apiote/bimba/czwek/departures/Departures.kt
index d30151cc7c7eecd37d11647c6c3ec2b41087d518..5f50f5add8aba3a517babe6d39eb0166eef25257 100644
--- a/app/src/main/java/xyz/apiote/bimba/czwek/departures/Departures.kt
+++ b/app/src/main/java/xyz/apiote/bimba/czwek/departures/Departures.kt
@@ -19,6 +19,7 @@ import android.widget.ImageView
 import android.widget.LinearLayout
 import android.widget.TextView
 import androidx.appcompat.widget.TooltipCompat
+import androidx.preference.PreferenceManager
 import androidx.recyclerview.widget.DiffUtil
 import androidx.recyclerview.widget.RecyclerView
 import androidx.recyclerview.widget.RecyclerView.ViewHolder
@@ -59,7 +60,8 @@ 			departure: Departure,
 			holder: BimbaDepartureViewHolder?,
 			context: Context?,
 			showAsTime: Boolean,
-			onClickListener: (Departure) -> Unit
+			onClickListener: (Departure) -> Unit,
+			showingTerminusArrivals: String
 		) {
 			holder?.root?.setOnClickListener {
 				onClickListener(departure)
@@ -74,31 +76,52 @@ 				context?.getString(
 					R.string.departure_headsign_content_description,
 					departure.vehicle.Headsign
 				)
+
 			when {
 				departure.isRealtime -> {
 					holder?.timeStatus?.setImageResource(R.drawable.radar)
-					holder?.timeStatus?.contentDescription = context?.getString(R.string.realtime_content_description)
-					holder?.timeStatus?.let { TooltipCompat.setTooltipText(it, context?.getString(R.string.realtime_content_description)) }
+					holder?.timeStatus?.contentDescription =
+						context?.getString(R.string.realtime_content_description)
+					holder?.timeStatus?.let {
+						TooltipCompat.setTooltipText(
+							it,
+							context?.getString(R.string.realtime_content_description)
+						)
+					}
 				}
+
 				departure.exact -> {
 					holder?.timeStatus?.setImageResource(R.drawable.calendar)
-					holder?.timeStatus?.contentDescription = context?.getString(R.string.exact_content_description)
-					holder?.timeStatus?.let { TooltipCompat.setTooltipText(it, context?.getString(R.string.exact_content_description)) }
+					holder?.timeStatus?.contentDescription =
+						context?.getString(R.string.exact_content_description)
+					holder?.timeStatus?.let {
+						TooltipCompat.setTooltipText(
+							it,
+							context?.getString(R.string.exact_content_description)
+						)
+					}
 				}
+
 				else -> {
 					holder?.timeStatus?.setImageResource(R.drawable.inexact)
-					holder?.timeStatus?.contentDescription = context?.getString(R.string.inexact_content_description)
-					holder?.timeStatus?.let { TooltipCompat.setTooltipText(it, context?.getString(R.string.inexact_content_description)) }
+					holder?.timeStatus?.contentDescription =
+						context?.getString(R.string.inexact_content_description)
+					holder?.timeStatus?.let {
+						TooltipCompat.setTooltipText(
+							it,
+							context?.getString(R.string.inexact_content_description)
+						)
+					}
 				}
 			}
 
 			holder?.departureTime?.text = departure.statusText(context, showAsTime)
-			if (departure.terminusArrival) {
-				holder?.departureTime?.alpha = .5f
-				holder?.lineIcon?.alpha = .5f
-				holder?.lineName?.alpha = .5f
-				holder?.headsign?.alpha = .5f
-			}
+			holder?.root?.alpha =
+				if (departure.terminusArrival && showingTerminusArrivals == BimbaDeparturesAdapter.TERMINUS_ARRIVAL_GREY_OUT) {
+					.5f
+				} else {
+					1f
+				}
 		}
 	}
 }
@@ -137,13 +160,26 @@ class BimbaDeparturesAdapter(
 	private val inflater: LayoutInflater,
 	private val context: Context?,
 	private var items: List<DepartureItem>,
-	private val onClickListener: ((Departure) -> Unit)
+	private val onClickListener: ((Departure) -> Unit),
 ) :
 	RecyclerView.Adapter<ViewHolder>() {
 
-		companion object {
-			const val ALERT_ITEM_ID = "alert"
-		}
+	companion object {
+		const val ALERT_ITEM_ID = "alert"
+
+		// TODO to enum
+		const val TERMINUS_ARRIVAL_SHOWING_KEY = "terminus_arrival_showing"
+		const val TERMINUS_ARRIVAL_GREY_OUT = "grey_out"
+		const val TERMINUS_ARRIVAL_HIDE = "hide"
+		const val TERMINUS_ARRIVAL_SHOW = "show"
+	}
+
+	var showingTerminusArrivals: String = context?.let {
+		PreferenceManager.getDefaultSharedPreferences(
+			it
+		).getString(TERMINUS_ARRIVAL_SHOWING_KEY, TERMINUS_ARRIVAL_GREY_OUT)
+	}
+	?: TERMINUS_ARRIVAL_GREY_OUT
 
 	var lastUpdate: ZonedDateTime =
 		ZonedDateTime.of(0, 1, 1, 0, 0, 0, 0, ZoneId.systemDefault())
@@ -167,6 +203,7 @@ 		override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
 			val oldDeparture = oldDepartures[oldItemPosition]
 			val newDeparture = newDepartures[newItemPosition]
 			return if (oldDeparture.departure != null && newDeparture.departure != null) {
+				!oldDeparture.departure.terminusArrival &&
 				oldDeparture.departure.terminusArrival == newDeparture.departure.terminusArrival &&
 					oldDeparture.departure.exact == newDeparture.departure.exact &&
 					oldDeparture.departure.vehicle.Line == newDeparture.departure.vehicle.Line &&
@@ -217,7 +254,8 @@ 				items[position].departure!!,
 				holder,
 				context,
 				showAsTime,
-				onClickListener
+				onClickListener,
+				showingTerminusArrivals
 			)
 		} else {
 			BimbaAlertViewHolder.bind(items[position].alert, holder as BimbaAlertViewHolder, context)




diff --git a/app/src/main/java/xyz/apiote/bimba/czwek/departures/DeparturesActivity.kt b/app/src/main/java/xyz/apiote/bimba/czwek/departures/DeparturesActivity.kt
index 94cd3b2272bb5374c418abd4d4eafbdac616328d..9289608fb3ffc39f739ab26b00997066d9781189 100644
--- a/app/src/main/java/xyz/apiote/bimba/czwek/departures/DeparturesActivity.kt
+++ b/app/src/main/java/xyz/apiote/bimba/czwek/departures/DeparturesActivity.kt
@@ -27,6 +27,7 @@ import androidx.core.view.WindowCompat
 import androidx.core.view.WindowInsetsCompat
 import androidx.core.view.updatePadding
 import androidx.lifecycle.ViewModelProvider
+import androidx.preference.PreferenceManager
 import androidx.recyclerview.widget.LinearLayoutManager
 import androidx.recyclerview.widget.RecyclerView
 import com.google.android.material.datepicker.MaterialDatePicker
@@ -41,6 +42,9 @@ import xyz.apiote.bimba.czwek.R
 import xyz.apiote.bimba.czwek.api.Error
 import xyz.apiote.bimba.czwek.api.Server
 import xyz.apiote.bimba.czwek.databinding.ActivityDeparturesBinding
+import xyz.apiote.bimba.czwek.departures.BimbaDeparturesAdapter.Companion.TERMINUS_ARRIVAL_GREY_OUT
+import xyz.apiote.bimba.czwek.departures.BimbaDeparturesAdapter.Companion.TERMINUS_ARRIVAL_HIDE
+import xyz.apiote.bimba.czwek.departures.BimbaDeparturesAdapter.Companion.TERMINUS_ARRIVAL_SHOWING_KEY
 import xyz.apiote.bimba.czwek.repo.DepartureItem
 import xyz.apiote.bimba.czwek.repo.Favourite
 import xyz.apiote.bimba.czwek.repo.OfflineRepository
@@ -68,6 +72,7 @@ 			putExtra(CODE_PARAM, code)
 			putExtra(NAME_PARAM, name)
 			putExtra(FEED_PARAM, feedID)
 		}
+
 		fun getIntent(
 			context: Context,
 			code: String,
@@ -104,7 +109,7 @@ 	// TODO [elizabeth] millisInFuture from header Cache-Control max-age
 	private val countdown =
 		object : CountDownTimer(Second(30).milliseconds(), Tim(1).milliseconds()) {
 			override fun onTick(millisUntilFinished: Long) {
-				val timsUntillFinished = Tim(Second(millisUntilFinished.toDouble()/1000))
+				val timsUntillFinished = Tim(Second(millisUntilFinished.toDouble() / 1000))
 				binding.departuresUpdatesProgress.progress = timsUntillFinished.tims.toInt()
 			}
 
@@ -133,6 +138,9 @@ 			windowInsets
 		}
 
 		viewModel = ViewModelProvider(this)[DeparturesViewModel::class.java]
+		viewModel.showingTerminusArrivals = PreferenceManager.getDefaultSharedPreferences(this)
+			.getString(TERMINUS_ARRIVAL_SHOWING_KEY, TERMINUS_ARRIVAL_GREY_OUT)
+			?: TERMINUS_ARRIVAL_GREY_OUT
 
 		getLine()?.let {
 			viewModel.mutableLinesFilter.value = mapOf(Pair(it, true))
@@ -164,6 +172,9 @@ 				.filter { d ->
 					it.values.all { !it } or (it[d.vehicle.Line.name] ?: false)
 				}
 				.filter { d ->
+					viewModel.showingTerminusArrivals != TERMINUS_ARRIVAL_HIDE || !d.terminusArrival
+				}
+				.filter { d ->
 					val t = LocalTime.of(d.time.Hour.toInt(), d.time.Minute.toInt())
 					t >= viewModel.startTime && t <= viewModel.endTime
 				}.map { DepartureItem(it) },
@@ -182,6 +193,9 @@ 				.filter { d ->
 					viewModel.linesFilter.value?.let { filter ->
 						filter.values.all { !it } or (filter[d.vehicle.Line.name] ?: false)
 					} ?: true
+				}
+				.filter { d ->
+					viewModel.showingTerminusArrivals != TERMINUS_ARRIVAL_HIDE || !d.terminusArrival
 				}
 				.filter { d ->
 					val t = LocalTime.of(d.time.Hour.toInt(), d.time.Minute.toInt())
@@ -325,11 +339,39 @@ 					timePickerStart!!.show(supportFragmentManager, "timePickerStart")
 					true
 				}
 
+				/* TODO elizabeth
+				R.id.terminus_arrival_showing -> {
+					val options = arrayOf(
+						TERMINUS_ARRIVAL_GREY_OUT,
+						TERMINUS_ARRIVAL_HIDE,
+						BimbaDeparturesAdapter.TERMINUS_ARRIVAL_SHOW
+					)
+					var selected = viewModel.showingTerminusArrivals!!
+					MaterialAlertDialogBuilder(this)
+						.setTitle(R.string.terminus_arrival_showing)
+						.setIcon(R.drawable.terminus)
+						.setSingleChoiceItems(
+							options,
+							options.indexOf(viewModel.showingTerminusArrivals)
+						) { _, i ->
+							selected = options[i]
+						}
+						.setPositiveButton(R.string.ok) { _, _ ->
+							viewModel.showingTerminusArrivals = selected
+							adapter.showingTerminusArrivals = selected
+							getDepartures()
+						}
+						.setNegativeButton(R.string.cancel) { _, _ -> }
+						.show()
+					true
+				}*/
+
 				else -> super.onOptionsItemSelected(it)
 			}
 		}
 
 		binding.departuresRecycler.layoutManager = LinearLayoutManager(this)
+		binding.departuresRecycler.itemAnimator = null
 		binding.departuresRecycler.addOnScrollListener(
 			object : RecyclerView.OnScrollListener() {
 				override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
@@ -352,6 +394,7 @@ 				viewModel.openBottomSheet = this
 				setOnCancel { viewModel.openBottomSheet = null }
 			}
 		}
+		adapter.showingTerminusArrivals = viewModel.showingTerminusArrivals!!
 		binding.departuresRecycler.adapter = adapter
 		WindowCompat.setDecorFitsSystemWindows(window, false)
 




diff --git a/app/src/main/java/xyz/apiote/bimba/czwek/departures/DeparturesViewModel.kt b/app/src/main/java/xyz/apiote/bimba/czwek/departures/DeparturesViewModel.kt
index c8ba0c68769ae2a9a4c561df995acbe21e8524ea..bd61194560e6a232a5754bbf33fa60b3ed03dc6f 100644
--- a/app/src/main/java/xyz/apiote/bimba/czwek/departures/DeparturesViewModel.kt
+++ b/app/src/main/java/xyz/apiote/bimba/czwek/departures/DeparturesViewModel.kt
@@ -37,6 +37,7 @@ 	var openBottomSheet: DepartureBottomSheet? = null
 	private lateinit var code: String
 	val mutableLinesFilter = MutableLiveData<Map<String, Boolean>>()
 	val linesFilter: LiveData<Map<String, Boolean>> = mutableLinesFilter
+	var showingTerminusArrivals: String? = null
 
 	// TODO observe in activity, maybe refreshing and not getting departures is enough
 	var startTime: LocalTime = LocalTime.MIN




diff --git a/app/src/main/res/layout/alert.xml b/app/src/main/res/layout/alert.xml
index 643fd32e34735a592742d869500910aa13314b87..56819d0fd9f418fd6c0e896db130906251530f78 100644
--- a/app/src/main/res/layout/alert.xml
+++ b/app/src/main/res/layout/alert.xml
@@ -34,7 +34,7 @@ 			android:layout_marginStart="8dp"
 			android:layout_marginTop="8dp"
 			android:layout_marginEnd="8dp"
 			android:ellipsize="end"
-			android:maxLines="2"
+			android:maxLines="3"
 			android:textColor="@color/black"
 			app:layout_constraintBottom_toBottomOf="parent"
 			app:layout_constraintEnd_toStartOf="@+id/more_button"




diff --git a/app/src/main/res/menu/departures_menu.xml b/app/src/main/res/menu/departures_menu.xml
index 0c3f27a935ef2b0bca568758f44f8d641d5d3c6c..9aef4cf6ed99cb06a663eeb16dedd94e4145a664 100644
--- a/app/src/main/res/menu/departures_menu.xml
+++ b/app/src/main/res/menu/departures_menu.xml
@@ -39,4 +39,12 @@ 		android:icon="@drawable/calendar"
 		app:showAsAction="ifRoom"
 		android:contentDescription="@string/title_select_date"
 		android:title="@string/title_select_date"/>
+	<!-- TODO elizabeth
+	<item
+		android:id="@+id/terminus_arrival_showing"
+		android:icon="@drawable/terminus"
+		app:showAsAction="ifRoom"
+		android:contentDescription="@string/terminus_arrival_showing"
+		android:title="@string/terminus_arrival_showing"
+		/>-->
 </menu>




diff --git a/app/src/main/res/values/arrays.xml b/app/src/main/res/values/arrays.xml
index 7cdbd4ec2b08693dbea5f88f9c457baea7dae35e..b5fe8239e1efd38779f8e15c8e111ad1f7177b01 100644
--- a/app/src/main/res/values/arrays.xml
+++ b/app/src/main/res/values/arrays.xml
@@ -58,4 +58,16 @@ 		italics
 		<item>colour</item>
 		<item>none</item>
 	</string-array>
+
+	<string-array name="terminus_arrival_showing">
+		<item>@string/grey_out</item>
+		<item>@string/hide</item>
+		<item>@string/show</item>
+	</string-array>
+
+	<string-array name="terminus_arrival_showing_values">
+		<item>grey_out</item>
+		<item>hide</item>
+		<item>show</item>
+	</string-array>
 </resources>
\ No newline at end of file




diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml
index d603ba7d948feef9f2abd3c68ed8d1427f69df28..4a02e80fb39e223e4d0a64e7b9e17f4b0640ecf8 100644
--- a/app/src/main/res/values/strings.xml
+++ b/app/src/main/res/values/strings.xml
@@ -278,4 +278,8 @@ 	Send with commend
 	<string name="acra_notification_comment">Comment added to crash report</string>
 	<string name="filtered_departures">Filtered departures</string>
 	<string name="alerts">Alerts</string>
+	<string name="grey_out">grey out</string>
+	<string name="hide">hide</string>
+	<string name="show">show</string>
+	<string name="terminus_arrival_showing">Terminus arrivals</string>
 </resources>




diff --git a/app/src/main/res/xml/root_preferences.xml b/app/src/main/res/xml/root_preferences.xml
index 4d40eb2a6f5b696bc91d93df8492454c72f5f80b..2127ae46dc0a24e21c53e4cc6fcd662fe492916e 100644
--- a/app/src/main/res/xml/root_preferences.xml
+++ b/app/src/main/res/xml/root_preferences.xml
@@ -32,5 +32,15 @@ 			app:key="line_decoration"
 			app:title="@string/line_decorations"
 			app:useSimpleSummaryProvider="true"
 			/>
+		<!-- TODO elizabeth
+		<ListPreference
+			app:defaultValue="grey_out"
+			app:entries="@array/terminus_arrival_showing"
+			app:entryValues="@array/terminus_arrival_showing_values"
+			app:icon="@drawable/terminus"
+			app:key="terminus_arrival_showing"
+			app:title="@string/terminus_arrival_showing"
+			app:useSimpleSummaryProvider="true"
+			/>-->
 	</PreferenceCategory>
 </PreferenceScreen>
\ No newline at end of file