From 53d2cd14c31d64e73fb03a83dc8c56738d90f161 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Thu, 21 Jan 2021 19:07:48 +0800 Subject: Save current account info to SharedPreferences. --- .../AccessTokenAuthenticator.java | 4 +- .../infinityforreddit/AppModule.java | 6 ++ .../infinityforreddit/PullNotificationWorker.java | 6 +- .../infinityforreddit/account/Account.java | 11 +-- .../activities/AccountPostsActivity.java | 2 +- .../activities/AccountSavedThingActivity.java | 2 +- .../activities/CreateMultiRedditActivity.java | 2 +- .../activities/EditMultiRedditActivity.java | 2 +- .../activities/FilteredPostsActivity.java | 2 +- .../activities/InboxActivity.java | 12 ++- .../activities/LoginActivity.java | 5 ++ .../infinityforreddit/activities/MainActivity.java | 94 ++++++++++++---------- .../activities/MultiredditSelectionActivity.java | 2 +- .../activities/PostImageActivity.java | 2 +- .../activities/PostVideoActivity.java | 2 +- .../activities/SearchActivity.java | 2 +- .../activities/SearchResultActivity.java | 2 +- .../activities/SearchSubredditsResultActivity.java | 2 +- .../activities/SearchUsersResultActivity.java | 2 +- .../activities/SelectUserFlairActivity.java | 2 +- .../activities/SettingsActivity.java | 2 +- .../SubredditMultiselectionActivity.java | 2 +- .../activities/SubredditSelectionActivity.java | 2 +- .../activities/SubscribedThingListingActivity.java | 2 +- .../activities/ViewMultiRedditDetailActivity.java | 2 +- .../activities/ViewPostDetailActivity.java | 18 +++-- .../activities/ViewPrivateMessagesActivity.java | 2 +- .../activities/ViewSubredditDetailActivity.java | 32 ++++---- .../activities/ViewUserDetailActivity.java | 32 ++++---- .../NavigationDrawerRecyclerViewAdapter.java | 4 +- .../asynctasks/SwitchAccount.java | 32 ++++++++ .../asynctasks/SwitchAccountAsyncTask.java | 36 --------- .../SwitchToAnonymousAccountAsyncTask.java | 37 --------- .../asynctasks/SwitchToAnonymousMode.java | 32 ++++++++ .../utils/SharedPreferencesUtils.java | 4 + 35 files changed, 216 insertions(+), 187 deletions(-) create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/asynctasks/SwitchAccount.java delete mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/asynctasks/SwitchAccountAsyncTask.java delete mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/asynctasks/SwitchToAnonymousAccountAsyncTask.java create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/asynctasks/SwitchToAnonymousMode.java (limited to 'app') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/AccessTokenAuthenticator.java b/app/src/main/java/ml/docilealligator/infinityforreddit/AccessTokenAuthenticator.java index 529f5ad2..8221b612 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/AccessTokenAuthenticator.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/AccessTokenAuthenticator.java @@ -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; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/AppModule.java b/app/src/main/java/ml/docilealligator/infinityforreddit/AppModule.java index 64a552cf..83cc3515 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/AppModule.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/AppModule.java @@ -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, diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PullNotificationWorker.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PullNotificationWorker.java index 773c96c1..a0017f1c 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PullNotificationWorker.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PullNotificationWorker.java @@ -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 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; } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/account/Account.java b/app/src/main/java/ml/docilealligator/infinityforreddit/account/Account.java index 200faaab..6232c94d 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/account/Account.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/account/Account.java @@ -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() { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/AccountPostsActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/AccountPostsActivity.java index 9dc34c59..2f82cf36 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/AccountPostsActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/AccountPostsActivity.java @@ -191,7 +191,7 @@ public class AccountPostsActivity extends BaseActivity implements SortTypeSelect mNullAccessToken = true; } else { mAccessToken = account.getAccessToken(); - mAccountName = account.getUsername(); + mAccountName = account.getAccountName(); } initializeFragment(); }); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/AccountSavedThingActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/AccountSavedThingActivity.java index e32133cd..d59ecc65 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/AccountSavedThingActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/AccountSavedThingActivity.java @@ -197,7 +197,7 @@ public class AccountSavedThingActivity extends BaseActivity implements ActivityT mNullAccessToken = true; } else { mAccessToken = account.getAccessToken(); - mAccountName = account.getUsername(); + mAccountName = account.getAccountName(); } initializeViewPager(); }); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/CreateMultiRedditActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/CreateMultiRedditActivity.java index 6077f232..39a56416 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/CreateMultiRedditActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/CreateMultiRedditActivity.java @@ -136,7 +136,7 @@ public class CreateMultiRedditActivity extends BaseActivity { finish(); } else { mAccessToken = account.getAccessToken(); - mAccountName = account.getUsername(); + mAccountName = account.getAccountName(); bindView(); } }); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/EditMultiRedditActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/EditMultiRedditActivity.java index 5ae37fb3..e7bb81ee 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/EditMultiRedditActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/EditMultiRedditActivity.java @@ -146,7 +146,7 @@ public class EditMultiRedditActivity extends BaseActivity { finish(); } else { mAccessToken = account.getAccessToken(); - mAccountName = account.getUsername(); + mAccountName = account.getAccountName(); bindView(); } }); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/FilteredPostsActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/FilteredPostsActivity.java index c7d07187..9441dd35 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/FilteredPostsActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/FilteredPostsActivity.java @@ -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); }); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/InboxActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/InboxActivity.java index 1b8366a5..951e92bc 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/InboxActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/InboxActivity.java @@ -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); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/LoginActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/LoginActivity.java index 50866ce4..a55567e8 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/LoginActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/LoginActivity.java @@ -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(), () -> { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/MainActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/MainActivity.java index 3045b6a4..2040a566 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/MainActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/MainActivity.java @@ -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(); - - mNewAccountName = null; - if (newAccount == null) { - mNullAccessToken = true; - } else { - mAccessToken = newAccount.getAccessToken(); - mAccountName = newAccount.getUsername(); - 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 (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.getAccountName(); + mProfileImageUrl = newAccount.getProfileImageUrl(); + mBannerImageUrl = newAccount.getBannerImageUrl(); + mKarma = newAccount.getKarma(); + } + + 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)); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/MultiredditSelectionActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/MultiredditSelectionActivity.java index adc68363..36b7b25d 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/MultiredditSelectionActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/MultiredditSelectionActivity.java @@ -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); }); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostImageActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostImageActivity.java index 6c100d8a..b26685c4 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostImageActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostImageActivity.java @@ -416,7 +416,7 @@ public class PostImageActivity extends BaseActivity implements FlairBottomSheetF mNullAccessToken = true; } else { mAccessToken = account.getAccessToken(); - mAccountName = account.getUsername(); + mAccountName = account.getAccountName(); } }); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostVideoActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostVideoActivity.java index 17c82ea6..88fe606c 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostVideoActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostVideoActivity.java @@ -421,7 +421,7 @@ public class PostVideoActivity extends BaseActivity implements FlairBottomSheetF mNullAccessToken = true; } else { mAccessToken = account.getAccessToken(); - mAccountName = account.getUsername(); + mAccountName = account.getAccountName(); } }); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SearchActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SearchActivity.java index 72d92738..19bba9cd 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SearchActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SearchActivity.java @@ -224,7 +224,7 @@ public class SearchActivity extends BaseActivity { if (account == null) { mNullAccountName = true; } else { - mAccountName = account.getUsername(); + mAccountName = account.getAccountName(); } bindView(); }); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SearchResultActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SearchResultActivity.java index f75e3f5f..7d052796 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SearchResultActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SearchResultActivity.java @@ -233,7 +233,7 @@ public class SearchResultActivity extends BaseActivity implements SortTypeSelect mNullAccessToken = true; } else { mAccessToken = account.getAccessToken(); - mAccountName = account.getUsername(); + mAccountName = account.getAccountName(); } bindView(); }); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SearchSubredditsResultActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SearchSubredditsResultActivity.java index 9caccdae..55de7e87 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SearchSubredditsResultActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SearchSubredditsResultActivity.java @@ -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(); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SearchUsersResultActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SearchUsersResultActivity.java index a1aea4bd..3fa7ab4f 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SearchUsersResultActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SearchUsersResultActivity.java @@ -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(); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SelectUserFlairActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SelectUserFlairActivity.java index ea44b77c..793029b8 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SelectUserFlairActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SelectUserFlairActivity.java @@ -129,7 +129,7 @@ public class SelectUserFlairActivity extends BaseActivity implements ActivityToo mNullAccessToken = true; } else { mAccessToken = account.getAccessToken(); - mAccountName = account.getUsername(); + mAccountName = account.getAccountName(); } bindView(); }); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SettingsActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SettingsActivity.java index 584abffa..3c1d8c3f 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SettingsActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SettingsActivity.java @@ -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) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SubredditMultiselectionActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SubredditMultiselectionActivity.java index 74888f2e..24b2b47e 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SubredditMultiselectionActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SubredditMultiselectionActivity.java @@ -161,7 +161,7 @@ public class SubredditMultiselectionActivity extends BaseActivity implements Act finish(); } else { mAccessToken = account.getAccessToken(); - mAccountName = account.getUsername(); + mAccountName = account.getAccountName(); bindView(); } }); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SubredditSelectionActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SubredditSelectionActivity.java index 5f9efb2d..3aa99783 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SubredditSelectionActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SubredditSelectionActivity.java @@ -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); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SubscribedThingListingActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SubscribedThingListingActivity.java index d48f0010..18f4c590 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SubscribedThingListingActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SubscribedThingListingActivity.java @@ -194,7 +194,7 @@ public class SubscribedThingListingActivity extends BaseActivity implements Acti mNullAccessToken = true; } else { mAccessToken = account.getAccessToken(); - mAccountName = account.getUsername(); + mAccountName = account.getAccountName(); } initializeViewPagerAndLoadSubscriptions(); }); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewMultiRedditDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewMultiRedditDetailActivity.java index 52651f87..93521a70 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewMultiRedditDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewMultiRedditDetailActivity.java @@ -194,7 +194,7 @@ public class ViewMultiRedditDetailActivity extends BaseActivity implements SortT mNullAccessToken = true; } else { mAccessToken = account.getAccessToken(); - mAccountName = account.getUsername(); + mAccountName = account.getAccountName(); } initializeFragment(); }); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewPostDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewPostDetailActivity.java index c7bf94b4..af881091 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewPostDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewPostDetailActivity.java @@ -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(); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewPrivateMessagesActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewPrivateMessagesActivity.java index 1c411e3c..81d97949 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewPrivateMessagesActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewPrivateMessagesActivity.java @@ -148,7 +148,7 @@ public class ViewPrivateMessagesActivity extends BaseActivity implements Activit mNullAccessToken = true; } else { mAccessToken = account.getAccessToken(); - mAccountName = account.getUsername(); + mAccountName = account.getAccountName(); } bindView(); }); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewSubredditDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewSubredditDetailActivity.java index d0fc6475..2189c732 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewSubredditDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewSubredditDetailActivity.java @@ -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); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewUserDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewUserDetailActivity.java index ee88a095..1574b2e2 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewUserDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewUserDetailActivity.java @@ -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); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/NavigationDrawerRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/NavigationDrawerRecyclerViewAdapter.java index c29cfb36..b7c527be 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/NavigationDrawerRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/NavigationDrawerRecyclerViewAdapter.java @@ -450,9 +450,9 @@ public class NavigationDrawerRecyclerViewAdapter extends RecyclerView.Adapter - itemClickListener.onAccountClick(accounts.get(position - 1).getUsername())); + itemClickListener.onAccountClick(accounts.get(position - 1).getAccountName())); } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/asynctasks/SwitchAccount.java b/app/src/main/java/ml/docilealligator/infinityforreddit/asynctasks/SwitchAccount.java new file mode 100644 index 00000000..94afd539 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/asynctasks/SwitchAccount.java @@ -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); + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/asynctasks/SwitchAccountAsyncTask.java b/app/src/main/java/ml/docilealligator/infinityforreddit/asynctasks/SwitchAccountAsyncTask.java deleted file mode 100644 index 227e6082..00000000 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/asynctasks/SwitchAccountAsyncTask.java +++ /dev/null @@ -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 { - 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); - } -} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/asynctasks/SwitchToAnonymousAccountAsyncTask.java b/app/src/main/java/ml/docilealligator/infinityforreddit/asynctasks/SwitchToAnonymousAccountAsyncTask.java deleted file mode 100644 index c0e0da14..00000000 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/asynctasks/SwitchToAnonymousAccountAsyncTask.java +++ /dev/null @@ -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 { - 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(); - } -} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/asynctasks/SwitchToAnonymousMode.java b/app/src/main/java/ml/docilealligator/infinityforreddit/asynctasks/SwitchToAnonymousMode.java new file mode 100644 index 00000000..ad1e7534 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/asynctasks/SwitchToAnonymousMode.java @@ -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(); + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/utils/SharedPreferencesUtils.java b/app/src/main/java/ml/docilealligator/infinityforreddit/utils/SharedPreferencesUtils.java index b0e2d901..ecfb2ba3 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/utils/SharedPreferencesUtils.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/utils/SharedPreferencesUtils.java @@ -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"; -- cgit v1.2.3