0

I'm using paging 3, with a remote mediator.

My mediator etc all works (tried with hard coded values), but my problem now is that I need to get a users location from a coroutine scope to pass to my adapter.

This is what I'm trying to achieve, but obviously this doesn't work.

    lateinit var parking: Flow<PagingData<Venue>>

    init {
        init()
    }

    fun init() {
        viewModelScope.launch {
            userLocationProvider.getUserLastKnownLocation().result?.let {
                parking = venueRepository.getVenuesByCategory(
                    category = "parking",
                    lat = it.latitude,
                    lon = it.longitude
                ).cachedIn(viewModelScope)
            }
        }
    }

And then collecting in my composable;

    VenueListRow(
        title = "Parking", venueList = viewModel.parking.collectAsLazyPagingItems()
    )

If I do this

val parking = venueRepository.getVenuesByCategory("parking").cachedIn(viewModelScope)

and hard code my lat/lon this works.

    override fun getVenuesByCategory(
        category: String,
        lat: Double,
        lon: Double
    ) = Pager(
        config = PagingConfig(pageSize = 10),
        remoteMediator = VenueRemoteMediator(
            query = category,
            venueDatabase = venueDatabase,
            venueServices = venueServices,
            lat = lat,
            lon = lon
        ),
        pagingSourceFactory = {
            venueDatabase.venueDao().pagingSource(
                category = category,
                lat = lat,
                lon = lon
            )
        }
    ).flow.map { venueDataObjectPagingData ->
        // map data
    }

If I could do userLocationProvider.getUserLastKnownLocation() in my pagingSourceFactory that could also work, but I can't find a way to use coroutines in there (I could create my own PagingSource, but I'm using the Room Paging Source and can't find anything about that.

0 Answers0