Save current account info to SharedPreferences.

This commit is contained in:
Alex Ning 2021-01-21 19:07:48 +08:00
parent 4ae21a18f0
commit 53d2cd14c3
35 changed files with 214 additions and 185 deletions

View File

@ -73,9 +73,9 @@ class AccessTokenAuthenticator implements Authenticator {
String newAccessToken = jsonObject.getString(APIUtils.ACCESS_TOKEN_KEY);
String newRefreshToken = jsonObject.has(APIUtils.REFRESH_TOKEN_KEY) ? jsonObject.getString(APIUtils.REFRESH_TOKEN_KEY) : null;
if (newRefreshToken == null) {
mRedditDataRoomDatabase.accountDao().updateAccessToken(account.getUsername(), newAccessToken);
mRedditDataRoomDatabase.accountDao().updateAccessToken(account.getAccountName(), newAccessToken);
} else {
mRedditDataRoomDatabase.accountDao().updateAccessTokenAndRefreshToken(account.getUsername(), newAccessToken, newRefreshToken);
mRedditDataRoomDatabase.accountDao().updateAccessTokenAndRefreshToken(account.getAccountName(), newAccessToken, newRefreshToken);
}
return newAccessToken;

View File

@ -230,6 +230,12 @@ class AppModule {
return mApplication.getSharedPreferences(SharedPreferencesUtils.POST_HISTORY_SHARED_PREFERENCES_FILE, Context.MODE_PRIVATE);
}
@Provides
@Named("current_account")
SharedPreferences provideCurrentAccountSharedPreferences() {
return mApplication.getSharedPreferences(SharedPreferencesUtils.CURRENT_ACCOUNT_SHARED_PREFERENCES_FILE, Context.MODE_PRIVATE);
}
@Provides
@Singleton
CustomThemeWrapper provideCustomThemeWrapper(@Named("light_theme") SharedPreferences lightThemeSharedPreferences,

View File

@ -74,7 +74,7 @@ public class PullNotificationWorker extends Worker {
for (int accountIndex = 0; accountIndex < accounts.size(); accountIndex++) {
Account account = accounts.get(accountIndex);
String accountName = account.getUsername();
String accountName = account.getAccountName();
Response<String> response = fetchMessages(account, 1);
@ -251,9 +251,9 @@ public class PullNotificationWorker extends Worker {
String newAccessToken = jsonObject.getString(APIUtils.ACCESS_TOKEN_KEY);
String newRefreshToken = jsonObject.has(APIUtils.REFRESH_TOKEN_KEY) ? jsonObject.getString(APIUtils.REFRESH_TOKEN_KEY) : null;
if (newRefreshToken == null) {
mRedditDataRoomDatabase.accountDao().updateAccessToken(account.getUsername(), newAccessToken);
mRedditDataRoomDatabase.accountDao().updateAccessToken(account.getAccountName(), newAccessToken);
} else {
mRedditDataRoomDatabase.accountDao().updateAccessTokenAndRefreshToken(account.getUsername(), newAccessToken, newRefreshToken);
mRedditDataRoomDatabase.accountDao().updateAccessTokenAndRefreshToken(account.getAccountName(), newAccessToken, newRefreshToken);
}
return newAccessToken;
}

View File

@ -10,7 +10,7 @@ public class Account {
@PrimaryKey
@NonNull
@ColumnInfo(name = "username")
private String username;
private String accountName;
@ColumnInfo(name = "profile_image_url")
private String profileImageUrl;
@ColumnInfo(name = "banner_image_url")
@ -26,9 +26,9 @@ public class Account {
@ColumnInfo(name = "is_current_user")
private boolean isCurrentUser;
public Account(@NonNull String username, String accessToken, String refreshToken, String code,
public Account(@NonNull String accountName, String accessToken, String refreshToken, String code,
String profileImageUrl, String bannerImageUrl, int karma, boolean isCurrentUser) {
this.username = username;
this.accountName = accountName;
this.accessToken = accessToken;
this.refreshToken = refreshToken;
this.code = code;
@ -38,8 +38,9 @@ public class Account {
this.isCurrentUser = isCurrentUser;
}
public String getUsername() {
return username;
@NonNull
public String getAccountName() {
return accountName;
}
public String getProfileImageUrl() {

View File

@ -191,7 +191,7 @@ public class AccountPostsActivity extends BaseActivity implements SortTypeSelect
mNullAccessToken = true;
} else {
mAccessToken = account.getAccessToken();
mAccountName = account.getUsername();
mAccountName = account.getAccountName();
}
initializeFragment();
});

View File

@ -197,7 +197,7 @@ public class AccountSavedThingActivity extends BaseActivity implements ActivityT
mNullAccessToken = true;
} else {
mAccessToken = account.getAccessToken();
mAccountName = account.getUsername();
mAccountName = account.getAccountName();
}
initializeViewPager();
});

View File

@ -136,7 +136,7 @@ public class CreateMultiRedditActivity extends BaseActivity {
finish();
} else {
mAccessToken = account.getAccessToken();
mAccountName = account.getUsername();
mAccountName = account.getAccountName();
bindView();
}
});

View File

@ -146,7 +146,7 @@ public class EditMultiRedditActivity extends BaseActivity {
finish();
} else {
mAccessToken = account.getAccessToken();
mAccountName = account.getUsername();
mAccountName = account.getAccountName();
bindView();
}
});

View File

@ -273,7 +273,7 @@ public class FilteredPostsActivity extends BaseActivity implements SortTypeSelec
mNullAccessToken = true;
} else {
mAccessToken = account.getAccessToken();
mAccountName = account.getUsername();
mAccountName = account.getAccountName();
}
bindView(postFilter, true);
});

View File

