diff --git a/app/src/main/java/com/example/dtf/dao/CategoryDao.kt b/app/src/main/java/com/example/dtf/dao/CategoryDao.kt index 238be5c..316969b 100644 --- a/app/src/main/java/com/example/dtf/dao/CategoryDao.kt +++ b/app/src/main/java/com/example/dtf/dao/CategoryDao.kt @@ -27,4 +27,7 @@ interface CategoryDao { @Query("select * from category limit 1") fun getFirst() : Flow + + @Query("select * from category where id >= :loadKey limit :limit") + fun getByLoadKey(loadKey: Int, limit: Int) : Flow> } diff --git a/app/src/main/java/com/example/dtf/dao/CommentDao.kt b/app/src/main/java/com/example/dtf/dao/CommentDao.kt index d89a301..d411302 100644 --- a/app/src/main/java/com/example/dtf/dao/CommentDao.kt +++ b/app/src/main/java/com/example/dtf/dao/CommentDao.kt @@ -22,6 +22,9 @@ interface CommentDao { @Query("select * from comment where comment.post_id = :postId ORDER BY id DESC") fun getByPost(postId: Int) : PagingSource - @Query("delete from comment where post_id = :query") - fun deleteByQuery(query: String) + @Query("select * from comment where id >= :loadKey and post_id = :postId order by id desc limit :limit") + fun getByLoadKeyInitial(postId: Int, loadKey: Int, limit: Int) : Flow> + + @Query("select * from comment where id <= :loadKey and post_id = :postId order by id desc limit :limit") + fun getByLoadKey(postId: Int, loadKey: Int, limit: Int) : Flow> } diff --git a/app/src/main/java/com/example/dtf/dao/PostDao.kt b/app/src/main/java/com/example/dtf/dao/PostDao.kt index c2a762b..7a8d690 100644 --- a/app/src/main/java/com/example/dtf/dao/PostDao.kt +++ b/app/src/main/java/com/example/dtf/dao/PostDao.kt @@ -22,6 +22,6 @@ interface PostDao { @Query("select * from post where post.category_id = :categoryId ORDER BY date DESC") fun getByCategory(categoryId: String) : PagingSource - @Query("delete from post where category_id = :query") - fun deleteByQuery(query: String) + @Query("select * from post where category_id = :categoryId and id >= :loadKey ORDER BY date DESC limit :limit") + fun getByLoadKey(categoryId: Int, loadKey: Int, limit: Int) : Flow> } diff --git a/app/src/main/java/com/example/dtf/repositories/online/mediator/CategoryMediator.kt b/app/src/main/java/com/example/dtf/repositories/online/mediator/CategoryMediator.kt index 445bb41..e0a053b 100644 --- a/app/src/main/java/com/example/dtf/repositories/online/mediator/CategoryMediator.kt +++ b/app/src/main/java/com/example/dtf/repositories/online/mediator/CategoryMediator.kt @@ -8,6 +8,7 @@ import androidx.room.withTransaction import com.example.dtf.api.ServerService import com.example.dtf.db.AppDatabase import com.example.dtf.models.Category +import kotlinx.coroutines.flow.first import java.io.IOException @OptIn(ExperimentalPagingApi::class) @@ -42,11 +43,14 @@ class CategoryMediator ( limit = state.config.pageSize ) - database.withTransaction { + val present = categoryDao.getByLoadKey(loadKey, state.config.pageSize).first() + if (loadType == LoadType.REFRESH) { - for (category in response.categories) { - categoryDao.delete(category) + for (category in present) { + if (category !in response.categories){ + categoryDao.delete(category) + } } } diff --git a/app/src/main/java/com/example/dtf/repositories/online/mediator/CommentMediator.kt b/app/src/main/java/com/example/dtf/repositories/online/mediator/CommentMediator.kt index 8c28105..8b6b088 100644 --- a/app/src/main/java/com/example/dtf/repositories/online/mediator/CommentMediator.kt +++ b/app/src/main/java/com/example/dtf/repositories/online/mediator/CommentMediator.kt @@ -8,6 +8,7 @@ import androidx.room.withTransaction import com.example.dtf.api.ServerService import com.example.dtf.db.AppDatabase import com.example.dtf.models.Comment +import kotlinx.coroutines.flow.first import java.io.IOException @OptIn(ExperimentalPagingApi::class) @@ -46,8 +47,18 @@ class CommentMediator ( database.withTransaction { + val present = if (loadKey == 0) { + commentDao.getByLoadKeyInitial(query.toInt(), loadKey, state.config.pageSize).first() + } else { + commentDao.getByLoadKey(query.toInt(), loadKey, state.config.pageSize).first() + } + if (loadType == LoadType.REFRESH) { - commentDao.deleteByQuery(query) + for (comment in present) { + if (comment !in response.comments) { + commentDao.delete(comment) + } + } } for (comment in response.comments) { diff --git a/app/src/main/java/com/example/dtf/repositories/online/mediator/PostMediator.kt b/app/src/main/java/com/example/dtf/repositories/online/mediator/PostMediator.kt index 97e166d..39a9e86 100644 --- a/app/src/main/java/com/example/dtf/repositories/online/mediator/PostMediator.kt +++ b/app/src/main/java/com/example/dtf/repositories/online/mediator/PostMediator.kt @@ -8,6 +8,7 @@ import androidx.room.withTransaction import com.example.dtf.api.ServerService import com.example.dtf.db.AppDatabase import com.example.dtf.models.Post +import kotlinx.coroutines.flow.first import java.io.IOException @OptIn(ExperimentalPagingApi::class) @@ -46,12 +47,18 @@ class PostMediator ( database.withTransaction { + val present = postDao.getByLoadKey(query.toInt(), loadKey, state.config.pageSize).first() + if (loadType == LoadType.REFRESH) { - postDao.deleteByQuery(query) + for (post in present) { + if (post !in response.posts) { + postDao.delete(post) + } + } } - for (comment in response.posts) { - postDao.insert(comment) + for (post in response.posts) { + postDao.insert(post) } }