Skip to content

Instantly share code, notes, and snippets.

@Aydogdyshka
Last active March 16, 2021 18:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Aydogdyshka/7ca3eb654adb91477a42128de2f06ea9 to your computer and use it in GitHub Desktop.
Save Aydogdyshka/7ca3eb654adb91477a42128de2f06ea9 to your computer and use it in GitHub Desktop.
Android-Paging3 - this is a gist for solving recyclerview items flickering and jumping when using network and database
@ExperimentalPagingApi
class BlogsRemoteMediator(private val categoryId: Int,
private val service: NewsAPIInterfaceKt,
private val newsDatabase: NewsDatabaseKt,
private val tagId : Int? = null ,
private val initialPage:Int = 1
) : RemoteMediator<Int, Blog>() {
override suspend fun initialize(): InitializeAction {
return InitializeAction.LAUNCH_INITIAL_REFRESH
}
override suspend fun load(loadType: LoadType, state: PagingState<Int, Blog>): MediatorResult {
try {
val page = when (loadType) {
REFRESH ->{
Log.d("BlogsRemoteMediator", "Refresh is called")
initialPage }
PREPEND -> { return MediatorResult.Success(endOfPaginationReached = true)}
APPEND -> {
Log.d("BlogsRemoteMediator", "Append is called")
val remoteKey = newsDatabase.withTransaction {
newsDatabase.remoteKeyDao().remoteKeyByLatest(categoryId.toString())
}
if(remoteKey.nextPageKey == null){
return MediatorResult.Success(endOfPaginationReached = true)
}
remoteKey.nextPageKey.toInt()
}
}
val apiResponse =
if(tagId == null) {
service.getCategoryResponsePage(RU, categoryId, page.toString())
}else{
service.getCategoryTagResponsePage(RU,categoryId,tagId,page.toString())
}
//getting the list of blogs from response
val blogs = apiResponse.blogs
val endOfPaginationReached = blogs.size < state.config.pageSize
newsDatabase.withTransaction {
// clear all tables in the database
if (loadType == LoadType.REFRESH) {
newsDatabase.remoteKeyDao().deleteByLatest(categoryId.toString())
if(tagId == null) {
newsDatabase.articleDAOKt().clearBlogsByCatId(categoryId)
}else {
newsDatabase.articleDAOKt().clearBlogsByCatId(categoryId,tagId)
}
}
blogs.map {blog ->
blog.categoryId = categoryId
if(tagId != null) {
blog.tagId = tagId
}
Log.d("BlogsRemoteMediator", "The blog id is " + blog.id.toString())
}
newsDatabase.remoteKeyDao().insert(LatestRemoteKey(categoryId.toString(),apiResponse.nextPageParam))
newsDatabase.articleDAOKt().insertAll(blogs)
}
return MediatorResult.Success(
endOfPaginationReached = endOfPaginationReached
)
} catch (exception: IOException) {
return MediatorResult.Error(exception)
} catch (exception: HttpException) {
return MediatorResult.Error(exception)
}
}
}
@ExperimentalPagingApi
private fun loadData(categoryId:Int, tagId : Int? = null) {
searchJob?.cancel()
searchJob = lifecycleScope.launch {
viewModelKt.getBlogsUniversalWithUiModel(categoryId, tagId).collectLatest {
/* I logged pages loaded by PagingSource like this, if i am doing it wrong please feel free to correct me...Thanks in advance
pagedBlogsAdapter.snapshot().items.map {
Log.d("PagingData","" + it.blog.id )
}
pagedBlogsAdapter.submitData(it)
}
}
}
2021-03-16 17:22:32.264 27389-27389/com.sportmedia D/BlogsRemoteMediator: Refresh is called
2021-03-16 17:22:32.653 27389-27427/com.sportmedia D/BlogsRemoteMediator: The blog id is 698
2021-03-16 17:22:32.653 27389-27427/com.sportmedia D/BlogsRemoteMediator: The blog id is 696
2021-03-16 17:22:32.653 27389-27427/com.sportmedia D/BlogsRemoteMediator: The blog id is 695
2021-03-16 17:22:32.653 27389-27427/com.sportmedia D/BlogsRemoteMediator: The blog id is 694
2021-03-16 17:22:32.653 27389-27427/com.sportmedia D/BlogsRemoteMediator: The blog id is 680
2021-03-16 17:22:32.653 27389-27427/com.sportmedia D/BlogsRemoteMediator: The blog id is 679
2021-03-16 17:22:32.653 27389-27427/com.sportmedia D/BlogsRemoteMediator: The blog id is 678
2021-03-16 17:22:32.653 27389-27427/com.sportmedia D/BlogsRemoteMediator: The blog id is 674
2021-03-16 17:22:32.653 27389-27427/com.sportmedia D/BlogsRemoteMediator: The blog id is 673
2021-03-16 17:22:32.653 27389-27427/com.sportmedia D/BlogsRemoteMediator: The blog id is 664
2021-03-16 17:22:34.472 27389-27389/com.sportmedia D/BlogsRemoteMediator: Append is called
2021-03-16 17:22:37.639 27389-27427/com.sportmedia D/BlogsRemoteMediator: The blog id is 663
2021-03-16 17:22:37.639 27389-27427/com.sportmedia D/BlogsRemoteMediator: The blog id is 662
2021-03-16 17:22:37.639 27389-27427/com.sportmedia D/BlogsRemoteMediator: The blog id is 661
2021-03-16 17:22:37.639 27389-27427/com.sportmedia D/BlogsRemoteMediator: The blog id is 660
2021-03-16 17:22:37.639 27389-27427/com.sportmedia D/BlogsRemoteMediator: The blog id is 647
2021-03-16 17:22:37.639 27389-27427/com.sportmedia D/BlogsRemoteMediator: The blog id is 645
2021-03-16 17:22:37.639 27389-27427/com.sportmedia D/BlogsRemoteMediator: The blog id is 644
2021-03-16 17:22:37.639 27389-27427/com.sportmedia D/BlogsRemoteMediator: The blog id is 643
2021-03-16 17:22:37.639 27389-27427/com.sportmedia D/BlogsRemoteMediator: The blog id is 642
2021-03-16 17:22:37.639 27389-27427/com.sportmedia D/BlogsRemoteMediator: The blog id is 641
2021-03-16 17:22:39.021 27389-27389/com.sportmedia D/BlogsRemoteMediator: Append is called
2021-03-16 17:22:39.157 27389-27427/com.sportmedia D/BlogsRemoteMediator: The blog id is 640
2021-03-16 17:22:39.157 27389-27427/com.sportmedia D/BlogsRemoteMediator: The blog id is 635
2021-03-16 17:22:39.157 27389-27427/com.sportmedia D/BlogsRemoteMediator: The blog id is 634
2021-03-16 17:22:39.157 27389-27427/com.sportmedia D/BlogsRemoteMediator: The blog id is 633
2021-03-16 17:22:39.157 27389-27427/com.sportmedia D/BlogsRemoteMediator: The blog id is 632
2021-03-16 17:22:39.157 27389-27427/com.sportmedia D/BlogsRemoteMediator: The blog id is 627
2021-03-16 17:22:39.157 27389-27427/com.sportmedia D/BlogsRemoteMediator: The blog id is 619
2021-03-16 17:22:39.157 27389-27427/com.sportmedia D/BlogsRemoteMediator: The blog id is 611
2021-03-16 17:22:39.157 27389-27427/com.sportmedia D/BlogsRemoteMediator: The blog id is 610
2021-03-16 17:22:39.157 27389-27427/com.sportmedia D/BlogsRemoteMediator: The blog id is 609
---when i do refresh the first time
2021-03-16 18:33:48.965 2386-2386/com.sportmedia D/PagingData: 698
2021-03-16 18:33:48.965 2386-2386/com.sportmedia D/PagingData: 696
2021-03-16 18:33:48.965 2386-2386/com.sportmedia D/PagingData: 695
2021-03-16 18:33:48.965 2386-2386/com.sportmedia D/PagingData: 694
2021-03-16 18:33:48.965 2386-2386/com.sportmedia D/PagingData: 680
2021-03-16 18:33:48.965 2386-2386/com.sportmedia D/PagingData: 679
2021-03-16 18:33:48.965 2386-2386/com.sportmedia D/PagingData: 678
---then i scroll till the end, and go to the top and refresh again
2021-03-16 18:35:59.238 2386-2386/com.sportmedia D/PagingData: 698
2021-03-16 18:35:59.238 2386-2386/com.sportmedia D/PagingData: 696
2021-03-16 18:35:59.238 2386-2386/com.sportmedia D/PagingData: 695
2021-03-16 18:35:59.238 2386-2386/com.sportmedia D/PagingData: 694
2021-03-16 18:35:59.238 2386-2386/com.sportmedia D/PagingData: 680
2021-03-16 18:35:59.238 2386-2386/com.sportmedia D/PagingData: 679
2021-03-16 18:35:59.238 2386-2386/com.sportmedia D/PagingData: 678
2021-03-16 18:35:59.238 2386-2386/com.sportmedia D/PagingData: 674
2021-03-16 18:35:59.238 2386-2386/com.sportmedia D/PagingData: 673
2021-03-16 18:35:59.238 2386-2386/com.sportmedia D/PagingData: 664
2021-03-16 18:35:59.238 2386-2386/com.sportmedia D/PagingData: 663
2021-03-16 18:35:59.238 2386-2386/com.sportmedia D/PagingData: 662
2021-03-16 18:35:59.238 2386-2386/com.sportmedia D/PagingData: 661
2021-03-16 18:35:59.238 2386-2386/com.sportmedia D/PagingData: 660
2021-03-16 18:35:59.238 2386-2386/com.sportmedia D/PagingData: 647
2021-03-16 18:35:59.238 2386-2386/com.sportmedia D/PagingData: 645
2021-03-16 18:35:59.238 2386-2386/com.sportmedia D/PagingData: 644
2021-03-16 18:35:59.238 2386-2386/com.sportmedia D/PagingData: 643
2021-03-16 18:35:59.238 2386-2386/com.sportmedia D/PagingData: 642
2021-03-16 18:35:59.238 2386-2386/com.sportmedia D/PagingData: 641
2021-03-16 18:35:59.238 2386-2386/com.sportmedia D/PagingData: 640
2021-03-16 18:35:59.238 2386-2386/com.sportmedia D/PagingData: 635
2021-03-16 18:35:59.238 2386-2386/com.sportmedia D/PagingData: 634
2021-03-16 18:35:59.238 2386-2386/com.sportmedia D/PagingData: 633
2021-03-16 18:35:59.238 2386-2386/com.sportmedia D/PagingData: 632
2021-03-16 18:35:59.238 2386-2386/com.sportmedia D/PagingData: 627
2021-03-16 18:35:59.238 2386-2386/com.sportmedia D/PagingData: 619
2021-03-16 18:35:59.238 2386-2386/com.sportmedia D/PagingData: 611
2021-03-16 18:35:59.238 2386-2386/com.sportmedia D/PagingData: 610
2021-03-16 18:35:59.238 2386-2386/com.sportmedia D/PagingData: 609
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment