Skip to content

Commit 57bee17

Browse files
committed
show ft when distance < 0.1 miles
1 parent 6684f06 commit 57bee17

2 files changed

Lines changed: 24 additions & 15 deletions

File tree

app/src/main/java/com/cornellappdev/transit/ui/viewmodels/HomeViewModel.kt

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import com.cornellappdev.transit.models.ecosystem.Library
1818
import com.cornellappdev.transit.models.ecosystem.Printer
1919
import com.cornellappdev.transit.models.ecosystem.UpliftGym
2020
import com.cornellappdev.transit.networking.ApiResponse
21+
import com.cornellappdev.transit.util.METERS_TO_FEET
2122
import com.cornellappdev.transit.util.StringUtils.fromMetersToMiles
2223
import com.cornellappdev.transit.util.calculateDistance
2324
import com.google.android.gms.maps.model.LatLng
@@ -36,6 +37,7 @@ import kotlinx.coroutines.flow.onEach
3637
import kotlinx.coroutines.flow.stateIn
3738
import kotlinx.coroutines.launch
3839
import 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
}

app/src/main/java/com/cornellappdev/transit/util/TransitConstants.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,6 @@ const val MEDIUM_CAPACITY_THRESHOLD = 0.35f
1212
/** When the capacity turns to red */
1313
const val HIGH_CAPACITY_THRESHOLD = 0.65f
1414

15-
const val NOTIFICATIONS_ENABLED = false
15+
const val NOTIFICATIONS_ENABLED = false
16+
17+
const val METERS_TO_FEET = 3.28

0 commit comments

Comments
 (0)