diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicActivity.java b/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicActivity.java index 56971178..ead5b63e 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicActivity.java +++ b/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(); diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicAdapter.java b/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicAdapter.java index 0181c49a..6da2b530 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicAdapter.java +++ b/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 { } 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; diff --git a/app/src/main/java/gr/thmmy/mthmmy/viewmodel/TopicViewModel.java b/app/src/main/java/gr/thmmy/mthmmy/viewmodel/TopicViewModel.java index eebd1ad6..a4816479 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/viewmodel/TopicViewModel.java +++ b/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; + } }