Loading locations data in fragment

*   BookmarkLocationsController: Changed `loadFavoritesLocations` to be a suspend function.
*   BookmarkLocationsFragment: Updated the `initList` function to call the controller's suspend function.
*   BookmarkLocationsDao: Changed `getAllBookmarksLocations` and `getAllBookmarksLocationsPlace` to be suspend functions.
This commit is contained in:
Saifuddin 2025-01-18 12:43:28 +05:30
parent 86d0475637
commit 32f7b4c887
3 changed files with 10 additions and 9 deletions

View file

@ -15,6 +15,6 @@ class BookmarkLocationsController @Inject constructor(
* Load bookmarked locations from the database. * Load bookmarked locations from the database.
* @return a list of Place objects. * @return a list of Place objects.
*/ */
fun loadFavoritesLocations(): Flow<List<Place>> = suspend fun loadFavoritesLocations(): List<Place> =
bookmarkLocationDao.getAllBookmarksLocationsPlace() bookmarkLocationDao.getAllBookmarksLocationsPlace()
} }

View file

@ -18,7 +18,7 @@ abstract class BookmarkLocationsDao {
abstract suspend fun addBookmarkLocation(bookmarkLocation: BookmarksLocations) abstract suspend fun addBookmarkLocation(bookmarkLocation: BookmarksLocations)
@Query("SELECT * FROM bookmarks_locations") @Query("SELECT * FROM bookmarks_locations")
abstract fun getAllBookmarksLocations(): Flow<List<BookmarksLocations>> abstract suspend fun getAllBookmarksLocations(): List<BookmarksLocations>
@Query("SELECT EXISTS (SELECT 1 FROM bookmarks_locations WHERE location_name = :name)") @Query("SELECT EXISTS (SELECT 1 FROM bookmarks_locations WHERE location_name = :name)")
abstract suspend fun findBookmarkLocation(name: String): Boolean abstract suspend fun findBookmarkLocation(name: String): Boolean
@ -44,7 +44,7 @@ abstract class BookmarkLocationsDao {
return !bookmarkLocationExists return !bookmarkLocationExists
} }
fun getAllBookmarksLocationsPlace(): Flow<List<Place>> { suspend fun getAllBookmarksLocationsPlace(): List<Place> {
return flow { getAllBookmarksLocations().map { it.map { it1 -> it1.toPlace() } } } return getAllBookmarksLocations().map { it.toPlace() }
} }
} }

View file

@ -130,13 +130,14 @@ class BookmarkLocationsFragment : DaggerFragment() {
} }
private fun initList() { private fun initList() {
var places: List<Place> = listOf() var places: List<Place>
viewLifecycleOwner.lifecycleScope.launch { viewLifecycleOwner.lifecycleScope.launch {
controller.loadFavoritesLocations().collect { places = controller.loadFavoritesLocations()
adapter.items = it updateUIList(places)
places = it
} }
} }
private fun updateUIList(places: List<Place>) {
adapter.items = places adapter.items = places
binding?.loadingImagesProgressBar?.visibility = View.GONE binding?.loadingImagesProgressBar?.visibility = View.GONE
if (places.isEmpty()) { if (places.isEmpty()) {