diff options
author | Alex Ning <chineseperson5@gmail.com> | 2019-08-18 15:51:27 +0000 |
---|---|---|
committer | Alex Ning <chineseperson5@gmail.com> | 2019-08-18 15:51:27 +0000 |
commit | a83aaa671a1c930c4402345783839b0db66118b3 (patch) | |
tree | 0a11ce7d70f1ca0100620af9eb5af0f925fd7e29 /app/src/main/java/ml | |
parent | ffd1d6e204fc7032a13216fe0c6dbe78205b2a98 (diff) | |
download | infinity-for-reddit-a83aaa671a1c930c4402345783839b0db66118b3.tar infinity-for-reddit-a83aaa671a1c930c4402345783839b0db66118b3.tar.gz infinity-for-reddit-a83aaa671a1c930c4402345783839b0db66118b3.tar.bz2 infinity-for-reddit-a83aaa671a1c930c4402345783839b0db66118b3.tar.lz infinity-for-reddit-a83aaa671a1c930c4402345783839b0db66118b3.tar.xz infinity-for-reddit-a83aaa671a1c930c4402345783839b0db66118b3.tar.zst infinity-for-reddit-a83aaa671a1c930c4402345783839b0db66118b3.zip |
Mark message as read when clicking the message in ViewMessageActivity.
Diffstat (limited to 'app/src/main/java/ml')
6 files changed, 96 insertions, 23 deletions
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/MessageRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/MessageRecyclerViewAdapter.java index 3865123b..3807a413 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/MessageRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/MessageRecyclerViewAdapter.java @@ -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)); } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/NotificationUtils.java b/app/src/main/java/ml/docilealligator/infinityforreddit/NotificationUtils.java index 89c92a0c..56bfaf88 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/NotificationUtils.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/NotificationUtils.java @@ -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, diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PullNotificationWorker.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PullNotificationWorker.java index 57c13212..5bf7e6a3 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PullNotificationWorker.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PullNotificationWorker.java @@ -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()); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ReadMessage.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ReadMessage.java new file mode 100644 index 00000000..900fb7eb --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ReadMessage.java @@ -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(); + } + }); + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java b/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java index a187b521..519bc0ec 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java @@ -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); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewMessageActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewMessageActivity.java index b61438f4..3f6d5960 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewMessageActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewMessageActivity.java @@ -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); |