Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ interface ContentLiteDao: BaseDao<ContentEntityLite> {
@Query("SELECT * FROM runningcontententity WHERE courseId = :courseId AND type = :type")
fun getRunningContents(courseId: Long, type: Int): PagingSource<Int, ContentEntityLite>

@Query("SELECT * FROM runningcontententity WHERE courseId = :courseId AND type = :type ORDER BY contentOrder DESC LIMIT 1")
suspend fun getLastContent(courseId: Long, type: Int): ContentEntityLite?

@Query("SELECT * FROM runningcontententity WHERE courseId = :courseId AND type = :type")
fun getUpcomingContents(courseId: Long, type: Int): PagingSource<Int, ContentEntityLite>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class CourseContentsRemoteMediator(
state: PagingState<Int, ContentEntityLite>
): MediatorResult {

pageNumber = getPageNumber(loadType,state)
pageNumber = getPageNumber(loadType)
if (pageNumber == -1) return MediatorResult.Success(endOfPaginationReached = true)

return try {
Expand All @@ -52,28 +52,23 @@ class CourseContentsRemoteMediator(
}
}

private suspend fun getPageNumber(
loadType: LoadType,
state: PagingState<Int, ContentEntityLite>
) : Int {
private suspend fun getPageNumber(loadType: LoadType) : Int {
return when (loadType) {
LoadType.REFRESH -> DEFAULT_PAGE_INDEX
LoadType.PREPEND -> -1
LoadType.APPEND -> getNextPageNumber(state)
LoadType.APPEND -> getNextPageNumber()
}
}

private suspend fun getNextPageNumber(state: PagingState<Int, ContentEntityLite>) : Int{
val remoteKeys = getRemoteKeyForLastItem(state)
private suspend fun getNextPageNumber() : Int{
val remoteKeys = getRemoteKeyForLastItem()
return remoteKeys?.nextKey ?: -1
}

private suspend fun getRemoteKeyForLastItem(state: PagingState<Int, ContentEntityLite>): ContentEntityLiteRemoteKey? {
return state.pages.lastOrNull() { it.data.isNotEmpty() }?.data
?.lastOrNull()
?.let { content ->
contentLiteRemoteKeyDao.remoteKeysContentId(content.id,type)
}
private suspend fun getRemoteKeyForLastItem(): ContentEntityLiteRemoteKey? {
return contentLiteDao.getLastContent(courseId, type)?.let { content ->
contentLiteRemoteKeyDao.remoteKeysContentId(content.id, type)
}
}

private suspend fun fetchCourseContents(page: Int = 1): ApiResponse<List<ContentEntityLite>> {
Expand Down