Skip to content

Commit 65984e3

Browse files
committed
fix: change loading from 60s to 30s for quicker error response, add better error messages for empty vs backend failure
1 parent 05ae137 commit 65984e3

4 files changed

Lines changed: 66 additions & 20 deletions

File tree

app/src/main/java/com/cornellappdev/transit/models/RouteRepository.kt

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import com.cornellappdev.transit.networking.EcosystemNetworkApi
77
import com.cornellappdev.transit.networking.RoutesNetworkApi
88
import com.cornellappdev.transit.util.ECOSYSTEM_FLAG
99
import com.google.android.gms.maps.model.LatLng
10+
import kotlinx.coroutines.CancellationException
1011
import kotlinx.coroutines.CoroutineScope
1112
import kotlinx.coroutines.Dispatchers
1213
import kotlinx.coroutines.flow.MutableStateFlow
@@ -165,7 +166,7 @@ class RouteRepository @Inject constructor(
165166
* @param arriveBy Whether the route must complete by a certain time
166167
* @param originName The name of the origin
167168
*/
168-
fun fetchRoute(
169+
suspend fun fetchRoute(
169170
end: LatLng,
170171
time: Double,
171172
destinationName: String,
@@ -174,22 +175,23 @@ class RouteRepository @Inject constructor(
174175
originName: String
175176
) {
176177
_lastRouteFlow.value = ApiResponse.Pending
177-
CoroutineScope(Dispatchers.IO).launch {
178-
try {
179-
val routeResponse = getRoute(
180-
RouteRequest(
181-
end = "${end.latitude}, ${end.longitude}",
182-
time = time,
183-
destinationName = destinationName,
184-
start = "${start.latitude}, ${start.longitude}",
185-
arriveBy = arriveBy,
186-
originName = originName
187-
)
178+
try {
179+
val routeResponse = getRoute(
180+
RouteRequest(
181+
end = "${end.latitude}, ${end.longitude}",
182+
time = time,
183+
destinationName = destinationName,
184+
start = "${start.latitude}, ${start.longitude}",
185+
arriveBy = arriveBy,
186+
originName = originName
188187
)
189-
_lastRouteFlow.value = ApiResponse.Success(routeResponse.unwrap())
190-
} catch (e: Exception) {
191-
_lastRouteFlow.value = ApiResponse.Error
192-
}
188+
)
189+
_lastRouteFlow.value = ApiResponse.Success(routeResponse.unwrap())
190+
} catch (e: CancellationException) {
191+
// Latest-only callers cancel stale requests; do not publish error for cancellations.
192+
throw e
193+
} catch (e: Exception) {
194+
_lastRouteFlow.value = ApiResponse.Error
193195
}
194196
}
195197

app/src/main/java/com/cornellappdev/transit/networking/NetworkModule.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ object NetworkModule {
4545
.Builder()
4646
.connectionPool(ConnectionPool(0, 1, TimeUnit.NANOSECONDS))
4747
.protocols(listOf(Protocol.HTTP_1_1))
48-
.readTimeout(60, TimeUnit.SECONDS)
49-
.connectTimeout(60, TimeUnit.SECONDS)
48+
.readTimeout(30, TimeUnit.SECONDS)
49+
.connectTimeout(30, TimeUnit.SECONDS)
5050
.addInterceptor(logging)
5151
.build()
5252
}

app/src/main/java/com/cornellappdev/transit/ui/screens/RouteScreen.kt

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,12 @@ private fun RouteList(
579579
onClick: (Route) -> Unit,
580580
onRefresh: () -> Unit,
581581
) {
582+
fun hasNoRoutes(options: RouteOptions): Boolean {
583+
return options.fromStop.isNullOrEmpty() &&
584+
options.boardingSoon.isNullOrEmpty() &&
585+
options.walking.isNullOrEmpty()
586+
}
587+
582588
TransitPullToRefreshBox(
583589
isRefreshing = lastRouteResponse is ApiResponse.Pending,
584590
onRefresh = onRefresh
@@ -593,7 +599,7 @@ private fun RouteList(
593599
item {
594600
Spacer(modifier = Modifier.height(80.dp))
595601
Text(
596-
text = "No Routes Found",
602+
text = "Unable to load routes",
597603
fontFamily = robotoFamily,
598604
fontWeight = FontWeight.Normal,
599605
color = MetadataGray,
@@ -603,6 +609,15 @@ private fun RouteList(
603609
.fillMaxWidth(),
604610
textAlign = TextAlign.Center
605611
)
612+
Text(
613+
text = "Check your connection and pull to refresh.",
614+
style = Style.heading4,
615+
color = MetadataGray,
616+
modifier = Modifier
617+
.padding(horizontal = 12.dp)
618+
.fillMaxWidth(),
619+
textAlign = TextAlign.Center
620+
)
606621
}
607622

608623
}
@@ -612,6 +627,31 @@ private fun RouteList(
612627
}
613628

614629
is ApiResponse.Success -> {
630+
if (hasNoRoutes(lastRouteResponse.data)) {
631+
item {
632+
Spacer(modifier = Modifier.height(80.dp))
633+
Text(
634+
text = "No Routes Found",
635+
fontFamily = robotoFamily,
636+
fontWeight = FontWeight.Normal,
637+
color = MetadataGray,
638+
fontSize = 24.sp,
639+
modifier = Modifier
640+
.padding(12.dp)
641+
.fillMaxWidth(),
642+
textAlign = TextAlign.Center
643+
)
644+
Text(
645+
text = "Try a nearby stop, different time, or switch Arrive By/Leave At.",
646+
style = Style.heading4,
647+
color = MetadataGray,
648+
modifier = Modifier
649+
.padding(horizontal = 12.dp)
650+
.fillMaxWidth(),
651+
textAlign = TextAlign.Center
652+
)
653+
}
654+
}
615655
lastRouteResponse.data.fromStop?.let {
616656
items(it) { item ->
617657
PaddedRouteCell(item.toTransport()) {

app/src/main/java/com/cornellappdev/transit/ui/viewmodels/RouteViewModel.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import com.google.android.gms.maps.model.LatLng
2121
import com.google.android.gms.maps.model.LatLngBounds
2222
import dagger.hilt.android.lifecycle.HiltViewModel
2323
import kotlinx.coroutines.FlowPreview
24+
import kotlinx.coroutines.Job
2425
import kotlinx.coroutines.flow.MutableStateFlow
2526
import kotlinx.coroutines.flow.StateFlow
2627
import kotlinx.coroutines.flow.asStateFlow
@@ -47,6 +48,8 @@ class RouteViewModel @Inject constructor(
4748
private val selectedRouteRepository: SelectedRouteRepository
4849
) : ViewModel() {
4950

51+
private var fetchRouteJob: Job? = null
52+
5053
/**
5154
* Value of the current location. Can be null
5255
*/
@@ -263,7 +266,8 @@ class RouteViewModel @Inject constructor(
263266
arriveBy: Boolean,
264267
originName: String
265268
) {
266-
viewModelScope.launch {
269+
fetchRouteJob?.cancel()
270+
fetchRouteJob = viewModelScope.launch {
267271
routeRepository.fetchRoute(
268272
end = end,
269273
time = time,

0 commit comments

Comments
 (0)