From 9e062126d824795821ff76c22edbe39f9dbcf5f8 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Mon, 4 Jan 2021 16:47:31 +0800 Subject: [PATCH] Reading all unread messages is now available. --- .../activities/InboxActivity.java | 41 +++++++++++++++++++ .../adapters/MessageRecyclerViewAdapter.java | 13 +++++- .../infinityforreddit/apis/RedditAPI.java | 3 ++ .../fragments/InboxFragment.java | 21 ++++++++++ app/src/main/res/menu/inbox_activity.xml | 6 +++ app/src/main/res/values/strings.xml | 5 +++ 6 files changed, 87 insertions(+), 2 deletions(-) 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 f47348cc..22d22c8c 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/InboxActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/InboxActivity.java @@ -41,13 +41,18 @@ import ml.docilealligator.infinityforreddit.ActivityToolbarInterface; import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.R; 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.customtheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.events.SwitchAccountEvent; import ml.docilealligator.infinityforreddit.fragments.InboxFragment; import ml.docilealligator.infinityforreddit.message.FetchMessage; +import ml.docilealligator.infinityforreddit.utils.APIUtils; import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils; +import retrofit2.Call; +import retrofit2.Callback; +import retrofit2.Response; import retrofit2.Retrofit; public class InboxActivity extends BaseActivity implements ActivityToolbarInterface { @@ -230,6 +235,33 @@ public class InboxActivity extends BaseActivity implements ActivityToolbarInterf sectionsPagerAdapter.refresh(); } return true; + } else if (item.getItemId() == R.id.action_read_all_messages_inbox_activity) { + if (mAccessToken != null) { + Toast.makeText(this, R.string.please_wait, Toast.LENGTH_SHORT).show(); + mOauthRetrofit.create(RedditAPI.class).readAllMessages(APIUtils.getOAuthHeader(mAccessToken)) + .enqueue(new Callback() { + @Override + public void onResponse(@NonNull Call call, @NonNull Response response) { + if (response.isSuccessful()) { + Toast.makeText(InboxActivity.this, R.string.read_all_messages_success, Toast.LENGTH_SHORT).show(); + if (sectionsPagerAdapter != null) { + sectionsPagerAdapter.readAllMessages(); + } + } else { + if (response.code() == 429) { + Toast.makeText(InboxActivity.this, R.string.read_all_messages_time_limit, Toast.LENGTH_LONG).show(); + } else { + Toast.makeText(InboxActivity.this, R.string.read_all_messages_failed, Toast.LENGTH_LONG).show(); + } + } + } + + @Override + public void onFailure(@NonNull Call call, @NonNull Throwable t) { + Toast.makeText(InboxActivity.this, R.string.read_all_messages_failed, Toast.LENGTH_LONG).show(); + } + }); + } } else if (item.getItemId() == android.R.id.home) { finish(); return true; @@ -354,5 +386,14 @@ public class InboxActivity extends BaseActivity implements ActivityToolbarInterf } } } + + void readAllMessages() { + if (tab1 != null) { + tab1.markAllMessagesRead(); + } + if (tab2 != null) { + tab2.markAllMessagesRead(); + } + } } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/MessageRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/MessageRecyclerViewAdapter.java index 504b3a51..e6da1344 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/MessageRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/MessageRecyclerViewAdapter.java @@ -78,6 +78,7 @@ public class MessageRecyclerViewAdapter extends PagedListAdapter getRandomNSFWPost(); + + @POST("/api/read_all_messages") + Call readAllMessages(@HeaderMap Map headers); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/InboxFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/InboxFragment.java index f23b6597..d97ad1ff 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/InboxFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/InboxFragment.java @@ -171,6 +171,27 @@ public class InboxFragment extends Fragment implements FragmentCommunicator { } } + public void markAllMessagesRead() { + if (mAdapter != null) { + mAdapter.setMarkAllMessagesAsRead(true); + + int previousPosition = -1; + if (mLinearLayoutManager != null) { + previousPosition = mLinearLayoutManager.findFirstVisibleItemPosition(); + } + + RecyclerView.LayoutManager layoutManager = mRecyclerView.getLayoutManager(); + mRecyclerView.setAdapter(null); + mRecyclerView.setLayoutManager(null); + mRecyclerView.setAdapter(mAdapter); + mRecyclerView.setLayoutManager(layoutManager); + + if (previousPosition > 0) { + mRecyclerView.scrollToPosition(previousPosition); + } + } + } + private void onRefresh() { mMessageViewModel.refresh(); mAdapter.setNetworkState(null); diff --git a/app/src/main/res/menu/inbox_activity.xml b/app/src/main/res/menu/inbox_activity.xml index f7d06665..c3eb4cc9 100644 --- a/app/src/main/res/menu/inbox_activity.xml +++ b/app/src/main/res/menu/inbox_activity.xml @@ -6,4 +6,10 @@ android:orderInCategory="1" android:title="@string/action_refresh" app:showAsAction="never" /> + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 8223e5f7..a5aa5353 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -75,6 +75,7 @@ Select User Flair Give Award Save to Database + Read All Messages Error occurred when parsing the JSON response Error Retrieving the token @@ -992,5 +993,9 @@ Subreddit User Leave it blank to apply this post filter to all the subreddits / users / multireddits + + You are doing this too frequently. Try again later. This is Reddit API\'s rate limit. + Read all messages successfully + Unable to read all messages