Mark message as read when clicking the message in ViewMessageActivity.

This commit is contained in:
Alex Ning 2019-08-18 23:51:27 +08:00
parent ffd1d6e204
commit a83aaa671a
7 changed files with 99 additions and 26 deletions

View File

@ -18,6 +18,7 @@ import androidx.recyclerview.widget.RecyclerView;
import CustomView.CustomMarkwonView;
import butterknife.BindView;
import butterknife.ButterKnife;
import retrofit2.Retrofit;
class MessageRecyclerViewAdapter extends PagedListAdapter<Message, RecyclerView.ViewHolder> {
private static final int VIEW_TYPE_DATA = 0;
@ -25,7 +26,9 @@ class MessageRecyclerViewAdapter extends PagedListAdapter<Message, RecyclerView.
private static final int VIEW_TYPE_LOADING = 2;
private Context mContext;
private Resources resources;
private Retrofit mOauthRetrofit;
private String mAccessToken;
private Resources mResources;
private NetworkState networkState;
private CommentsListingRecyclerViewAdapter.RetryLoadingMoreCallback mRetryLoadingMoreCallback;
@ -34,10 +37,12 @@ class MessageRecyclerViewAdapter extends PagedListAdapter<Message, RecyclerView.
void retryLoadingMore();
}
MessageRecyclerViewAdapter(Context context) {
MessageRecyclerViewAdapter(Context context, Retrofit oauthRetrofit, String accessToken) {
super(DIFF_CALLBACK);
mContext = context;
resources = context.getResources();
mOauthRetrofit = oauthRetrofit;
mAccessToken = accessToken;
mResources = context.getResources();
}
private static final DiffUtil.ItemCallback<Message> DIFF_CALLBACK = new DiffUtil.ItemCallback<Message>() {
@ -71,11 +76,11 @@ class MessageRecyclerViewAdapter extends PagedListAdapter<Message, RecyclerView.
if(message != null) {
if(message.isNew()) {
((DataViewHolder) holder).itemView.setBackgroundColor(
resources.getColor(R.color.unreadMessageBackgroundColor));
mResources.getColor(R.color.unreadMessageBackgroundColor));
}
if(message.wasComment()) {
((DataViewHolder) holder).authorTextView.setTextColor(resources.getColor(R.color.colorPrimaryDarkDayNightTheme));
((DataViewHolder) holder).authorTextView.setTextColor(mResources.getColor(R.color.colorPrimaryDarkDayNightTheme));
((DataViewHolder) holder).titleTextView.setText(message.getTitle());
} else {
((DataViewHolder) holder).titleTextView.setVisibility(View.GONE);
@ -93,6 +98,21 @@ class MessageRecyclerViewAdapter extends PagedListAdapter<Message, RecyclerView.
intent.setData(uri);
mContext.startActivity(intent);
}
if(message.isNew()) {
((DataViewHolder) holder).itemView.setBackgroundColor(mResources.getColor(android.R.color.white));
ReadMessage.readMessage(mOauthRetrofit, mAccessToken, message.getFullname(),
new ReadMessage.ReadMessageListener() {
@Override
public void readSuccess() {}
@Override
public void readFailed() {
((DataViewHolder) holder).itemView.setBackgroundColor(mResources.getColor(R.color.unreadMessageBackgroundColor));
}
});
}
});
((DataViewHolder) holder).authorTextView.setOnClickListener(view -> {
@ -134,9 +154,9 @@ class MessageRecyclerViewAdapter extends PagedListAdapter<Message, RecyclerView.
public void onViewRecycled(@NonNull RecyclerView.ViewHolder holder) {
super.onViewRecycled(holder);
if(holder instanceof DataViewHolder) {
((DataViewHolder) holder).itemView.setBackgroundColor(resources.getColor(android.R.color.white));
((DataViewHolder) holder).itemView.setBackgroundColor(mResources.getColor(android.R.color.white));
((DataViewHolder) holder).titleTextView.setVisibility(View.VISIBLE);
((DataViewHolder) holder).authorTextView.setTextColor(resources.getColor(R.color.primaryTextColor));
((DataViewHolder) holder).authorTextView.setTextColor(mResources.getColor(R.color.primaryTextColor));
}
}

View File

@ -14,12 +14,7 @@ class NotificationUtils {
static final String GROUP_NEW_COMMENTS = "ml.docilealligator.infinityforreddit.NEW_COMMENTS";
static final int SUMMARY_ID_NEW_COMMENTS = 0;
static final int BASE_ID_COMMENT = 1;
static final int BASE_ID_ACCOUNT = 1000;
static final int BASE_ID_POST = 2000;
static final int BASE_ID_MESSAGE = 3000;
static final int BASE_ID_SUBREDDIT = 4000;
static final int BASE_ID_AWARD = 5000;
static final int BASE_ID_UNREAD_MESSAGE = 1;
static NotificationCompat.Builder buildNotification(NotificationManagerCompat notificationManager,
Context context, String title, String content,

View File

@ -75,15 +75,13 @@ public class PullNotificationWorker extends Worker {
String kind = message.getKind();
String title;
String summary;
if(kind.equals(Message.TYPE_COMMENT)) {
if(kind.equals(Message.TYPE_COMMENT) || kind.equals(Message.TYPE_LINK)) {
title = message.getAuthor();
summary = context.getString(R.string.notification_summary_comment);
} else {
title = message.getTitle();
if(kind.equals(Message.TYPE_ACCOUNT)) {
summary = context.getString(R.string.notification_summary_account);
} else if(kind.equals(Message.TYPE_LINK)) {
summary = context.getString(R.string.notification_summary_post);
} else if(kind.equals(Message.TYPE_MESSAGE)) {
summary = context.getString(R.string.notification_summary_message);
} else if(kind.equals(Message.TYPE_SUBREDDIT)) {
@ -104,18 +102,30 @@ public class PullNotificationWorker extends Worker {
intent.setData(uri);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
builder.setContentIntent(pendingIntent);
notificationManager.notify(NotificationUtils.BASE_ID_COMMENT + i, builder.build());
} else if(kind.equals(Message.TYPE_ACCOUNT)) {
notificationManager.notify(NotificationUtils.BASE_ID_ACCOUNT + i, builder.build());
Intent intent = new Intent(context, ViewMessageActivity.class);
PendingIntent summaryPendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
builder.setContentIntent(summaryPendingIntent);
} else if(kind.equals(Message.TYPE_LINK)) {
notificationManager.notify(NotificationUtils.BASE_ID_POST + i, builder.build());
Intent intent = new Intent(context, LinkResolverActivity.class);
Uri uri = LinkResolverActivity.getRedditUriByPath(message.getContext());
intent.setData(uri);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
builder.setContentIntent(pendingIntent);
} else if(kind.equals(Message.TYPE_MESSAGE)) {
notificationManager.notify(NotificationUtils.BASE_ID_MESSAGE + i, builder.build());
Intent intent = new Intent(context, ViewMessageActivity.class);
PendingIntent summaryPendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
builder.setContentIntent(summaryPendingIntent);
} else if(kind.equals(Message.TYPE_SUBREDDIT)) {
notificationManager.notify(NotificationUtils.BASE_ID_SUBREDDIT + i, builder.build());
Intent intent = new Intent(context, ViewMessageActivity.class);
PendingIntent summaryPendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
builder.setContentIntent(summaryPendingIntent);
} else {
notificationManager.notify(NotificationUtils.BASE_ID_AWARD + i, builder.build());
Intent intent = new Intent(context, ViewMessageActivity.class);
PendingIntent summaryPendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
builder.setContentIntent(summaryPendingIntent);
}
notificationManager.notify(NotificationUtils.BASE_ID_UNREAD_MESSAGE + i, builder.build());
}
inboxStyle.setBigContentTitle(messages.size() + " New Messages")
@ -123,6 +133,10 @@ public class PullNotificationWorker extends Worker {
summaryBuilder.setStyle(inboxStyle);
Intent summaryIntent = new Intent(context, ViewMessageActivity.class);
PendingIntent summaryPendingIntent = PendingIntent.getActivity(context, 0, summaryIntent, 0);
summaryBuilder.setContentIntent(summaryPendingIntent);
notificationManager.notify(NotificationUtils.SUMMARY_ID_NEW_COMMENTS, summaryBuilder.build());
Log.i("workmanager", "message size " + messages.size());

View File

@ -0,0 +1,40 @@
package ml.docilealligator.infinityforreddit;
import androidx.annotation.NonNull;
import java.util.HashMap;
import java.util.Map;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
class ReadMessage {
interface ReadMessageListener {
void readSuccess();
void readFailed();
}
static void readMessage(Retrofit oauthRetrofit, String accessToken, String commaSeparatedFullnames,
ReadMessageListener readMessageListener) {
Map<String, String> params = new HashMap<>();
params.put(RedditUtils.ID_KEY, commaSeparatedFullnames);
oauthRetrofit.create(RedditAPI.class).readMessage(RedditUtils.getOAuthHeader(accessToken), params)
.enqueue(new Callback<String>() {
@Override
public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) {
if(response.isSuccessful()) {
readMessageListener.readSuccess();
} else {
readMessageListener.readFailed();
}
}
@Override
public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) {
readMessageListener.readFailed();
}
});
}
}

View File

@ -159,4 +159,8 @@ public interface RedditAPI {
@GET("/message/{where}.json?raw_json=1")
Call<String> getMessages(@HeaderMap Map<String, String> headers, @Path("where") String where, @Query("after") String after);
@FormUrlEncoded
@POST("/api/read_message")
Call<String> readMessage(@HeaderMap Map<String, String> headers, @FieldMap Map<String, String> ids);
}

View File

@ -156,7 +156,7 @@ public class ViewMessageActivity extends AppCompatActivity {
}
private void bindView() {
mAdapter = new MessageRecyclerViewAdapter(this);
mAdapter = new MessageRecyclerViewAdapter(this, mOauthRetrofit, mAccessToken);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(mAdapter);

View File

@ -224,10 +224,10 @@
<string name="view_all_comments">Click here to browse all comments</string>
<string name="notification_summary_comment">New Comments</string>
<string name="notification_summary_comment">New Comment</string>
<string name="notification_summary_account">Account</string>
<string name="notification_summary_post">Post</string>
<string name="notification_summary_message">New Messages</string>
<string name="notification_summary_post">New Post Comment</string>
<string name="notification_summary_message">New Message</string>
<string name="notification_summary_subreddit">Subreddit</string>
<string name="notification_summary_award">Award</string>
<string name="notification_new_messages">%1$d New Messages</string>