Update accesstoken in current account SharedPreferences after refreshing it.

This commit is contained in:
Alex Ning 2021-01-31 21:58:01 +08:00
parent 323af18c69
commit c9f88caa64
3 changed files with 18 additions and 3 deletions

View File

@ -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;
}

View File

@ -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)

View File

@ -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 "";