From 976841605ea94c647d5df363c33de5a3704ebd80 Mon Sep 17 00:00:00 2001 From: KaiserBh Date: Mon, 20 Nov 2023 07:45:39 +1100 Subject: [PATCH] refactor: remove the redundant syncPrefs Signed-off-by: KaiserBh --- .../sync/service/GoogleDriveSyncService.kt | 22 ++++++++++--------- .../tachiyomi/domain/sync/SyncPreferences.kt | 17 ++------------ 2 files changed, 14 insertions(+), 25 deletions(-) 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 0a2113f0e..3995b8cdb 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 @@ -175,8 +175,8 @@ class GoogleDriveService(private val context: Context) { * and setting up the service using the obtained tokens. */ private fun initGoogleDriveService() { - val accessToken = syncPreferences.getGoogleDriveAccessToken() - val refreshToken = syncPreferences.getGoogleDriveRefreshToken() + val accessToken = syncPreferences.googleDriveAccessToken().get() + val refreshToken = syncPreferences.googleDriveRefreshToken().get() if (accessToken == "" || refreshToken == "") { googleDriveService = null @@ -226,6 +226,9 @@ class GoogleDriveService(private val context: Context) { .build() } internal suspend fun refreshToken() = withContext(Dispatchers.IO) { + val refreshToken = syncPreferences.googleDriveRefreshToken().get() + val accessToken = syncPreferences.googleDriveAccessToken().get() + val jsonFactory: JsonFactory = JacksonFactory.getDefaultInstance() val secrets = GoogleClientSecrets.load( jsonFactory, @@ -238,22 +241,21 @@ class GoogleDriveService(private val context: Context) { .setClientSecrets(secrets) .build() - if (syncPreferences.getGoogleDriveRefreshToken() == "") { + if (refreshToken == "") { throw Exception(context.getString(R.string.google_drive_not_signed_in)) } - credential.refreshToken = syncPreferences.getGoogleDriveRefreshToken() + credential.refreshToken = refreshToken - logcat(LogPriority.DEBUG) { "Refreshing access token with: ${syncPreferences.getGoogleDriveRefreshToken()}" } + logcat(LogPriority.DEBUG) { "Refreshing access token with: $refreshToken" } try { credential.refreshToken() val newAccessToken = credential.accessToken - val oldAccessToken = syncPreferences.getGoogleDriveAccessToken() // Save the new access token - syncPreferences.setGoogleDriveAccessToken(newAccessToken) + syncPreferences.googleDriveAccessToken().set(newAccessToken) setupGoogleDriveService(newAccessToken, credential.refreshToken) - logcat(LogPriority.DEBUG) { "Google Access token refreshed old: $oldAccessToken new: $newAccessToken" } + logcat(LogPriority.DEBUG) { "Google Access token refreshed old: $accessToken new: $newAccessToken" } } catch (e: TokenResponseException) { if (e.details.error == "invalid_grant") { // The refresh token is invalid, prompt the user to sign in again @@ -338,8 +340,8 @@ class GoogleDriveService(private val context: Context) { val refreshToken = tokenResponse.refreshToken // Save the tokens to SyncPreferences - syncPreferences.setGoogleDriveAccessToken(accessToken) - syncPreferences.setGoogleDriveRefreshToken(refreshToken) + syncPreferences.googleDriveAccessToken().set(accessToken) + syncPreferences.googleDriveRefreshToken().set(refreshToken) setupGoogleDriveService(accessToken, refreshToken) initGoogleDriveService() diff --git a/domain/src/main/java/tachiyomi/domain/sync/SyncPreferences.kt b/domain/src/main/java/tachiyomi/domain/sync/SyncPreferences.kt index 9c4d99970..5f2b6ab98 100644 --- a/domain/src/main/java/tachiyomi/domain/sync/SyncPreferences.kt +++ b/domain/src/main/java/tachiyomi/domain/sync/SyncPreferences.kt @@ -11,28 +11,15 @@ class SyncPreferences( fun lastSyncTimestamp() = preferenceStore.getLong(Preference.appStateKey("last_sync_timestamp"), 0L) fun syncInterval() = preferenceStore.getInt("sync_interval", 0) - fun syncService() = preferenceStore.getInt("sync_service", 0) - private fun googleDriveAccessToken() = preferenceStore.getString( + fun googleDriveAccessToken() = preferenceStore.getString( Preference.appStateKey("google_drive_access_token"), "", ) - fun setGoogleDriveAccessToken(accessToken: String) { - googleDriveAccessToken().set(accessToken) - } - - fun getGoogleDriveAccessToken() = googleDriveAccessToken().get() - - private fun googleDriveRefreshToken() = preferenceStore.getString( + fun googleDriveRefreshToken() = preferenceStore.getString( Preference.appStateKey("google_drive_refresh_token"), "", ) - - fun setGoogleDriveRefreshToken(refreshToken: String) { - googleDriveRefreshToken().set(refreshToken) - } - - fun getGoogleDriveRefreshToken() = googleDriveRefreshToken().get() }