diff --git a/app/src/main/java/zechs/drive/stream/ui/files/FilesViewModel.kt b/app/src/main/java/zechs/drive/stream/ui/files/FilesViewModel.kt index 5253353..37dcb63 100755 --- a/app/src/main/java/zechs/drive/stream/ui/files/FilesViewModel.kt +++ b/app/src/main/java/zechs/drive/stream/ui/files/FilesViewModel.kt @@ -107,28 +107,26 @@ class FilesViewModel @Inject constructor( val filesDataModel = mutableListOf() 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 @@ -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() val sharedDrives = teamDrives.teamDrives @@ -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())) }