From 4a767f0719f6020ab8554422eb5e2c7bf283131b Mon Sep 17 00:00:00 2001 From: Sneh Date: Mon, 6 Jan 2025 12:05:25 +0530 Subject: [PATCH 1/2] Fix address naming for moving journeys. --- .../journey/components/LocationHistory.kt | 28 +++++++++++++------ 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/com/canopas/yourspace/ui/flow/journey/components/LocationHistory.kt b/app/src/main/java/com/canopas/yourspace/ui/flow/journey/components/LocationHistory.kt index 28ebeba3..67a155b9 100644 --- a/app/src/main/java/com/canopas/yourspace/ui/flow/journey/components/LocationHistory.kt +++ b/app/src/main/java/com/canopas/yourspace/ui/flow/journey/components/LocationHistory.kt @@ -480,23 +480,33 @@ fun Address.formattedTitle(toAddress: Address?): String { } private fun extractLocationName(address: Address): String { + val roadName = address.thoroughfare?.trim() + val area = address.subLocality?.trim() val featureName = address.featureName?.trim() - val thoroughfare = address.thoroughfare?.trim() + val city = address.locality?.trim() + val state = address.adminArea?.trim() val potentialNames = listOf( - featureName, - thoroughfare + roadName, + area, + city, + state, + featureName ).filterNot { it.isNullOrEmpty() } val cleanedNames = potentialNames.map { it?.replace(Regex("^[A-Za-z0-9]+\\+.*"), "")?.trim() } val name = cleanedNames.firstOrNull { it?.isNotEmpty() == true } ?: "Unknown Road" - val resultName = if (name.matches(Regex("^[0-9].*"))) { - val streetName = cleanedNames.getOrNull(1) ?: "" - "$name $streetName".trim() - } else { - name + val addressName = when { + name.matches(Regex("^[0-9].*")) -> { + val streetName = cleanedNames.getOrNull(1) ?: "" + "$name $streetName".trim() + } + roadName != null -> "$roadName, ${area ?: city}" + area != null -> "$area, $city" + featureName != null -> "$featureName, $city" + else -> name } - return resultName + return addressName } From 14eab9ea8bd801888d36e8b5041f9af839369c78 Mon Sep 17 00:00:00 2001 From: Sneh Date: Tue, 7 Jan 2025 15:14:52 +0530 Subject: [PATCH 2/2] Modify address format for journeys. --- .../journey/components/LocationHistory.kt | 65 +++++++++---------- 1 file changed, 30 insertions(+), 35 deletions(-) diff --git a/app/src/main/java/com/canopas/yourspace/ui/flow/journey/components/LocationHistory.kt b/app/src/main/java/com/canopas/yourspace/ui/flow/journey/components/LocationHistory.kt index 5a5388ac..f0bba78e 100644 --- a/app/src/main/java/com/canopas/yourspace/ui/flow/journey/components/LocationHistory.kt +++ b/app/src/main/java/com/canopas/yourspace/ui/flow/journey/components/LocationHistory.kt @@ -101,7 +101,7 @@ fun JourneyLocationItem( } } - val title = fromAddress?.formattedTitle(toAddress) ?: "" + val title = formattedAddress(fromAddress, toAddress) Row( verticalAlignment = Alignment.Top, @@ -469,44 +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 formattedAddress(this, toAddress) +} - return if (toAddress == null) { - fromName - } else { - "$fromName -> $toName" +fun formattedAddress(fromPlace: Address?, toPlace: Address?): String { + val fromCity = fromPlace?.locality ?: "" + val toCity = toPlace?.locality ?: "" + + val fromArea = fromPlace?.subLocality ?: "" + val toArea = toPlace?.subLocality ?: "" + + val fromState = fromPlace?.adminArea ?: "" + val toState = toPlace?.adminArea ?: "" + + 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)) } } -private fun extractLocationName(address: Address): String { - val roadName = address.thoroughfare?.trim() - val area = address.subLocality?.trim() - val featureName = address.featureName?.trim() - val city = address.locality?.trim() - val state = address.adminArea?.trim() - - val potentialNames = listOf( - roadName, - area, - city, - state, - featureName - ).filterNot { it.isNullOrEmpty() } - - val cleanedNames = potentialNames.map { it?.replace(Regex("^[A-Za-z0-9]+\\+.*"), "")?.trim() } - val name = cleanedNames.firstOrNull { it?.isNotEmpty() == true } ?: "Unknown Road" - - val addressName = when { - name.matches(Regex("^[0-9].*")) -> { - val streetName = cleanedNames.getOrNull(1) ?: "" - "$name $streetName".trim() - } - roadName != null -> "$roadName, ${area ?: city}" - area != null -> "$area, $city" - featureName != null -> "$featureName, $city" - else -> name +fun formatTwoPlaceAddress(fromPlace: List, toPlace: List): 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 addressName +fun formatAddress(parts: List): String { + return parts.joinToString(", ") { it.ifEmpty { "Unknown" } } }