diff options
10 files changed, 70 insertions, 53 deletions
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/account/AccountDao.java b/app/src/main/java/ml/docilealligator/infinityforreddit/account/AccountDao.java index e29c6623..7648c1f1 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/account/AccountDao.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/account/AccountDao.java @@ -31,6 +31,9 @@ public interface AccountDao { @Query("DELETE FROM accounts WHERE is_current_user = 1 AND username != '-'") void deleteCurrentAccount(); + @Query("DELETE FROM accounts WHERE username = :accountName") + void deleteAccount(String accountName); + @Query("DELETE FROM accounts WHERE username != '-'") void deleteAllAccounts(); 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 b0288907..30879333 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/InboxActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/InboxActivity.java @@ -41,7 +41,7 @@ import ml.docilealligator.infinityforreddit.RecyclerViewContentScrollingInterfac import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; import ml.docilealligator.infinityforreddit.account.Account; import ml.docilealligator.infinityforreddit.apis.RedditAPI; -import ml.docilealligator.infinityforreddit.asynctasks.SwitchAccount; +import ml.docilealligator.infinityforreddit.asynctasks.AccountManagement; import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.customviews.slidr.Slidr; import ml.docilealligator.infinityforreddit.databinding.ActivityInboxBinding; @@ -215,7 +215,7 @@ public class InboxActivity extends BaseActivity implements ActivityToolbarInterf private void getCurrentAccountAndFetchMessage(Bundle savedInstanceState) { if (mNewAccountName != null) { if (accountName.equals(Account.ANONYMOUS_ACCOUNT) || !accountName.equals(mNewAccountName)) { - SwitchAccount.switchAccount(mRedditDataRoomDatabase, mCurrentAccountSharedPreferences, + AccountManagement.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(); 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 2665c6b7..feece4f1 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/MainActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/MainActivity.java @@ -80,8 +80,7 @@ import ml.docilealligator.infinityforreddit.adapters.SubredditAutocompleteRecycl import ml.docilealligator.infinityforreddit.adapters.navigationdrawer.NavigationDrawerRecyclerViewMergedAdapter; import ml.docilealligator.infinityforreddit.apis.RedditAPI; import ml.docilealligator.infinityforreddit.asynctasks.InsertSubscribedThings; -import ml.docilealligator.infinityforreddit.asynctasks.SwitchAccount; -import ml.docilealligator.infinityforreddit.asynctasks.SwitchToAnonymousMode; +import ml.docilealligator.infinityforreddit.asynctasks.AccountManagement; import ml.docilealligator.infinityforreddit.bottomsheetfragments.FABMoreOptionsBottomSheetFragment; import ml.docilealligator.infinityforreddit.bottomsheetfragments.PostLayoutBottomSheetFragment; import ml.docilealligator.infinityforreddit.bottomsheetfragments.PostTypeBottomSheetFragment; @@ -360,7 +359,7 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb if (mNewAccountName != null) { if (accountName.equals(Account.ANONYMOUS_ACCOUNT) || !accountName.equals(mNewAccountName)) { - SwitchAccount.switchAccount(mRedditDataRoomDatabase, mCurrentAccountSharedPreferences, + AccountManagement.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(); @@ -798,14 +797,14 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb } else if (stringId == R.string.add_account) { intent = new Intent(MainActivity.this, LoginActivity.class); } else if (stringId == R.string.anonymous_account) { - SwitchToAnonymousMode.switchToAnonymousMode(mRedditDataRoomDatabase, mCurrentAccountSharedPreferences, + AccountManagement.switchToAnonymousMode(mRedditDataRoomDatabase, mCurrentAccountSharedPreferences, mExecutor, new Handler(), false, () -> { Intent anonymousIntent = new Intent(MainActivity.this, MainActivity.class); startActivity(anonymousIntent); finish(); }); } else if (stringId == R.string.log_out) { - SwitchToAnonymousMode.switchToAnonymousMode(mRedditDataRoomDatabase, mCurrentAccountSharedPreferences, + AccountManagement.switchToAnonymousMode(mRedditDataRoomDatabase, mCurrentAccountSharedPreferences, mExecutor, new Handler(), true, () -> { Intent logOutIntent = new Intent(MainActivity.this, MainActivity.class); @@ -828,14 +827,25 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb @Override public void onAccountClick(@NonNull String accountName) { - SwitchAccount.switchAccount(mRedditDataRoomDatabase, mCurrentAccountSharedPreferences, + AccountManagement.switchAccount(mRedditDataRoomDatabase, mCurrentAccountSharedPreferences, mExecutor, new Handler(), accountName, newAccount -> { Intent intent = new Intent(MainActivity.this, MainActivity.class); startActivity(intent); finish(); }); } - }); + + @Override + public void onAccountLongClick(@NonNull String accountName) { + new MaterialAlertDialogBuilder(MainActivity.this, R.style.MaterialAlertDialogTheme) + .setTitle(R.string.log_out) + .setMessage(accountName) + .setPositiveButton(R.string.yes, + (dialogInterface, i) -> AccountManagement.removeAccount(mRedditDataRoomDatabase, mExecutor, accountName)) + .setNegativeButton(R.string.no, null) + .show(); + } + }); adapter.setInboxCount(inboxCount); binding.navDrawerRecyclerViewMainActivity.setLayoutManager(new LinearLayoutManagerBugFixed(this)); binding.navDrawerRecyclerViewMainActivity.setAdapter(adapter.getConcatAdapter()); 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 5a84d0ce..40aaafe7 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewPostDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewPostDetailActivity.java @@ -59,7 +59,7 @@ import ml.docilealligator.infinityforreddit.SortType; import ml.docilealligator.infinityforreddit.SortTypeSelectionCallback; import ml.docilealligator.infinityforreddit.account.Account; import ml.docilealligator.infinityforreddit.apis.RedditAPI; -import ml.docilealligator.infinityforreddit.asynctasks.SwitchAccount; +import ml.docilealligator.infinityforreddit.asynctasks.AccountManagement; import ml.docilealligator.infinityforreddit.comment.Comment; import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.customviews.slidr.Slidr; @@ -339,7 +339,7 @@ public class ViewPostDetailActivity extends BaseActivity implements SortTypeSele private void checkNewAccountAndBindView(Bundle savedInstanceState) { if (mNewAccountName != null) { if (accountName.equals(Account.ANONYMOUS_ACCOUNT) || !accountName.equals(mNewAccountName)) { - SwitchAccount.switchAccount(mRedditDataRoomDatabase, mCurrentAccountSharedPreferences, + AccountManagement.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(); 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 f466a036..9e8de3df 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewSubredditDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewSubredditDetailActivity.java @@ -78,7 +78,7 @@ import ml.docilealligator.infinityforreddit.apis.RedditAPI; import ml.docilealligator.infinityforreddit.asynctasks.AddSubredditOrUserToMultiReddit; import ml.docilealligator.infinityforreddit.asynctasks.CheckIsSubscribedToSubreddit; import ml.docilealligator.infinityforreddit.asynctasks.InsertSubredditData; -import ml.docilealligator.infinityforreddit.asynctasks.SwitchAccount; +import ml.docilealligator.infinityforreddit.asynctasks.AccountManagement; import ml.docilealligator.infinityforreddit.bottomsheetfragments.CopyTextBottomSheetFragment; import ml.docilealligator.infinityforreddit.bottomsheetfragments.FABMoreOptionsBottomSheetFragment; import ml.docilealligator.infinityforreddit.bottomsheetfragments.PostLayoutBottomSheetFragment; @@ -521,7 +521,7 @@ public class ViewSubredditDetailActivity extends BaseActivity implements SortTyp private void checkNewAccountAndBindView() { if (mNewAccountName != null) { if (accountName.equals(Account.ANONYMOUS_ACCOUNT) || !accountName.equals(mNewAccountName)) { - SwitchAccount.switchAccount(mRedditDataRoomDatabase, mCurrentAccountSharedPreferences, + AccountManagement.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(); 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 3a90e082..a5905254 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewUserDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewUserDetailActivity.java @@ -74,7 +74,7 @@ import ml.docilealligator.infinityforreddit.adapters.SubredditAutocompleteRecycl import ml.docilealligator.infinityforreddit.apis.RedditAPI; import ml.docilealligator.infinityforreddit.asynctasks.AddSubredditOrUserToMultiReddit; import ml.docilealligator.infinityforreddit.asynctasks.CheckIsFollowingUser; -import ml.docilealligator.infinityforreddit.asynctasks.SwitchAccount; +import ml.docilealligator.infinityforreddit.asynctasks.AccountManagement; import ml.docilealligator.infinityforreddit.bottomsheetfragments.CopyTextBottomSheetFragment; import ml.docilealligator.infinityforreddit.bottomsheetfragments.FABMoreOptionsBottomSheetFragment; import ml.docilealligator.infinityforreddit.bottomsheetfragments.KarmaInfoBottomSheetFragment; @@ -606,7 +606,7 @@ public class ViewUserDetailActivity extends BaseActivity implements SortTypeSele private void checkNewAccountAndInitializeViewPager() { if (mNewAccountName != null) { if (accountName.equals(Account.ANONYMOUS_ACCOUNT) || !accountName.equals(mNewAccountName)) { - SwitchAccount.switchAccount(mRedditDataRoomDatabase, mCurrentAccountSharedPreferences, + AccountManagement.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(); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/navigationdrawer/AccountManagementSectionRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/navigationdrawer/AccountManagementSectionRecyclerViewAdapter.java index 98da3d64..a921b8cd 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/navigationdrawer/AccountManagementSectionRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/navigationdrawer/AccountManagementSectionRecyclerViewAdapter.java @@ -72,8 +72,6 @@ public class AccountManagementSectionRecyclerViewAdapter extends RecyclerView.Ad .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(128, 0))) .into(((AccountViewHolder) holder).binding.profileImageItemAccount); ((AccountViewHolder) holder).binding.usernameTextViewItemAccount.setText(accounts.get(position).getAccountName()); - holder.itemView.setOnClickListener(view -> - itemClickListener.onAccountClick(accounts.get(position).getAccountName())); } else if (holder instanceof MenuItemViewHolder) { int stringId = 0; int drawableId = 0; @@ -136,6 +134,14 @@ public class AccountManagementSectionRecyclerViewAdapter extends RecyclerView.Ad binding.usernameTextViewItemAccount.setTypeface(baseActivity.typeface); } binding.usernameTextViewItemAccount.setTextColor(primaryTextColor); + + itemView.setOnClickListener(view -> + itemClickListener.onAccountClick(accounts.get(getBindingAdapterPosition()).getAccountName())); + + itemView.setOnLongClickListener(view -> { + itemClickListener.onAccountLongClick(accounts.get(getBindingAdapterPosition()).getAccountName()); + return true; + }); } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/navigationdrawer/NavigationDrawerRecyclerViewMergedAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/navigationdrawer/NavigationDrawerRecyclerViewMergedAdapter.java index 6d738811..7fe52d54 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/navigationdrawer/NavigationDrawerRecyclerViewMergedAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/navigationdrawer/NavigationDrawerRecyclerViewMergedAdapter.java @@ -144,5 +144,6 @@ public class NavigationDrawerRecyclerViewMergedAdapter { void onMenuClick(int stringId); void onSubscribedSubredditClick(String subredditName); void onAccountClick(@NonNull String accountName); + void onAccountLongClick(@NonNull String accountName); } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/asynctasks/SwitchAccount.java b/app/src/main/java/ml/docilealligator/infinityforreddit/asynctasks/AccountManagement.java index 0ff5a9b5..85122c72 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/asynctasks/SwitchAccount.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/asynctasks/AccountManagement.java @@ -7,9 +7,11 @@ import java.util.concurrent.Executor; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; import ml.docilealligator.infinityforreddit.account.Account; +import ml.docilealligator.infinityforreddit.account.AccountDao; import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils; -public class SwitchAccount { +public class AccountManagement { + public static void switchAccount(RedditDataRoomDatabase redditDataRoomDatabase, SharedPreferences currentAccountSharedPreferences, Executor executor, Handler handler, String newAccountName, @@ -28,6 +30,36 @@ public class SwitchAccount { } + 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(); + + String redgifsAccessToken = currentAccountSharedPreferences.getString(SharedPreferencesUtils.REDGIFS_ACCESS_TOKEN, ""); + + currentAccountSharedPreferences.edit().clear().apply(); + currentAccountSharedPreferences.edit().putString(SharedPreferencesUtils.REDGIFS_ACCESS_TOKEN, redgifsAccessToken).apply(); + + handler.post(switchToAnonymousAccountAsyncTaskListener::logoutSuccess); + }); + } + + public static void removeAccount(RedditDataRoomDatabase redditDataRoomDatabase, + Executor executor, String accoutName) { + executor.execute(() -> { + redditDataRoomDatabase.accountDao().deleteAccount(accoutName); + }); + } + + public interface SwitchToAnonymousAccountAsyncTaskListener { + void logoutSuccess(); + } + public interface SwitchAccountListener { void switched(Account account); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/asynctasks/SwitchToAnonymousMode.java b/app/src/main/java/ml/docilealligator/infinityforreddit/asynctasks/SwitchToAnonymousMode.java deleted file mode 100644 index bc15245d..00000000 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/asynctasks/SwitchToAnonymousMode.java +++ /dev/null @@ -1,35 +0,0 @@ -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(); - - String redgifsAccessToken = currentAccountSharedPreferences.getString(SharedPreferencesUtils.REDGIFS_ACCESS_TOKEN, ""); - - currentAccountSharedPreferences.edit().clear().apply(); - currentAccountSharedPreferences.edit().putString(SharedPreferencesUtils.REDGIFS_ACCESS_TOKEN, redgifsAccessToken).apply(); - - handler.post(switchToAnonymousAccountAsyncTaskListener::logoutSuccess); - }); - } - - public interface SwitchToAnonymousAccountAsyncTaskListener { - void logoutSuccess(); - } -} |