Android Paging 3 leading to duplicate rows - android

I was trying out the Paging 3.0.1 version. The API calls are happening right when I printed the log. But the data shown is duplicate. Could someone tell me where I went wrong?
Page data source class
class MyPageDataSource(private val api: RetrofitInstance) :
PagingSource<Int, APIDataResponse>() {
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, APIDataResponse> {
return try {
val nextPageNumber = params.key ?: FIRST_PAGE_NUMBER
val response = api.getData(nextPageNumber, PAGE_SIZE)
LoadResult.Page(
data = response.APIS!!,
prevKey = if (nextPageNumber > FIRST_PAGE_NUMBER) nextPageNumber - 1 else null,
nextKey = if (nextPageNumber * PAGE_SIZE < response.total!!) nextPageNumber + 1 else null
)
} catch (e: Exception) {
LoadResult.Error(e)
}
}
override fun getRefreshKey(state: PagingState<Int, APIDataResponse>): Int? {
return state.anchorPosition
}
companion object {
const val FIRST_PAGE_NUMBER = 1
const val PAGE_SIZE = 20
}
}
Adapter:
class MyListingAdapter() : PagingDataAdapter<APIDataResponse, MyListingAdapter.MyViewHolder>(MyComparator) {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
return MyViewHolder(
FragmentItemBinding.inflate(
LayoutInflater.from(parent.context),
parent,
false
)
)
}
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
holder.bind(item = getItem(position))
}
inner class MyViewHolder(binding: FragmentItemBinding) :
RecyclerView.ViewHolder(binding.root) {
private val title: TextView = binding.title
fun bind(item: APIDataResponse?) {
if(item != null) {
title.text = item.title
}
}
}
object MyComparator : DiffUtil.ItemCallback<APIDataResponse>() {
override fun areItemsTheSame(
oldItem: APIDataResponse,
newItem: APIDataResponse
): Boolean {
return oldItem.id == newItem.id
}
override fun areContentsTheSame(
oldItem: APIDataResponse,
newItem: APIDataResponse
): Boolean {
return oldItem == newItem
}
}
}
View Model:
class PagingViewModel : ViewModel() {
fun getData() : Flow<PagingData<APIDataResponse>> {
return Pager(
PagingConfig(
pageSize = 20,
enablePlaceholders = false,
maxSize = 40,
initialLoadSize = 20,
prefetchDistance = 10
)
) {
MyPageDataSource(RetrofitInstance())
}.flow.cachedIn(viewModelScope)
}
}
Recycler view setting up in the fragment:
val myAdapter = MyListingAdapter(myActivity)
//Setup the recyclerview
binding.myList.apply {
layoutManager = when {
columnCount <= 1 -> LinearLayoutManager(context)
else -> GridLayoutManager(context, columnCount)
}
myAdapter.stateRestorationPolicy = RecyclerView.Adapter.StateRestorationPolicy.PREVENT_WHEN_EMPTY
val decoration =
DividerItemDecoration(myActivity, DividerItemDecoration.VERTICAL)
addItemDecoration(decoration)
setHasFixedSize(true)
adapter = myAdapter
}
lifecycleScope.launch {
viewModel.getData().distinctUntilChanged().collectLatest { pagedData ->
myAdapter.submitData(pagedData)
}
}

I had the same problem.
It solved by setting the correct pageSize.
This problem can also happen by setting the wrong maxSize, initialLoadSize or other attributes.

You have set setHasFixedSize(true) it means data won't change because of a change in the adapter content. For example, the RecyclerView size can change because of a size change on its parent. Maybe that's why you are getting the same records. try to remove it and then check it works or not.

Related

Is it possible to display chat messages through the Android Paging Library?

