2020-11-27 17:02:52 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2018 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2020-12-26 12:10:54 +01:00
|
|
|
package com.h.pixeldroid.posts.feeds.cachedFeeds
|
2020-11-27 17:02:52 +01:00
|
|
|
|
|
|
|
import androidx.paging.*
|
2020-12-26 12:10:54 +01:00
|
|
|
import com.h.pixeldroid.utils.db.AppDatabase
|
|
|
|
import com.h.pixeldroid.utils.db.dao.feedContent.FeedContentDao
|
|
|
|
import com.h.pixeldroid.utils.api.objects.FeedContentDatabase
|
2020-11-27 17:02:52 +01:00
|
|
|
import kotlinx.coroutines.flow.Flow
|
|
|
|
import javax.inject.Inject
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Repository class that works with local and remote data sources.
|
|
|
|
*/
|
|
|
|
class FeedContentRepository<T: FeedContentDatabase> @ExperimentalPagingApi
|
|
|
|
@Inject constructor(
|
|
|
|
private val db: AppDatabase,
|
|
|
|
private val dao: FeedContentDao<T>,
|
|
|
|
private val mediator: RemoteMediator<Int, T>
|
|
|
|
) {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* [FeedContentDatabase], exposed as a stream of data that will emit
|
|
|
|
* every time we get more data from the network.
|
|
|
|
*/
|
2020-12-19 21:12:56 +01:00
|
|
|
@ExperimentalPagingApi
|
2020-11-27 17:02:52 +01:00
|
|
|
fun stream(): Flow<PagingData<T>> {
|
|
|
|
|
|
|
|
val pagingSourceFactory = {
|
|
|
|
val user = db.userDao().getActiveUser()!!
|
|
|
|
dao.feedContent(user.user_id, user.instance_uri)
|
|
|
|
}
|
|
|
|
|
|
|
|
return Pager(
|
|
|
|
config = PagingConfig(initialLoadSize = NETWORK_PAGE_SIZE,
|
|
|
|
pageSize = NETWORK_PAGE_SIZE,
|
|
|
|
enablePlaceholders = false,
|
|
|
|
prefetchDistance = 50
|
|
|
|
),
|
|
|
|
remoteMediator = mediator,
|
|
|
|
pagingSourceFactory = pagingSourceFactory
|
|
|
|
).flow
|
|
|
|
}
|
|
|
|
|
|
|
|
companion object {
|
|
|
|
private const val NETWORK_PAGE_SIZE = 20
|
|
|
|
}
|
|
|
|
}
|