@ -50,7 +50,7 @@ import ml.docilealligator.infinityforreddit.RecyclerViewContentScrollingInterfac
import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase;
import ml.docilealligator.infinityforreddit.apis.RedditAPI;
import ml.docilealligator.infinityforreddit.asynctasks.GetCurrentAccount;
import ml.docilealligator.infinityforreddit.asynctasks.SwitchAccountAsyncTask;
import ml.docilealligator.infinityforreddit.asynctasks.SwitchAccount;
import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper;
import ml.docilealligator.infinityforreddit.events.SwitchAccountEvent;
import ml.docilealligator.infinityforreddit.fragments.InboxFragment;
@ -95,6 +95,9 @@ public class InboxActivity extends BaseActivity implements ActivityToolbarInterf
@Named("default")
SharedPreferences mSharedPreferences;
@Inject
@Named("current_account")
SharedPreferences mCurrentAccountSharedPreferences;
@Inject
CustomThemeWrapper mCustomThemeWrapper;
@Inject
Executor mExecutor;
@ -234,8 +237,9 @@ public class InboxActivity extends BaseActivity implements ActivityToolbarInterf
private void getCurrentAccountAndFetchMessage(Bundle savedInstanceState) {
GetCurrentAccount.getCurrentAccount(mExecutor, new Handler(), mRedditDataRoomDatabase, account -> {
if (mNewAccountName != null) {
if (account == null || !account.getUsername().equals(mNewAccountName)) {
new SwitchAccountAsyncTask(mRedditDataRoomDatabase, mNewAccountName, newAccount -> {
if (account == null || !account.getAccountName().equals(mNewAccountName)) {
SwitchAccount.switchAccount(mRedditDataRoomDatabase, mCurrentAccountSharedPreferences,
mExecutor, new Handler(), mNewAccountName, newAccount -> {
EventBus.getDefault().post(new SwitchAccountEvent(getClass().getName()));
Toast.makeText(this, R.string.account_switched, Toast.LENGTH_SHORT).show();
@ -247,7 +251,7 @@ public class InboxActivity extends BaseActivity implements ActivityToolbarInterf
}
bindView(savedInstanceState);
}).execute();
});
} else {
mAccessToken = account.getAccessToken();
bindView(savedInstanceState);

View File

@ -73,6 +73,9 @@ public class LoginActivity extends BaseActivity {
@Named("default")
SharedPreferences mSharedPreferences;
@Inject
@Named("current_account")
SharedPreferences mCurrentAccountSharedPreferences;
@Inject
CustomThemeWrapper mCustomThemeWrapper;
private String authCode;
@ -157,6 +160,8 @@ public class LoginActivity extends BaseActivity {
accessToken, new FetchMyInfo.FetchMyInfoListener() {
@Override
public void onFetchMyInfoSuccess(String name, String profileImageUrl, String bannerImageUrl, int karma) {
mCurrentAccountSharedPreferences.edit().putString(SharedPreferencesUtils.ACCESS_TOKEN, accessToken)
.putString(SharedPreferencesUtils.ACCOUNT_NAME, name).apply();
new ParseAndInsertNewAccountAsyncTask(name, accessToken, refreshToken, profileImageUrl, bannerImageUrl,
karma, authCode, mRedditDataRoomDatabase.accountDao(),
() -> {

View File

@ -72,9 +72,9 @@ import ml.docilealligator.infinityforreddit.FetchMyInfo;
import ml.docilealligator.infinityforreddit.FetchSubscribedThing;
import ml.docilealligator.infinityforreddit.Infinity;
import ml.docilealligator.infinityforreddit.MarkPostAsReadInterface;
import ml.docilealligator.infinityforreddit.RecyclerViewContentScrollingInterface;
import ml.docilealligator.infinityforreddit.PullNotificationWorker;
import ml.docilealligator.infinityforreddit.R;
import ml.docilealligator.infinityforreddit.RecyclerViewContentScrollingInterface;
import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase;
import ml.docilealligator.infinityforreddit.SortType;
import ml.docilealligator.infinityforreddit.SortTypeSelectionCallback;
@ -82,8 +82,8 @@ import ml.docilealligator.infinityforreddit.account.AccountViewModel;
import ml.docilealligator.infinityforreddit.adapters.NavigationDrawerRecyclerViewAdapter;
import ml.docilealligator.infinityforreddit.asynctasks.GetCurrentAccount;
import ml.docilealligator.infinityforreddit.asynctasks.InsertSubscribedThingsAsyncTask;
import ml.docilealligator.infinityforreddit.asynctasks.SwitchAccountAsyncTask;
import ml.docilealligator.infinityforreddit.asynctasks.SwitchToAnonymousAccountAsyncTask;
import ml.docilealligator.infinityforreddit.asynctasks.SwitchAccount;
import ml.docilealligator.infinityforreddit.asynctasks.SwitchToAnonymousMode;
import ml.docilealligator.infinityforreddit.bottomsheetfragments.FABMoreOptionsBottomSheetFragment;
import ml.docilealligator.infinityforreddit.bottomsheetfragments.PostLayoutBottomSheetFragment;
import ml.docilealligator.infinityforreddit.bottomsheetfragments.PostTypeBottomSheetFragment;
@ -199,6 +199,9 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb
@Named("bottom_app_bar")
SharedPreferences bottomAppBarSharedPreference;
@Inject
@Named("current_account")
SharedPreferences currentAccountSharedPreferences;
@Inject
CustomThemeWrapper mCustomThemeWrapper;
@Inject
Executor mExecutor;
@ -367,44 +370,45 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb
WorkManager workManager = WorkManager.getInstance(this);
if (mNewAccountName != null) {
if (account == null || !account.getUsername().equals(mNewAccountName)) {
new SwitchAccountAsyncTask(mRedditDataRoomDatabase, mNewAccountName, newAccount -> {
EventBus.getDefault().post(new SwitchAccountEvent(getClass().getName()));
Toast.makeText(this, R.string.account_switched, Toast.LENGTH_SHORT).show();
if (account == null || !account.getAccountName().equals(mNewAccountName)) {
SwitchAccount.switchAccount(mRedditDataRoomDatabase, currentAccountSharedPreferences,
mExecutor, new Handler(), mNewAccountName, newAccount -> {
EventBus.getDefault().post(new SwitchAccountEvent(getClass().getName()));
Toast.makeText(this, R.string.account_switched, Toast.LENGTH_SHORT).show();
mNewAccountName = null;
if (newAccount == null) {
mNullAccessToken = true;
} else {
mAccessToken = newAccount.getAccessToken();
mAccountName = newAccount.getUsername();
mProfileImageUrl = newAccount.getProfileImageUrl();
mBannerImageUrl = newAccount.getBannerImageUrl();
mKarma = newAccount.getKarma();
}
mNewAccountName = null;
if (newAccount == null) {
mNullAccessToken = true;
} else {
mAccessToken = newAccount.getAccessToken();
mAccountName = newAccount.getAccountName();
mProfileImageUrl = newAccount.getProfileImageUrl();
mBannerImageUrl = newAccount.getBannerImageUrl();
mKarma = newAccount.getKarma();
}
if (enableNotification) {
Constraints constraints = new Constraints.Builder()
.setRequiredNetworkType(NetworkType.CONNECTED)
.build();
PeriodicWorkRequest pullNotificationRequest =
new PeriodicWorkRequest.Builder(PullNotificationWorker.class,
notificationInterval, timeUnit)
.setConstraints(constraints)
if (enableNotification) {
Constraints constraints = new Constraints.Builder()
.setRequiredNetworkType(NetworkType.CONNECTED)
.build();
workManager.enqueueUniquePeriodicWork(PullNotificationWorker.UNIQUE_WORKER_NAME,
ExistingPeriodicWorkPolicy.KEEP, pullNotificationRequest);
} else {
workManager.cancelUniqueWork(PullNotificationWorker.UNIQUE_WORKER_NAME);
}
PeriodicWorkRequest pullNotificationRequest =
new PeriodicWorkRequest.Builder(PullNotificationWorker.class,
notificationInterval, timeUnit)
.setConstraints(constraints)
.build();
bindView();
}).execute();
workManager.enqueueUniquePeriodicWork(PullNotificationWorker.UNIQUE_WORKER_NAME,
ExistingPeriodicWorkPolicy.KEEP, pullNotificationRequest);
} else {
workManager.cancelUniqueWork(PullNotificationWorker.UNIQUE_WORKER_NAME);
}
bindView();
});
} else {
mAccessToken = account.getAccessToken();
mAccountName = account.getUsername();
mAccountName = account.getAccountName();
mProfileImageUrl = account.getProfileImageUrl();
mBannerImageUrl = account.getBannerImageUrl();
mKarma = account.getKarma();
@ -433,7 +437,7 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb
mNullAccessToken = true;
} else {
mAccessToken = account.getAccessToken();
mAccountName = account.getUsername();
mAccountName = account.getAccountName();
mProfileImageUrl = account.getProfileImageUrl();
mBannerImageUrl = account.getBannerImageUrl();
mKarma = account.getKarma();
@ -792,19 +796,20 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb
Intent addAccountIntent = new Intent(MainActivity.this, LoginActivity.class);
startActivityForResult(addAccountIntent, LOGIN_ACTIVITY_REQUEST_CODE);
} else if (stringId == R.string.anonymous_account) {
new SwitchToAnonymousAccountAsyncTask(mRedditDataRoomDatabase, false,
() -> {
SwitchToAnonymousMode.switchToAnonymousMode(mRedditDataRoomDatabase, currentAccountSharedPreferences,
mExecutor, new Handler(), false, () -> {
Intent anonymousIntent = new Intent(MainActivity.this, MainActivity.class);
startActivity(anonymousIntent);
finish();
}).execute();
});
} else if (stringId == R.string.log_out) {
new SwitchToAnonymousAccountAsyncTask(mRedditDataRoomDatabase, true,
SwitchToAnonymousMode.switchToAnonymousMode(mRedditDataRoomDatabase, currentAccountSharedPreferences,
mExecutor, new Handler(), true,
() -> {
Intent logOutIntent = new Intent(MainActivity.this, MainActivity.class);
startActivity(logOutIntent);
finish();
}).execute();
});
}
if (intent != null) {
startActivity(intent);
@ -821,11 +826,12 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb
@Override
public void onAccountClick(String accountName) {
new SwitchAccountAsyncTask(mRedditDataRoomDatabase, accountName, newAccount -> {
SwitchAccount.switchAccount(mRedditDataRoomDatabase, currentAccountSharedPreferences,
mExecutor, new Handler(), accountName, newAccount -> {
Intent intent = new Intent(MainActivity.this, MainActivity.class);
startActivity(intent);
finish();
}).execute();
});
}
});
navDrawerRecyclerView.setLayoutManager(new LinearLayoutManager(this));

View File

@ -158,7 +158,7 @@ public class MultiredditSelectionActivity extends BaseActivity implements Activi
mNullAccessToken = true;
} else {
mAccessToken = account.getAccessToken();
mAccountName = account.getUsername();
mAccountName = account.getAccountName();
}
bindView(true);
});

View File

@ -416,7 +416,7 @@ public class PostImageActivity extends BaseActivity implements FlairBottomSheetF
mNullAccessToken = true;
} else {
mAccessToken = account.getAccessToken();
mAccountName = account.getUsername();
mAccountName = account.getAccountName();
}
});
}

View File

@ -421,7 +421,7 @@ public class PostVideoActivity extends BaseActivity implements FlairBottomSheetF
mNullAccessToken = true;
} else {
mAccessToken = account.getAccessToken();
mAccountName = account.getUsername();
mAccountName = account.getAccountName();
}
});
}