I want to merge two message list(stored chat message, incoming chat message)
these two data is split based on the last check time.
I want stored chat message to be Paging and merge with incoming data
This is my code:
#Query("SELECT * FROM msgentity WHERE room_id =:roomId and time <= :lastReadTime ORDER BY time")
fun getPassedMsg(roomId: String, lastReadTime: Date) : PagingSource<Int, MsgEntity>
#Query("SELECT * FROM msgentity WHERE room_id=:roomId and time > :lastReadTime ORDER BY time")
suspend fun getNewMsg(roomId: String, lastReadTime: Date) : Flow<List<MsgEntity>>
Is it possible to merge these two items of data?
Or do I have to make my own paging object?
Yeah definitely, you can show messages using paging.
if your question is that how ?
So here I am using MVVM architecture , Kotlin coroutine
Step 1. you need to create a paging source for request and getting response
(here MessageList is api response model )
class MessagePaging(private val apiService: ApiService) : PagingSource<Int, MessageList.Data>() {
private var STARTING_PAGE_INDEX = 1
override fun getRefreshKey(state: PagingState<Int, MessageList.Data>): Int? {
return 1
}
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, MessageList.Data> {
val page = params.key ?: STARTING_PAGE_INDEX
return try {
val response = apiService.getMessageList(page)
val data = response.body()?.data!!
if (!data.isNullOrEmpty()){
LoadResult.Page(
data = data,
prevKey = if (page == STARTING_PAGE_INDEX) null else page -1,
nextKey = if (data.isEmpty()) null else page + 1
)
}else{
return LoadResult.Error(NullPointerException("Data is Null or Empty"))
}
} catch (e : HttpException){
return LoadResult.Error(e)
} catch (e : IOException){
return LoadResult.Error(e)
} catch (e : Exception){
return LoadResult.Error(e)
}
}
}
Step 2 :- Create request in apiservice
#GET( GET_CONSULTATION_LIST )
suspend fun getMessageList(#Query("page") page : Int) : Response<MessageList>
Step 3:- in ApiHelperImpl interface
fun getConsultationList(): Flow<PagingData<ConsultationList.Data>> {
return Pager(
config = PagingConfig(
pageSize = 40,
enablePlaceholders = false,
prefetchDistance = 1
),
pagingSourceFactory = { ConsultantPaging(apiService) }
).flow
}
Step 4 :- In View model
fun getConsultationList() = apiHelper.getConsultationList().cachedIn(viewModelScope)
Step 5 :- In Fragment
private fun observeConsultations() {
lifecycleScope.launch {
viewModel.getConsultationList().collectLatest {
launch(Dispatchers.Main) {
adapter.loadStateFlow.collectLatest { loadStates ->
if (loadStates.refresh is LoadState.Loading) {
// loader.show()
} else {
// loader.dismiss()
if (loadStates.refresh is LoadState.Error) {
if (adapter.itemCount < 1) {
binding.clNoConsult.visibility = View.VISIBLE
} else {
binding.clNoConsult.visibility = View.GONE
}
}
}
}
}
adapter.submitData(it)
}
}
}
Step 6:- Create Paging Adapter
class ConsultationPagingAdapter (private val click: GetClicksOnItem ) : PagingDataAdapter<MessageList.Data, ConsultationPagingAdapter.ViewHolder>(DiffCallback()) {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
return ViewHolder(ListConsultationsBinding.inflate(LayoutInflater.from(parent.context), parent, false))
}
#SuppressLint("SetTextI18n")
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val result = getItem(position)!!
holder.binding.apply {
tvName.text = result.name.capitalizeWords()
root.setOnClickListener {
click.action(result)
}
executePendingBindings()
}
}
class ViewHolder(val binding: ListConsultationsBinding) : RecyclerView.ViewHolder(binding.root)
private class DiffCallback : DiffUtil.ItemCallback<MessageList.Data>() {
override fun areItemsTheSame(
oldItem: MessageList.Data,
newItem: MessageList.Data
): Boolean = oldItem == newItem
override fun areContentsTheSame(
oldItem: MessageList.Data,
newItem: MessageList.Data
): Boolean = oldItem == newItem
}
interface GetClicksOnItem {
fun action(data: MessageList.Data)
}
}
If you getting understating then approve the answer

Multiple times list item populated in recycler-view, when using Paging v3

