diff options
author | Alex Ning <chineseperson5@gmail.com> | 2021-01-31 13:58:01 +0000 |
---|---|---|
committer | Alex Ning <chineseperson5@gmail.com> | 2021-01-31 13:58:01 +0000 |
commit | c9f88caa647c116327815d8b0234eba2abf9e650 (patch) | |
tree | de64fd1a3305016321705d8b2bcfbb70014b09c8 /app/src/main/java | |
parent | 323af18c696a4fbc84e8a6189c2856f7d8ac965f (diff) | |
download | infinity-for-reddit-c9f88caa647c116327815d8b0234eba2abf9e650.tar infinity-for-reddit-c9f88caa647c116327815d8b0234eba2abf9e650.tar.gz infinity-for-reddit-c9f88caa647c116327815d8b0234eba2abf9e650.tar.bz2 infinity-for-reddit-c9f88caa647c116327815d8b0234eba2abf9e650.tar.lz infinity-for-reddit-c9f88caa647c116327815d8b0234eba2abf9e650.tar.xz infinity-for-reddit-c9f88caa647c116327815d8b0234eba2abf9e650.tar.zst infinity-for-reddit-c9f88caa647c116327815d8b0234eba2abf9e650.zip |
Update accesstoken in current account SharedPreferences after refreshing it.
Diffstat (limited to 'app/src/main/java')
3 files changed, 18 insertions, 3 deletions
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 ""; |