View File

@ -224,7 +224,7 @@ public class SearchActivity extends BaseActivity {
if (account == null) {
mNullAccountName = true;
} else {
mAccountName = account.getUsername();
mAccountName = account.getAccountName();
}
bindView();
});

View File

@ -233,7 +233,7 @@ public class SearchResultActivity extends BaseActivity implements SortTypeSelect
mNullAccessToken = true;
} else {
mAccessToken = account.getAccessToken();
mAccountName = account.getUsername();
mAccountName = account.getAccountName();
}
bindView();
});

View File

@ -151,7 +151,7 @@ public class SearchSubredditsResultActivity extends BaseActivity implements Acti
mNullAccessToken = true;
} else {
mAccessToken = account.getAccessToken();
mAccountName = account.getUsername();
mAccountName = account.getAccountName();
}
mFragment = new SubredditListingFragment();

View File

@ -149,7 +149,7 @@ public class SearchUsersResultActivity extends BaseActivity implements ActivityT
mNullAccessToken = true;
} else {
mAccessToken = account.getAccessToken();
mAccountName = account.getUsername();
mAccountName = account.getAccountName();
}
mFragment = new UserListingFragment();

View File

@ -129,7 +129,7 @@ public class SelectUserFlairActivity extends BaseActivity implements ActivityToo
mNullAccessToken = true;
} else {
mAccessToken = account.getAccessToken();
mAccountName = account.getUsername();
mAccountName = account.getAccountName();
}
bindView();
});

