From be7e0c83826cd2fef7ed8885bba5737f00e90e2f Mon Sep 17 00:00:00 2001 From: oogee Date: Sun, 2 Dec 2018 14:06:01 +0200 Subject: [PATCH] fix not deleting draft when stopping activity after clicking send; fix edits not being saved when editing a post and scrolling up and down --- .../activities/topic/TopicActivity.java | 13 ++++++++ .../mthmmy/activities/topic/TopicAdapter.java | 32 +++++++++++++++++++ 2 files changed, 45 insertions(+) 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 5602bacb..ddc80ed8 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 @@ -531,6 +531,11 @@ public class TopicActivity extends BaseActivity implements TopicAdapter.OnPostFo replyFAB.setTag(true); bottomNavBar.setVisibility(View.VISIBLE); viewModel.setWritingReply(false); + + SharedPreferences drafts = getSharedPreferences(getString(R.string.pref_topic_drafts_key), + Context.MODE_PRIVATE); + drafts.edit().remove(String.valueOf(viewModel.getTopicId())).apply(); + if ((((Post) topicItems.get(topicItems.size() - 1)).getPostNumber() + 1) % 15 == 0) { Timber.i("Reply was posted in new page. Switching to last page."); viewModel.loadUrl(ParseHelpers.getBaseURL(viewModel.getTopicUrl()) + "." + 2147483647); @@ -541,6 +546,14 @@ public class TopicActivity extends BaseActivity implements TopicAdapter.OnPostFo case NEW_REPLY_WHILE_POSTING: Timber.i("New reply while writing a reply"); + //cache reply + if (viewModel.isWritingReply()) { + SharedPreferences drafts2 = getSharedPreferences(getString(R.string.pref_topic_drafts_key), MODE_PRIVATE); + Post reply = (Post) topicItems.get(topicItems.size() - 1); + drafts2.edit().putString(String.valueOf(viewModel.getTopicId()), reply.getContent()).apply(); + viewModel.setWritingReply(false); + } + Runnable addReply = () -> { AlertDialog.Builder builder = new AlertDialog.Builder(TopicActivity.this, R.style.AppTheme_Dark_Dialog); 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 1fee9e01..6eecba83 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 @@ -721,6 +721,38 @@ class TopicAdapter extends RecyclerView.Adapter { viewModel.editPost(position, holder.editSubject.getText().toString(), holder.editEditor.getText().toString()); }); + holder.editSubject.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) { + ((Post) topicItems.get(holder.getAdapterPosition())).setSubject(editable.toString()); + } + }); + holder.editEditor.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) { + ((Post) topicItems.get(holder.getAdapterPosition())).setContent(editable.toString()); + } + }); if (backPressHidden) { holder.editEditor.requestEditTextFocus(); backPressHidden = false;