From c9f88caa647c116327815d8b0234eba2abf9e650 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Sun, 31 Jan 2021 21:58:01 +0800 Subject: Update accesstoken in current account SharedPreferences after refreshing it. --- .../infinityforreddit/AccessTokenAuthenticator.java | 10 +++++++++- .../java/ml/docilealligator/infinityforreddit/AppModule.java | 5 +++-- .../infinityforreddit/PullNotificationWorker.java | 6 ++++++ 3 files changed, 18 insertions(+), 3 deletions(-) (limited to 'app/src') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/AccessTokenAuthenticator.java b/app/src/main/java/ml/docilealligator/infinityforreddit/AccessTokenAuthenticator.java index 8221b612..1ac3b368 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/AccessTokenAuthenticator.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/AccessTokenAuthenticator.java @@ -1,5 +1,7 @@ package ml.docilealligator.infinityforreddit; +import android.content.SharedPreferences; + import androidx.annotation.NonNull; import androidx.annotation.Nullable; @@ -13,6 +15,7 @@ import java.util.Map; import ml.docilealligator.infinityforreddit.account.Account; import ml.docilealligator.infinityforreddit.apis.RedditAPI; import ml.docilealligator.infinityforreddit.utils.APIUtils; +import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils; import okhttp3.Authenticator; import okhttp3.Headers; import okhttp3.Request; @@ -24,10 +27,12 @@ import retrofit2.Retrofit; class AccessTokenAuthenticator implements Authenticator { private Retrofit mRetrofit; private RedditDataRoomDatabase mRedditDataRoomDatabase; + private SharedPreferences mCurrentAccountSharedPreferences; - AccessTokenAuthenticator(Retrofit retrofit, RedditDataRoomDatabase accountRoomDatabase) { + AccessTokenAuthenticator(Retrofit retrofit, RedditDataRoomDatabase accountRoomDatabase, SharedPreferences currentAccountSharedPreferences) { mRetrofit = retrofit; mRedditDataRoomDatabase = accountRoomDatabase; + mCurrentAccountSharedPreferences = currentAccountSharedPreferences; } @Nullable @@ -77,6 +82,9 @@ class AccessTokenAuthenticator implements Authenticator { } else { mRedditDataRoomDatabase.accountDao().updateAccessTokenAndRefreshToken(account.getAccountName(), newAccessToken, newRefreshToken); } + if (mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCOUNT_NAME, "").equals(account.getAccountName())) { + mCurrentAccountSharedPreferences.edit().putString(SharedPreferencesUtils.ACCESS_TOKEN, newAccessToken).apply(); + } return newAccessToken; } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/AppModule.java b/app/src/main/java/ml/docilealligator/infinityforreddit/AppModule.java index 83cc3515..82796cec 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/AppModule.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/AppModule.java @@ -144,9 +144,10 @@ class AppModule { @Provides @Singleton - OkHttpClient provideOkHttpClient(@Named("no_oauth") Retrofit retrofit, RedditDataRoomDatabase accountRoomDatabase) { + OkHttpClient provideOkHttpClient(@Named("no_oauth") Retrofit retrofit, RedditDataRoomDatabase accountRoomDatabase, + @Named("current_account") SharedPreferences currentAccountSharedPreferences) { OkHttpClient.Builder okHttpClientBuilder = new OkHttpClient.Builder(); - okHttpClientBuilder.authenticator(new AccessTokenAuthenticator(retrofit, accountRoomDatabase)) + okHttpClientBuilder.authenticator(new AccessTokenAuthenticator(retrofit, accountRoomDatabase, currentAccountSharedPreferences)) .connectTimeout(30, TimeUnit.SECONDS) .readTimeout(30, TimeUnit.SECONDS) .writeTimeout(30, TimeUnit.SECONDS) diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PullNotificationWorker.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PullNotificationWorker.java index a0017f1c..44668b13 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PullNotificationWorker.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PullNotificationWorker.java @@ -56,6 +56,9 @@ public class PullNotificationWorker extends Worker { @Named("default") SharedPreferences mSharedPreferences; @Inject + @Named("current_account") + SharedPreferences mCurrentAccountSharedPreferences; + @Inject CustomThemeWrapper mCustomThemeWrapper; private Context context; @@ -255,6 +258,9 @@ public class PullNotificationWorker extends Worker { } else { mRedditDataRoomDatabase.accountDao().updateAccessTokenAndRefreshToken(account.getAccountName(), newAccessToken, newRefreshToken); } + if (mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCOUNT_NAME, "").equals(account.getAccountName())) { + mCurrentAccountSharedPreferences.edit().putString(SharedPreferencesUtils.ACCESS_TOKEN, newAccessToken).apply(); + } return newAccessToken; } return ""; -- cgit v1.2.3