androidkotlinobserver-patternkotlin-flowandroid-paging-3

Why can not load new list after delete or change item status?


I use paging3 libary to get data from api with pagination. I get it and load succesfully in onViewCreated. But when use delete api item from list. It can not load new list to adapter. When I recrated fragment it show that's succesfully. How can do this too in viewModel.stateDeleteMatch.observe method? I just tried likesAdapter?.notifyItemChanged(position) and likesAdapter?.notifyDataSetChanged. Also, try to call viewModel.getPaginationData() method. But that's all not work.

LikesFragment.kt

@AndroidEntryPoint
class LikesFragment : BaseFragment<FragmentLikesBinding>(
    bindingInflater = FragmentLikesBinding::inflate
) {
    private var likesAdapter: LikesAdapter? = null

    private val viewModel: LikesFragmentViewModel by viewModels()

    override fun onCreateFinished() {
        initLikesAdapter()
        viewModel.getPaginationData()
    }

override fun initObservers() {
        lifecycleScope.launch {
            viewModel.pagingData.collect { pagingData ->
                hideProgress()
                likesAdapter?.submitData(pagingData)
            }
        }
    viewModel.stateDeleteMatch.observe(viewLifecycleOwner) { state ->
            state?.let { result ->
                when (result.isLoading) {
                    false -> {
                        hideProgress()
                        if (result.error != "hata") {
                            result.deletePositionId?.let { position ->
                                //I need load adapter with new data here!!!
                                viewModel.getPaginationData()
                               // likesAdapter?.notifyItemChanged(position)
                            }
                        } else {
                            InfoBottomSheetDialog(
                                customMessage = getString(R.string.can_not_delete_now)
                            ).show(childFragmentManager, InfoBottomSheetDialog.TAG)
                        }

                    }

                    true -> showProgress()
                }
            }
        }
}
}

LikesFragmentViewModel.kt

@HiltViewModel
class LikesFragmentViewModel @Inject constructor(
    private val createUserAnswerUseCase: CreateUserAnswerUseCase,
    private val deleteMatchUseCase: DeleteMatchUseCase,
    private val likesPagingSource: LikesPagingSource
) : ViewModel() {
    var stateCreateUserAnswer = SingleLiveEvent<CreateUserAnswerState>()
        private set

    private val _pagingData = MutableStateFlow<PagingData<Likes>>(PagingData.empty())
    val pagingData: Flow<PagingData<Likes>> get() = _pagingData

    private var pageSize = 20

    private val pagingConfig = PagingConfig(
        pageSize = pageSize,
        enablePlaceholders = false
    )

    fun getPaginationData() {
        val pager = Pager(
            config = pagingConfig,
            pagingSourceFactory = { likesPagingSource }
        )

        // Observe the paging data and update the MutableStateFlow
        viewModelScope.launch {
            pager.flow.collectLatest { pagingData ->
                _pagingData.value = pagingData
            }
        }
    }}

Solution

  • If someone need solution for this situation I solved it by using adapter.refresh.

    viewModel.stateDeleteMatch.observe(viewLifecycleOwner) { state ->
                state?.let { result ->
                    when (result.isLoading) {
                        false -> {
                            hideProgress()
                            if (result.error != "hata") {
                                result.deletePositionId?.let { position ->
                                   likesAdapter?.refresh
                                }
                            } else {
                                InfoBottomSheetDialog(
                                    customMessage = getString(R.string.can_not_delete_now)
                                ).show(childFragmentManager, InfoBottomSheetDialog.TAG)
                            }
    
                        }
    
                        true -> showProgress()
                    }
                }
            }
    }