diff --git a/data/cloud/src/main/java/com/twofasapp/data/cloud/googledrive/GoogleDrive.kt b/data/cloud/src/main/java/com/twofasapp/data/cloud/googledrive/GoogleDrive.kt index 6776d4f4..32f4a921 100644 --- a/data/cloud/src/main/java/com/twofasapp/data/cloud/googledrive/GoogleDrive.kt +++ b/data/cloud/src/main/java/com/twofasapp/data/cloud/googledrive/GoogleDrive.kt @@ -3,5 +3,5 @@ package com.twofasapp.data.cloud.googledrive interface GoogleDrive { suspend fun getBackupFile(): GoogleDriveFileResult suspend fun updateBackupFile(backupContent: String): GoogleDriveResult - suspend fun deleteBackupFile(): GoogleDriveResult + suspend fun deleteBackupFiles(): GoogleDriveResult } \ No newline at end of file diff --git a/data/cloud/src/main/java/com/twofasapp/data/cloud/googledrive/GoogleDriveImpl.kt b/data/cloud/src/main/java/com/twofasapp/data/cloud/googledrive/GoogleDriveImpl.kt index 47b40e24..4a4061b8 100644 --- a/data/cloud/src/main/java/com/twofasapp/data/cloud/googledrive/GoogleDriveImpl.kt +++ b/data/cloud/src/main/java/com/twofasapp/data/cloud/googledrive/GoogleDriveImpl.kt @@ -118,7 +118,7 @@ internal class GoogleDriveImpl( } } - override suspend fun deleteBackupFile(): GoogleDriveResult { + override suspend fun deleteBackupFiles(): GoogleDriveResult { return withContext(dispatchers.io) { try { Timber.d("DeleteFile -> Starting...") @@ -127,9 +127,12 @@ internal class GoogleDriveImpl( ?: return@withContext GoogleDriveResult.Failure(error = GoogleDriveError.CredentialsNotFound) val drive = getDrive(credentials) - val backupFileId = getFiles(drive)?.firstOrNull { it.name == backupVersions.first() }?.id - if (backupFileId.isNullOrBlank().not()) { - drive.files().delete(backupFileId).execute() + + getFiles(drive)?.forEach { file -> + if (backupVersions.contains(file.name)) { + Timber.d("DeleteFile -> ${file.name}") + drive.files().delete(file.id).execute() + } } Timber.d("DeleteFile <- Success") @@ -154,12 +157,13 @@ internal class GoogleDriveImpl( } } - private fun getFiles(drive: Drive) = - drive.files().list() + private fun getFiles(drive: Drive): List? { + return drive.files().list() .setSpaces("appDataFolder") .execute() ?.files ?.sortedByDescending { it.name.replace("2fas-backup-v", "").replace(".json", "").toIntOrNull() } + } private fun getDrive(accountCredentials: AccountCredentials): Drive { val googleAccountCredential = GoogleAccountCredential diff --git a/data/services/src/main/java/com/twofasapp/data/services/remote/WipeGoogleDriveWork.kt b/data/services/src/main/java/com/twofasapp/data/services/remote/WipeGoogleDriveWork.kt index cfcf4bf6..3ee200c9 100644 --- a/data/services/src/main/java/com/twofasapp/data/services/remote/WipeGoogleDriveWork.kt +++ b/data/services/src/main/java/com/twofasapp/data/services/remote/WipeGoogleDriveWork.kt @@ -17,7 +17,7 @@ class WipeGoogleDriveWork( private val googleAuth: GoogleAuth by inject() override suspend fun doWork(): Result { - googleDrive.deleteBackupFile() + googleDrive.deleteBackupFiles() googleAuth.revokeAccess() return Result.success()