androidXP
androidXP

Reputation: 1719

Android ViewModel and Paging 3 Library

I am building a chat app where every data enter in room database first and it's on data insert the recyclerview get update with new data. It works as expected but now i need to update my paging library to version 3. For this i need to update these changes as per this link and this

enter image description here

I have made all the changes but i am stuck at viewmodel class. I am not getting what changes i need to make in this class to make it work with the paging library version 3

This is my ViewModel

public class Chat_ViewModel extends ViewModel {

    public final LiveData<PagedList<ChatEntity_TableColums>> chatList;


    public Chat_ViewModel() {
        chatList = new LivePagedListBuilder<>(
                RoomDatabase.getInstance(MyApplication.getmContext(),1).chatDAO().getPaginationChat(String.valueOf(UserID())),20).build();
}

    }

In above class i know the PagedList and LivePagedListBuilder is deprecated but here i am not sure about the what to replace in updated library to make it work perfectly.

My room Database Query

@Query("SELECT * FROM tapChatter WHERE userName = :UserName ORDER BY ID DESC")
    PagingSource<Integer, ChatEntity_TableColums>  getPaginationChat(String UserName);

Issue facing:

1: How can i Update ViewModel for optimal performance for chat app? Is my current ViewModel is perfect for the chat app?

2: Is it possible to update the RecyclerView UI on Room Database data update ?

Upvotes: 2

Views: 2360

Answers (1)

androidXP
androidXP

Reputation: 1719

I successfully migrated from Paging 2 to Paging 3 and to be true its very smooth compare to paging 2 library with the help of ViewModel and Room Database. It was hard to find much documentation/tutorial for the java users so at last i found these links to get successful migration. Link 1 Link 2

These are the changes i made in my ViewHolder and Fragment for paging

Create Live Data in ViewModel

  public final LiveData<PagingData<ChatEntity_TableColums>> chatList;

public Chat_ViewModel() {
    Pager<Integer, ChatEntity_TableColums> pager = new Pager(
            // Create new paging config
            new PagingConfig(20, //  Count of items in one page
                    20, //  Number of items to prefetch
                    false, // Enable placeholders for data which is not yet loaded
                    20, // initialLoadSize - Count of items to be loaded initially
                    200),// maxSize - Count of total items to be shown in recyclerview
            () ->                 RoomDatabase.getInstance(MyApplication.getmContext(),1).chatDAO().getPaginationChat(UserID));
    );

    CoroutineScope coroutineScope = ViewModelKt.getViewModelScope(this);
    chatList = PagingLiveData.cachedIn(PagingLiveData.getLiveData(pager),coroutineScope);

}

Fragment

 viewModel = new ViewModelProvider(this).get(Chat_ViewModel.class);

        viewModel.chatList.observe(getViewLifecycleOwner(), new Observer<PagingData<ChatEntity_TableColums>>() {
            @Override
            public void onChanged(PagingData<ChatEntity_TableColums> chatEntity_tableColumsPagingData) {
                adapter.submitData(getLifecycle(),chatEntity_tableColumsPagingData);
            }
        });

Upvotes: 2

Related Questions