View File

@ -113,7 +113,7 @@ public class SettingsActivity extends BaseActivity implements
if (account == null) {
mNullAccountName = true;
} else {
mAccountName = account.getUsername();
mAccountName = account.getAccountName();
}
if (savedInstanceState == null) {

View File

@ -161,7 +161,7 @@ public class SubredditMultiselectionActivity extends BaseActivity implements Act
finish();
} else {
mAccessToken = account.getAccessToken();
mAccountName = account.getUsername();
mAccountName = account.getAccountName();
bindView();
}
});

View File

@ -169,7 +169,7 @@ public class SubredditSelectionActivity extends BaseActivity implements Activity
mNullAccessToken = true;
} else {
mAccessToken = account.getAccessToken();
mAccountName = account.getUsername();
mAccountName = account.getAccountName();
mAccountProfileImageUrl = account.getProfileImageUrl();
}
bindView(true);

View File

@ -194,7 +194,7 @@ public class SubscribedThingListingActivity extends BaseActivity implements Acti
mNullAccessToken = true;
} else {
mAccessToken = account.getAccessToken();
mAccountName = account.getUsername();
mAccountName = account.getAccountName();
}
initializeViewPagerAndLoadSubscriptions();
});

View File

@ -194,7 +194,7 @@ public class ViewMultiRedditDetailActivity extends BaseActivity implements SortT
mNullAccessToken = true;
} else {
mAccessToken = account.getAccessToken();
mAccountName = account.getUsername();
mAccountName = account.getAccountName();
}
initializeFragment();
});

View File

@ -78,7 +78,7 @@ import ml.docilealligator.infinityforreddit.SortTypeSelectionCallback;
import ml.docilealligator.infinityforreddit.adapters.CommentAndPostRecyclerViewAdapter;
import ml.docilealligator.infinityforreddit.apis.RedditAPI;
import ml.docilealligator.infinityforreddit.asynctasks.GetCurrentAccount;
import ml.docilealligator.infinityforreddit.asynctasks.SwitchAccountAsyncTask;
import ml.docilealligator.infinityforreddit.asynctasks.SwitchAccount;
import ml.docilealligator.infinityforreddit.bottomsheetfragments.FlairBottomSheetFragment;
import ml.docilealligator.infinityforreddit.bottomsheetfragments.PostCommentSortTypeBottomSheetFragment;
import ml.docilealligator.infinityforreddit.comment.Comment;
@ -205,6 +205,9 @@ public class ViewPostDetailActivity extends BaseActivity implements FlairBottomS
@Named("nsfw_and_spoiler")
SharedPreferences mNsfwAndSpoilerSharedPreferences;
@Inject
@Named("current_account")
SharedPreferences mCurrentAccountSharedPreferences;
@Inject
CustomThemeWrapper mCustomThemeWrapper;
@Inject
ExoCreator mExoCreator;
@ -595,8 +598,9 @@ public class ViewPostDetailActivity extends BaseActivity implements FlairBottomS
private void getCurrentAccountAndBindView() {
GetCurrentAccount.getCurrentAccount(mExecutor, new Handler(), mRedditDataRoomDatabase, account -> {
if (mNewAccountName != null) {
if (account == null || !account.getUsername().equals(mNewAccountName)) {
new SwitchAccountAsyncTask(mRedditDataRoomDatabase, mNewAccountName, newAccount -> {
if (account == null || !account.getAccountName().equals(mNewAccountName)) {
SwitchAccount.switchAccount(mRedditDataRoomDatabase, mCurrentAccountSharedPreferences,
mExecutor, new Handler(), mNewAccountName, newAccount -> {
EventBus.getDefault().post(new SwitchAccountEvent(getClass().getName()));
Toast.makeText(this, R.string.account_switched, Toast.LENGTH_SHORT).show();
@ -605,14 +609,14 @@ public class ViewPostDetailActivity extends BaseActivity implements FlairBottomS
mNullAccessToken = true;
} else {
mAccessToken = newAccount.getAccessToken();
mAccountName = newAccount.getUsername();
mAccountName = newAccount.getAccountName();
}
bindView();
}).execute();
});
} else {
mAccessToken = account.getAccessToken();
mAccountName = account.getUsername();
mAccountName = account.getAccountName();
bindView();
}
} else {
@ -620,7 +624,7 @@ public class ViewPostDetailActivity extends BaseActivity implements FlairBottomS
mNullAccessToken = true;
} else {
mAccessToken = account.getAccessToken();
mAccountName = account.getUsername();
mAccountName = account.getAccountName();
}
bindView();

View File

@ -148,7 +148,7 @@ public class ViewPrivateMessagesActivity extends BaseActivity implements Activit
mNullAccessToken = true;
} else {
mAccessToken = account.getAccessToken();
mAccountName = account.getUsername();
mAccountName = account.getAccountName();
}
bindView();
});

