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 ddf48d9f..56971178 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 @@ -4,6 +4,7 @@ import android.annotation.SuppressLint; import android.app.NotificationManager; import android.content.Context; import android.content.Intent; +import android.content.SharedPreferences; import android.graphics.Rect; import android.net.Uri; import android.os.Build; @@ -270,6 +271,12 @@ public class TopicActivity extends BaseActivity implements TopicAdapter.OnPostFo emojiKeyboard.setVisibility(View.GONE); return; } 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(); + topicItems.remove(topicItems.size() - 1); topicAdapter.notifyItemRemoved(topicItems.size()); topicAdapter.setBackButtonHidden(); @@ -303,6 +310,18 @@ public class TopicActivity extends BaseActivity implements TopicAdapter.OnPostFo viewModel.stopLoading(); } + @Override + protected void onStop() { + super.onStop(); + // 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(); + } + } + @Override public void onPostFocusChange(int position) { recyclerView.scrollToPosition(position); 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 efb0192e..7aa43428 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 @@ -4,6 +4,7 @@ import android.annotation.SuppressLint; import android.annotation.TargetApi; import android.content.Context; import android.content.Intent; +import android.content.SharedPreferences; import android.graphics.Color; import android.graphics.Typeface; import android.graphics.drawable.Drawable; @@ -632,6 +633,10 @@ class TopicAdapter extends RecyclerView.Adapter { holder.itemView.setEnabled(false); emojiKeyboard.hide(); + SharedPreferences drafts = context.getSharedPreferences(context.getString(R.string.pref_topic_drafts_key), + Context.MODE_PRIVATE); + drafts.edit().remove(String.valueOf(viewModel.getTopicId())).apply(); + viewModel.postReply(context, holder.quickReplySubject.getText().toString(), holder.replyEditor.getText().toString()); }); @@ -639,8 +644,12 @@ class TopicAdapter extends RecyclerView.Adapter { if (reply.getContent() != null) { holder.replyEditor.setText(reply.getContent()); - } else { + } else if (viewModel.getBuildedQuotes() != null && !viewModel.getBuildedQuotes().isEmpty()) { holder.replyEditor.setText(viewModel.getBuildedQuotes()); + } else { + SharedPreferences drafts = context.getSharedPreferences(context.getString(R.string.pref_topic_drafts_key), + Context.MODE_PRIVATE); + holder.replyEditor.setText(drafts.getString(String.valueOf(viewModel.getTopicId()), "")); } if (backPressHidden) { 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 e68dcfb0..aa669185 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/viewmodel/TopicViewModel.java +++ b/app/src/main/java/gr/thmmy/mthmmy/viewmodel/TopicViewModel.java @@ -313,6 +313,12 @@ public class TopicViewModel extends BaseViewModel implements TopicTask.OnTopicTa // <-------------Just getters, setters and helper methods below here----------------> + public int getTopicId() { + if (pageTopicId.getValue() == null) + throw new NullPointerException("No page has been loaded yet!"); + return pageTopicId.getValue(); + } + public void setRemoveVoteTaskStartedListener(ExternalAsyncTask.OnTaskStartedListener removeVoteTaskStartedListener) { this.removeVoteTaskStartedListener = removeVoteTaskStartedListener; diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 30cd4fe3..bf3d9e5f 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -75,6 +75,7 @@ You may only select %d options hide results + preference-topic-drafts-key Username