I have implemented android-paging v3 following https://proandroiddev.com/paging-3-easier-way-to-pagination-part-1-584cad1f4f61 & https://proandroiddev.com/how-to-use-the-paging-3-library-in-android-part-2-e2011070a37d.
But I see the data is populated multiple times even when there are just 3 records in the local database.
Notifications list screen
Can anyone suggest what I am doing wrong? Thanks in advance.
My code is as follows:
NotificationsFragment
class NotificationsFragment : Fragment() {
private lateinit var binding: FragmentNotificationsBinding
private val alertViewModel: NotificationsViewModel by viewModel()
private val pagingAdapter by lazy { AlertsPagingAdapter() }
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
binding = FragmentNotificationsBinding.inflate(inflater, container, false)
return binding.root
}
override fun onResume() {
super.onResume()
(activity as MainActivity).setUpCustomToolbar(
getString(R.string.alerts),
""
)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding.lifecycleOwner = viewLifecycleOwner
initRecyclerView()
}
private fun initRecyclerView() {
binding.rvAlerts.apply {
adapter = pagingAdapter.withLoadStateFooter(AlertLoadStateAdapter {})
layoutManager = LinearLayoutManager(requireContext())
}
lifecycleScope.launch {
alertViewModel.alertListFlow.collectLatest { pagingData ->
pagingAdapter.submitData(
pagingData
)
}
}
}
}
NotificationsViewModel
class NotificationsViewModel(private val useCase: NotificationsUseCase) : BaseViewModel() {
val alertListFlow = Pager(PagingConfig(1)) { NotificationsPagingSource(useCase) }
.flow
.cachedIn(viewModelScope)
}
NotificationsPagingSource
import androidx.paging.PagingSource
import androidx.paging.PagingState
import com.example.demo.model.entity.Notifications
import com.example.demo.NotificationsUseCase
class NotificationsPagingSource(private val useCase: NotificationsUseCase) : PagingSource<Int, Notifications>() {
private companion object {
const val INITIAL_PAGE_INDEX = 0
}
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, Notifications> {
val position = params.key ?: INITIAL_PAGE_INDEX
val randomNotifications : List<Notifications> = useCase.fetchNotifications(params.loadSize)
return LoadResult.Page(
data = randomNotifications ,
prevKey = if (position == INITIAL_PAGE_INDEX) null else position - 1,
nextKey = if (randomAlerts.isNullOrEmpty()) null else position + 1
)
}
override fun getRefreshKey(state: PagingState<Int, Notifications>): Int? {
// We need to get the previous key (or next key if previous is null) of the page
// that was closest to the most recently accessed index.
// Anchor position is the most recently accessed index
return state.anchorPosition?.let { anchorPosition ->
state.closestPageToPosition(anchorPosition)?.prevKey?.plus(1)
?: state.closestPageToPosition(anchorPosition)?.nextKey?.minus(1)
}
}
}
PagingAdapter
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.paging.PagingDataAdapter
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
class NotificationsPagingAdapter :
PagingDataAdapter<Notifications, NotificationsPagingAdapter.ItemNotificationsViewHolder>(NotificationsEntityDiff()) {
override fun onBindViewHolder(holder: ItemNotificationsViewHolder, position: Int) {
getItem(position)?.let { userPostEntity -> holder.bind(userPostEntity) }
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemNotificationsViewHolder {
return ItemNotificationsViewHolder(
ItemLayoutNotificationsBinding.inflate(
LayoutInflater.from(parent.context),
parent,
false
)
)
}
/**
* Viewholder for each Notifications layout item
*/
inner class ItemNotificationsViewHolder(private val binding: ItemLayoutNotificationsBinding) :
RecyclerView.ViewHolder(binding.root) {
fun bind(alert: Notifications) {
binding.tvMessage.text = alert.title
}
}
class NotificationsEntityDiff : DiffUtil.ItemCallback<Notifications>() {
override fun areItemsTheSame(oldItem: Notifications, newItem: Notifications): Boolean =
oldItem.id == newItem.id
override fun areContentsTheSame(oldItem: Alert, newItem: Notifications): Boolean =
oldItem == newItem
}
}
NotificationsLoadStateAdapter
class NotificationsLoadStateAdapter(
private val retry: () -> Unit
) : LoadStateAdapter<NotificationsLoadStateAdapter.LoadStateViewHolder>() {
override fun onBindViewHolder(holder: LoadStateViewHolder, loadState: LoadState) {
val progress = holder.itemView.load_state_progress
val btnRetry = holder.itemView.load_state_retry
val txtErrorMessage = holder.itemView.load_state_errorMessage
btnRetry.isVisible = loadState !is LoadState.Loading
// txtErrorMessage.isVisible = loadState !is LoadState.Loading
progress.isVisible = loadState is LoadState.Loading
if (loadState is LoadState.Error) {
// txtErrorMessage.text = loadState.error.localizedMessage
}
btnRetry.setOnClickListener {
retry.invoke()
}
}
override fun onCreateViewHolder(parent: ViewGroup, loadState: LoadState): LoadStateViewHolder {
return LoadStateViewHolder(
LayoutInflater.from(parent.context)
.inflate(R.layout.layout_load_state_view, parent, false)
)
}
class LoadStateViewHolder(private val view: View) : RecyclerView.ViewHolder(view)
}
Dao
#Query("SELECT * FROM notifications ORDER BY createdAt DESC LIMIT :size")
fun fetchNotifications(size: Int): List<Notifications>
The issue is fixed. Actually, the Dao query was wrong and the Paging attributes were wrong.
Dao
#Query("SELECT * FROM notifications ORDER By createdAt DESC LIMIT :size OFFSET (:page * :size)")
fun fetchAlerts(page: Int, size: Int): List<Notifications>
NotificationsViewModel
class NotificationsViewModel(private val useCase: NotificationUseCase) : BaseViewModel() {
// If you want to load at a time one page keep pageSize 1
val alertListFlow =
Pager(PagingConfig(pageSize = 1)) { createPagingSource() }
.flow
.cachedIn(viewModelScope)
/**
* Set up the paging source and the initial page should be 1
*/
private fun createPagingSource(): BasePagingSource<Notifications> {
return BasePagingSource() { page, pageSize, _ ->
useCase.fetchNotifications(page, pageSize)
}
}
}
and use BasePagingSource instead of NotificationsPagingSource
/**
* This is the base class for a custom [PagingSource]
*
* #param T the data expected to process
* #property pageSize
* #property initialPage
* #property fetchDataCallback where the concrete API call is executed to fetch the data
*
*/
class BasePagingSource<T : Any>(
private val pageSize: Int = 10,
private val initialPage: Int = 0,
private val fetchDataCallback: suspend (Int, Int, Boolean) -> List<T>
) : PagingSource<Int, T>() {
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, T> {
val page = params.key ?: initialPage
val data = fetchDataCallback.invoke(page, pageSize, page == initialPage)
// https://android-developers.googleblog.com/2020/07/getting-on-same-page-with-paging-3.html
val prevKey = null
val nextKey = if (data.isNullOrEmpty() || data.size < pageSize) {
null
} else {
page + 1
}
MyLogger.d("page=$page, params.key=${params.key}, pageSize=$pageSize, prevKey=$prevKey, nextKey=$nextKey, resultSize=${data.size}")
return LoadResult.Page(data, prevKey, nextKey)
}
override fun getRefreshKey(state: PagingState<Int, T>): Int? {
// just return null and eventually it will use the passed initialPage
return null
}
}

Paging 3 keeps doing api calls without reaching the end of recyclerview

Paging 3 keeps doing api calls without reaching the end of recyclerview
i tired to change page size to 15 but still the same
does using base paging source could lead to any problem?
this is the XML of the view
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="#+id/stl"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:descendantFocusability="blocksDescendants"
android:paddingStart="8dp"
android:paddingTop="8dp"
android:paddingEnd="8dp"
android:paddingBottom="80dp" />
base adapter
abstract class BasePagingAdapter<T : Any, VH : BasePagingAdapter.BaseViewHolder<T>>(diffCallback: DiffUtil.ItemCallback<T>) :
PagingDataAdapter<T, VH>(diffCallback) {
override fun onBindViewHolder(holder: VH, position: Int) {
getItem(position).let { data -> holder.bindData(data!!) }
}
abstract class BaseViewHolder<T>(view: View) : RecyclerView.ViewHolder(view) {
abstract fun bindData(data: T)
}
fun ViewGroup.inflateView(layoutRes: Int): View =
LayoutInflater.from(this.context).inflate(layoutRes, this, false)
}
my adapter
class OrdersPagingAdapter(val onCancelClick: (Order) -> Unit) :
BasePagingAdapter<Order, BasePagingAdapter.BaseViewHolder<Order>>(
OrdersPagingComparator
) {
override fun onCreateViewHolder(
parent: ViewGroup,
viewType: Int
): BaseViewHolder<Order> =
OrdersViewHolder(parent.inflateView(R.layout.item_order))
}
base paging source
open class BasePagingSource<T : Any>(
val call: suspend (Int) -> Response<BasePagingResponse<T>>
) :
PagingSource<Int, T>() {
override suspend fun load(
params: LoadParams<Int>
): LoadResult<Int, T> {
return try {
val nextPageNumber = params.key ?: 1
val response: Response<BasePagingResponse<T>> =
call(nextPageNumber)
if (response.code() == 200) {
LoadResult.Page(
data = response.body()?.data!!,
prevKey = null,
nextKey = if (response.body()?.currentPage == response.body()?.totalPages) null
else
response.body()?.currentPage!! + 1
)
} else {
LoadResult.Page(
data = emptyList(),
prevKey = null,
nextKey = null
)
}
} catch (e: IOException) {
return LoadResult.Error(e)
} catch (e: HttpException) {
return LoadResult.Error(e)
}
}
override fun getRefreshKey(state: PagingState<Int, T>): Int? {
return state.anchorPosition?.let { anchorPosition ->
val anchorPage = state.closestPageToPosition(anchorPosition)
anchorPage?.prevKey?.plus(1) ?: anchorPage?.nextKey?.minus(1)
}
}
}
my repository call I tries to chang initial page size but still not working
fun getOrders() = Pager(
config = PagingConfig(
pageSize = 10,
enablePlaceholders = false
), pagingSourceFactory = { OrdersPagingSource() }
).flow
Under your PagingSource class, change the logic to this. nextKey should be null if reach the end of the page.
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, ProgressHistory> {
return try {
val position = params.key ?: 1
val response = apiService.getProgressHistory(jsonObject = payload, page = position)
val nextKey = if (response.code() != 200) {
null
} else {
position + 1
}
LoadResult.Page(data = response.body()!!.data!!,
prevKey = if (position == 1) null else position - 1,
nextKey = nextKey)
} catch (e: Exception) {
LoadResult.Error(e)
}
}

Android Paging 3 - experiencing flickers, glitches or jumps in position when scrolling & loading new pages

Hello Guys im using Android Jetpack Paging library 3, I'm creating a news app that implements network + database scenario, and im following the codelab by google https://codelabs.developers.google.com/codelabs/android-paging , im doing it almost like in the codelab i almost matched all the operations shown in the examples https://github.com/android/architecture-components-samples/tree/main/PagingWithNetworkSample.
It works almost as it should...but my backend response is page keyed, i mean response comes with the list of news and the next page url, remote mediator fetches the data, populates the database, repository is set, viewmodel is set...
The problem is :
when recyclerview loads the data , following happens:recyclerview flickers, items jump, are removed , added again and so on.
I dont know why recyclerview or its itemanimator behaves like that , that looks so ugly and glitchy.
More than that, when i scroll to the end of the list new items are fetched and that glitchy and jumping effect is happening again.
I would be very grateful if you could help me, im sitting on it for three days , thank you very much in advance.Here are my code snippets:
#Entity(tableName = "blogs")
data class Blog(
#PrimaryKey(autoGenerate = true)
val databaseid:Int,
#field:SerializedName("id")
val id: Int,
#field:SerializedName("title")
val title: String,
#field:SerializedName("image")
val image: String,
#field:SerializedName("date")
val date: String,
#field:SerializedName("share_link")
val shareLink: String,
#field:SerializedName("status")
val status: Int,
#field:SerializedName("url")
val url: String
) {
var categoryId: Int? = null
var tagId: Int? = null
}
Here's the DAO
#Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insertAll(blogs: List<Blog>)
#Query("DELETE FROM blogs")
suspend fun deleteAllBlogs()
#Query("SELECT * FROM blogs WHERE categoryId= :categoryId ORDER BY id DESC")
fun getBlogsSourceUniversal(categoryId:Int?): PagingSource<Int, Blog>
#Query("SELECT * FROM blogs WHERE categoryId= :categoryId AND tagId= :tagId ORDER BY id DESC")
fun getBlogsSourceUniversalWithTags(categoryId:Int?,tagId:Int?): PagingSource<Int, Blog>
NewsDatabaseKt
abstract class NewsDatabaseKt : RoomDatabase() {
abstract fun articleDAOKt(): ArticleDAOKt
abstract fun remoteKeyDao(): RemoteKeyDao
companion object {
#Volatile
private var INSTANCE: NewsDatabaseKt? = null
fun getDatabase(context: Context): NewsDatabaseKt =
INSTANCE ?: synchronized(this) {
INSTANCE ?: buildDatabase(context).also { INSTANCE = it }
}
private fun buildDatabase(context: Context) =
Room.databaseBuilder(context.applicationContext,
NewsDatabaseKt::class.java,
"news_database_kt")
.build()
}
RemoteMediator
#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 ->{
initialPage
}
PREPEND -> {
return MediatorResult.Success(endOfPaginationReached = true)}
APPEND -> {
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())
}
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
}
}
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)
}
}
PagingRepository
class PagingRepository(
private val service: NewsAPIInterfaceKt,
private val databaseKt: NewsDatabaseKt
){
#ExperimentalPagingApi
fun getBlogsResultStreamUniversal(int: Int, tagId : Int? = null) : Flow<PagingData<Blog>>{
val pagingSourceFactory = {
if(tagId == null) {
databaseKt.articleDAOKt().getBlogsSourceUniversal(int)
}else databaseKt.articleDAOKt().getBlogsSourceUniversalWithTags(int,tagId)
}
return Pager(
config = PagingConfig(
pageSize = 1
)
,remoteMediator =
BlogsRemoteMediator(int, service, databaseKt,tagId)
,pagingSourceFactory = pagingSourceFactory
).flow
}
}
BlogsViewmodel
class BlogsViewModel(private val repository: PagingRepository):ViewModel(){
private var currentResultUiModel: Flow<PagingData<UiModel.BlogModel>>? = null
private var categoryId:Int?=null
#ExperimentalPagingApi
fun getBlogsUniversalWithUiModel(int: Int, tagId : Int? = null):
Flow<PagingData<UiModel.BlogModel>> {
val lastResult = currentResultUiModel
if(lastResult != null && int == categoryId){
return lastResult
}
val newResult: Flow<PagingData<UiModel.BlogModel>> =
repository.getBlogsResultStreamUniversal(int, tagId)
.map { pagingData -> pagingData.map { UiModel.BlogModel(it)}}
.cachedIn(viewModelScope)
currentResultUiModel = newResult
categoryId = int
return newResult
}
sealed class UiModel{
data class BlogModel(val blog: Blog) : UiModel()
}
PoliticsFragmentKotlin
#ExperimentalPagingApi
class PoliticsFragmentKotlin : Fragment() {
private lateinit var recyclerView: RecyclerView
private lateinit var pagedBlogsAdapter:BlogsAdapter
lateinit var viewModelKt: BlogsViewModel
lateinit var viewModel:NewsViewModel
private var searchJob: Job? = null
#ExperimentalPagingApi
private fun loadData(categoryId:Int, tagId : Int? = null) {
searchJob?.cancel()
searchJob = lifecycleScope.launch {
viewModelKt.getBlogsUniversalWithUiModel(categoryId, tagId).collectLatest {
pagedBlogsAdapter.submitData(it)
}
}
}
#ExperimentalPagingApi
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
val view = inflater.inflate(R.layout.fragment_blogs, container, false)
viewModelKt = ViewModelProvider(requireActivity(),Injection.provideViewModelFactory(requireContext())).get(BlogsViewModel::class.java)
viewModel = ViewModelProvider(requireActivity()).get(NewsViewModel::class.java)
pagedBlogsAdapter = BlogsAdapter(context,viewModel)
val decoration = DividerItemDecoration(context, DividerItemDecoration.VERTICAL)
recyclerView = view.findViewById(R.id.politics_recyclerView)
recyclerView.addItemDecoration(decoration)
initAdapter()
loadData(categoryId)
initLoad()
return view
}
private fun initLoad() {
lifecycleScope.launchWhenCreated {
Log.d("meylis", "lqunched loadstate scope")
pagedBlogsAdapter.loadStateFlow
// Only emit when REFRESH LoadState for RemoteMediator changes.
.distinctUntilChangedBy { it.refresh }
// Only react to cases where Remote REFRESH completes i.e., NotLoading.
.filter { it.refresh is LoadState.NotLoading }
.collect { recyclerView.scrollToPosition(0) }
}
}
private fun initAdapter() {
recyclerView.adapter = pagedBlogsAdapter.withLoadStateHeaderAndFooter(
header = BlogsLoadStateAdapter { pagedBlogsAdapter.retry() },
footer = BlogsLoadStateAdapter { pagedBlogsAdapter.retry() }
)
lifecycleScope.launchWhenCreated {
pagedBlogsAdapter.loadStateFlow.collectLatest {
swipeRefreshLayout.isRefreshing = it.refresh is LoadState.Loading
}
}
pagedBlogsAdapter.addLoadStateListener { loadState ->
// Only show the list if refresh succeeds.
recyclerView.isVisible = loadState.source.refresh is LoadState.NotLoading
// Show loading spinner during initial load or refresh.
progressBar.isVisible = loadState.source.refresh is LoadState.Loading
// Show the retry state if initial load or refresh fails.
retryButton.isVisible = loadState.source.refresh is LoadState.Error
// Toast on any error, regardless of whether it came from RemoteMediator or PagingSource
val errorState = loadState.source.append as? LoadState.Error
?: loadState.source.prepend as? LoadState.Error
?: loadState.append as? LoadState.Error
?: loadState.prepend as? LoadState.Error
errorState?.let {
Toast.makeText(context, "\uD83D\uDE28 Wooops ${it.error}", Toast.LENGTH_LONG
).show()
}
}
}
companion object {
#JvmStatic
fun newInstance(categoryId: Int, tags : ArrayList<Tag>): PoliticsFragmentKotlin {
val args = Bundle()
args.putInt(URL, categoryId)
args.putSerializable(TAGS,tags)
val fragmentKotlin = PoliticsFragmentKotlin()
fragmentKotlin.arguments = args
Log.d("meylis", "created instance")
return fragmentKotlin
}
}
BlogsAdapter
class BlogsAdapter(var context: Context?, var newsViewModel:NewsViewModel) :
PagingDataAdapter<BlogsViewModel.UiModel.BlogModel, RecyclerView.ViewHolder>
(REPO_COMPARATOR) {
private val VIEW = 10
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
return when (viewType) {
VIEW -> MyViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.card_layout, parent, false))
}
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
val uiModel = getItem(position)
if(uiModel == null){
if(uiModel is BlogsViewModel.UiModel.BlogModel){(holder as MyViewHolder).bind(null)}
}
if(uiModel is BlogsViewModel.UiModel.BlogModel){(holder as
MyViewHolder).bind(uiModel.blog)}
}
override fun getItemViewType(position: Int): Int {
return VIEW
}
companion object {
private val REPO_COMPARATOR = object : DiffUtil.ItemCallback<BlogsViewModel.UiModel.BlogModel>() {
override fun areItemsTheSame(oldItem: BlogsViewModel.UiModel.BlogModel, newItem: BlogsViewModel.UiModel.BlogModel): Boolean =
oldItem.blog.title == newItem.blog.title
override fun areContentsTheSame(oldItem: BlogsViewModel.UiModel.BlogModel, newItem: BlogsViewModel.UiModel.BlogModel): Boolean =
oldItem == newItem
}
}
MyViewHolder
class MyViewHolder(var container: View) : RecyclerView.ViewHolder(container) {
var cv: CardView
#JvmField
var mArticle: TextView
var date: TextView? = null
#JvmField
var time: TextView
#JvmField
var articleImg: ImageView
#JvmField
var shareView: View
var button: MaterialButton? = null
#JvmField
var checkBox: CheckBox
var progressBar: ProgressBar
private var blog:Blog? = null
init {
cv = container.findViewById<View>(R.id.cardvmain) as CardView
mArticle = container.findViewById<View>(R.id.article) as TextView
articleImg = container.findViewById<View>(R.id.imgvmain) as ImageView
//button = (MaterialButton) itemView.findViewById(R.id.sharemain);
checkBox = container.findViewById<View>(R.id.checkboxmain) as CheckBox
time = container.findViewById(R.id.card_time)
shareView = container.findViewById(R.id.shareView)
progressBar = container.findViewById(R.id.blog_progress)
}
fun bind(blog: Blog?){
if(blog == null){
mArticle.text = "loading"
time.text = "loading"
articleImg.visibility = View.GONE
}else {
this.blog = blog
mArticle.text = blog.title
time.text = blog.date
if (blog.image.startsWith("http")) {
articleImg.visibility = View.VISIBLE
val options: RequestOptions = RequestOptions()
.centerCrop()
.priority(Priority.HIGH)
GlideImageLoader(articleImg,
progressBar).load(blog.image, options)
} else {
articleImg.visibility = View.GONE
}
}
}
}
NewsApiInterface
interface NewsAPIInterfaceKt {
#GET("sort?")
suspend fun getCategoryResponsePage(#Header("Language") language: String, #Query("category")
categoryId: Int, #Query("page") pageNumber: String): BlogsResponse
#GET("sort?")
suspend fun getCategoryTagResponsePage(#Header("Language") language: String,
#Query("category") categoryId: Int,#Query("tag") tagId:Int, #Query("page") pageNumber: String)
:BlogsResponse
companion object {
fun create(): NewsAPIInterfaceKt {
val logger = HttpLoggingInterceptor()
logger.level = HttpLoggingInterceptor.Level.BASIC
val okHttpClient = UnsafeOkHttpClient.getUnsafeOkHttpClient()
return Retrofit.Builder()
.baseUrl(BASE_URL)
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create())
.build()
.create(NewsAPIInterfaceKt::class.java)
}
}
}
I have tried setting initialLoadSize = 1
But the problem still persists
EDIT: Thanks for your answer #dlam , yes, it does , my network API returns the list of results ordered by id. BTW, items do this jump when the application is run offline as well.
Videos when refreshing and loading online
online loading and paging
online loading and paging(2)
Videos when refreshing and loading offline
offline loading and refreshing
Thanks again, here is my gist link https://gist.github.com/Aydogdyshka/7ca3eb654adb91477a42128de2f06ea9
EDIT
Thanks a lot to #dlam, when I set pageSize=10, jumping has disappeared...Then i remembered why i set pageSize=1 in the first place... when i refresh , 3 x pageSize of items are loaded, even if i overrided initialLoadSize = 10 , it still loads 3 x pageSize calling append 2x times after refresh , what could i be doing wrong, what's the correct way to only load first page when i refresh ?
Just following up here from comments:
Setting pageSize = 10 fixes the issue.
The issue was with pageSize being too small, resulting in PagingSource refreshes loading pages that did not cover the viewport. Since source refresh replaces the list and goes through DiffUtil, you need to provide an initialLoadSize that is large enough so that there is some overlap (otherwise scroll position will be lost).
BTW - Paging loads additional data automatically based on PagingConfig.prefetchDistance. If RecyclerView binds items close enough to the edge of the list, it will automatically trigger APPEND / PREPEND loads. This is why the default of initialLoadSize is 3 * pageSize, but if you're still experiencing additional loads, I would suggest either adjusting prefetchDistance, or increasing initialLoadSize further.
config = PagingConfig(
pageSize = PAGE_SIZE,
enablePlaceholders = true,
prefetchDistance = 3* PAGE_SIZE,
initialLoadSize = 2*PAGE_SIZE,
)
make sure enablePlaceholders is set to true and set the page size to around 10 to 20
recyclerview flickers becouse from dao you get items not the same order it was responded from network.
I will suggest you my solution.
we will get items from database order by primary key, databaseid, descending.
first of all delete autogenerated = true.
we will set databaseid manualy, in same order we got items from network.
next lets edit remoteMediator load function.
when (loadType) {
LoadType.PREPEND -> {
blogs.map {
val databaseid = getFirstBlogDatabaseId(state)?.databaseid?:0
movies.forEachIndexed{
index, blog ->
blog.databaseid = roomId - (movies.size -index.toLong())
}
}
}
LoadType.APPEND -> {
val roomId = getLastBlogDatabaseId(state)?.databaseid ?:0
blogs.forEachIndexed{
index, blog ->
blog.databaseid = roomId + index.toLong() + 1
}
}
LoadType.REFRESH -> {
blogs.forEachIndexed{
index, blog ->
blog.databaseid = index.toLong()
}
}
}
private fun getFirstBlogDatabaseId(state: PagingState<Int, Blog>): Blog? {
return state.pages.firstOrNull { it.data.isNotEmpty() }?.data?.firstOrNull()
}
private fun getLastBlogDatabaseId(state: PagingState<Int, Blog>): Blog? {
return state.lastItemOrNull()
}

How to add ItemClick on PagedListAdapter?

Here is my first time to apply MVVM concept to my Android Application. I follow the steps at the referenced article
https://medium.com/swlh/realtime-firestore-pagination-on-android-with-mvvm-b5e30cea437
And I am managed to load data successfully. When it comes to implementing the onclick event at the row of my RecyclerView List, it comes out that there has no onlick response .
Would you please suggest the better method to implement the onCLick method given that I have applied PageListAdapter?
When I study the PageListAdapter documentation on Android, it seems no clue for me to implement the onclick method.
class MovieViewModel(movieRepository: MovieRepository) : ViewModel() {
private val viewModelJob = SupervisorJob()
private val uiScope = CoroutineScope(Dispatchers.Main + viewModelJob)
var selected: MutableLiveData<RealtimeMovie>? = null
private val config = PagedList.Config.Builder()
.setEnablePlaceholders(false)
.setPrefetchDistance(10)
.setPageSize(20)
.build()
val records: LiveData<PagedList<RealtimeMovie>> =
LivePagedListBuilder<String, RealtimeMovie>(
MovieDataSource.Factory(movieRepository, uiScope),
config
).build()
override fun onCleared() {
super.onCleared()
viewModelJob.cancel()
}
}
Here is my adapter:
class MovieAdapter : PagedListAdapter<RealtimeMovie, MovieAdapter.MovieViewHolder>(
object : DiffUtil.ItemCallback<RealtimeMovie>() {
override fun areItemsTheSame(old: RealtimeMovie, new: RealtimeMovie): Boolean =
old.id == new.id
override fun areContentsTheSame(old: RealtimeMovie, new: RealtimeMovie): Boolean =
old == new
}
) {
private lateinit var onItemClick: (movie: RealtimeMovie) -> Unit
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MovieViewHolder {
val view = LayoutInflater.from(parent.context).inflate(
R.layout.list_movie,
parent,
false
)
return MovieViewHolder(view)
}
infix fun setOnItemClick(onClick: (movie: RealtimeMovie) -> Unit) {
this.onItemClick = onClick
}
override fun onBindViewHolder(holder: MovieViewHolder, position: Int) {
val record = getItem(position)
holder.bind(record)
holder.itemView.setOnClickListener { onItemClick(record!!) }
}
override fun onViewRecycled(holder: MovieViewHolder) {
super.onViewRecycled(holder)
holder.apply {
txtRecordName.text = ""
crdRecord.isEnabled = true
crdRecord.setCardBackgroundColor(
ContextCompat.getColor(
view.context,
android.R.color.white
)
)
viewHolderDisposables.clear()
}
}
inner class MovieViewHolder(val view: View) : RecyclerView.ViewHolder(view) {
val viewHolderDisposables = CompositeDisposable()
val crdRecord by lazy { view.findViewById<MaterialCardView>(R.id.crd_record) }
val txtRecordName by lazy { view.findViewById<TextView>(R.id.txt_record_name) }
fun bind(RealtimeMovie: RealtimeMovie?) {
RealtimeMovie?.let {
it.record
.subscribeBy(
onNext = {
txtRecordName.text = it.title
},
onError = {
// Handle error here
// Record maybe deleted
}
)
.addTo(viewHolderDisposables)
}
}
}
}
Here is my fragment :
..
viewModel = ViewModelProviders.of(this, factory).get(MovieViewModel::class.java)
viewModel.records.observe(this, Observer {
swpRecords.isRefreshing = false
recordsAdapter.submitList(it)
recordsAdapter.setOnItemClick {
print("movie : ${it.id}" )
print("movie : ${it.record}" )
}
})

Categories

Resources