mirror of
https://codeberg.org/Bazsalanszky/Infinity-For-Lemmy.git
synced 2025-01-02 14:27:10 +01:00
Update accesstoken in current account SharedPreferences after refreshing it.
This commit is contained in:
parent
323af18c69
commit
c9f88caa64
@ -1,5 +1,7 @@
|
|||||||
package ml.docilealligator.infinityforreddit;
|
package ml.docilealligator.infinityforreddit;
|
||||||
|
|
||||||
|
import android.content.SharedPreferences;
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
import androidx.annotation.Nullable;
|
import androidx.annotation.Nullable;
|
||||||
|
|
||||||
@ -13,6 +15,7 @@ import java.util.Map;
|
|||||||
import ml.docilealligator.infinityforreddit.account.Account;
|
import ml.docilealligator.infinityforreddit.account.Account;
|
||||||
import ml.docilealligator.infinityforreddit.apis.RedditAPI;
|
import ml.docilealligator.infinityforreddit.apis.RedditAPI;
|
||||||
import ml.docilealligator.infinityforreddit.utils.APIUtils;
|
import ml.docilealligator.infinityforreddit.utils.APIUtils;
|
||||||
|
import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils;
|
||||||
import okhttp3.Authenticator;
|
import okhttp3.Authenticator;
|
||||||
import okhttp3.Headers;
|
import okhttp3.Headers;
|
||||||
import okhttp3.Request;
|
import okhttp3.Request;
|
||||||
@ -24,10 +27,12 @@ import retrofit2.Retrofit;
|
|||||||
class AccessTokenAuthenticator implements Authenticator {
|
class AccessTokenAuthenticator implements Authenticator {
|
||||||
private Retrofit mRetrofit;
|
private Retrofit mRetrofit;
|
||||||
private RedditDataRoomDatabase mRedditDataRoomDatabase;
|
private RedditDataRoomDatabase mRedditDataRoomDatabase;
|
||||||
|
private SharedPreferences mCurrentAccountSharedPreferences;
|
||||||
|
|
||||||
AccessTokenAuthenticator(Retrofit retrofit, RedditDataRoomDatabase accountRoomDatabase) {
|
AccessTokenAuthenticator(Retrofit retrofit, RedditDataRoomDatabase accountRoomDatabase, SharedPreferences currentAccountSharedPreferences) {
|
||||||
mRetrofit = retrofit;
|
mRetrofit = retrofit;
|
||||||
mRedditDataRoomDatabase = accountRoomDatabase;
|
mRedditDataRoomDatabase = accountRoomDatabase;
|
||||||
|
mCurrentAccountSharedPreferences = currentAccountSharedPreferences;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
@ -77,6 +82,9 @@ class AccessTokenAuthenticator implements Authenticator {
|
|||||||
} else {
|
} else {
|
||||||
mRedditDataRoomDatabase.accountDao().updateAccessTokenAndRefreshToken(account.getAccountName(), newAccessToken, newRefreshToken);
|
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 newAccessToken;
|
||||||
}
|
}
|
||||||
|
@ -144,9 +144,10 @@ class AppModule {
|
|||||||
|
|
||||||
@Provides
|
@Provides
|
||||||
@Singleton
|
@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();
|
OkHttpClient.Builder okHttpClientBuilder = new OkHttpClient.Builder();
|
||||||
okHttpClientBuilder.authenticator(new AccessTokenAuthenticator(retrofit, accountRoomDatabase))
|
okHttpClientBuilder.authenticator(new AccessTokenAuthenticator(retrofit, accountRoomDatabase, currentAccountSharedPreferences))
|
||||||
.connectTimeout(30, TimeUnit.SECONDS)
|
.connectTimeout(30, TimeUnit.SECONDS)
|
||||||
.readTimeout(30, TimeUnit.SECONDS)
|
.readTimeout(30, TimeUnit.SECONDS)
|
||||||
.writeTimeout(30, TimeUnit.SECONDS)
|
.writeTimeout(30, TimeUnit.SECONDS)
|
||||||
|
@ -56,6 +56,9 @@ public class PullNotificationWorker extends Worker {
|
|||||||
@Named("default")
|
@Named("default")
|
||||||
SharedPreferences mSharedPreferences;
|
SharedPreferences mSharedPreferences;
|
||||||
@Inject
|
@Inject
|
||||||
|
@Named("current_account")
|
||||||
|
SharedPreferences mCurrentAccountSharedPreferences;
|
||||||
|
@Inject
|
||||||
CustomThemeWrapper mCustomThemeWrapper;
|
CustomThemeWrapper mCustomThemeWrapper;
|
||||||
private Context context;
|
private Context context;
|
||||||
|
|
||||||
@ -255,6 +258,9 @@ public class PullNotificationWorker extends Worker {
|
|||||||
} else {
|
} else {
|
||||||
mRedditDataRoomDatabase.accountDao().updateAccessTokenAndRefreshToken(account.getAccountName(), newAccessToken, newRefreshToken);
|
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 newAccessToken;
|
||||||
}
|
}
|
||||||
return "";
|
return "";
|
||||||
|
Loading…
Reference in New Issue
Block a user