View File

@ -74,7 +74,7 @@ import ml.docilealligator.infinityforreddit.asynctasks.AddSubredditOrUserToMulti
import ml.docilealligator.infinityforreddit.asynctasks.CheckIsSubscribedToSubredditAsyncTask;
import ml.docilealligator.infinityforreddit.asynctasks.GetCurrentAccount;
import ml.docilealligator.infinityforreddit.asynctasks.InsertSubredditDataAsyncTask;
import ml.docilealligator.infinityforreddit.asynctasks.SwitchAccountAsyncTask;
import ml.docilealligator.infinityforreddit.asynctasks.SwitchAccount;
import ml.docilealligator.infinityforreddit.bottomsheetfragments.FABMoreOptionsBottomSheetFragment;
import ml.docilealligator.infinityforreddit.bottomsheetfragments.PostLayoutBottomSheetFragment;
import ml.docilealligator.infinityforreddit.bottomsheetfragments.PostTypeBottomSheetFragment;
@ -189,7 +189,10 @@ public class ViewSubredditDetailActivity extends BaseActivity implements SortTyp
SharedPreferences mPostLayoutSharedPreferences;
@Inject
@Named("bottom_app_bar")
SharedPreferences bottomAppBarSharedPreference;
SharedPreferences mBottomAppBarSharedPreference;
@Inject
@Named("current_account")
SharedPreferences mCurrentAccountSharedPreferences;
@Inject
CustomThemeWrapper mCustomThemeWrapper;
@Inject
@ -494,8 +497,9 @@ public class ViewSubredditDetailActivity extends BaseActivity implements SortTyp
private void getCurrentAccountAndBindView() {
GetCurrentAccount.getCurrentAccount(mExecutor, new Handler(), mRedditDataRoomDatabase, account -> {
if (mNewAccountName != null) {
if (account == null || !account.getUsername().equals(mNewAccountName)) {
new SwitchAccountAsyncTask(mRedditDataRoomDatabase, mNewAccountName, newAccount -> {
if (account == null || !account.getAccountName().equals(mNewAccountName)) {
SwitchAccount.switchAccount(mRedditDataRoomDatabase, mCurrentAccountSharedPreferences,
mExecutor, new Handler(), mNewAccountName, newAccount -> {
EventBus.getDefault().post(new SwitchAccountEvent(getClass().getName()));
Toast.makeText(this, R.string.account_switched, Toast.LENGTH_SHORT).show();
@ -504,14 +508,14 @@ public class ViewSubredditDetailActivity extends BaseActivity implements SortTyp
mNullAccessToken = true;
} else {
mAccessToken = newAccount.getAccessToken();
mAccountName = newAccount.getUsername();
mAccountName = newAccount.getAccountName();
}
bindView();
}).execute();
});
} else {
mAccessToken = account.getAccessToken();
mAccountName = account.getUsername();
mAccountName = account.getAccountName();
bindView();
}
} else {
@ -519,7 +523,7 @@ public class ViewSubredditDetailActivity extends BaseActivity implements SortTyp
mNullAccessToken = true;
} else {
mAccessToken = account.getAccessToken();
mAccountName = account.getUsername();
mAccountName = account.getAccountName();
}
bindView();
@ -673,9 +677,9 @@ public class ViewSubredditDetailActivity extends BaseActivity implements SortTyp
}
if (showBottomAppBar) {
int optionCount = bottomAppBarSharedPreference.getInt(SharedPreferencesUtils.OTHER_ACTIVITIES_BOTTOM_APP_BAR_OPTION_COUNT, 4);
int option1 = bottomAppBarSharedPreference.getInt(SharedPreferencesUtils.OTHER_ACTIVITIES_BOTTOM_APP_BAR_OPTION_1, SharedPreferencesUtils.OTHER_ACTIVITIES_BOTTOM_APP_BAR_OPTION_HOME);
int option2 = bottomAppBarSharedPreference.getInt(SharedPreferencesUtils.OTHER_ACTIVITIES_BOTTOM_APP_BAR_OPTION_2, SharedPreferencesUtils.OTHER_ACTIVITIES_BOTTOM_APP_BAR_OPTION_SUBSCRIPTIONS);
int optionCount = mBottomAppBarSharedPreference.getInt(SharedPreferencesUtils.OTHER_ACTIVITIES_BOTTOM_APP_BAR_OPTION_COUNT, 4);
int option1 = mBottomAppBarSharedPreference.getInt(SharedPreferencesUtils.OTHER_ACTIVITIES_BOTTOM_APP_BAR_OPTION_1, SharedPreferencesUtils.OTHER_ACTIVITIES_BOTTOM_APP_BAR_OPTION_HOME);
int option2 = mBottomAppBarSharedPreference.getInt(SharedPreferencesUtils.OTHER_ACTIVITIES_BOTTOM_APP_BAR_OPTION_2, SharedPreferencesUtils.OTHER_ACTIVITIES_BOTTOM_APP_BAR_OPTION_SUBSCRIPTIONS);
bottomNavigationView.setVisibility(View.VISIBLE);
@ -695,8 +699,8 @@ public class ViewSubredditDetailActivity extends BaseActivity implements SortTyp
bottomAppBarOptionAction(option2);
});
} else {
int option3 = bottomAppBarSharedPreference.getInt(SharedPreferencesUtils.OTHER_ACTIVITIES_BOTTOM_APP_BAR_OPTION_3, SharedPreferencesUtils.OTHER_ACTIVITIES_BOTTOM_APP_BAR_OPTION_INBOX);
int option4 = bottomAppBarSharedPreference.getInt(SharedPreferencesUtils.OTHER_ACTIVITIES_BOTTOM_APP_BAR_OPTION_4, SharedPreferencesUtils.OTHER_ACTIVITIES_BOTTOM_APP_BAR_OPTION_PROFILE);
int option3 = mBottomAppBarSharedPreference.getInt(SharedPreferencesUtils.OTHER_ACTIVITIES_BOTTOM_APP_BAR_OPTION_3, SharedPreferencesUtils.OTHER_ACTIVITIES_BOTTOM_APP_BAR_OPTION_INBOX);
int option4 = mBottomAppBarSharedPreference.getInt(SharedPreferencesUtils.OTHER_ACTIVITIES_BOTTOM_APP_BAR_OPTION_4, SharedPreferencesUtils.OTHER_ACTIVITIES_BOTTOM_APP_BAR_OPTION_PROFILE);
option1BottomAppBar.setImageResource(getBottomAppBarOptionDrawableResource(option1));
option2BottomAppBar.setImageResource(getBottomAppBarOptionDrawableResource(option2));
@ -732,7 +736,7 @@ public class ViewSubredditDetailActivity extends BaseActivity implements SortTyp
fab.setLayoutParams(lp);
bottomNavigationView.setVisibility(View.GONE);
}
fabOption = bottomAppBarSharedPreference.getInt(SharedPreferencesUtils.OTHER_ACTIVITIES_BOTTOM_APP_BAR_FAB, SharedPreferencesUtils.OTHER_ACTIVITIES_BOTTOM_APP_BAR_FAB_SUBMIT_POSTS);
fabOption = mBottomAppBarSharedPreference.getInt(SharedPreferencesUtils.OTHER_ACTIVITIES_BOTTOM_APP_BAR_FAB, SharedPreferencesUtils.OTHER_ACTIVITIES_BOTTOM_APP_BAR_FAB_SUBMIT_POSTS);
switch (fabOption) {
case SharedPreferencesUtils.OTHER_ACTIVITIES_BOTTOM_APP_BAR_FAB_REFRESH:
fab.setImageResource(R.drawable.ic_refresh_24dp);

View File

@ -77,7 +77,7 @@ import ml.docilealligator.infinityforreddit.SortTypeSelectionCallback;
import ml.docilealligator.infinityforreddit.asynctasks.AddSubredditOrUserToMultiReddit;
import ml.docilealligator.infinityforreddit.asynctasks.CheckIsFollowingUserAsyncTask;
import ml.docilealligator.infinityforreddit.asynctasks.GetCurrentAccount;
import ml.docilealligator.infinityforreddit.asynctasks.SwitchAccountAsyncTask;
import ml.docilealligator.infinityforreddit.asynctasks.SwitchAccount;
import ml.docilealligator.infinityforreddit.bottomsheetfragments.FABMoreOptionsBottomSheetFragment;
import ml.docilealligator.infinityforreddit.bottomsheetfragments.PostLayoutBottomSheetFragment;
import ml.docilealligator.infinityforreddit.bottomsheetfragments.PostTypeBottomSheetFragment;
@ -191,7 +191,10 @@ public class ViewUserDetailActivity extends BaseActivity implements SortTypeSele
SharedPreferences mNsfwAndSpoilerSharedPreferences;
@Inject
@Named("bottom_app_bar")
SharedPreferences bottomAppBarSharedPreference;
SharedPreferences mBottomAppBarSharedPreference;
@Inject
@Named("current_account")
SharedPreferences mCurrentAccountSharedPreferences;
@Inject
CustomThemeWrapper mCustomThemeWrapper;
@Inject
@ -563,8 +566,9 @@ public class ViewUserDetailActivity extends BaseActivity implements SortTypeSele
private void getCurrentAccountAndInitializeViewPager() {
GetCurrentAccount.getCurrentAccount(mExecutor, new Handler(), mRedditDataRoomDatabase, account -> {
if (mNewAccountName != null) {
if (account == null || !account.getUsername().equals(mNewAccountName)) {
new SwitchAccountAsyncTask(mRedditDataRoomDatabase, mNewAccountName, newAccount -> {
if (account == null || !account.getAccountName().equals(mNewAccountName)) {
SwitchAccount.switchAccount(mRedditDataRoomDatabase, mCurrentAccountSharedPreferences,
mExecutor, new Handler(), mNewAccountName, newAccount -> {
EventBus.getDefault().post(new SwitchAccountEvent(getClass().getName()));
Toast.makeText(this, R.string.account_switched, Toast.LENGTH_SHORT).show();
@ -573,14 +577,14 @@ public class ViewUserDetailActivity extends BaseActivity implements SortTypeSele
mNullAccessToken = true;
} else {
mAccessToken = newAccount.getAccessToken();
mAccountName = newAccount.getUsername();
mAccountName = newAccount.getAccountName();
}
initializeViewPager();
}).execute();
});
} else {
mAccessToken = account.getAccessToken();
mAccountName = account.getUsername();
mAccountName = account.getAccountName();
initializeViewPager();
}
} else {
@ -588,7 +592,7 @@ public class ViewUserDetailActivity extends BaseActivity implements SortTypeSele
mNullAccessToken = true;
} else {
mAccessToken = account.getAccessToken();
mAccountName = account.getUsername();
mAccountName = account.getAccountName();
}
initializeViewPager();
@ -650,9 +654,9 @@ public class ViewUserDetailActivity extends BaseActivity implements SortTypeSele
}
if (showBottomAppBar) {
int optionCount = bottomAppBarSharedPreference.getInt(SharedPreferencesUtils.OTHER_ACTIVITIES_BOTTOM_APP_BAR_OPTION_COUNT, 4);
int option1 = bottomAppBarSharedPreference.getInt(SharedPreferencesUtils.OTHER_ACTIVITIES_BOTTOM_APP_BAR_OPTION_1, SharedPreferencesUtils.OTHER_ACTIVITIES_BOTTOM_APP_BAR_OPTION_HOME);
int option2 = bottomAppBarSharedPreference.getInt(SharedPreferencesUtils.OTHER_ACTIVITIES_BOTTOM_APP_BAR_OPTION_2, SharedPreferencesUtils.OTHER_ACTIVITIES_BOTTOM_APP_BAR_OPTION_SUBSCRIPTIONS);
int optionCount = mBottomAppBarSharedPreference.getInt(SharedPreferencesUtils.OTHER_ACTIVITIES_BOTTOM_APP_BAR_OPTION_COUNT, 4);
int option1 = mBottomAppBarSharedPreference.getInt(SharedPreferencesUtils.OTHER_ACTIVITIES_BOTTOM_APP_BAR_OPTION_1, SharedPreferencesUtils.OTHER_ACTIVITIES_BOTTOM_APP_BAR_OPTION_HOME);
int option2 = mBottomAppBarSharedPreference.getInt(SharedPreferencesUtils.OTHER_ACTIVITIES_BOTTOM_APP_BAR_OPTION_2, SharedPreferencesUtils.OTHER_ACTIVITIES_BOTTOM_APP_BAR_OPTION_SUBSCRIPTIONS);
bottomNavigationView.setVisibility(View.VISIBLE);
@ -672,8 +676,8 @@ public class ViewUserDetailActivity extends BaseActivity implements SortTypeSele
bottomAppBarOptionAction(option2);
});
} else {
int option3 = bottomAppBarSharedPreference.getInt(SharedPreferencesUtils.OTHER_ACTIVITIES_BOTTOM_APP_BAR_OPTION_3, SharedPreferencesUtils.OTHER_ACTIVITIES_BOTTOM_APP_BAR_OPTION_INBOX);
int option4 = bottomAppBarSharedPreference.getInt(SharedPreferencesUtils.OTHER_ACTIVITIES_BOTTOM_APP_BAR_OPTION_4, SharedPreferencesUtils.OTHER_ACTIVITIES_BOTTOM_APP_BAR_OPTION_PROFILE);
int option3 = mBottomAppBarSharedPreference.getInt(SharedPreferencesUtils.OTHER_ACTIVITIES_BOTTOM_APP_BAR_OPTION_3, SharedPreferencesUtils.OTHER_ACTIVITIES_BOTTOM_APP_BAR_OPTION_INBOX);
int option4 = mBottomAppBarSharedPreference.getInt(SharedPreferencesUtils.OTHER_ACTIVITIES_BOTTOM_APP_BAR_OPTION_4, SharedPreferencesUtils.OTHER_ACTIVITIES_BOTTOM_APP_BAR_OPTION_PROFILE);
option1BottomAppBar.setImageResource(getBottomAppBarOptionDrawableResource(option1));
option2BottomAppBar.setImageResource(getBottomAppBarOptionDrawableResource(option2));
@ -709,7 +713,7 @@ public class ViewUserDetailActivity extends BaseActivity implements SortTypeSele
fab.setLayoutParams(lp);
bottomNavigationView.setVisibility(View.GONE);
}
fabOption = bottomAppBarSharedPreference.getInt(SharedPreferencesUtils.OTHER_ACTIVITIES_BOTTOM_APP_BAR_FAB, SharedPreferencesUtils.OTHER_ACTIVITIES_BOTTOM_APP_BAR_FAB_SUBMIT_POSTS);
fabOption = mBottomAppBarSharedPreference.getInt(SharedPreferencesUtils.OTHER_ACTIVITIES_BOTTOM_APP_BAR_FAB, SharedPreferencesUtils.OTHER_ACTIVITIES_BOTTOM_APP_BAR_FAB_SUBMIT_POSTS);
switch (fabOption) {
case SharedPreferencesUtils.OTHER_ACTIVITIES_BOTTOM_APP_BAR_FAB_REFRESH:
fab.setImageResource(R.drawable.ic_refresh_24dp);

View File

@ -450,9 +450,9 @@ public class NavigationDrawerRecyclerViewAdapter extends RecyclerView.Adapter<Re
.error(glide.load(R.drawable.subreddit_default_icon))
.apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(128, 0)))
.into(((AccountViewHolder) holder).profileImageGifImageView);
((AccountViewHolder) holder).usernameTextView.setText(accounts.get(position - 1).getUsername());
((AccountViewHolder) holder).usernameTextView.setText(accounts.get(position - 1).getAccountName());
((AccountViewHolder) holder).itemView.setOnClickListener(view ->
itemClickListener.onAccountClick(accounts.get(position - 1).getUsername()));
itemClickListener.onAccountClick(accounts.get(position - 1).getAccountName()));
}
}

