From eff385ccd61caea2e272e378d572ca53b67a1e67 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Wed, 1 Jul 2020 21:48:39 +0800 Subject: Read all the replies when replying a message. Update the replied message in InboxFragment. --- .../Activity/ViewPrivateMessagesActivity.java | 37 ++++++++++++++++++++-- .../Adapter/MessageRecyclerViewAdapter.java | 28 +++++++++++++--- .../PrivateMessagesDetailRecyclerViewAdapter.java | 5 --- .../Event/RepliedToPrivateMessageEvent.java | 13 ++++++++ .../infinityforreddit/Fragment/InboxFragment.java | 27 ++++++++++++++-- .../res/layout/activity_view_private_messages.xml | 1 + 6 files changed, 97 insertions(+), 14 deletions(-) create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/Event/RepliedToPrivateMessageEvent.java (limited to 'app/src') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewPrivateMessagesActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewPrivateMessagesActivity.java index 94bf8287..aecc78ab 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewPrivateMessagesActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Activity/ViewPrivateMessagesActivity.java @@ -23,6 +23,8 @@ import com.google.android.material.appbar.AppBarLayout; import com.google.android.material.snackbar.Snackbar; import com.r0adkll.slidr.Slidr; +import org.greenrobot.eventbus.EventBus; + import java.util.ArrayList; import javax.inject.Inject; @@ -35,9 +37,11 @@ import ml.docilealligator.infinityforreddit.Adapter.PrivateMessagesDetailRecycle import ml.docilealligator.infinityforreddit.AsyncTask.GetCurrentAccountAsyncTask; import ml.docilealligator.infinityforreddit.AsyncTask.LoadUserDataAsyncTask; import ml.docilealligator.infinityforreddit.CustomTheme.CustomThemeWrapper; +import ml.docilealligator.infinityforreddit.Event.RepliedToPrivateMessageEvent; import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.Message; import ml.docilealligator.infinityforreddit.R; +import ml.docilealligator.infinityforreddit.ReadMessage; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; import ml.docilealligator.infinityforreddit.ReplyMessage; import ml.docilealligator.infinityforreddit.Utils.SharedPreferencesUtils; @@ -46,6 +50,7 @@ import retrofit2.Retrofit; public class ViewPrivateMessagesActivity extends BaseActivity implements ActivityToolbarInterface { public static final String EXTRA_PRIVATE_MESSAGE = "EPM"; + public static final String EXTRA_MESSAGE_POSITION = "EMP"; private static final String NULL_ACCESS_TOKEN_STATE = "NATS"; private static final String ACCESS_TOKEN_STATE = "ATS"; private static final String ACCOUNT_NAME_STATE = "ANS"; @@ -66,6 +71,8 @@ public class ViewPrivateMessagesActivity extends BaseActivity implements Activit EditText mEditText; @BindView(R.id.send_image_view_view_private_messages_activity) ImageView mSendImageView; + @BindView(R.id.edit_text_wrapper_linear_layout_view_private_messages_activity) + LinearLayout mEditTextLinearLayout; @Inject @Named("oauth") Retrofit mOauthRetrofit; @@ -163,9 +170,9 @@ public class ViewPrivateMessagesActivity extends BaseActivity implements Activit Message replyTo; ArrayList replies = privateMessage.getReplies(); if (replies != null && !replies.isEmpty()) { - replyTo = privateMessage; - } else { replyTo = replies.get(replies.size() - 1); + } else { + replyTo = privateMessage; } if (replyTo != null) { ReplyMessage.replyMessage(mEditText.getText().toString(), replyTo.getFullname(), @@ -178,6 +185,7 @@ public class ViewPrivateMessagesActivity extends BaseActivity implements Activit } goToBottom(); mEditText.setText(""); + EventBus.getDefault().post(new RepliedToPrivateMessageEvent(message, getIntent().getIntExtra(EXTRA_MESSAGE_POSITION, -1))); } @Override @@ -189,6 +197,28 @@ public class ViewPrivateMessagesActivity extends BaseActivity implements Activit } } }); + StringBuilder fullnames = new StringBuilder(); + if (privateMessage.isNew()) { + fullnames.append(privateMessage.getFullname()).append(","); + } + if (replies != null && !replies.isEmpty()) { + for (Message m : replies) { + if (m.isNew()) { + fullnames.append(m).append(","); + } + } + } + if (fullnames.length() > 0) { + fullnames.deleteCharAt(fullnames.length() - 1); + ReadMessage.readMessage(mOauthRetrofit, mAccessToken, fullnames.toString(), + new ReadMessage.ReadMessageListener() { + @Override + public void readSuccess() {} + + @Override + public void readFailed() {} + }); + } } else { Snackbar.make(mCoordinatorLayout, R.string.error_getting_message, Snackbar.LENGTH_LONG).show(); } @@ -259,6 +289,9 @@ public class ViewPrivateMessagesActivity extends BaseActivity implements Activit mLinearLayout.setBackgroundColor(mCustomThemeWrapper.getBackgroundColor()); applyAppBarLayoutAndToolbarTheme(mAppBarLayout, mToolbar); mDivider.setBackgroundColor(mCustomThemeWrapper.getDividerColor()); + mEditText.setTextColor(mCustomThemeWrapper.getPrimaryTextColor()); + mEditText.setHintTextColor(mCustomThemeWrapper.getSecondaryTextColor()); + mEditTextLinearLayout.setBackgroundColor(mCustomThemeWrapper.getBackgroundColor()); mSendImageView.setColorFilter(Color.parseColor("#4185F4"), android.graphics.PorterDuff.Mode.SRC_IN); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/MessageRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/MessageRecyclerViewAdapter.java index d3f010da..bd9e5ed1 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/MessageRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/MessageRecyclerViewAdapter.java @@ -18,6 +18,8 @@ import androidx.paging.PagedListAdapter; import androidx.recyclerview.widget.DiffUtil; import androidx.recyclerview.widget.RecyclerView; +import java.util.ArrayList; + import butterknife.BindView; import butterknife.ButterKnife; import io.noties.markwon.AbstractMarkwonPlugin; @@ -137,6 +139,13 @@ public class MessageRecyclerViewAdapter extends PagedListAdapter replies = message.getReplies(); + Message displayedMessage; + if (replies != null && !replies.isEmpty() && replies.get(replies.size() - 1) != null) { + displayedMessage = replies.get(replies.size() - 1); + } else { + displayedMessage = message; + } if (message.isNew()) { ((DataViewHolder) holder).itemView.setBackgroundColor( mUnreadMessageBackgroundColor); @@ -148,10 +157,10 @@ public class MessageRecyclerViewAdapter extends PagedListAdapter { if (mMessageType == FetchMessages.MESSAGE_TYPE_NOTIFICATION @@ -163,10 +172,11 @@ public class MessageRecyclerViewAdapter extends PagedListAdapter= 0 && position < super.getItemCount()) { + Message message = getItem(position); + if (message != null) { + message.addReply(newReply); + notifyItemChanged(position); + } + } + } + public interface RetryLoadingMoreCallback { void retryLoadingMore(); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/PrivateMessagesDetailRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/PrivateMessagesDetailRecyclerViewAdapter.java index 4b17a966..04bc5423 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/PrivateMessagesDetailRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/PrivateMessagesDetailRecyclerViewAdapter.java @@ -112,10 +112,6 @@ public class PrivateMessagesDetailRecyclerViewAdapter extends RecyclerView.Adapt } if (message != null) { if (holder instanceof MessageViewHolder) { - if (message.isNew()) { - ((MessageViewHolder) holder).itemView.setBackgroundColor( - mUnreadMessageBackgroundColor); - } mMarkwon.setMarkdown(((MessageViewHolder) holder).messageTextView, message.getBody()); ((MessageViewHolder) holder).messageTextView.setOnClickListener(view -> ((MessageViewHolder) holder).itemView.performClick()); @@ -212,7 +208,6 @@ public class PrivateMessagesDetailRecyclerViewAdapter extends RecyclerView.Adapt this.messageTextView = messageTextView; this.timeTextView = timeTextView; - itemView.setBackgroundColor(mMessageBackgroundColor); messageTextView.setTextColor(Color.WHITE); timeTextView.setTextColor(mSecondaryTextColor); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Event/RepliedToPrivateMessageEvent.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Event/RepliedToPrivateMessageEvent.java new file mode 100644 index 00000000..4e89b4ec --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Event/RepliedToPrivateMessageEvent.java @@ -0,0 +1,13 @@ +package ml.docilealligator.infinityforreddit.Event; + +import ml.docilealligator.infinityforreddit.Message; + +public class RepliedToPrivateMessageEvent { + public Message newReply; + public int messagePosition; + + public RepliedToPrivateMessageEvent(Message newReply, int messagePosition) { + this.newReply = newReply; + this.messagePosition = messagePosition; + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/InboxFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/InboxFragment.java index f8b9ad31..1b7a12c9 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/InboxFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Fragment/InboxFragment.java @@ -21,6 +21,9 @@ import androidx.swiperefreshlayout.widget.SwipeRefreshLayout; import com.bumptech.glide.Glide; import com.bumptech.glide.RequestManager; +import org.greenrobot.eventbus.EventBus; +import org.greenrobot.eventbus.Subscribe; + import javax.inject.Inject; import javax.inject.Named; @@ -29,6 +32,8 @@ import butterknife.ButterKnife; import ml.docilealligator.infinityforreddit.Activity.BaseActivity; import ml.docilealligator.infinityforreddit.Adapter.MessageRecyclerViewAdapter; import ml.docilealligator.infinityforreddit.CustomTheme.CustomThemeWrapper; +import ml.docilealligator.infinityforreddit.Event.RepliedToPrivateMessageEvent; +import ml.docilealligator.infinityforreddit.FetchMessages; import ml.docilealligator.infinityforreddit.FragmentCommunicator; import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.MessageViewModel; @@ -63,6 +68,7 @@ public class InboxFragment extends Fragment implements FragmentCommunicator { @Inject CustomThemeWrapper mCustomThemeWrapper; private String mAccessToken; + private String mWhere; private MessageRecyclerViewAdapter mAdapter; private RequestManager mGlide; private LinearLayoutManager mLinearLayoutManager; @@ -81,6 +87,8 @@ public class InboxFragment extends Fragment implements FragmentCommunicator { ButterKnife.bind(this, rootView); + EventBus.getDefault().register(this); + applyTheme(); Bundle arguments = getArguments(); @@ -94,9 +102,9 @@ public class InboxFragment extends Fragment implements FragmentCommunicator { mRecyclerView.setPadding(0, 0, 0, mActivity.getNavBarHeight()); } - String where = arguments.getString(EXTRA_MESSAGE_WHERE); + mWhere = arguments.getString(EXTRA_MESSAGE_WHERE); mAdapter = new MessageRecyclerViewAdapter(mActivity, mOauthRetrofit, mCustomThemeWrapper, - mAccessToken, where, () -> mMessageViewModel.retryLoadingMore()); + mAccessToken, mWhere, () -> mMessageViewModel.retryLoadingMore()); mLinearLayoutManager = new LinearLayoutManager(mActivity); mRecyclerView.setLayoutManager(mLinearLayoutManager); mRecyclerView.setAdapter(mAdapter); @@ -104,7 +112,7 @@ public class InboxFragment extends Fragment implements FragmentCommunicator { mRecyclerView.addItemDecoration(dividerItemDecoration); MessageViewModel.Factory factory = new MessageViewModel.Factory(mOauthRetrofit, - getResources().getConfiguration().locale, mAccessToken, where); + getResources().getConfiguration().locale, mAccessToken, mWhere); mMessageViewModel = new ViewModelProvider(this, factory).get(MessageViewModel.class); mMessageViewModel.getMessages().observe(getViewLifecycleOwner(), messages -> mAdapter.submitList(messages)); @@ -168,9 +176,22 @@ public class InboxFragment extends Fragment implements FragmentCommunicator { mAdapter.setNetworkState(null); } + @Override + public void onDestroy() { + super.onDestroy(); + EventBus.getDefault().unregister(this); + } + @Override public void onAttach(@NonNull Context context) { super.onAttach(context); mActivity = (BaseActivity) context; } + + @Subscribe + public void onRepliedToPrivateMessageEvent(RepliedToPrivateMessageEvent repliedToPrivateMessageEvent) { + if (mAdapter != null && mWhere.equals(FetchMessages.WHERE_MESSAGES)) { + mAdapter.updateMessageReply(repliedToPrivateMessageEvent.newReply, repliedToPrivateMessageEvent.messagePosition); + } + } } \ No newline at end of file diff --git a/app/src/main/res/layout/activity_view_private_messages.xml b/app/src/main/res/layout/activity_view_private_messages.xml index 728d5df1..bd0bfbe5 100644 --- a/app/src/main/res/layout/activity_view_private_messages.xml +++ b/app/src/main/res/layout/activity_view_private_messages.xml @@ -40,6 +40,7 @@