diff --git a/app/src/main/java/com/infomaniak/drive/ui/fileList/fileDetails/FileDetailsActivitiesFragment.kt b/app/src/main/java/com/infomaniak/drive/ui/fileList/fileDetails/FileDetailsActivitiesFragment.kt index 7b13511011..fdb29b2abe 100644 --- a/app/src/main/java/com/infomaniak/drive/ui/fileList/fileDetails/FileDetailsActivitiesFragment.kt +++ b/app/src/main/java/com/infomaniak/drive/ui/fileList/fileDetails/FileDetailsActivitiesFragment.kt @@ -23,6 +23,8 @@ import android.view.View import android.view.ViewGroup import androidx.core.view.isGone import com.infomaniak.drive.R +import com.infomaniak.drive.data.api.ErrorCode.Companion.translateError +import com.infomaniak.drive.utils.isLastPage import kotlinx.android.synthetic.main.fragment_file_details.* import kotlinx.android.synthetic.main.fragment_file_details_activities.* @@ -44,7 +46,7 @@ class FileDetailsActivitiesFragment : FileDetailsSubFragment() { fileDetailsViewModel.getFileActivities(file).observe(viewLifecycleOwner) { apiResponse -> apiResponse?.data?.let { activities -> addAll(activities) - isComplete = apiResponse.page == apiResponse.pages + isComplete = apiResponse.isLastPage() } ?: also { isComplete = true } diff --git a/app/src/main/java/com/infomaniak/drive/ui/fileList/fileDetails/FileDetailsCommentsFragment.kt b/app/src/main/java/com/infomaniak/drive/ui/fileList/fileDetails/FileDetailsCommentsFragment.kt index ee67e7f3a0..f370f47371 100644 --- a/app/src/main/java/com/infomaniak/drive/ui/fileList/fileDetails/FileDetailsCommentsFragment.kt +++ b/app/src/main/java/com/infomaniak/drive/ui/fileList/fileDetails/FileDetailsCommentsFragment.kt @@ -107,7 +107,7 @@ class FileDetailsCommentsFragment : FileDetailsSubFragment() { fileDetailsViewModel.getFileComments(currentFile).observe(viewLifecycleOwner) { apiResponse -> apiResponse?.data?.let { comments -> addAll(comments) - isComplete = apiResponse.page == apiResponse.pages + isComplete = apiResponse.isLastPage() } ?: also { isComplete = true } diff --git a/app/src/main/java/com/infomaniak/drive/ui/fileList/fileDetails/FileDetailsViewModel.kt b/app/src/main/java/com/infomaniak/drive/ui/fileList/fileDetails/FileDetailsViewModel.kt index 56c8f0aea0..96cde15092 100644 --- a/app/src/main/java/com/infomaniak/drive/ui/fileList/fileDetails/FileDetailsViewModel.kt +++ b/app/src/main/java/com/infomaniak/drive/ui/fileList/fileDetails/FileDetailsViewModel.kt @@ -20,6 +20,7 @@ package com.infomaniak.drive.ui.fileList.fileDetails import androidx.lifecycle.* import com.infomaniak.drive.data.api.ApiRepository import com.infomaniak.drive.data.models.* +import com.infomaniak.drive.utils.isLastPage import com.infomaniak.lib.core.models.ApiResponse import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Job @@ -83,7 +84,7 @@ class FileDetailsViewModel : ViewModel() { if (isSuccess()) { when { data.isNullOrEmpty() -> emit(null) - page == pages || data!!.count() < itemsPerPage -> emit(this) // TODO delete second condition for api-v2 + isLastPage() -> emit(this) else -> { emit(this) recursive(page + 1) 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 efa2bfa91d..2efc873f8c 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 @@ -28,7 +28,6 @@ import androidx.navigation.navGraphViewModels import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView import com.infomaniak.drive.R -import com.infomaniak.drive.data.api.ApiRepository import com.infomaniak.drive.ui.MainViewModel import com.infomaniak.drive.utils.* import com.infomaniak.lib.core.utils.safeNavigate @@ -115,7 +114,7 @@ class HomeActivitiesFragment : Fragment() { it?.let { (apiResponse, mergedActivities) -> if (apiResponse.page == 1 && itemCount > 0) clean() addAll(mergedActivities) - isComplete = (apiResponse.data?.size ?: 0) < ApiRepository.PER_PAGE + isComplete = apiResponse.isLastPage() } ?: also { isComplete = true addAll(arrayListOf()) diff --git a/app/src/main/java/com/infomaniak/drive/ui/home/HomeViewModel.kt b/app/src/main/java/com/infomaniak/drive/ui/home/HomeViewModel.kt index 863f82d4df..3d84cb1dca 100644 --- a/app/src/main/java/com/infomaniak/drive/ui/home/HomeViewModel.kt +++ b/app/src/main/java/com/infomaniak/drive/ui/home/HomeViewModel.kt @@ -85,7 +85,7 @@ class HomeViewModel : ViewModel() { ApiResponse( data = localActivities, page = 1, - pages = 1, + itemsPerPage = localActivities.size + 1, result = SUCCESS ) to mergeAndCleanActivities ) 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 850cce916d..a233838b8d 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 @@ -22,6 +22,7 @@ import com.infomaniak.drive.data.api.ApiRepository import com.infomaniak.drive.data.cache.FileController import com.infomaniak.drive.data.models.File import com.infomaniak.drive.utils.IsComplete +import com.infomaniak.drive.utils.isLastPage import com.infomaniak.lib.core.models.ApiResponse import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Job @@ -65,12 +66,12 @@ class PicturesViewModel : ViewModel() { ) { val data = apiResponse.data val isFirstPage = page == 1 - val isComplete = (data?.size ?: 0) < ApiRepository.PER_PAGE if (data.isNullOrEmpty()) { emit(null) } else { FileController.storePicturesDrive(data, isFirstPage) + val isComplete = apiResponse.isLastPage() emit(data to isComplete) } diff --git a/app/src/main/java/com/infomaniak/drive/ui/menu/RecentChangesViewModel.kt b/app/src/main/java/com/infomaniak/drive/ui/menu/RecentChangesViewModel.kt index 87b6a8f15d..4a7a41aa0f 100644 --- a/app/src/main/java/com/infomaniak/drive/ui/menu/RecentChangesViewModel.kt +++ b/app/src/main/java/com/infomaniak/drive/ui/menu/RecentChangesViewModel.kt @@ -21,9 +21,9 @@ import androidx.lifecycle.LiveData 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.utils.isLastPage import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Job @@ -41,7 +41,7 @@ class RecentChangesViewModel : ViewModel() { val apiResponse = ApiRepository.getLastModifiedFiles(driveId, currentPage) if (apiResponse.isSuccess()) { apiResponse.data?.let { data -> - val isComplete = data.size < PER_PAGE + val isComplete = apiResponse.isLastPage() val isFirstPage = currentPage == 1 FileController.storeRecentChanges(data, isFirstPage) emit(FileListFragment.FolderFilesResult(files = data, isComplete = isComplete, page = currentPage)) diff --git a/app/src/main/java/com/infomaniak/drive/utils/Extensions.kt b/app/src/main/java/com/infomaniak/drive/utils/Extensions.kt index ca85fbcd52..a900218f93 100644 --- a/app/src/main/java/com/infomaniak/drive/utils/Extensions.kt +++ b/app/src/main/java/com/infomaniak/drive/utils/Extensions.kt @@ -76,6 +76,7 @@ import com.infomaniak.drive.ui.bottomSheetDialogs.NotSupportedExtensionBottomShe import com.infomaniak.drive.ui.fileList.UploadInProgressFragmentArgs import com.infomaniak.drive.ui.fileList.fileShare.AvailableShareableItemsAdapter import com.infomaniak.drive.utils.MatomoUtils.trackShareRightsEvent +import com.infomaniak.lib.core.models.ApiResponse import com.infomaniak.lib.core.models.user.User import com.infomaniak.lib.core.networking.HttpUtils import com.infomaniak.lib.core.utils.lightNavigationBar @@ -502,4 +503,6 @@ fun Activity.getAdjustedColumnNumber(expectedItemSize: Int, minColumns: Int = 2, return min(max(minColumns, screenWidth / expectedItemSize), maxColumns) } +fun ApiResponse>.isLastPage() = (data?.size ?: 0) < itemsPerPage + operator fun Regex.contains(input: String) = containsMatchIn(input)