diff options
Diffstat (limited to 'app/src/main/java/ml')
4 files changed, 76 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); |