Skip to content

Commit

Permalink
fix user's journey distance.
Browse files Browse the repository at this point in the history
  • Loading branch information
cp-sneh-s committed Sep 26, 2024
1 parent b0bab19 commit 7c9ec73
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ import androidx.compose.ui.unit.dp
import com.canopas.yourspace.R
import com.canopas.yourspace.data.models.location.LocationJourney
import com.canopas.yourspace.data.models.location.isSteadyLocation
import com.canopas.yourspace.data.models.location.toLocationFromMovingJourney
import com.canopas.yourspace.data.models.location.toLocationFromSteadyJourney
import com.canopas.yourspace.domain.utils.getAddress
import com.canopas.yourspace.domain.utils.getPlaceAddress
import com.canopas.yourspace.domain.utils.isToday
Expand Down Expand Up @@ -104,7 +102,7 @@ fun JourneyLocationItem(location: LocationJourney, lastItem: Boolean, onTap: ()
.weight(1f)
) {
val time = getFormattedJourneyTime(location.created_at ?: 0, location.update_at ?: 0)
val distance = getDistanceString(location)
val distance = getDistanceString(location.route_distance ?: 0.0)

PlaceInfo(title, "$time - $distance")
Spacer(modifier = Modifier.size(16.dp))
Expand Down Expand Up @@ -316,16 +314,13 @@ internal fun getFormattedJourneyTime(startAt: Long, endsAt: Long): String {
}

internal fun getDistanceString(
location: LocationJourney
routeDistance: Double
): String {
return location.toLocationFromSteadyJourney().distanceTo(location.toLocationFromMovingJourney()).let {
val routeDistance = it
if (routeDistance < 1000) {
"${routeDistance.roundToInt()} m"
} else {
val distanceInKm = (routeDistance / 1000)
"${distanceInKm.roundToInt()} km"
}
return if (routeDistance < 1000) {
"${routeDistance.roundToInt()} m"
} else {
val distanceInKm = (routeDistance / 1000)
"${distanceInKm.roundToInt()} km"
}
}

Expand All @@ -350,11 +345,14 @@ internal fun getFormattedLocationTimeForFirstItem(createdAt: Long): String {
}

fun Address.formattedTitle(toAddress: Address?): String {
val fromCity = this.locality ?: this.subAdminArea ?: this.adminArea ?: this.featureName ?: "Unknown"
val toCity = toAddress?.locality ?: toAddress?.subAdminArea ?: toAddress?.adminArea ?: toAddress?.featureName ?: "Unknown"
val fromCity =
this.locality ?: this.subAdminArea ?: this.adminArea ?: this.featureName ?: "Unknown"
val toCity = toAddress?.locality ?: toAddress?.subAdminArea ?: toAddress?.adminArea
?: toAddress?.featureName ?: "Unknown"

val fromArea = this.subLocality ?: this.thoroughfare ?: this.featureName ?: fromCity
val toArea = toAddress?.subLocality ?: toAddress?.thoroughfare ?: toAddress?.featureName ?: toCity
val toArea =
toAddress?.subLocality ?: toAddress?.thoroughfare ?: toAddress?.featureName ?: toCity

val fromState = this.adminArea ?: this.countryName ?: "Unknown"
val toState = toAddress?.adminArea ?: toAddress?.countryName ?: "Unknown"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ private fun JourneyInfo(journey: LocationJourney) {
}
}

val distance = getDistanceString(location = journey)
val duration = getRouteDurationString(journey)
val distance = getDistanceString(journey.route_distance ?: 0.0)
val duration = getRouteDurationString(journey.route_duration ?: 0)

Text(
text = "$distance - $duration",
Expand Down Expand Up @@ -275,9 +275,8 @@ private fun JourneyMarker(bgColor: Color, content: @Composable BoxScope.() -> Un
}

internal fun getRouteDurationString(
journey: LocationJourney
routeDuration: Long
): String {
val routeDuration = journey.update_at!! - journey.created_at!!
val hours = TimeUnit.MILLISECONDS.toHours(routeDuration)
val minutes = TimeUnit.MILLISECONDS.toMinutes(routeDuration) % 60
val seconds = TimeUnit.MILLISECONDS.toSeconds(routeDuration) % 60
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ data class LocationJourney(
val from_longitude: Double = 0.0,
var to_latitude: Double? = null,
var to_longitude: Double? = null,
val route_distance: Double? = null,
val route_duration: Long? = null,
val routes: List<JourneyRoute> = emptyList(),
val created_at: Long? = System.currentTimeMillis(),
val update_at: Long? = System.currentTimeMillis()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,8 @@ class JourneyRepository @Inject constructor(
)
}

val timeDifference = (geometricMedian?.time ?: extractedLocation.time) - lastKnownJourney.update_at!!
val timeDifference =
(geometricMedian?.time ?: extractedLocation.time) - lastKnownJourney.update_at!!

if (lastKnownJourney.isSteadyLocation()) {
// Handle steady user
Expand All @@ -166,7 +167,8 @@ class JourneyRepository @Inject constructor(
saveJourneyWhenUserStartsMoving(
userId,
extractedLocation,
lastKnownJourney
lastKnownJourney,
distance
)
}
} else {
Expand All @@ -178,15 +180,17 @@ class JourneyRepository @Inject constructor(
updateJourneyForContinuedMovingUser(
userId,
extractedLocation,
lastKnownJourney
lastKnownJourney,
distance
)
} else if (distance < MIN_DISTANCE && timeDifference > MIN_TIME_DIFFERENCE) {
// Here, means last known journey is moving and user has stopped moving
// Save journey for steady user and update last known journey:
saveJourneyOnJourneyStopped(
userId,
extractedLocation,
lastKnownJourney
lastKnownJourney,
distance
)
}
}
Expand All @@ -198,7 +202,8 @@ class JourneyRepository @Inject constructor(
private suspend fun saveJourneyWhenUserStartsMoving(
userId: String,
extractedLocation: Location,
lastKnownJourney: LocationJourney
lastKnownJourney: LocationJourney,
distance: Float
) {
var newJourneyId = ""
val journey = LocationJourney(
Expand All @@ -207,6 +212,8 @@ class JourneyRepository @Inject constructor(
from_longitude = lastKnownJourney.from_longitude,
to_latitude = extractedLocation.latitude,
to_longitude = extractedLocation.longitude,
route_distance = distance.toDouble(),
route_duration = null,
routes = listOf(
lastKnownJourney.toLocationFromSteadyJourney().toRoute(),
extractedLocation.toRoute()
Expand All @@ -217,7 +224,9 @@ class JourneyRepository @Inject constructor(
fromLatitude = lastKnownJourney.from_latitude,
fromLongitude = lastKnownJourney.from_longitude,
toLatitude = extractedLocation.latitude,
toLongitude = extractedLocation.longitude
toLongitude = extractedLocation.longitude,
routeDistance = null,
routeDuration = (extractedLocation.time - lastKnownJourney.created_at!!)
) {
newJourneyId = it
}
Expand All @@ -230,7 +239,8 @@ class JourneyRepository @Inject constructor(
private suspend fun updateJourneyForContinuedMovingUser(
userId: String,
extractedLocation: Location,
lastKnownJourney: LocationJourney
lastKnownJourney: LocationJourney,
distance: Float
) {
val journey = LocationJourney(
id = lastKnownJourney.id,
Expand All @@ -239,6 +249,8 @@ class JourneyRepository @Inject constructor(
from_longitude = lastKnownJourney.from_longitude,
to_latitude = extractedLocation.latitude,
to_longitude = extractedLocation.longitude,
route_distance = distance.toDouble() + (lastKnownJourney.route_distance ?: 0.0),
route_duration = (extractedLocation.time - lastKnownJourney.created_at!!),
routes = lastKnownJourney.routes + listOf(extractedLocation.toRoute()),
created_at = lastKnownJourney.created_at
)
Expand All @@ -255,7 +267,8 @@ class JourneyRepository @Inject constructor(
private suspend fun saveJourneyOnJourneyStopped(
userId: String,
extractedLocation: Location,
lastKnownJourney: LocationJourney
lastKnownJourney: LocationJourney,
distance: Float
) {
val movingJourney = LocationJourney(
id = lastKnownJourney.id,
Expand All @@ -264,6 +277,8 @@ class JourneyRepository @Inject constructor(
from_longitude = lastKnownJourney.from_longitude,
to_latitude = extractedLocation.latitude,
to_longitude = extractedLocation.longitude,
route_distance = distance.toDouble() + (lastKnownJourney.route_distance ?: 0.0),
route_duration = (extractedLocation.time - lastKnownJourney.created_at!!),
routes = lastKnownJourney.routes + listOf(extractedLocation.toRoute()),
created_at = lastKnownJourney.created_at,
update_at = lastKnownJourney.update_at
Expand Down Expand Up @@ -295,7 +310,7 @@ class JourneyRepository @Inject constructor(
/**
* Calculate distance between two locations
* */
fun distanceBetween(location1: Location, location2: Location): Float {
private fun distanceBetween(location1: Location, location2: Location): Float {
val distance = FloatArray(1)
Location.distanceBetween(
location1.latitude,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ class ApiJourneyService @Inject constructor(
fromLongitude: Double,
toLatitude: Double? = null,
toLongitude: Double? = null,
routeDistance: Double? = null,
routeDuration: Long? = null,
routes: List<JourneyRoute> = emptyList(),
createdAt: Long? = null,
updateAt: Long? = null,
Expand All @@ -45,6 +47,8 @@ class ApiJourneyService @Inject constructor(
from_longitude = fromLongitude,
to_latitude = toLatitude,
to_longitude = toLongitude,
route_distance = routeDistance,
route_duration = routeDuration,
routes = routes,
created_at = createdAt ?: System.currentTimeMillis(),
update_at = updateAt ?: System.currentTimeMillis()
Expand Down

0 comments on commit 7c9ec73

Please sign in to comment.