Modern Apps git
git clone https://git.maproj.org/Modern-Apps.git
cov/c3 ▾

Modern-Apps.git / maps / src / main / java / com / vayunmathur / maps / ui / TripSheet.kt

TripSheet.kt223 lines · 9.1 KiBRaw
1package com.vayunmathur.maps.ui
2
3import androidx.compose.foundation.clickable
4import androidx.compose.foundation.layout.Arrangement
5import androidx.compose.foundation.layout.Column
6import androidx.compose.foundation.layout.Row
7import androidx.compose.foundation.layout.Spacer
8import androidx.compose.foundation.layout.fillMaxWidth
9import androidx.compose.foundation.layout.height
10import androidx.compose.foundation.layout.heightIn
11import androidx.compose.foundation.layout.padding
12import androidx.compose.foundation.layout.width
13import androidx.compose.foundation.lazy.LazyColumn
14import androidx.compose.foundation.lazy.items
15import androidx.compose.foundation.lazy.rememberLazyListState
16import androidx.compose.runtime.Composable
17import androidx.compose.runtime.ReadOnlyComposable
18import androidx.compose.runtime.getValue
19import androidx.compose.runtime.mutableLongStateOf
20import androidx.compose.runtime.remember
21import androidx.compose.runtime.setValue
22import androidx.compose.ui.Alignment
23import androidx.compose.ui.Modifier
24import androidx.compose.ui.draw.alpha
25import androidx.compose.ui.graphics.Color
26import androidx.compose.ui.res.stringResource
27import androidx.compose.ui.text.font.FontWeight
28import androidx.compose.ui.text.style.TextDecoration
29import androidx.compose.ui.text.style.TextOverflow
30import androidx.compose.ui.unit.dp
31import com.vayunmathur.library.ui.EmptyState
32import com.vayunmathur.library.ui.ExperimentalMaterial3Api
33import com.vayunmathur.library.ui.HorizontalDivider
34import com.vayunmathur.library.ui.LoadingState
35import com.vayunmathur.library.ui.MaterialTheme
36import com.vayunmathur.library.ui.ModalBottomSheet
37import com.vayunmathur.library.ui.SheetValue
38import com.vayunmathur.library.ui.Text
39import com.vayunmathur.library.ui.rememberBottomSheetState
40import com.vayunmathur.maps.R
41import com.vayunmathur.maps.data.transit.TripItinerary
42import com.vayunmathur.maps.data.transit.TripStop
43import com.vayunmathur.maps.util.TripItineraryState
44
45/**
46 * Fixed height for the sheet's status states (loading / missing trip).
47 *
48 * Both [LoadingState] and [EmptyState] default to `fillMaxSize`, which inside a
49 * `ModalBottomSheet` measures the sheet at full height: the sheet opens
50 * fullscreen while fetching and then snaps down once the itinerary arrives. A
51 * bounded status block keeps the sheet at content height through the fetch.
52 */
53private val BoardStatusHeight = 160.dp
54
55/**
56 * A vehicle's full run: every station it was at or will be at, and when.
57 *
58 * Opened by tapping a vehicle sprite on the map, or a train on a departure
59 * board (both resolve to the same pack trip). Times are countdowns like the
60 * board, so no timezone applies. Tapping a station opens its departure
61 * board; the trip sheet is replaced rather than stacked (one tap, one back).
62 *
63 * Mirrors [DeparturesSheet]: the same [ModalBottomSheet], the same 15 s
64 * countdown ticker, the same cancelled styling.
65 */
66@OptIn(ExperimentalMaterial3Api::class)
67@Composable
68fun TripSheet(
69 state: TripItineraryState,
70 onDismiss: () -> Unit,
71 onStopTap: (TripStop) -> Unit,
72) {
73 val itinerary: TripItinerary? = (state as? TripItineraryState.Loaded)?.itinerary
74
75 var now by remember { mutableLongStateOf(System.currentTimeMillis()) }
76 DeparturesTicker { now = it }
77
78 val sheetState = rememberBottomSheetState(
79 initialValue = SheetValue.Hidden,
80 enabledValues = setOf(SheetValue.Hidden, SheetValue.Expanded),
81 )
82
83 ModalBottomSheet(onDismissRequest = onDismiss, sheetState = sheetState) {
84 Column(Modifier.fillMaxWidth().padding(start = 16.dp, end = 16.dp, bottom = 24.dp)) {
85 when (state) {
86 TripItineraryState.Idle -> Unit
87 TripItineraryState.Loading -> LoadingState(
88 modifier = Modifier.height(BoardStatusHeight),
89 message = stringResource(R.string.transit_departures_loading),
90 )
91 TripItineraryState.Missing -> EmptyState(
92 title = stringResource(R.string.transit_departures_none),
93 modifier = Modifier.height(BoardStatusHeight),
94 )
95 is TripItineraryState.Loaded -> {
96 val itin = state.itinerary
97 Row(
98 Modifier.fillMaxWidth(),
99 horizontalArrangement = Arrangement.SpaceBetween,
100 verticalAlignment = Alignment.CenterVertically,
101 ) {
102 Column(Modifier.weight(1f)) {
103 Row(verticalAlignment = Alignment.CenterVertically) {
104 LineBadge(itin.routeName, itin.routeColor)
105 Spacer(Modifier.width(8.dp))
106 Text(
107 itin.headsign.ifBlank { itin.routeName },
108 style = MaterialTheme.typography.titleMedium,
109 color = MaterialTheme.colorScheme.primary,
110 maxLines = 2,
111 overflow = TextOverflow.Ellipsis,
112 )
113 }
114 Text(
115 stringResource(R.string.transit_trip_title),
116 style = MaterialTheme.typography.bodySmall,
117 color = MaterialTheme.colorScheme.onSurfaceVariant,
118 )
119 }
120 if (itin.cancelled) {
121 Text(
122 stringResource(R.string.transit_cancelled),
123 style = MaterialTheme.typography.labelMedium,
124 fontWeight = FontWeight.Bold,
125 color = MaterialTheme.colorScheme.error,
126 )
127 }
128 }
129 Spacer(Modifier.height(8.dp))
130 TripStopList(itin, now, onStopTap)
131 }
132 }
133 }
134 }
135}
136
137/**
138 * Every station on the run in travel order, scrolled to the next one the
139 * trip has not yet served. Past stations dim; the one being served reads
140 * "now". A tap opens that station's departure board.
141 */
142@Composable
143private fun TripStopList(
144 itinerary: TripItinerary,
145 now: Long,
146 onStopTap: (TripStop) -> Unit,
147 modifier: Modifier = Modifier,
148) {
149 val stops = itinerary.stops
150 val listState = rememberLazyListState()
151 androidx.compose.runtime.LaunchedEffect(stops) {
152 if (stops.isNotEmpty()) {
153 val next = stops.indexOfFirst { it.departsMillis >= now }.coerceAtLeast(0)
154 listState.scrollToItem(next.coerceAtMost(stops.lastIndex))
155 }
156 }
157 LazyColumn(modifier.fillMaxWidth().heightIn(max = 420.dp), state = listState) {
158 items(stops, key = { "${it.name}|${it.departsMillis}" }) { stop ->
159 TripStopRow(stop, now, onStopTap)
160 HorizontalDivider(color = MaterialTheme.colorScheme.outlineVariant)
161 }
162 }
163}
164
165@Composable
166private fun TripStopRow(
167 stop: TripStop,
168 now: Long,
169 onStopTap: (TripStop) -> Unit,
170 modifier: Modifier = Modifier,
171) {
172 Row(
173 modifier.fillMaxWidth().clickable { onStopTap(stop) }.padding(vertical = 6.dp)
174 .alpha(departedStopAlpha(stop.departsMillis, now)),
175 horizontalArrangement = Arrangement.SpaceBetween,
176 verticalAlignment = Alignment.CenterVertically,
177 ) {
178 Column(Modifier.weight(1f)) {
179 Text(
180 stop.name.ifBlank { "—" },
181 style = MaterialTheme.typography.bodyMedium,
182 maxLines = 1,
183 overflow = TextOverflow.Ellipsis,
184 )
185 Text(
186 tripStopTime(stop, now),
187 style = MaterialTheme.typography.bodySmall,
188 color = MaterialTheme.colorScheme.onSurfaceVariant,
189 )
190 }
191 }
192}
193
194/** Arrival when dwelling or done, departure when still to come. */
195@Composable
196@ReadOnlyComposable
197private fun tripStopTime(stop: TripStop, now: Long): String {
198 val target = if (now < stop.departsMillis) stop.departsMillis else stop.arrivesMillis
199 return when (val label = DepartureTime.label(target, now)) {
200 DepartureTime.Label.Now -> stringResource(R.string.transit_now)
201 is DepartureTime.Label.InMinutes ->
202 stringResource(R.string.transit_minutes, label.minutes.toInt())
203 is DepartureTime.Label.MinutesAgo ->
204 stringResource(R.string.transit_minutes_ago, label.minutes.toInt())
205 is DepartureTime.Label.Clock -> label.text
206 }
207}
208
209/** A stop already served dims, so the eye lands on the next one to catch. */
210@Composable
211@ReadOnlyComposable
212private fun departedStopAlpha(target: Long, now: Long): Float = if (target < now) 0.5f else 1f
213
214/** The 15 s countdown ticker, shared with the departure board's cadence. */
215@Composable
216private fun DeparturesTicker(onTick: (Long) -> Unit) {
217 androidx.compose.runtime.LaunchedEffect(Unit) {
218 while (true) {
219 onTick(System.currentTimeMillis())
220 kotlinx.coroutines.delay(15_000L)
221 }
222 }
223}