Read all the replies when replying a message. Update the replied message in InboxFragment.

This commit is contained in:
Alex Ning 2020-07-01 21:48:39 +08:00
parent 707316c7ad
commit eff385ccd6
6 changed files with 97 additions and 14 deletions

View File

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

View File

@ -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<Message, Recycl
if (holder instanceof DataViewHolder) {
Message message = getItem(holder.getAdapterPosition());
if (message != null) {
ArrayList<Message> 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<Message, Recycl
((DataViewHolder) holder).titleTextView.setVisibility(View.GONE);
}
((DataViewHolder) holder).authorTextView.setText(message.getAuthor());
String subject = message.getSubject().substring(0, 1).toUpperCase() + message.getSubject().substring(1);
((DataViewHolder) holder).authorTextView.setText(displayedMessage.getAuthor());
String subject = displayedMessage.getSubject().substring(0, 1).toUpperCase() + displayedMessage.getSubject().substring(1);
((DataViewHolder) holder).subjectTextView.setText(subject);
mMarkwon.setMarkdown(((DataViewHolder) holder).contentCustomMarkwonView, message.getBody());
mMarkwon.setMarkdown(((DataViewHolder) holder).contentCustomMarkwonView, displayedMessage.getBody());
((DataViewHolder) holder).itemView.setOnClickListener(view -> {
if (mMessageType == FetchMessages.MESSAGE_TYPE_NOTIFICATION
@ -163,10 +172,11 @@ public class MessageRecyclerViewAdapter extends PagedListAdapter<Message, Recycl
} else if (mMessageType == FetchMessages.MESSAGE_TYPE_PRIVATE_MESSAGE) {
Intent intent = new Intent(mContext, ViewPrivateMessagesActivity.class);
intent.putExtra(ViewPrivateMessagesActivity.EXTRA_PRIVATE_MESSAGE, message);
intent.putExtra(ViewPrivateMessagesActivity.EXTRA_MESSAGE_POSITION, holder.getAdapterPosition());
mContext.startActivity(intent);
}
if (message.isNew()) {
if (displayedMessage.isNew()) {
((DataViewHolder) holder).itemView.setBackgroundColor(mMessageBackgroundColor);
message.setNew(false);
@ -247,6 +257,16 @@ public class MessageRecyclerViewAdapter extends PagedListAdapter<Message, Recycl
}
}
public void updateMessageReply(Message newReply, int position) {
if (position >= 0 && position < super.getItemCount()) {
Message message = getItem(position);
if (message != null) {
message.addReply(newReply);
notifyItemChanged(position);
}
}
}
public interface RetryLoadingMoreCallback {
void retryLoadingMore();
}

View File

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

View File

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

View File

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

View File

@ -40,6 +40,7 @@
</androidx.coordinatorlayout.widget.CoordinatorLayout>
<LinearLayout
android:id="@+id/edit_text_wrapper_linear_layout_view_private_messages_activity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"