Skip to content

Commit

Permalink
Merge pull request #152 from canopas/sneh/fix-journey-address-name
Browse files Browse the repository at this point in the history
Fix address naming for moving journeys
  • Loading branch information
cp-sneh-s authored Jan 9, 2025
2 parents cd11194 + 14eab9e commit 9b0fe65
Showing 1 changed file with 29 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ fun JourneyLocationItem(
}
}

val title = fromAddress?.formattedTitle(toAddress) ?: ""
val title = formattedAddress(fromAddress, toAddress)

Row(
verticalAlignment = Alignment.Top,
Expand Down Expand Up @@ -469,34 +469,39 @@ internal fun getFormattedLocationTimeForFirstItem(createdAt: Long): String {
}

fun Address.formattedTitle(toAddress: Address?): String {
val fromName = extractLocationName(this)
val toName = toAddress?.let { extractLocationName(it) } ?: "Unknown Road"

return if (toAddress == null) {
fromName
} else {
"$fromName -> $toName"
}
return formattedAddress(this, toAddress)
}

private fun extractLocationName(address: Address): String {
val featureName = address.featureName?.trim()
val thoroughfare = address.thoroughfare?.trim()
fun formattedAddress(fromPlace: Address?, toPlace: Address?): String {
val fromCity = fromPlace?.locality ?: ""
val toCity = toPlace?.locality ?: ""

val potentialNames = listOf(
featureName,
thoroughfare
).filterNot { it.isNullOrEmpty() }
val fromArea = fromPlace?.subLocality ?: ""
val toArea = toPlace?.subLocality ?: ""

val cleanedNames = potentialNames.map { it?.replace(Regex("^[A-Za-z0-9]+\\+.*"), "")?.trim() }
val name = cleanedNames.firstOrNull { it?.isNotEmpty() == true } ?: "Unknown Road"
val fromState = fromPlace?.adminArea ?: ""
val toState = toPlace?.adminArea ?: ""

val resultName = if (name.matches(Regex("^[0-9].*"))) {
val streetName = cleanedNames.getOrNull(1) ?: ""
"$name $streetName".trim()
} else {
name
return when {
toPlace == null -> formatAddress(listOf(fromArea, fromCity))
fromArea == toArea -> formatAddress(listOf(fromArea, fromCity))
fromCity == toCity -> formatTwoPlaceAddress(listOf(fromArea), listOf(toArea, fromCity))
fromState == toState -> formatTwoPlaceAddress(listOf(fromArea, fromCity), listOf(toArea, toCity))
else -> formatTwoPlaceAddress(listOf(fromCity, fromState), listOf(toCity, toState))
}
}

fun formatTwoPlaceAddress(fromPlace: List<String>, toPlace: List<String>): String {
val isFromPlaceEmpty = fromPlace.all { it.isEmpty() }
val isToPlaceEmpty = toPlace.all { it.isEmpty() }

return when {
!isFromPlaceEmpty && !isToPlaceEmpty -> "${formatAddress(fromPlace)} -> ${formatAddress(toPlace)}"
!isFromPlaceEmpty && isToPlaceEmpty -> formatAddress(fromPlace)
else -> formatAddress(toPlace)
}
}

return resultName
fun formatAddress(parts: List<String>): String {
return parts.joinToString(", ") { it.ifEmpty { "Unknown" } }
}

0 comments on commit 9b0fe65

Please sign in to comment.