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

Changed gallery images loading so it charges when scrolled far enough #638

Merged
merged 4 commits into from
Mar 14, 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
2 changes: 1 addition & 1 deletion Core
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
43 changes: 39 additions & 4 deletions app/src/main/java/com/infomaniak/drive/ui/menu/PicturesFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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.*
Expand All @@ -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 {
Expand All @@ -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(
Expand Down Expand Up @@ -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
)
}
}

Expand Down Expand Up @@ -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)
Expand All @@ -183,7 +215,10 @@ class PicturesFragment : MultiSelectFragment(MATOMO_CATEGORY) {
showRefreshButton = true,
)
}

onDownloadFinished()

isDownloadingPictures = false
}
}
}
Expand All @@ -198,7 +233,7 @@ class PicturesFragment : MultiSelectFragment(MATOMO_CATEGORY) {
fun onRefreshPictures() {
if (isResumed) {
picturesAdapter.clearPictures()
getPictures()
loadMorePictures(AccountUtils.currentDriveId, true)
}
}

Expand Down
48 changes: 23 additions & 25 deletions app/src/main/java/com/infomaniak/drive/ui/menu/PicturesViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,44 +30,42 @@ 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<Pair<ArrayList<File>, 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)
}
}

override fun onCleared() {
getPicturesJob.cancel()
super.onCleared()
}

}
}