Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mark all inbox messages as read when leaving the inbox #1210

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,16 @@ public final class InboxListingActivity extends ViewsBaseActivity {

private static final int OPTIONS_MENU_MARK_ALL_AS_READ = 0;
private static final int OPTIONS_MENU_SHOW_UNREAD_ONLY = 1;
private static final int OPTIONS_MENU_MARK_INBOX_AS_READ_WHEN_BACK = 2;

public enum InboxType {
INBOX, SENT, MODMAIL
}

private static final String PREF_ONLY_UNREAD = "inbox_only_show_unread";

private static final String PREF_MARK_INBOX_READ_BACK = "inbox_mark_as_read_when_back";

private GroupedRecyclerViewAdapter adapter;

private LoadingView loadingView;
Expand All @@ -99,6 +102,10 @@ public enum InboxType {
private InboxType inboxType;
private boolean mOnlyShowUnread;

private boolean mMarkInboxAsReadWhenBack;

private boolean mUserWantsBack = false;

private RRThemeAttributes mTheme;
private RedditChangeDataManager mChangeDataManager;

Expand Down Expand Up @@ -186,6 +193,8 @@ public void onCreate(final Bundle savedInstanceState) {

mOnlyShowUnread = sharedPreferences.getBoolean(PREF_ONLY_UNREAD, false);

mMarkInboxAsReadWhenBack = sharedPreferences.getBoolean(PREF_MARK_INBOX_READ_BACK, false);

switch(inboxType) {
case SENT:
title = getString(R.string.mainmenu_sent_messages);
Expand Down Expand Up @@ -424,9 +433,46 @@ public void onFailure(@NonNull final RRError error) {
cm.makeRequest(request);
}

private void markInboxAsRead() {
RedditAPI.markAllAsRead(
CacheManager.getInstance(this),
new APIResponseHandler.ActionResponseHandler(this) {
@Override
protected void onSuccess() {
if (!mUserWantsBack) { //Don't annoy the user
General.quickToast(
context,
R.string.mark_all_as_read_success);
}
}

@Override
protected void onCallbackException(final Throwable t) {
BugReportActivity.addGlobalError(new RRError(
"Mark all as Read failed",
"Callback exception",
true,
t));
}

@Override
protected void onFailure(@NonNull final RRError error) {
if (!mUserWantsBack) { //Don't annoy the user
General.showResultDialog(
InboxListingActivity.this,
error);
}
}
},
RedditAccountManager.getInstance(this).getDefaultAccount(),
this);
}

@Override
public void onBackPressed() {
if(General.onBackPressed()) {
mUserWantsBack = true;
markInboxAsRead();
super.onBackPressed();
}
}
Expand All @@ -439,48 +485,40 @@ public boolean onCreateOptionsMenu(final Menu menu) {

menu.add(0, OPTIONS_MENU_MARK_ALL_AS_READ, 0, R.string.mark_all_as_read);
menu.add(0, OPTIONS_MENU_SHOW_UNREAD_ONLY, 1, R.string.inbox_unread_only);
menu.add(0, OPTIONS_MENU_MARK_INBOX_AS_READ_WHEN_BACK,
2, R.string.mark_inbox_as_read_when_back);
menu.getItem(1).setCheckable(true);
if(mOnlyShowUnread) {
menu.getItem(1).setChecked(true);
}
menu.getItem(2).setCheckable(true);
if(mMarkInboxAsReadWhenBack) {
menu.getItem(2).setChecked(true);
}
return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(final MenuItem item) {
switch(item.getItemId()) {
case OPTIONS_MENU_MARK_ALL_AS_READ:
RedditAPI.markAllAsRead(
CacheManager.getInstance(this),
new APIResponseHandler.ActionResponseHandler(this) {
@Override
protected void onSuccess() {
General.quickToast(
context,
R.string.mark_all_as_read_success);
}
markInboxAsRead();
return true;

@Override
protected void onCallbackException(final Throwable t) {
BugReportActivity.addGlobalError(new RRError(
"Mark all as Read failed",
"Callback exception",
true,
t));
}
case OPTIONS_MENU_MARK_INBOX_AS_READ_WHEN_BACK: {

@Override
protected void onFailure(@NonNull final RRError error) {
General.showResultDialog(
InboxListingActivity.this,
error);
}
},
RedditAccountManager.getInstance(this).getDefaultAccount(),
this);
final boolean enabled = !item.isChecked();

return true;
item.setChecked(enabled);
mMarkInboxAsReadWhenBack = enabled;

General.getSharedPrefs(this)
.edit()
.putBoolean(PREF_MARK_INBOX_READ_BACK, enabled)
.apply();

return true;
}
case OPTIONS_MENU_SHOW_UNREAD_ONLY: {

final boolean enabled = !item.isChecked();
Expand Down
3 changes: 3 additions & 0 deletions src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1894,4 +1894,7 @@

<string name="error_invalid_account_title">Invalid account</string>
<string name="error_invalid_account_message">Selected account is not currently logged in</string>

<!-- 2024-07-20 -->
<string name="mark_inbox_as_read_when_back">Read all when leaving</string>
</resources>