diff --git a/core/src/main/java/in/testpress/database/dao/ContentLiteDao.kt b/core/src/main/java/in/testpress/database/dao/ContentLiteDao.kt index c22cbf022..d9f4c1c80 100644 --- a/core/src/main/java/in/testpress/database/dao/ContentLiteDao.kt +++ b/core/src/main/java/in/testpress/database/dao/ContentLiteDao.kt @@ -12,6 +12,9 @@ interface ContentLiteDao: BaseDao { @Query("SELECT * FROM runningcontententity WHERE courseId = :courseId AND type = :type") fun getRunningContents(courseId: Long, type: Int): PagingSource + @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 diff --git a/course/src/main/java/in/testpress/course/pagination/CourseContentsRemoteMediator.kt b/course/src/main/java/in/testpress/course/pagination/CourseContentsRemoteMediator.kt index dde93bde1..05722a43c 100644 --- a/course/src/main/java/in/testpress/course/pagination/CourseContentsRemoteMediator.kt +++ b/course/src/main/java/in/testpress/course/pagination/CourseContentsRemoteMediator.kt @@ -36,7 +36,7 @@ class CourseContentsRemoteMediator( state: PagingState ): MediatorResult { - pageNumber = getPageNumber(loadType,state) + pageNumber = getPageNumber(loadType) if (pageNumber == -1) return MediatorResult.Success(endOfPaginationReached = true) return try { @@ -52,28 +52,23 @@ class CourseContentsRemoteMediator( } } - private suspend fun getPageNumber( - loadType: LoadType, - state: PagingState - ) : 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{ - val remoteKeys = getRemoteKeyForLastItem(state) + private suspend fun getNextPageNumber() : Int{ + val remoteKeys = getRemoteKeyForLastItem() return remoteKeys?.nextKey ?: -1 } - private suspend fun getRemoteKeyForLastItem(state: PagingState): 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> {