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

Change downloading strategy of RecentChanges Fragment #627

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 @@ -25,11 +25,13 @@ import com.infomaniak.drive.R
import com.infomaniak.drive.data.cache.FileController
import com.infomaniak.drive.utils.AccountUtils
import com.infomaniak.drive.utils.Utils
import com.infomaniak.lib.core.utils.setPagination
import kotlinx.android.synthetic.main.fragment_file_list.*

class RecentChangesFragment : FileSubTypeListFragment() {

private val recentChangesViewModel: RecentChangesViewModel by viewModels()
private var isDownloadingChanges = false

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
downloadFiles = DownloadFiles()
Expand All @@ -38,6 +40,15 @@ class RecentChangesFragment : FileSubTypeListFragment() {

super.onViewCreated(view, savedInstanceState)

fileRecyclerView.apply {
setPagination({
if (!fileAdapter.isComplete && !isDownloadingChanges) {
recentChangesViewModel.currentPage++
downloadFiles(false, false)
}
})
}

sortButton.isGone = true
collapsingToolbarLayout.title = getString(R.string.lastEditsTitle)
}
Expand All @@ -57,8 +68,9 @@ class RecentChangesFragment : FileSubTypeListFragment() {
override fun invoke(ignoreCache: Boolean, isNewSort: Boolean) {
showLoadingTimer.start()
fileAdapter.isComplete = false
isDownloadingChanges = true

recentChangesViewModel.getRecentChanges(AccountUtils.currentDriveId, false).observe(viewLifecycleOwner) { result ->
recentChangesViewModel.getRecentChanges(AccountUtils.currentDriveId).observe(viewLifecycleOwner) { result ->
populateFileList(
files = result?.files ?: arrayListOf(),
folderId = FileController.RECENT_CHANGES_FILE_ID,
Expand All @@ -67,6 +79,7 @@ class RecentChangesFragment : FileSubTypeListFragment() {
realm = mainViewModel.realm,
isNewSort = isNewSort,
)
isDownloadingChanges = false
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,62 +18,43 @@
package com.infomaniak.drive.ui.menu

import androidx.lifecycle.LiveData
import androidx.lifecycle.LiveDataScope
import androidx.lifecycle.ViewModel
import androidx.lifecycle.liveData
import com.infomaniak.drive.data.api.ApiRepository
import com.infomaniak.drive.data.api.ApiRepository.PER_PAGE
import com.infomaniak.drive.data.cache.FileController
import com.infomaniak.drive.ui.fileList.FileListFragment
import com.infomaniak.drive.ui.home.HomeViewModel.Companion.DOWNLOAD_INTERVAL
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import java.util.*

class RecentChangesViewModel : ViewModel() {

private var getRecentChangesJob = Job()
private var lastModifiedTime: Long = 0

fun getRecentChanges(
driveId: Int,
onlyFirstPage: Boolean,
forceDownload: Boolean = false,
): LiveData<FileListFragment.FolderFilesResult?> {
var currentPage = 1

fun getRecentChanges(driveId: Int): LiveData<FileListFragment.FolderFilesResult?> {
getRecentChangesJob.cancel()
getRecentChangesJob = Job()
val ignoreDownload = lastModifiedTime != 0L && (Date().time - lastModifiedTime) < DOWNLOAD_INTERVAL && !forceDownload
return liveData(Dispatchers.IO + getRecentChangesJob) {
if (ignoreDownload) {
emit(FileListFragment.FolderFilesResult(files = FileController.getRecentChanges(), isComplete = true, page = 1))
} else {
getRecentChangesRecursive(1, driveId, onlyFirstPage)
}
}
}

private suspend fun LiveDataScope<FileListFragment.FolderFilesResult?>.getRecentChangesRecursive(
page: Int,
driveId: Int,
onlyFirstPage: Boolean,
) {
val isFirstPage = page == 1
val apiResponse = ApiRepository.getLastModifiedFiles(driveId, page)
when {
apiResponse.isSuccess() -> apiResponse.data?.let { data ->
if (isFirstPage) emit(FileListFragment.FolderFilesResult(files = data, isComplete = true, page = page))
if (data.size >= ApiRepository.PER_PAGE && !onlyFirstPage) {
getRecentChangesRecursive(page + 1, driveId, onlyFirstPage)
return liveData(Dispatchers.IO + getRecentChangesJob) {
val apiResponse = ApiRepository.getLastModifiedFiles(driveId, currentPage)
if (apiResponse.isSuccess()) {
apiResponse.data?.let { data ->
val isComplete = data.size < PER_PAGE
val isFirstPage = currentPage == 1
emit(FileListFragment.FolderFilesResult(files = data, isComplete = isComplete, page = currentPage))
FileController.storeRecentChanges(data, isFirstPage)
}
FileController.storeRecentChanges(data, isFirstPage)
}
isFirstPage -> emit(
FileListFragment.FolderFilesResult(
files = FileController.getRecentChanges(),
isComplete = true,
page = 1,
} else {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No error message in case of ... error?

emit(
FileListFragment.FolderFilesResult(
files = FileController.getRecentChanges(),
isComplete = true,
page = currentPage
)
)
)
else -> emit(null)
}
}
}
}