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

refactor api v2 pages #793

Merged
merged 2 commits into from
Jun 22, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -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.*

Expand All @@ -43,8 +45,9 @@ class FileDetailsActivitiesFragment : FileDetailsSubFragment() {
isComplete = false
fileDetailsViewModel.getFileActivities(file).observe(viewLifecycleOwner) { apiResponse ->
apiResponse?.data?.let { activities ->
apiResponse.translateError()
addAll(activities)
isComplete = apiResponse.page == apiResponse.pages
isComplete = apiResponse.isLastPage()
} ?: also {
isComplete = true
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -65,7 +66,7 @@ class PicturesViewModel : ViewModel() {
) {
val data = apiResponse.data
val isFirstPage = page == 1
val isComplete = (data?.size ?: 0) < ApiRepository.PER_PAGE
val isComplete = apiResponse.isLastPage()

if (data.isNullOrEmpty()) {
emit(null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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))
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/java/com/infomaniak/drive/utils/Extensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -502,4 +503,6 @@ fun Activity.getAdjustedColumnNumber(expectedItemSize: Int, minColumns: Int = 2,
return min(max(minColumns, screenWidth / expectedItemSize), maxColumns)
}

fun <T> ApiResponse<ArrayList<T>>.isLastPage() = data != null && data!!.size < itemsPerPage

operator fun Regex.contains(input: String) = containsMatchIn(input)