Skip to content

Instantly share code, notes, and snippets.

@sdetilly
Last active July 31, 2023 18:59
Show Gist options
  • Select an option

  • Save sdetilly/287fc691bd5992acb19ae355bf85c6b4 to your computer and use it in GitHub Desktop.

Select an option

Save sdetilly/287fc691bd5992acb19ae355bf85c6b4 to your computer and use it in GitHub Desktop.
RestaurantFinder
fun restaurantsNearUser(): Flow<List<Restaurant>> =
flow {
emit(null)
gpsService.currentUserLocation().also { location ->
emit(location)
}
}
.map { userLocation ->
buildRestaurantList(userLocation)
}
.distinctUntilChanged() // we won't notify observers if the list hasn't changed
.flowOn(Dispatchers.Default) // All sorting work will be done in a non-UI Thread
// Here, we can build the restaurant list from our Restaurant data
// For example, if userLocation is null, then the list is unsorted
// If userLocation is non-null, then we can sort the list so that closer restaurants appear on top
fun buildRestaurantList(userLocation: GPSCoordinates?): List<Restaurant> {
// ...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment