Skip to content

Commit 9ba4016

Browse files
Logging out by long clicking an account. Rename SwitchAccount to AccountManagement. Move SwitchToAnonymousMode's methods to AccountManagement.
1 parent a37eefa commit 9ba4016

File tree

10 files changed

+70
-53
lines changed

10 files changed

+70
-53
lines changed

app/src/main/java/ml/docilealligator/infinityforreddit/account/AccountDao.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ public interface AccountDao {
3131
@Query("DELETE FROM accounts WHERE is_current_user = 1 AND username != '-'")
3232
void deleteCurrentAccount();
3333

34+
@Query("DELETE FROM accounts WHERE username = :accountName")
35+
void deleteAccount(String accountName);
36+
3437
@Query("DELETE FROM accounts WHERE username != '-'")
3538
void deleteAllAccounts();
3639

app/src/main/java/ml/docilealligator/infinityforreddit/activities/InboxActivity.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase;
4242
import ml.docilealligator.infinityforreddit.account.Account;
4343
import ml.docilealligator.infinityforreddit.apis.RedditAPI;
44-
import ml.docilealligator.infinityforreddit.asynctasks.SwitchAccount;
44+
import ml.docilealligator.infinityforreddit.asynctasks.AccountManagement;
4545
import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper;
4646
import ml.docilealligator.infinityforreddit.customviews.slidr.Slidr;
4747
import ml.docilealligator.infinityforreddit.databinding.ActivityInboxBinding;
@@ -215,7 +215,7 @@ protected void applyCustomTheme() {
215215
private void getCurrentAccountAndFetchMessage(Bundle savedInstanceState) {
216216
if (mNewAccountName != null) {
217217
if (accountName.equals(Account.ANONYMOUS_ACCOUNT) || !accountName.equals(mNewAccountName)) {
218-
SwitchAccount.switchAccount(mRedditDataRoomDatabase, mCurrentAccountSharedPreferences,
218+
AccountManagement.switchAccount(mRedditDataRoomDatabase, mCurrentAccountSharedPreferences,
219219
mExecutor, new Handler(), mNewAccountName, newAccount -> {
220220
EventBus.getDefault().post(new SwitchAccountEvent(getClass().getName()));
221221
Toast.makeText(this, R.string.account_switched, Toast.LENGTH_SHORT).show();

app/src/main/java/ml/docilealligator/infinityforreddit/activities/MainActivity.java

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,7 @@
8080
import ml.docilealligator.infinityforreddit.adapters.navigationdrawer.NavigationDrawerRecyclerViewMergedAdapter;
8181
import ml.docilealligator.infinityforreddit.apis.RedditAPI;
8282
import ml.docilealligator.infinityforreddit.asynctasks.InsertSubscribedThings;
83-
import ml.docilealligator.infinityforreddit.asynctasks.SwitchAccount;
84-
import ml.docilealligator.infinityforreddit.asynctasks.SwitchToAnonymousMode;
83+
import ml.docilealligator.infinityforreddit.asynctasks.AccountManagement;
8584
import ml.docilealligator.infinityforreddit.bottomsheetfragments.FABMoreOptionsBottomSheetFragment;
8685
import ml.docilealligator.infinityforreddit.bottomsheetfragments.PostLayoutBottomSheetFragment;
8786
import ml.docilealligator.infinityforreddit.bottomsheetfragments.PostTypeBottomSheetFragment;
@@ -360,7 +359,7 @@ private void initializeNotificationAndBindView() {
360359

361360
if (mNewAccountName != null) {
362361
if (accountName.equals(Account.ANONYMOUS_ACCOUNT) || !accountName.equals(mNewAccountName)) {
363-
SwitchAccount.switchAccount(mRedditDataRoomDatabase, mCurrentAccountSharedPreferences,
362+
AccountManagement.switchAccount(mRedditDataRoomDatabase, mCurrentAccountSharedPreferences,
364363
mExecutor, new Handler(), mNewAccountName, newAccount -> {
365364
EventBus.getDefault().post(new SwitchAccountEvent(getClass().getName()));
366365
Toast.makeText(this, R.string.account_switched, Toast.LENGTH_SHORT).show();
@@ -798,14 +797,14 @@ public void onMenuClick(int stringId) {
798797
} else if (stringId == R.string.add_account) {
799798
intent = new Intent(MainActivity.this, LoginActivity.class);
800799
} else if (stringId == R.string.anonymous_account) {
801-
SwitchToAnonymousMode.switchToAnonymousMode(mRedditDataRoomDatabase, mCurrentAccountSharedPreferences,
800+
AccountManagement.switchToAnonymousMode(mRedditDataRoomDatabase, mCurrentAccountSharedPreferences,
802801
mExecutor, new Handler(), false, () -> {
803802
Intent anonymousIntent = new Intent(MainActivity.this, MainActivity.class);
804803
startActivity(anonymousIntent);
805804
finish();
806805
});
807806
} else if (stringId == R.string.log_out) {
808-
SwitchToAnonymousMode.switchToAnonymousMode(mRedditDataRoomDatabase, mCurrentAccountSharedPreferences,
807+
AccountManagement.switchToAnonymousMode(mRedditDataRoomDatabase, mCurrentAccountSharedPreferences,
809808
mExecutor, new Handler(), true,
810809
() -> {
811810
Intent logOutIntent = new Intent(MainActivity.this, MainActivity.class);
@@ -828,14 +827,25 @@ public void onSubscribedSubredditClick(String subredditName) {
828827

829828
@Override
830829
public void onAccountClick(@NonNull String accountName) {
831-
SwitchAccount.switchAccount(mRedditDataRoomDatabase, mCurrentAccountSharedPreferences,
830+
AccountManagement.switchAccount(mRedditDataRoomDatabase, mCurrentAccountSharedPreferences,
832831
mExecutor, new Handler(), accountName, newAccount -> {
833832
Intent intent = new Intent(MainActivity.this, MainActivity.class);
834833
startActivity(intent);
835834
finish();
836835
});
837836
}
838-
});
837+
838+
@Override
839+
public void onAccountLongClick(@NonNull String accountName) {
840+
new MaterialAlertDialogBuilder(MainActivity.this, R.style.MaterialAlertDialogTheme)
841+
.setTitle(R.string.log_out)
842+
.setMessage(accountName)
843+
.setPositiveButton(R.string.yes,
844+
(dialogInterface, i) -> AccountManagement.removeAccount(mRedditDataRoomDatabase, mExecutor, accountName))
845+
.setNegativeButton(R.string.no, null)
846+
.show();
847+
}
848+
});
839849
adapter.setInboxCount(inboxCount);
840850
binding.navDrawerRecyclerViewMainActivity.setLayoutManager(new LinearLayoutManagerBugFixed(this));
841851
binding.navDrawerRecyclerViewMainActivity.setAdapter(adapter.getConcatAdapter());

app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewPostDetailActivity.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
import ml.docilealligator.infinityforreddit.SortTypeSelectionCallback;
6060
import ml.docilealligator.infinityforreddit.account.Account;
6161
import ml.docilealligator.infinityforreddit.apis.RedditAPI;
62-
import ml.docilealligator.infinityforreddit.asynctasks.SwitchAccount;
62+
import ml.docilealligator.infinityforreddit.asynctasks.AccountManagement;
6363
import ml.docilealligator.infinityforreddit.comment.Comment;
6464
import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper;
6565
import ml.docilealligator.infinityforreddit.customviews.slidr.Slidr;
@@ -339,7 +339,7 @@ protected void applyCustomTheme() {
339339
private void checkNewAccountAndBindView(Bundle savedInstanceState) {
340340
if (mNewAccountName != null) {
341341
if (accountName.equals(Account.ANONYMOUS_ACCOUNT) || !accountName.equals(mNewAccountName)) {
342-
SwitchAccount.switchAccount(mRedditDataRoomDatabase, mCurrentAccountSharedPreferences,
342+
AccountManagement.switchAccount(mRedditDataRoomDatabase, mCurrentAccountSharedPreferences,
343343
mExecutor, new Handler(), mNewAccountName, newAccount -> {
344344
EventBus.getDefault().post(new SwitchAccountEvent(getClass().getName()));
345345
Toast.makeText(this, R.string.account_switched, Toast.LENGTH_SHORT).show();

app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewSubredditDetailActivity.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@
7878
import ml.docilealligator.infinityforreddit.asynctasks.AddSubredditOrUserToMultiReddit;
7979
import ml.docilealligator.infinityforreddit.asynctasks.CheckIsSubscribedToSubreddit;
8080
import ml.docilealligator.infinityforreddit.asynctasks.InsertSubredditData;
81-
import ml.docilealligator.infinityforreddit.asynctasks.SwitchAccount;
81+
import ml.docilealligator.infinityforreddit.asynctasks.AccountManagement;
8282
import ml.docilealligator.infinityforreddit.bottomsheetfragments.CopyTextBottomSheetFragment;
8383
import ml.docilealligator.infinityforreddit.bottomsheetfragments.FABMoreOptionsBottomSheetFragment;
8484
import ml.docilealligator.infinityforreddit.bottomsheetfragments.PostLayoutBottomSheetFragment;
@@ -521,7 +521,7 @@ public void onGlobalLayout() {
521521
private void checkNewAccountAndBindView() {
522522
if (mNewAccountName != null) {
523523
if (accountName.equals(Account.ANONYMOUS_ACCOUNT) || !accountName.equals(mNewAccountName)) {
524-
SwitchAccount.switchAccount(mRedditDataRoomDatabase, mCurrentAccountSharedPreferences,
524+
AccountManagement.switchAccount(mRedditDataRoomDatabase, mCurrentAccountSharedPreferences,
525525
mExecutor, new Handler(), mNewAccountName, newAccount -> {
526526
EventBus.getDefault().post(new SwitchAccountEvent(getClass().getName()));
527527
Toast.makeText(this, R.string.account_switched, Toast.LENGTH_SHORT).show();

app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewUserDetailActivity.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@
7474
import ml.docilealligator.infinityforreddit.apis.RedditAPI;
7575
import ml.docilealligator.infinityforreddit.asynctasks.AddSubredditOrUserToMultiReddit;
7676
import ml.docilealligator.infinityforreddit.asynctasks.CheckIsFollowingUser;
77-
import ml.docilealligator.infinityforreddit.asynctasks.SwitchAccount;
77+
import ml.docilealligator.infinityforreddit.asynctasks.AccountManagement;
7878
import ml.docilealligator.infinityforreddit.bottomsheetfragments.CopyTextBottomSheetFragment;
7979
import ml.docilealligator.infinityforreddit.bottomsheetfragments.FABMoreOptionsBottomSheetFragment;
8080
import ml.docilealligator.infinityforreddit.bottomsheetfragments.KarmaInfoBottomSheetFragment;
@@ -606,7 +606,7 @@ public void onGlobalLayout() {
606606
private void checkNewAccountAndInitializeViewPager() {
607607
if (mNewAccountName != null) {
608608
if (accountName.equals(Account.ANONYMOUS_ACCOUNT) || !accountName.equals(mNewAccountName)) {
609-
SwitchAccount.switchAccount(mRedditDataRoomDatabase, mCurrentAccountSharedPreferences,
609+
AccountManagement.switchAccount(mRedditDataRoomDatabase, mCurrentAccountSharedPreferences,
610610
mExecutor, new Handler(), mNewAccountName, newAccount -> {
611611
EventBus.getDefault().post(new SwitchAccountEvent(getClass().getName()));
612612
Toast.makeText(this, R.string.account_switched, Toast.LENGTH_SHORT).show();

app/src/main/java/ml/docilealligator/infinityforreddit/adapters/navigationdrawer/AccountManagementSectionRecyclerViewAdapter.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,6 @@ public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int positi
7272
.apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(128, 0)))
7373
.into(((AccountViewHolder) holder).binding.profileImageItemAccount);
7474
((AccountViewHolder) holder).binding.usernameTextViewItemAccount.setText(accounts.get(position).getAccountName());
75-
holder.itemView.setOnClickListener(view ->
76-
itemClickListener.onAccountClick(accounts.get(position).getAccountName()));
7775
} else if (holder instanceof MenuItemViewHolder) {
7876
int stringId = 0;
7977
int drawableId = 0;
@@ -136,6 +134,14 @@ class AccountViewHolder extends RecyclerView.ViewHolder {
136134
binding.usernameTextViewItemAccount.setTypeface(baseActivity.typeface);
137135
}
138136
binding.usernameTextViewItemAccount.setTextColor(primaryTextColor);
137+
138+
itemView.setOnClickListener(view ->
139+
itemClickListener.onAccountClick(accounts.get(getBindingAdapterPosition()).getAccountName()));
140+
141+
itemView.setOnLongClickListener(view -> {
142+
itemClickListener.onAccountLongClick(accounts.get(getBindingAdapterPosition()).getAccountName());
143+
return true;
144+
});
139145
}
140146
}
141147

app/src/main/java/ml/docilealligator/infinityforreddit/adapters/navigationdrawer/NavigationDrawerRecyclerViewMergedAdapter.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,5 +144,6 @@ public interface ItemClickListener {
144144
void onMenuClick(int stringId);
145145
void onSubscribedSubredditClick(String subredditName);
146146
void onAccountClick(@NonNull String accountName);
147+
void onAccountLongClick(@NonNull String accountName);
147148
}
148149
}

app/src/main/java/ml/docilealligator/infinityforreddit/asynctasks/SwitchAccount.java renamed to app/src/main/java/ml/docilealligator/infinityforreddit/asynctasks/AccountManagement.java

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77

88
import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase;
99
import ml.docilealligator.infinityforreddit.account.Account;
10+
import ml.docilealligator.infinityforreddit.account.AccountDao;
1011
import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils;
1112

12-
public class SwitchAccount {
13+
public class AccountManagement {
14+
1315
public static void switchAccount(RedditDataRoomDatabase redditDataRoomDatabase,
1416
SharedPreferences currentAccountSharedPreferences, Executor executor,
1517
Handler handler, String newAccountName,
@@ -28,6 +30,36 @@ public static void switchAccount(RedditDataRoomDatabase redditDataRoomDatabase,
2830

2931
}
3032

33+
public static void switchToAnonymousMode(RedditDataRoomDatabase redditDataRoomDatabase, SharedPreferences currentAccountSharedPreferences,
34+
Executor executor, Handler handler, boolean removeCurrentAccount,
35+
SwitchToAnonymousAccountAsyncTaskListener switchToAnonymousAccountAsyncTaskListener) {
36+
executor.execute(() -> {
37+
AccountDao accountDao = redditDataRoomDatabase.accountDao();
38+
if (removeCurrentAccount) {
39+
accountDao.deleteCurrentAccount();
40+
}
41+
accountDao.markAllAccountsNonCurrent();
42+
43+
String redgifsAccessToken = currentAccountSharedPreferences.getString(SharedPreferencesUtils.REDGIFS_ACCESS_TOKEN, "");
44+
45+
currentAccountSharedPreferences.edit().clear().apply();
46+
currentAccountSharedPreferences.edit().putString(SharedPreferencesUtils.REDGIFS_ACCESS_TOKEN, redgifsAccessToken).apply();
47+
48+
handler.post(switchToAnonymousAccountAsyncTaskListener::logoutSuccess);
49+
});
50+
}
51+
52+
public static void removeAccount(RedditDataRoomDatabase redditDataRoomDatabase,
53+
Executor executor, String accoutName) {
54+
executor.execute(() -> {
55+
redditDataRoomDatabase.accountDao().deleteAccount(accoutName);
56+
});
57+
}
58+
59+
public interface SwitchToAnonymousAccountAsyncTaskListener {
60+
void logoutSuccess();
61+
}
62+
3163
public interface SwitchAccountListener {
3264
void switched(Account account);
3365
}

app/src/main/java/ml/docilealligator/infinityforreddit/asynctasks/SwitchToAnonymousMode.java

Lines changed: 0 additions & 35 deletions
This file was deleted.

0 commit comments

Comments
 (0)