Skip to content

Commit

Permalink
Merge pull request #776 from Infomaniak/fix-observers
Browse files Browse the repository at this point in the history
Fix pictures multiple observers
  • Loading branch information
sirambd authored May 12, 2022
2 parents de85cf0 + fdc64fb commit 941a0f2
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 25 deletions.
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

0 comments on commit 941a0f2

Please sign in to comment.