Browse Source

cache reply message and subject on viewmodel

pull/61/merge
oogee 6 years ago
parent
commit
9414d386c7
  1. 21
      app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicActivity.java
  2. 34
      app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicAdapter.java
  3. 18
      app/src/main/java/gr/thmmy/mthmmy/viewmodel/TopicViewModel.java

21
app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicActivity.java

@ -10,6 +10,7 @@ import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
@ -273,9 +274,9 @@ public class TopicActivity extends BaseActivity implements TopicAdapter.OnPostFo
} else if (viewModel.isWritingReply()) {
// persist reply
SharedPreferences drafts = getSharedPreferences(getString(R.string.pref_topic_drafts_key), MODE_PRIVATE);
TopicAdapter.QuickReplyViewHolder replyHolder = (TopicAdapter.QuickReplyViewHolder)
recyclerView.findViewHolderForAdapterPosition(topicItems.size() - 1);
drafts.edit().putString(String.valueOf(viewModel.getTopicId()), replyHolder.replyEditor.getText().toString()).apply();
Editable cachedReply = viewModel.getCachedReply();
if (cachedReply != null)
drafts.edit().putString(String.valueOf(viewModel.getTopicId()), cachedReply.toString()).apply();
topicItems.remove(topicItems.size() - 1);
topicAdapter.notifyItemRemoved(topicItems.size());
@ -316,9 +317,9 @@ public class TopicActivity extends BaseActivity implements TopicAdapter.OnPostFo
// persist reply
if (viewModel.isWritingReply()) {
SharedPreferences drafts = getSharedPreferences(getString(R.string.pref_topic_drafts_key), MODE_PRIVATE);
TopicAdapter.QuickReplyViewHolder replyHolder = (TopicAdapter.QuickReplyViewHolder)
recyclerView.findViewHolderForAdapterPosition(topicItems.size() - 1);
drafts.edit().putString(String.valueOf(viewModel.getTopicId()), replyHolder.replyEditor.getText().toString()).apply();
Editable cachedReply = viewModel.getCachedReply();
if (cachedReply != null)
drafts.edit().putString(String.valueOf(viewModel.getTopicId()), cachedReply.toString()).apply();
}
}
@ -535,13 +536,11 @@ public class TopicActivity extends BaseActivity implements TopicAdapter.OnPostFo
break;
case NEW_REPLY_WHILE_POSTING:
Timber.i("New reply while writing a reply");
TopicAdapter.QuickReplyViewHolder replyHolder = (TopicAdapter.QuickReplyViewHolder)
recyclerView.findViewHolderForAdapterPosition(topicItems.size() - 1);
String subject = replyHolder.quickReplySubject.getText().toString();
String message = replyHolder.replyEditor.getText().toString();
Editable subject = viewModel.getCachedSubject();
Editable message = viewModel.getCachedReply();
Runnable addReply = () -> {
viewModel.setWritingReply(true);
topicItems.add(Post.newQuickReply(subject, message));
topicItems.add(Post.newQuickReply(subject.toString(), message.toString()));
topicAdapter.notifyItemInserted(topicItems.size());
recyclerView.scrollToPosition(topicItems.size() - 1);
replyFAB.hide();

34
app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicAdapter.java

@ -11,9 +11,11 @@ import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.text.Editable;
import android.text.Html;
import android.text.InputType;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.method.LinkMovementMethod;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
@ -658,11 +660,43 @@ class TopicAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
}
holder.replyEditor.setText(replyText);
holder.replyEditor.getEditText().setSelection(holder.replyEditor.getText().length());
holder.replyEditor.getEditText().addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
viewModel.setCachedReply(editable);
}
});
if (backPressHidden) {
holder.replyEditor.requestEditTextFocus();
backPressHidden = false;
}
holder.quickReplySubject.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
viewModel.setCachedSubject(editable);
}
});
} else if (currentHolder instanceof EditMessageViewHolder) {
final EditMessageViewHolder holder = (EditMessageViewHolder) currentHolder;

18
app/src/main/java/gr/thmmy/mthmmy/viewmodel/TopicViewModel.java

@ -4,6 +4,7 @@ import android.content.Context;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.preference.PreferenceManager;
import android.text.Editable;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
@ -40,6 +41,7 @@ public class TopicViewModel extends BaseViewModel implements TopicTask.OnTopicTa
*/
private boolean editingPost = false;
private boolean writingReply = false;
private Editable cachedSubject, cachedReply;
/**
* A list of {@link Post#getPostIndex()} for building quotes for replying
*/
@ -486,4 +488,20 @@ public class TopicViewModel extends BaseViewModel implements TopicTask.OnTopicTa
throw new NullPointerException("No page has been loaded yet!");
return topicItems.getValue().size();
}
public Editable getCachedSubject() {
return cachedSubject;
}
public void setCachedSubject(Editable cachedSubject) {
this.cachedSubject = cachedSubject;
}
public Editable getCachedReply() {
return cachedReply;
}
public void setCachedReply(Editable cachedReply) {
this.cachedReply = cachedReply;
}
}

Loading…
Cancel
Save