Paging 3 return last value after observe again to LiveData in a Fragment

714 views Asked by At

In my case, I fetched a list of data and show it in a RecyclerView using PagingDataAdapter but the issue arose after navigation to a different Fragment then navigate back to the previous Fragment (A -> B -> A), so after reobserve again to the LiveData, I get the latest value from paging source and I don't get all the list data.

Here is the code I wrote:

OrdersFragmentViewModel.kt

class OrdersFragmentViewModel @ViewModelInject constructor(
        private val restRepository: RestRepository,
        @Assisted state: SavedStateHandle
) : ViewModel() {

    var orderStatus: Int? = null

    private val _progressBar = MutableLiveData<Boolean>()
    val progressBar: LiveData<Boolean> = _progressBar

    private val currentStatus = state.getLiveData(CURRENT_STATUS, orderStatus)

    //getting data from Paging source 
    val orders = restRepository.getAllOrders(
            OCApplication.preferencesManager.token,
            status = null,
    ).cachedIn(viewModelScope)

}

Fragment

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)

    binding.swiperefresh.setOnRefreshListener(this)

    ordersAdapter = OrdersAdapter(this, preferencesManager)
    binding.orderList.adapter = ordersAdapter
    handelOrdersStates()
    observe(viewModel.orders, ::setOrders)
    handleHomePopup()
    onCreateFirstOrderClick()

}

private fun setOrders(orders: PagingData<OrderDetails>) {
    ordersAdapter.submitData(viewLifecycleOwner.lifecycle, orders)
}

Paging DataSource

private const val ORDERS_STARTING_PAGE_INDEX = 0

class OrdersDataSource(
        private val remoteRepository: RestRepository,
        private val token: String,
        private val status: Int?,
) : PagingSource<Int, OrderDetails>() {


    override suspend fun load(params: LoadParams<Int>): LoadResult<Int, OrderDetails> {
        val position = params.key ?: ORDERS_STARTING_PAGE_INDEX

        return try {
            val response = remoteRepository.getAllOrders(token, status, position, params.loadSize)
            val orders = response.data?.data?.data ?: ArrayList()
            val ordersFiltered = if (status == null) {
                orders.filter {
                    it.clientPurchaseOrderStatus != ClientPurchaseOrderStatus.WaitingForApprovalOwner.ordinal
                }
            } else {
                orders.filter {
                    it.clientPurchaseOrderStatus <= ClientPurchaseOrderStatus.WaitingForApprovalFromClient.ordinal
                            || it.clientPurchaseOrderStatus == ClientPurchaseOrderStatus.WaitingForApprovalOwner.ordinal
                }
            }

            LoadResult.Page(
                    data = ordersFiltered.sortedByDescending { it.createdAt.getOrdersTimeStamp() },
                    prevKey = if (position == ORDERS_STARTING_PAGE_INDEX) null else position - params.loadSize,
                    nextKey = if (orders.isEmpty()) null else position + params.loadSize
            )
        } catch (exception: IOException) {
            LoadResult.Error(exception)
        } catch (exception: HttpException) {
            LoadResult.Error(exception)
        }
    }
}
0

There are 0 answers