Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix pictures multiple observers #776

Merged
merged 1 commit into from
May 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 27 additions & 20 deletions app/src/main/java/com/infomaniak/drive/ui/menu/PicturesFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -35,7 +32,12 @@ class PicturesViewModel : ViewModel() {
var lastPicturesPage = 1
var lastPicturesLastPage = 1

fun getLastPictures(
val loadMorePictures = MutableLiveData<Pair<Int, Boolean>>()
val picturesApiResult = Transformations.switchMap(loadMorePictures) { (driveId, ignoreCloud) ->
getLastPictures(driveId, ignoreCloud)
}

private fun getLastPictures(
driveId: Int,
ignoreCloud: Boolean = false,
): LiveData<Pair<ArrayList<File>, IsComplete>?> {
Expand Down