@@ -56,11 +56,18 @@ class HomeViewModel @Inject constructor(
5656) : ViewModel() {
5757
5858 val libraryCardsFlow: StateFlow <ApiResponse <List <LibraryCardUiState >>> =
59- routeRepository.libraryFlow.map { response ->
59+ combine(
60+ routeRepository.libraryFlow,
61+ locationRepository.currentLocation
62+ ) { response, location ->
63+ val usersLocation = location?.let { LatLng (it.latitude, it.longitude) }
6064 when (val filteredResponse = response.withExcludedLibrariesRemoved()) {
6165 is ApiResponse .Success -> {
66+ val sortedLibraries = filteredResponse.data.sortedBy {
67+ numericalDistanceToPlace(it.latitude, it.longitude, usersLocation)
68+ }
6269 ApiResponse .Success (
63- filteredResponse.data
70+ sortedLibraries
6471 .map { it.toLibraryCardUiState() }
6572 )
6673 }
@@ -119,6 +126,9 @@ class HomeViewModel @Inject constructor(
119126 gymRepository.gymFlow,
120127 locationRepository.currentLocation
121128 ) { printers, libraries, eateries, gyms, location ->
129+ val userLocation = location?.let { LatLng (it.latitude, it.longitude) }
130+
131+
122132 StaticPlaces (
123133 printers = sortApiResponse(
124134 response = if (printers is ApiResponse .Success ) {
@@ -127,23 +137,27 @@ class HomeViewModel @Inject constructor(
127137 printers
128138 },
129139 getLatitude = { it.latitude },
130- getLongitude = { it.longitude }
140+ getLongitude = { it.longitude },
141+ userLocation = userLocation
131142 ),
132143 libraries = sortApiResponse(
133- response = libraries,
144+ response = libraries.withExcludedLibrariesRemoved() ,
134145 getLatitude = { it.latitude },
135- getLongitude = { it.longitude }
136- ).withExcludedLibrariesRemoved(),
146+ getLongitude = { it.longitude },
147+ userLocation = userLocation
148+ ),
137149 eateries = sortApiResponse(
138150 response = eateries,
139151 getLatitude = { it.latitude },
140152 getLongitude = { it.longitude },
153+ userLocation = userLocation,
141154 getIsOpen = { TimeUtils .getOpenStatus(it.operatingHours()).isOpen }
142155 ),
143156 gyms = sortApiResponse(
144157 response = gyms,
145158 getLatitude = { it.latitude },
146159 getLongitude = { it.longitude },
160+ userLocation = userLocation,
147161 getIsOpen = { TimeUtils .getOpenStatus(it.operatingHours()).isOpen }
148162 )
149163 )
@@ -513,14 +527,14 @@ class HomeViewModel @Inject constructor(
513527 /* *
514528 * Returns a numerical distance from a location to the current location if both exist, otherwise returns Double.MAX_VALUE
515529 */
516- fun numericalDistanceToPlace (latitude : Double? , longitude : Double? ): Double {
517- val currentLocationSnapshot = currentLocation.value
518- return if (currentLocationSnapshot != null && latitude != null && longitude != null ) {
530+ fun numericalDistanceToPlace (
531+ latitude : Double? ,
532+ longitude : Double? ,
533+ userLocation : LatLng ?
534+ ): Double {
535+ return if (userLocation != null && latitude != null && longitude != null ) {
519536 calculateDistance(
520- LatLng (
521- currentLocationSnapshot.latitude,
522- currentLocationSnapshot.longitude
523- ), LatLng (latitude, longitude)
537+ userLocation, LatLng (latitude, longitude)
524538 )
525539 } else {
526540 Double .MAX_VALUE
@@ -534,15 +548,16 @@ class HomeViewModel @Inject constructor(
534548 response : ApiResponse <List <T >>,
535549 getLatitude : (T ) -> Double? ,
536550 getLongitude : (T ) -> Double? ,
551+ userLocation : LatLng ? ,
537552 getIsOpen : ((T ) -> Boolean )? = null
538553 ): ApiResponse <List <T >> {
539554 if (response is ApiResponse .Success ) {
540555 val sortedData = response.data.sortedWith(
541556 if (getIsOpen != null ) {
542557 compareByDescending<T > { getIsOpen(it) }
543- .thenBy { numericalDistanceToPlace(getLatitude(it),getLongitude(it)) }
558+ .thenBy { numericalDistanceToPlace(getLatitude(it),getLongitude(it), userLocation ) }
544559 } else {
545- compareBy { numericalDistanceToPlace(getLatitude(it),getLongitude(it)) }
560+ compareBy { numericalDistanceToPlace(getLatitude(it),getLongitude(it), userLocation ) }
546561 }
547562 )
548563 return ApiResponse .Success (sortedData)
0 commit comments