View File

@ -0,0 +1,32 @@
package ml.docilealligator.infinityforreddit.asynctasks;
import android.content.SharedPreferences;
import android.os.Handler;
import java.util.concurrent.Executor;
import ml.docilealligator.infinityforreddit.account.Account;
import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase;
import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils;
public class SwitchAccount {
public static void switchAccount(RedditDataRoomDatabase redditDataRoomDatabase,
SharedPreferences currentAccountSharedPreferences, Executor executor,
Handler handler, String newAccountName,
SwitchAccountListener switchAccountListener) {
executor.execute(() -> {
redditDataRoomDatabase.accountDao().markAllAccountsNonCurrent();
redditDataRoomDatabase.accountDao().markAccountCurrent(newAccountName);
Account account = redditDataRoomDatabase.accountDao().getCurrentAccount();
currentAccountSharedPreferences.edit()
.putString(SharedPreferencesUtils.ACCESS_TOKEN, account.getAccessToken())
.putString(SharedPreferencesUtils.ACCOUNT_NAME, account.getAccountName()).apply();
handler.post(() -> switchAccountListener.switched(account));
});
}
public interface SwitchAccountListener {
void switched(Account account);
}
}

View File

@ -1,36 +0,0 @@
package ml.docilealligator.infinityforreddit.asynctasks;
import android.os.AsyncTask;
import ml.docilealligator.infinityforreddit.account.Account;
import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase;
public class SwitchAccountAsyncTask extends AsyncTask<Void, Void, Void> {
private RedditDataRoomDatabase redditDataRoomDatabase;
private String newAccountName;
private Account account;
private SwitchAccountAsyncTaskListener switchAccountAsyncTaskListener;
public SwitchAccountAsyncTask(RedditDataRoomDatabase redditDataRoomDatabase, String newAccountName,
SwitchAccountAsyncTaskListener switchAccountAsyncTaskListener) {
this.redditDataRoomDatabase = redditDataRoomDatabase;
this.newAccountName = newAccountName;
this.switchAccountAsyncTaskListener = switchAccountAsyncTaskListener;
}
@Override
protected Void doInBackground(Void... voids) {
redditDataRoomDatabase.accountDao().markAllAccountsNonCurrent();
redditDataRoomDatabase.accountDao().markAccountCurrent(newAccountName);
account = redditDataRoomDatabase.accountDao().getCurrentAccount();
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
switchAccountAsyncTaskListener.switched(account);
}
public interface SwitchAccountAsyncTaskListener {
void switched(Account account);
}
}

