@@ -18,6 +18,7 @@ import com.cornellappdev.transit.models.ecosystem.Library
1818import com.cornellappdev.transit.models.ecosystem.Printer
1919import com.cornellappdev.transit.models.ecosystem.UpliftGym
2020import com.cornellappdev.transit.networking.ApiResponse
21+ import com.cornellappdev.transit.util.METERS_TO_FEET
2122import com.cornellappdev.transit.util.StringUtils.fromMetersToMiles
2223import com.cornellappdev.transit.util.calculateDistance
2324import com.google.android.gms.maps.model.LatLng
@@ -36,6 +37,7 @@ import kotlinx.coroutines.flow.onEach
3637import kotlinx.coroutines.flow.stateIn
3738import kotlinx.coroutines.launch
3839import javax.inject.Inject
40+ import kotlin.text.toDouble
3941
4042/* *
4143 * ViewModel handling home screen UI state and search functionality
@@ -140,10 +142,10 @@ class HomeViewModel @Inject constructor(
140142 val addSearchResultsFlow: StateFlow <ApiResponse <List <Place >>> =
141143 unifiedSearchRepository.mergedSearchResults(_addSearchQuery )
142144 .stateIn(
143- scope = viewModelScope,
144- started = SharingStarted .WhileSubscribed (5_000 ),
145- initialValue = ApiResponse .Success (emptyList())
146- )
145+ scope = viewModelScope,
146+ started = SharingStarted .WhileSubscribed (5_000 ),
147+ initialValue = ApiResponse .Success (emptyList())
148+ )
147149
148150 fun toggleAddFavoritesSheet (show : Boolean ) {
149151 _showAddFavoritesSheet .value = show
@@ -272,8 +274,8 @@ class HomeViewModel @Inject constructor(
272274 .distinctUntilChanged()
273275 .filter { it.isNotEmpty() }
274276 .onEach {
275- routeRepository.makeSearch(it)
276- }.launchIn(viewModelScope)
277+ routeRepository.makeSearch(it)
278+ }.launchIn(viewModelScope)
277279 }
278280
279281 /* *
@@ -415,14 +417,19 @@ class HomeViewModel @Inject constructor(
415417 fun distanceStringIfCurrentLocationExists (latitude : Double? , longitude : Double? ): String {
416418 val currentLocationSnapshot = currentLocation.value
417419 if (currentLocationSnapshot != null && latitude != null && longitude != null ) {
418- return " - " +
419- calculateDistance(
420- LatLng (
421- currentLocationSnapshot.latitude,
422- currentLocationSnapshot.longitude
423- ), LatLng (latitude, longitude)
424- ).toString().fromMetersToMiles() + " mi"
425-
420+ var distance: String
421+ val distanceInMeters = calculateDistance(
422+ LatLng (
423+ currentLocationSnapshot.latitude,
424+ currentLocationSnapshot.longitude
425+ ), LatLng (latitude, longitude)
426+ ).toString()
427+ if (distanceInMeters.toDouble() > 160 ) {
428+ distance = distanceInMeters.fromMetersToMiles() + " mi"
429+ } else {
430+ distance = (distanceInMeters.toDouble() * METERS_TO_FEET ).toInt().toString() + " ft"
431+ }
432+ return " - $distance "
426433 }
427434 return " "
428435 }
0 commit comments