diff --git a/Core b/Core index 4f4610c211..4a7e25114e 160000 --- a/Core +++ b/Core @@ -1 +1 @@ -Subproject commit 4f4610c2114991e4f2817d0406806e52b111895e +Subproject commit 4a7e25114e40cdfd1baa9c6a9040cbb02355c97c diff --git a/app/src/main/java/com/infomaniak/drive/ui/home/HomeActivitiesFragment.kt b/app/src/main/java/com/infomaniak/drive/ui/home/HomeActivitiesFragment.kt index 449e6bd50b..42443a1300 100644 --- a/app/src/main/java/com/infomaniak/drive/ui/home/HomeActivitiesFragment.kt +++ b/app/src/main/java/com/infomaniak/drive/ui/home/HomeActivitiesFragment.kt @@ -60,7 +60,7 @@ class HomeActivitiesFragment : Fragment() { private fun initAdapter() { homeTabsRecyclerView.apply { homeViewModel.lastActivityPage = 1 - paginationListener?.let { removeOnScrollListener(it) } + paginationListener?.let(::removeOnScrollListener) val lastActivitiesAdapter = LastActivitiesAdapter() lastActivitiesAdapter.stateRestorationPolicy = RecyclerView.Adapter.StateRestorationPolicy.PREVENT 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 99d44da05f..a5ad151c33 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 @@ -43,6 +43,7 @@ import com.infomaniak.drive.ui.fileList.multiSelect.MultiSelectFragment import com.infomaniak.drive.ui.fileList.multiSelect.PicturesMultiSelectActionsBottomSheetDialog import com.infomaniak.drive.utils.* import com.infomaniak.lib.core.utils.Utils.createRefreshTimer +import com.infomaniak.lib.core.utils.setPagination import com.infomaniak.lib.core.utils.toDp import kotlinx.android.synthetic.main.activity_main.* import kotlinx.android.synthetic.main.cardview_picture.* @@ -58,6 +59,9 @@ class PicturesFragment : MultiSelectFragment(MATOMO_CATEGORY) { private val picturesViewModel: PicturesViewModel by viewModels() private lateinit var picturesAdapter: PicturesAdapter + private var paginationListener: RecyclerView.OnScrollListener? = null + private var isDownloadingPictures = false + var menuPicturesBinding: FragmentMenuPicturesBinding? = null private val refreshTimer: CountDownTimer by lazy { @@ -74,6 +78,8 @@ class PicturesFragment : MultiSelectFragment(MATOMO_CATEGORY) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) + setupPagination() + val isCurrentlyInGallery = menuPicturesBinding != null noPicturesLayout.setup( @@ -128,7 +134,24 @@ class PicturesFragment : MultiSelectFragment(MATOMO_CATEGORY) { if (!isPicturesAdapterInitialized) { if (isCurrentlyInGallery) refreshTimer.start() - getPictures() + loadMorePictures(AccountUtils.currentDriveId, true) + } + } + + private fun setupPagination() { + picturesRecyclerView.apply { + paginationListener?.let(::removeOnScrollListener) + paginationListener = setPagination( + whenLoadMoreIsPossible = { + if (!picturesAdapter.isComplete && !isDownloadingPictures) { + picturesViewModel.lastPicturesPage++ + picturesViewModel.lastPicturesLastPage++ + + loadMorePictures(AccountUtils.currentDriveId) + } + }, + triggerOffset = 100 + ) } } @@ -163,12 +186,21 @@ class PicturesFragment : MultiSelectFragment(MATOMO_CATEGORY) { picturesRecyclerView.layoutManager = gridLayoutManager } - private fun getPictures() { + private fun loadMorePictures(driveId: Int, forceDownload: Boolean = false) { picturesAdapter.apply { + if (forceDownload) { + picturesViewModel.apply { + lastPicturesPage = 1 + lastPicturesLastPage = 1 + } + clean() + } + val ignoreCloud = mainViewModel.isInternetAvailable.value == false showLoading() isComplete = false - picturesViewModel.getAllPicturesFiles(AccountUtils.currentDriveId, ignoreCloud).observe(viewLifecycleOwner) { + isDownloadingPictures = true + picturesViewModel.getLastPictures(driveId, ignoreCloud).observe(viewLifecycleOwner) { it?.let { (pictures, isComplete) -> stateRestorationPolicy = RecyclerView.Adapter.StateRestorationPolicy.PREVENT_WHEN_EMPTY val pictureList = formatList(requireContext(), pictures) @@ -183,7 +215,10 @@ class PicturesFragment : MultiSelectFragment(MATOMO_CATEGORY) { showRefreshButton = true, ) } + onDownloadFinished() + + isDownloadingPictures = false } } } @@ -198,7 +233,7 @@ class PicturesFragment : MultiSelectFragment(MATOMO_CATEGORY) { fun onRefreshPictures() { if (isResumed) { picturesAdapter.clearPictures() - getPictures() + loadMorePictures(AccountUtils.currentDriveId, true) } } 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 bd3cb2b7ce..617526da40 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 @@ -30,38 +30,37 @@ import kotlinx.coroutines.Job class PicturesViewModel : ViewModel() { private var getPicturesJob: Job = Job() - fun getAllPicturesFiles( + var lastPicturesPage = 1 + var lastPicturesLastPage = 1 + + fun getLastPictures( driveId: Int, - ignoreCloud: Boolean = false + ignoreCloud: Boolean = false, ): LiveData, IsComplete>?> { getPicturesJob.cancel() getPicturesJob = Job() + return liveData(Dispatchers.IO + getPicturesJob) { - suspend fun recursive(page: Int) { - if (!ignoreCloud) { - val apiResponse = ApiRepository.getLastPictures(driveId = driveId, page = page) - if (apiResponse.isSuccess()) { - val data = apiResponse.data - val isFirstPage = page == 1 - when { - data.isNullOrEmpty() -> emit(null) - data.size < ApiRepository.PER_PAGE -> { - FileController.storePicturesDrive(data, isFirstPage) - emit(data to true) - } - else -> { - FileController.storePicturesDrive(data, isFirstPage) - emit(data to false) - recursive(page + 1) - } - } - if (isFirstPage) FileController.removeOrphanFiles() + if (!ignoreCloud) { + val page = lastPicturesPage + val apiResponse = ApiRepository.getLastPictures(driveId = driveId, page = page) + if (apiResponse.isSuccess()) { + val data = apiResponse.data + val isFirstPage = page == 1 + val isComplete = (data?.size ?: 0) < ApiRepository.PER_PAGE + + if (data.isNullOrEmpty()) { + emit(null) } else { - emit(FileController.getPicturesDrive() to true) + FileController.storePicturesDrive(data, isFirstPage) + emit(data to isComplete) } + + if (isFirstPage) FileController.removeOrphanFiles() + } else { + emit(FileController.getPicturesDrive() to true) } } - recursive(1) } } @@ -69,5 +68,4 @@ class PicturesViewModel : ViewModel() { getPicturesJob.cancel() super.onCleared() } - -} \ No newline at end of file +}