mirror of
https://github.com/mihonapp/mihon.git
synced 2024-11-15 15:02:49 +01:00
refactor: remove the redundant syncPrefs
Signed-off-by: KaiserBh <kaiserbh@proton.me>
This commit is contained in:
parent
90cceeb040
commit
976841605e
@ -175,8 +175,8 @@ class GoogleDriveService(private val context: Context) {
|
|||||||
* and setting up the service using the obtained tokens.
|
* and setting up the service using the obtained tokens.
|
||||||
*/
|
*/
|
||||||
private fun initGoogleDriveService() {
|
private fun initGoogleDriveService() {
|
||||||
val accessToken = syncPreferences.getGoogleDriveAccessToken()
|
val accessToken = syncPreferences.googleDriveAccessToken().get()
|
||||||
val refreshToken = syncPreferences.getGoogleDriveRefreshToken()
|
val refreshToken = syncPreferences.googleDriveRefreshToken().get()
|
||||||
|
|
||||||
if (accessToken == "" || refreshToken == "") {
|
if (accessToken == "" || refreshToken == "") {
|
||||||
googleDriveService = null
|
googleDriveService = null
|
||||||
@ -226,6 +226,9 @@ class GoogleDriveService(private val context: Context) {
|
|||||||
.build()
|
.build()
|
||||||
}
|
}
|
||||||
internal suspend fun refreshToken() = withContext(Dispatchers.IO) {
|
internal suspend fun refreshToken() = withContext(Dispatchers.IO) {
|
||||||
|
val refreshToken = syncPreferences.googleDriveRefreshToken().get()
|
||||||
|
val accessToken = syncPreferences.googleDriveAccessToken().get()
|
||||||
|
|
||||||
val jsonFactory: JsonFactory = JacksonFactory.getDefaultInstance()
|
val jsonFactory: JsonFactory = JacksonFactory.getDefaultInstance()
|
||||||
val secrets = GoogleClientSecrets.load(
|
val secrets = GoogleClientSecrets.load(
|
||||||
jsonFactory,
|
jsonFactory,
|
||||||
@ -238,22 +241,21 @@ class GoogleDriveService(private val context: Context) {
|
|||||||
.setClientSecrets(secrets)
|
.setClientSecrets(secrets)
|
||||||
.build()
|
.build()
|
||||||
|
|
||||||
if (syncPreferences.getGoogleDriveRefreshToken() == "") {
|
if (refreshToken == "") {
|
||||||
throw Exception(context.getString(R.string.google_drive_not_signed_in))
|
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 {
|
try {
|
||||||
credential.refreshToken()
|
credential.refreshToken()
|
||||||
val newAccessToken = credential.accessToken
|
val newAccessToken = credential.accessToken
|
||||||
val oldAccessToken = syncPreferences.getGoogleDriveAccessToken()
|
|
||||||
// Save the new access token
|
// Save the new access token
|
||||||
syncPreferences.setGoogleDriveAccessToken(newAccessToken)
|
syncPreferences.googleDriveAccessToken().set(newAccessToken)
|
||||||
setupGoogleDriveService(newAccessToken, credential.refreshToken)
|
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) {
|
} catch (e: TokenResponseException) {
|
||||||
if (e.details.error == "invalid_grant") {
|
if (e.details.error == "invalid_grant") {
|
||||||
// The refresh token is invalid, prompt the user to sign in again
|
// 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
|
val refreshToken = tokenResponse.refreshToken
|
||||||
|
|
||||||
// Save the tokens to SyncPreferences
|
// Save the tokens to SyncPreferences
|
||||||
syncPreferences.setGoogleDriveAccessToken(accessToken)
|
syncPreferences.googleDriveAccessToken().set(accessToken)
|
||||||
syncPreferences.setGoogleDriveRefreshToken(refreshToken)
|
syncPreferences.googleDriveRefreshToken().set(refreshToken)
|
||||||
|
|
||||||
setupGoogleDriveService(accessToken, refreshToken)
|
setupGoogleDriveService(accessToken, refreshToken)
|
||||||
initGoogleDriveService()
|
initGoogleDriveService()
|
||||||
|
@ -11,28 +11,15 @@ class SyncPreferences(
|
|||||||
fun lastSyncTimestamp() = preferenceStore.getLong(Preference.appStateKey("last_sync_timestamp"), 0L)
|
fun lastSyncTimestamp() = preferenceStore.getLong(Preference.appStateKey("last_sync_timestamp"), 0L)
|
||||||
|
|
||||||
fun syncInterval() = preferenceStore.getInt("sync_interval", 0)
|
fun syncInterval() = preferenceStore.getInt("sync_interval", 0)
|
||||||
|
|
||||||
fun syncService() = preferenceStore.getInt("sync_service", 0)
|
fun syncService() = preferenceStore.getInt("sync_service", 0)
|
||||||
|
|
||||||
private fun googleDriveAccessToken() = preferenceStore.getString(
|
fun googleDriveAccessToken() = preferenceStore.getString(
|
||||||
Preference.appStateKey("google_drive_access_token"),
|
Preference.appStateKey("google_drive_access_token"),
|
||||||
"",
|
"",
|
||||||
)
|
)
|
||||||
|
|
||||||
fun setGoogleDriveAccessToken(accessToken: String) {
|
fun googleDriveRefreshToken() = preferenceStore.getString(
|
||||||
googleDriveAccessToken().set(accessToken)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun getGoogleDriveAccessToken() = googleDriveAccessToken().get()
|
|
||||||
|
|
||||||
private fun googleDriveRefreshToken() = preferenceStore.getString(
|
|
||||||
Preference.appStateKey("google_drive_refresh_token"),
|
Preference.appStateKey("google_drive_refresh_token"),
|
||||||
"",
|
"",
|
||||||
)
|
)
|
||||||
|
|
||||||
fun setGoogleDriveRefreshToken(refreshToken: String) {
|
|
||||||
googleDriveRefreshToken().set(refreshToken)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun getGoogleDriveRefreshToken() = googleDriveRefreshToken().get()
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user