diff options
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<String>() { + @Override + public void onResponse(@NonNull Call<String> call, @NonNull Response<String> 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<String> 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<Message, Recycl private int mUnreadMessageBackgroundColor; private int mColorPrimaryLightTheme; private int mButtonTextColor; + private boolean markAllMessagesAsRead = false; public MessageRecyclerViewAdapter(Context context, Retrofit oauthRetrofit, CustomThemeWrapper customThemeWrapper, @@ -216,8 +217,12 @@ public class MessageRecyclerViewAdapter extends PagedListAdapter<Message, Recycl displayedMessage = message; } if (message.isNew()) { - ((DataViewHolder) holder).itemView.setBackgroundColor( - mUnreadMessageBackgroundColor); + if (markAllMessagesAsRead) { + message.setNew(false); + } else { + ((DataViewHolder) holder).itemView.setBackgroundColor( + mUnreadMessageBackgroundColor); + } } if (message.wasComment()) { @@ -340,6 +345,10 @@ public class MessageRecyclerViewAdapter extends PagedListAdapter<Message, Recycl } } + public void setMarkAllMessagesAsRead(boolean markAllMessagesAsRead) { + this.markAllMessagesAsRead = markAllMessagesAsRead; + } + public interface RetryLoadingMoreCallback { void retryLoadingMore(); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/apis/RedditAPI.java b/app/src/main/java/ml/docilealligator/infinityforreddit/apis/RedditAPI.java index 3bee57fb..516d6e36 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/apis/RedditAPI.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/apis/RedditAPI.java @@ -336,4 +336,7 @@ public interface RedditAPI { @GET("/r/randnsfw/new.json?sort=new&t=all&limit=1&raw_json=1") Call<String> getRandomNSFWPost(); + + @POST("/api/read_all_messages") + Call<String> readAllMessages(@HeaderMap Map<String, String> 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" /> + + <item + android:id="@+id/action_read_all_messages_inbox_activity" + android:orderInCategory="2" + android:title="@string/action_read_all_messages" + app:showAsAction="never" /> </menu>
\ 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 @@ <string name="action_select_user_flair">Select User Flair</string> <string name="action_give_award">Give Award</string> <string name="action_save_to_database">Save to Database</string> + <string name="action_read_all_messages">Read All Messages</string> <string name="parse_json_response_error">Error occurred when parsing the JSON response</string> <string name="retrieve_token_error">Error Retrieving the token</string> @@ -992,5 +993,9 @@ <string name="subreddit">Subreddit</string> <string name="user">User</string> <string name="edit_post_filter_name_of_usage_info">Leave it blank to apply this post filter to all the subreddits / users / multireddits</string> + + <string name="read_all_messages_time_limit">You are doing this too frequently. Try again later. This is Reddit API\'s rate limit.</string> + <string name="read_all_messages_success">Read all messages successfully</string> + <string name="read_all_messages_failed">Unable to read all messages</string> </resources> |