From fdc64fb6d0e1829f8b6ed1c5e2d2b368026e21f3 Mon Sep 17 00:00:00 2001 From: Gibran Chevalley Date: Wed, 11 May 2022 12:28:52 +0200 Subject: [PATCH] Fix pictures multiple observers --- .../drive/ui/menu/PicturesFragment.kt | 47 +++++++++++-------- .../drive/ui/menu/PicturesViewModel.kt | 12 +++-- 2 files changed, 34 insertions(+), 25 deletions(-) diff --git a/app/src/main/java/com/infomaniak/drive/ui/menu/PicturesFragment.kt b/app/src/main/java/com/infomaniak/drive/ui/menu/PicturesFragment.kt index 6b5f7a578f..5867e9fc7c 100644 --- a/app/src/main/java/com/infomaniak/drive/ui/menu/PicturesFragment.kt +++ b/app/src/main/java/com/infomaniak/drive/ui/menu/PicturesFragment.kt @@ -129,6 +129,31 @@ class PicturesFragment : MultiSelectFragment(MATOMO_CATEGORY) { if (isCurrentlyInGallery) refreshTimer.start() loadMorePictures(AccountUtils.currentDriveId, true) } + + observeApiResultPagination() + } + + private fun observeApiResultPagination() = with(picturesAdapter) { + picturesViewModel.picturesApiResult.observe(viewLifecycleOwner) { + it?.let { (pictures, isComplete) -> + stateRestorationPolicy = RecyclerView.Adapter.StateRestorationPolicy.PREVENT_WHEN_EMPTY + val pictureList = formatList(pictures) + picturesRecyclerView.post { addAll(pictureList) } + this.isComplete = isComplete + noPicturesLayout.toggleVisibility(pictureList.isEmpty()) + } ?: run { + isComplete = true + noPicturesLayout.toggleVisibility( + noNetwork = mainViewModel.isInternetAvailable.value == false, + isVisible = pictureList.isEmpty(), + showRefreshButton = true, + ) + } + + onDownloadFinished() + + isDownloadingPictures = false + } } private fun setupPagination() { @@ -182,30 +207,12 @@ class PicturesFragment : MultiSelectFragment(MATOMO_CATEGORY) { clean() } - val ignoreCloud = mainViewModel.isInternetAvailable.value == false showLoading() isComplete = false isDownloadingPictures = true - picturesViewModel.getLastPictures(driveId, ignoreCloud).observe(viewLifecycleOwner) { - it?.let { (pictures, isComplete) -> - stateRestorationPolicy = RecyclerView.Adapter.StateRestorationPolicy.PREVENT_WHEN_EMPTY - val pictureList = formatList(pictures) - picturesRecyclerView.post { addAll(pictureList) } - this.isComplete = isComplete - noPicturesLayout.toggleVisibility(pictureList.isEmpty()) - } ?: run { - isComplete = true - noPicturesLayout.toggleVisibility( - noNetwork = ignoreCloud, - isVisible = pictureList.isEmpty(), - showRefreshButton = true, - ) - } - onDownloadFinished() - - isDownloadingPictures = false - } + val ignoreCloud = mainViewModel.isInternetAvailable.value == false + picturesViewModel.loadMorePictures.value = driveId to ignoreCloud } } diff --git a/app/src/main/java/com/infomaniak/drive/ui/menu/PicturesViewModel.kt b/app/src/main/java/com/infomaniak/drive/ui/menu/PicturesViewModel.kt index 19bc65e845..850cce916d 100644 --- a/app/src/main/java/com/infomaniak/drive/ui/menu/PicturesViewModel.kt +++ b/app/src/main/java/com/infomaniak/drive/ui/menu/PicturesViewModel.kt @@ -17,10 +17,7 @@ */ package com.infomaniak.drive.ui.menu -import androidx.lifecycle.LiveData -import androidx.lifecycle.LiveDataScope -import androidx.lifecycle.ViewModel -import androidx.lifecycle.liveData +import androidx.lifecycle.* import com.infomaniak.drive.data.api.ApiRepository import com.infomaniak.drive.data.cache.FileController import com.infomaniak.drive.data.models.File @@ -35,7 +32,12 @@ class PicturesViewModel : ViewModel() { var lastPicturesPage = 1 var lastPicturesLastPage = 1 - fun getLastPictures( + val loadMorePictures = MutableLiveData>() + val picturesApiResult = Transformations.switchMap(loadMorePictures) { (driveId, ignoreCloud) -> + getLastPictures(driveId, ignoreCloud) + } + + private fun getLastPictures( driveId: Int, ignoreCloud: Boolean = false, ): LiveData, IsComplete>?> {