From 624d65d9fd3d1714ff6050820d1805e986384ca3 Mon Sep 17 00:00:00 2001 From: kaiserbh Date: Thu, 25 Jan 2024 17:39:44 +1100 Subject: [PATCH] refactor: add more error guard for gdrive. Also changed it to be app specific, we don't want them to use sync data from SY or other forks as some of the model and backup is different. So if people using other forks they should use the same data and not mismatch. --- .../settings/screen/SettingsDataScreen.kt | 7 +++ .../sync/service/GoogleDriveSyncService.kt | 43 ++++++++++++------- .../commonMain/resources/MR/base/strings.xml | 2 + 3 files changed, 36 insertions(+), 16 deletions(-) diff --git a/app/src/main/java/eu/kanade/presentation/more/settings/screen/SettingsDataScreen.kt b/app/src/main/java/eu/kanade/presentation/more/settings/screen/SettingsDataScreen.kt index dc9ce50252..32a8bf5fec 100644 --- a/app/src/main/java/eu/kanade/presentation/more/settings/screen/SettingsDataScreen.kt +++ b/app/src/main/java/eu/kanade/presentation/more/settings/screen/SettingsDataScreen.kt @@ -400,12 +400,19 @@ object SettingsDataScreen : SearchableSettings { when (result) { GoogleDriveSyncService.DeleteSyncDataStatus.NOT_INITIALIZED -> context.toast( MR.strings.google_drive_not_signed_in, + duration = 5000, ) GoogleDriveSyncService.DeleteSyncDataStatus.NO_FILES -> context.toast( MR.strings.google_drive_sync_data_not_found, + duration = 5000, ) GoogleDriveSyncService.DeleteSyncDataStatus.SUCCESS -> context.toast( MR.strings.google_drive_sync_data_purged, + duration = 5000, + ) + GoogleDriveSyncService.DeleteSyncDataStatus.ERROR -> context.toast( + MR.strings.google_drive_sync_data_purge_error, + duration = 10000, ) } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/sync/service/GoogleDriveSyncService.kt b/app/src/main/java/eu/kanade/tachiyomi/data/sync/service/GoogleDriveSyncService.kt index b532d89f82..63b819edf6 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/sync/service/GoogleDriveSyncService.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/sync/service/GoogleDriveSyncService.kt @@ -56,18 +56,22 @@ class GoogleDriveSyncService(context: Context, json: Json, syncPreferences: Sync NOT_INITIALIZED, NO_FILES, SUCCESS, + ERROR, } - private val remoteFileName = "tachiyomi_sync_data.gz" + private val appName = context.stringResource(MR.strings.app_name) - private val lockFileName = "tachiyomi_sync.lock" + private val remoteFileName = "${appName}_sync_data.gz" + + private val lockFileName = "${appName}_sync.lock" private val googleDriveService = GoogleDriveService(context) override suspend fun beforeSync() { try { googleDriveService.refreshToken() - val drive = googleDriveService.driveService ?: throw Exception("Google Drive service not initialized") + val drive = googleDriveService.driveService + ?: throw Exception(context.stringResource(MR.strings.google_drive_not_signed_in)) var backoff = 1000L @@ -105,6 +109,7 @@ class GoogleDriveSyncService(context: Context, json: Json, syncPreferences: Sync } } catch (e: Exception) { logcat(LogPriority.ERROR) { "Error in GoogleDrive beforeSync: ${e.message}" } + throw Exception(context.stringResource(MR.strings.error_before_sync_gdrive) + ": ${e.message}") } } @@ -234,6 +239,7 @@ class GoogleDriveSyncService(context: Context, json: Json, syncPreferences: Sync Log.d("GoogleDrive", "Created lock file with ID: ${file.id}") } catch (e: Exception) { Log.e("GoogleDrive", "Error creating lock file: ${e.message}") + throw Exception(e.message) } } @@ -282,19 +288,24 @@ class GoogleDriveSyncService(context: Context, json: Json, syncPreferences: Sync googleDriveService.refreshToken() return withContext(Dispatchers.IO) { - val appDataFileList = getAppDataFileList(drive) - - if (appDataFileList.isEmpty()) { - logcat(LogPriority.DEBUG) { "No sync data file found in appData folder of Google Drive" } - DeleteSyncDataStatus.NO_FILES - } else { - for (file in appDataFileList) { - drive.files().delete(file.id).execute() - logcat( - LogPriority.DEBUG, - ) { "Deleted sync data file in appData folder of Google Drive with file ID: ${file.id}" } + try { + val appDataFileList = getAppDataFileList(drive) + + if (appDataFileList.isEmpty()) { + logcat(LogPriority.DEBUG) { "No sync data file found in appData folder of Google Drive" } + DeleteSyncDataStatus.NO_FILES + } else { + for (file in appDataFileList) { + drive.files().delete(file.id).execute() + logcat( + LogPriority.DEBUG, + ) { "Deleted sync data file in appData folder of Google Drive with file ID: ${file.id}" } + } + DeleteSyncDataStatus.SUCCESS } - DeleteSyncDataStatus.SUCCESS + } catch (e: Exception) { + logcat(LogPriority.ERROR) { "Error occurred while interacting with Google Drive: ${e.message}" } + DeleteSyncDataStatus.ERROR } } } @@ -441,7 +452,7 @@ class GoogleDriveService(private val context: Context) { NetHttpTransport(), jsonFactory, credential, - ).setApplicationName("Tachiyomi") + ).setApplicationName(context.stringResource(MR.strings.app_name)) .build() } diff --git a/i18n/src/commonMain/resources/MR/base/strings.xml b/i18n/src/commonMain/resources/MR/base/strings.xml index b96002f3df..7af35e7585 100644 --- a/i18n/src/commonMain/resources/MR/base/strings.xml +++ b/i18n/src/commonMain/resources/MR/base/strings.xml @@ -577,11 +577,13 @@ Clear Sync Data from Google Drive Sync data purged from Google Drive No sync data found in Google Drive + Error purging sync data from Google Drive, Try to sign in again. Logged in to Google Drive Failed to log in to Google Drive: %s Not signed in to Google Drive Error uploading sync data to Google Drive Error Deleting Google Drive Lock File + Error before sync: %s Purge confirmation Purging sync data will delete all your sync data from Google Drive. Are you sure you want to continue? Create sync triggers