Reading all unread messages is now available.

This commit is contained in:
Alex Ning 2021-01-04 16:47:31 +08:00
parent f2d35eeb18
commit 9e062126d8
6 changed files with 87 additions and 2 deletions

View File

@ -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();
}
}
}
}

View File

@ -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,9 +217,13 @@ public class MessageRecyclerViewAdapter extends PagedListAdapter<Message, Recycl
displayedMessage = message;
}
if (message.isNew()) {
if (markAllMessagesAsRead) {
message.setNew(false);
} else {
((DataViewHolder) holder).itemView.setBackgroundColor(
mUnreadMessageBackgroundColor);
}
}
if (message.wasComment()) {
((DataViewHolder) holder).titleTextView.setText(message.getTitle());
@ -340,6 +345,10 @@ public class MessageRecyclerViewAdapter extends PagedListAdapter<Message, Recycl
}
}
public void setMarkAllMessagesAsRead(boolean markAllMessagesAsRead) {
this.markAllMessagesAsRead = markAllMessagesAsRead;
}
public interface RetryLoadingMoreCallback {
void retryLoadingMore();
}

View File

@ -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);
}

View File

@ -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);

View File

@ -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>

View File

@ -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>
@ -993,4 +994,8 @@
<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>