Skip to content

Commit

Permalink
Merge pull request #14 from itsZECHS/patch_shared_drive_pagination
Browse files Browse the repository at this point in the history
Fix: Pagination for Shared drives list
  • Loading branch information
itszechs authored Jun 21, 2022
2 parents dd05e5f + 3a271fe commit 0b568d0
Showing 1 changed file with 38 additions and 19 deletions.
57 changes: 38 additions & 19 deletions app/src/main/java/zechs/drive/stream/ui/files/FilesViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -107,28 +107,26 @@ class FilesViewModel @Inject constructor(
val filesDataModel = mutableListOf<FilesDataModel>()

try {
val filesList = files.files.map {
DriveFile(
id = it.id,
name = it.name,
size = it.getSize(),
mimeType = it.mimeType,
iconLink = it.iconLink
)
}.distinctBy { it.id }.toMutableList()
val filesList = files.files
.map {
FilesDataModel.File(
DriveFile(
id = it.id,
name = it.name,
size = it.getSize(),
mimeType = it.mimeType,
iconLink = it.iconLink
)
)
}.distinctBy { it.driveFile.id }

response = if (response == null) {
filesDataModel.addAll(
filesList.map {
FilesDataModel.File(it)
}
)
filesDataModel.addAll(filesList)
filesDataModel
} else {
response!!.addAll(
// append new list of files
filesList.map { FilesDataModel.File(it) }
)
// append new list of files
response!!.addAll(filesList)

// return new list and remove all Loading
response!!.filter {
it != FilesDataModel.Loading
Expand All @@ -153,9 +151,13 @@ class FilesViewModel @Inject constructor(
val teamDrives = drive
.teamdrives()
.list()
.setPageSize(pageSize)
.setPageToken(nextPageToken)
.execute()

Log.d(TAG, teamDrives.toString())
nextPageToken = teamDrives.nextPageToken
isLastPage = nextPageToken == null

val filesDataModel = mutableListOf<FilesDataModel>()
val sharedDrives = teamDrives.teamDrives
Expand All @@ -173,7 +175,24 @@ class FilesViewModel @Inject constructor(
.distinctBy { it.driveFile.id }
.sortedBy { it.driveFile.name }

filesDataModel.addAll(sharedDrives)
response = if (response == null) {
filesDataModel.addAll(sharedDrives)
filesDataModel
} else {
// append new list of files
response!!.addAll(sharedDrives)

// return new list and remove all Loading
response!!.filter {
it != FilesDataModel.Loading
}.toMutableList()
}

// before submitting add Loading
// if list is not at last page
if (!isLastPage) {
response!!.add(FilesDataModel.Loading)
}

_filesList.postValue(Resource.Success(filesDataModel.toList()))
}
Expand Down

0 comments on commit 0b568d0

Please sign in to comment.