Курсовая: Вроде как доделано нормальное кэширование
This commit is contained in:
parent
87332767e7
commit
7b3fcfa792
@ -27,4 +27,7 @@ interface CategoryDao {
|
|||||||
|
|
||||||
@Query("select * from category limit 1")
|
@Query("select * from category limit 1")
|
||||||
fun getFirst() : Flow<Category>
|
fun getFirst() : Flow<Category>
|
||||||
|
|
||||||
|
@Query("select * from category where id >= :loadKey limit :limit")
|
||||||
|
fun getByLoadKey(loadKey: Int, limit: Int) : Flow<List<Category>>
|
||||||
}
|
}
|
||||||
|
@ -22,6 +22,9 @@ interface CommentDao {
|
|||||||
@Query("select * from comment where comment.post_id = :postId ORDER BY id DESC")
|
@Query("select * from comment where comment.post_id = :postId ORDER BY id DESC")
|
||||||
fun getByPost(postId: Int) : PagingSource<Int, Comment>
|
fun getByPost(postId: Int) : PagingSource<Int, Comment>
|
||||||
|
|
||||||
@Query("delete from comment where post_id = :query")
|
@Query("select * from comment where id >= :loadKey and post_id = :postId order by id desc limit :limit")
|
||||||
fun deleteByQuery(query: String)
|
fun getByLoadKeyInitial(postId: Int, loadKey: Int, limit: Int) : Flow<List<Comment>>
|
||||||
|
|
||||||
|
@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<List<Comment>>
|
||||||
}
|
}
|
||||||
|
@ -22,6 +22,6 @@ interface PostDao {
|
|||||||
@Query("select * from post where post.category_id = :categoryId ORDER BY date DESC")
|
@Query("select * from post where post.category_id = :categoryId ORDER BY date DESC")
|
||||||
fun getByCategory(categoryId: String) : PagingSource<Int, Post>
|
fun getByCategory(categoryId: String) : PagingSource<Int, Post>
|
||||||
|
|
||||||
@Query("delete from post where category_id = :query")
|
@Query("select * from post where category_id = :categoryId and id >= :loadKey ORDER BY date DESC limit :limit")
|
||||||
fun deleteByQuery(query: String)
|
fun getByLoadKey(categoryId: Int, loadKey: Int, limit: Int) : Flow<List<Post>>
|
||||||
}
|
}
|
||||||
|
@ -8,6 +8,7 @@ import androidx.room.withTransaction
|
|||||||
import com.example.dtf.api.ServerService
|
import com.example.dtf.api.ServerService
|
||||||
import com.example.dtf.db.AppDatabase
|
import com.example.dtf.db.AppDatabase
|
||||||
import com.example.dtf.models.Category
|
import com.example.dtf.models.Category
|
||||||
|
import kotlinx.coroutines.flow.first
|
||||||
import java.io.IOException
|
import java.io.IOException
|
||||||
|
|
||||||
@OptIn(ExperimentalPagingApi::class)
|
@OptIn(ExperimentalPagingApi::class)
|
||||||
@ -42,11 +43,14 @@ class CategoryMediator (
|
|||||||
limit = state.config.pageSize
|
limit = state.config.pageSize
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
database.withTransaction {
|
database.withTransaction {
|
||||||
|
val present = categoryDao.getByLoadKey(loadKey, state.config.pageSize).first()
|
||||||
|
|
||||||
if (loadType == LoadType.REFRESH) {
|
if (loadType == LoadType.REFRESH) {
|
||||||
for (category in response.categories) {
|
for (category in present) {
|
||||||
categoryDao.delete(category)
|
if (category !in response.categories){
|
||||||
|
categoryDao.delete(category)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -8,6 +8,7 @@ import androidx.room.withTransaction
|
|||||||
import com.example.dtf.api.ServerService
|
import com.example.dtf.api.ServerService
|
||||||
import com.example.dtf.db.AppDatabase
|
import com.example.dtf.db.AppDatabase
|
||||||
import com.example.dtf.models.Comment
|
import com.example.dtf.models.Comment
|
||||||
|
import kotlinx.coroutines.flow.first
|
||||||
import java.io.IOException
|
import java.io.IOException
|
||||||
|
|
||||||
@OptIn(ExperimentalPagingApi::class)
|
@OptIn(ExperimentalPagingApi::class)
|
||||||
@ -46,8 +47,18 @@ class CommentMediator (
|
|||||||
|
|
||||||
|
|
||||||
database.withTransaction {
|
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) {
|
if (loadType == LoadType.REFRESH) {
|
||||||
commentDao.deleteByQuery(query)
|
for (comment in present) {
|
||||||
|
if (comment !in response.comments) {
|
||||||
|
commentDao.delete(comment)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (comment in response.comments) {
|
for (comment in response.comments) {
|
||||||
|
@ -8,6 +8,7 @@ import androidx.room.withTransaction
|
|||||||
import com.example.dtf.api.ServerService
|
import com.example.dtf.api.ServerService
|
||||||
import com.example.dtf.db.AppDatabase
|
import com.example.dtf.db.AppDatabase
|
||||||
import com.example.dtf.models.Post
|
import com.example.dtf.models.Post
|
||||||
|
import kotlinx.coroutines.flow.first
|
||||||
import java.io.IOException
|
import java.io.IOException
|
||||||
|
|
||||||
@OptIn(ExperimentalPagingApi::class)
|
@OptIn(ExperimentalPagingApi::class)
|
||||||
@ -46,12 +47,18 @@ class PostMediator (
|
|||||||
|
|
||||||
|
|
||||||
database.withTransaction {
|
database.withTransaction {
|
||||||
|
val present = postDao.getByLoadKey(query.toInt(), loadKey, state.config.pageSize).first()
|
||||||
|
|
||||||
if (loadType == LoadType.REFRESH) {
|
if (loadType == LoadType.REFRESH) {
|
||||||
postDao.deleteByQuery(query)
|
for (post in present) {
|
||||||
|
if (post !in response.posts) {
|
||||||
|
postDao.delete(post)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (comment in response.posts) {
|
for (post in response.posts) {
|
||||||
postDao.insert(comment)
|
postDao.insert(post)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user