View File

@ -1,37 +0,0 @@
package ml.docilealligator.infinityforreddit.asynctasks;
import android.os.AsyncTask;
import ml.docilealligator.infinityforreddit.account.AccountDao;
import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase;
public class SwitchToAnonymousAccountAsyncTask extends AsyncTask<Void, Void, Void> {
private RedditDataRoomDatabase redditDataRoomDatabase;
private boolean removeCurrentAccount;
private SwitchToAnonymousAccountAsyncTaskListener switchToAnonymousAccountAsyncTaskListener;
public SwitchToAnonymousAccountAsyncTask(RedditDataRoomDatabase redditDataRoomDatabase, boolean removeCurrentAccount,
SwitchToAnonymousAccountAsyncTaskListener switchToAnonymousAccountAsyncTaskListener) {
this.redditDataRoomDatabase = redditDataRoomDatabase;
this.removeCurrentAccount = removeCurrentAccount;
this.switchToAnonymousAccountAsyncTaskListener = switchToAnonymousAccountAsyncTaskListener;
}
@Override
protected Void doInBackground(Void... voids) {
AccountDao accountDao = redditDataRoomDatabase.accountDao();
if (removeCurrentAccount) {
accountDao.deleteCurrentAccount();
}
accountDao.markAllAccountsNonCurrent();
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
switchToAnonymousAccountAsyncTaskListener.logoutSuccess();
}
public interface SwitchToAnonymousAccountAsyncTaskListener {
void logoutSuccess();
}
}

