Skip to content

Commit

Permalink
🐛 Handle erroneous polylines (#477)
Browse files Browse the repository at this point in the history
  • Loading branch information
jheubuch authored Jan 27, 2025
1 parent b92c357 commit 7357825
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import com.google.gson.annotations.SerializedName

data class Geometry(
@SerializedName("type") val type: String?,
@SerializedName("coordinates") val coordinates: List<List<Double>>?
@SerializedName("coordinates") val coordinates: List<List<Double?>>?
)

data class Properties(
Expand Down
9 changes: 6 additions & 3 deletions app/src/main/kotlin/de/hbch/traewelling/ui/composables/Map.kt
Original file line number Diff line number Diff line change
Expand Up @@ -153,12 +153,15 @@ fun getPolyLinesFromFeatureCollection(featureCollection: FeatureCollection?, col
feature.geometry?.coordinates?.forEach { coordinate ->
polyline.addPoint(
GeoPoint(
coordinate[1],
coordinate[0]
coordinate[1] ?: 0.0,
coordinate[0] ?: 0.0
)
)
}
polyLines.add(polyline)
// Skip erroneous polylines which have (0/0) as a point
if (!polyline.actualPoints.any { it.latitude == 0.0 && it.longitude == 0.0 }) {
polyLines.add(polyline)
}

polyline.outlinePaint.color = color
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ fun StatusDetail(
viewModelStoreOwner = LocalContext.current as ViewModelStoreOwner
)
val displayTagsInCard by settingsViewModel.displayTagsInCard.observeAsState(true)
var displayMap by remember { mutableStateOf<Boolean?>(null) }

LaunchedEffect(status) {
if (status == null) {
Expand All @@ -112,23 +113,26 @@ fun StatusDetail(
StatusDetailMap(
modifier = mapModifier.align(Alignment.TopCenter),
statusId = statusId,
statusDetailViewModel = statusDetailViewModel
statusDetailViewModel = statusDetailViewModel,
mapLoaded = { displayMap = it }
)
IconToggleButton(
modifier = Modifier.align(Alignment.TopEnd),
checked = mapExpanded,
onCheckedChange = {
mapExpanded = it
},
colors = IconButtonDefaults.filledIconToggleButtonColors()
) {
AnimatedContent(mapExpanded, label = "MapExpansionIcon") {
val iconSource =
if (it) R.drawable.ic_fullscreen_exit else R.drawable.ic_fullscreen
Icon(
painter = painterResource(id = iconSource),
contentDescription = null
)
if (displayMap == true) {
IconToggleButton(
modifier = Modifier.align(Alignment.TopEnd),
checked = mapExpanded,
onCheckedChange = {
mapExpanded = it
},
colors = IconButtonDefaults.filledIconToggleButtonColors()
) {
AnimatedContent(mapExpanded, label = "MapExpansionIcon") {
val iconSource =
if (it) R.drawable.ic_fullscreen_exit else R.drawable.ic_fullscreen
Icon(
painter = painterResource(id = iconSource),
contentDescription = null
)
}
}
}
}
Expand Down Expand Up @@ -161,6 +165,13 @@ fun StatusDetail(
tags = status?.tags ?: listOf()
)
}
if (displayMap == false) {
Text(
text = stringResource(R.string.no_map_for_check_in),
modifier = Modifier.fillMaxWidth(),
textAlign = TextAlign.Center
)
}
status?.likes?.let {
if (it > 0) {
StatusLikes(
Expand Down Expand Up @@ -239,7 +250,8 @@ fun StatusDetail(
private fun StatusDetailMap(
modifier: Modifier = Modifier,
statusId: Int,
statusDetailViewModel: StatusDetailViewModel
statusDetailViewModel: StatusDetailViewModel,
mapLoaded: (Boolean) -> Unit,
) {
val color = PolylineColor.toArgb()
val polyLines = remember { mutableStateListOf<Polyline>() }
Expand All @@ -252,7 +264,11 @@ private fun StatusDetailMap(
statusDetailViewModel.getPolylineForStatus(
statusId = statusId,
successfulCallback = {
polyLines.addAll(getPolyLinesFromFeatureCollection(it, color))
val polylines = getPolyLinesFromFeatureCollection(it, color)
polyLines.addAll(polylines)
if (polylines.isEmpty()) {
mapLoaded(false)
}
},
failureCallback = {}
)
Expand All @@ -269,6 +285,7 @@ private fun StatusDetailMap(

val bounds = getBoundingBoxFromPolyLines(polyLines)
map.zoomToBoundingBox(bounds.increaseByScale(1.1f), false)
mapLoaded(true)
}
}

Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values-de-rDE/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -342,4 +342,5 @@
<string name="timetable_api_error">Der Dienst, der die Fahrplandaten bereitstellt, konnte nicht erreicht werden oder antwortet nicht. Bitte versuche es später erneut.</string>
<string name="notice">Hinweis</string>
<string name="trwl_api_error">Aktuell gibt es vermehrt Probleme bei Träwelling (traewelling.de), daher ist die Funktionalität eingeschränkt. Bitte prüfe die Träwelling-Webseite.</string>
<string name="no_map_for_check_in">Für diesen Check-In steht moment leider keine Kartenansicht zur Verfügung.</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -370,4 +370,5 @@
<string name="notice">Notice</string>
<string name="trwl_api_error">Currently there are ongoing service issues with Träwelling (traewelling.de). Please check the Träwelling website.</string>
<string name="traewelling_de" translatable="false">traewelling.de</string>
<string name="no_map_for_check_in">Currently there is no map view available for this check-in.</string>
</resources>

0 comments on commit 7357825

Please sign in to comment.