Refactor: Load bookmarked locations using Flow

*   `BookmarkLocationsController`: Changed to use `Flow` to load bookmarked locations.
*   `BookmarkLocationsDao`: Changed to return `Flow` of bookmark location instead of a list.
*   `BookmarkLocationsFragment`: Used `LifecycleScope` to collect data from flow and display it.
*   Removed unused `getAllBookmarkLocations()` from `BookmarkLocationsViewModel`.
*   Used `map` in `BookmarkLocationsDao` to convert from `BookmarksLocations` to `Place` list.
This commit is contained in:
Saifuddin 2025-01-18 11:33:16 +05:30
parent 8a41b3db1b
commit 86d0475637
4 changed files with 19 additions and 7 deletions

View file

@ -1,6 +1,8 @@
package fr.free.nrw.commons.bookmarks.locations
import fr.free.nrw.commons.nearby.Place
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import javax.inject.Inject
import javax.inject.Singleton
@ -13,5 +15,6 @@ class BookmarkLocationsController @Inject constructor(
* Load bookmarked locations from the database.
* @return a list of Place objects.
*/
fun loadFavoritesLocations(): List<Place> = listOf()
fun loadFavoritesLocations(): Flow<List<Place>> =
bookmarkLocationDao.getAllBookmarksLocationsPlace()
}

View file

@ -9,6 +9,7 @@ import fr.free.nrw.commons.nearby.NearbyController
import fr.free.nrw.commons.nearby.Place
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.map
@Dao
abstract class BookmarkLocationsDao {
@ -17,7 +18,7 @@ abstract class BookmarkLocationsDao {
abstract suspend fun addBookmarkLocation(bookmarkLocation: BookmarksLocations)
@Query("SELECT * FROM bookmarks_locations")
abstract suspend fun getAllBookmarksLocations(): List<BookmarksLocations>
abstract fun getAllBookmarksLocations(): Flow<List<BookmarksLocations>>
@Query("SELECT EXISTS (SELECT 1 FROM bookmarks_locations WHERE location_name = :name)")
abstract suspend fun findBookmarkLocation(name: String): Boolean
@ -44,6 +45,6 @@ abstract class BookmarkLocationsDao {
}
fun getAllBookmarksLocationsPlace(): Flow<List<Place>> {
return flow { getAllBookmarksLocations().map { it.toPlace() } }
return flow { getAllBookmarksLocations().map { it.map { it1 -> it1.toPlace() } } }
}
}

View file

@ -15,8 +15,10 @@ import fr.free.nrw.commons.R
import fr.free.nrw.commons.contributions.ContributionController
import fr.free.nrw.commons.databinding.FragmentBookmarksLocationsBinding
import fr.free.nrw.commons.filepicker.FilePicker
import fr.free.nrw.commons.nearby.Place
import fr.free.nrw.commons.nearby.fragments.CommonPlaceClickActions
import fr.free.nrw.commons.nearby.fragments.PlaceAdapter
import kotlinx.coroutines.launch
import javax.inject.Inject
@ -128,7 +130,13 @@ class BookmarkLocationsFragment : DaggerFragment() {
}
private fun initList() {
val places = controller.loadFavoritesLocations()
var places: List<Place> = listOf()
viewLifecycleOwner.lifecycleScope.launch {
controller.loadFavoritesLocations().collect {
adapter.items = it
places = it
}
}
adapter.items = places
binding?.loadingImagesProgressBar?.visibility = View.GONE
if (places.isEmpty()) {

View file

@ -8,8 +8,8 @@ class BookmarkLocationsViewModel(
private val bookmarkLocationsDao: BookmarkLocationsDao
): ViewModel() {
fun getAllBookmarkLocations(): Flow<List<Place>> {
return bookmarkLocationsDao.getAllBookmarksLocationsPlace()
}
// fun getAllBookmarkLocations(): List<Place> {
// return bookmarkLocationsDao.getAllBookmarksLocationsPlace()
// }
}