View File

@ -0,0 +1,32 @@
package ml.docilealligator.infinityforreddit.asynctasks;
import android.content.SharedPreferences;
import android.os.Handler;
import java.util.concurrent.Executor;
import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase;
import ml.docilealligator.infinityforreddit.account.AccountDao;
import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils;
public class SwitchToAnonymousMode {
public static void switchToAnonymousMode(RedditDataRoomDatabase redditDataRoomDatabase, SharedPreferences currentAccountSharedPreferences,
Executor executor, Handler handler, boolean removeCurrentAccount,
SwitchToAnonymousAccountAsyncTaskListener switchToAnonymousAccountAsyncTaskListener) {
executor.execute(() -> {
AccountDao accountDao = redditDataRoomDatabase.accountDao();
if (removeCurrentAccount) {
accountDao.deleteCurrentAccount();
}
accountDao.markAllAccountsNonCurrent();
currentAccountSharedPreferences.edit().remove(SharedPreferencesUtils.ACCESS_TOKEN).remove(SharedPreferencesUtils.ACCOUNT_NAME).apply();
handler.post(switchToAnonymousAccountAsyncTaskListener::logoutSuccess);
});
}
public interface SwitchToAnonymousAccountAsyncTaskListener {
void logoutSuccess();
}
}

View File

@ -256,6 +256,10 @@ public class SharedPreferencesUtils {
public static final String MARK_POSTS_AS_READ_ON_SCROLL_BASE = "_mark_posts_as_read_on_scroll";
public static final String HIDE_READ_POSTS_AUTOMATICALLY_BASE = "_hide_read_posts_automatically";
public static final String CURRENT_ACCOUNT_SHARED_PREFERENCES_FILE = "ml.docilealligator.infinityforreddit.current_account";
public static final String ACCOUNT_NAME = "account_name";
public static final String ACCESS_TOKEN = "access_token";
//Legacy Settings
public static final String MAIN_PAGE_TAB_1_TITLE_LEGACY = "main_page_tab_1_title";
public static final String MAIN_PAGE_TAB_2_TITLE_LEGACY = "main_page_tab_2_title";