Browse Source

save unfinished replies as drafts

pull/61/merge
oogee 6 years ago
parent
commit
445b7bd8cf
  1. 19
      app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicActivity.java
  2. 11
      app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicAdapter.java
  3. 6
      app/src/main/java/gr/thmmy/mthmmy/viewmodel/TopicViewModel.java
  4. 1
      app/src/main/res/values/strings.xml

19
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.app.NotificationManager;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Rect; import android.graphics.Rect;
import android.net.Uri; import android.net.Uri;
import android.os.Build; import android.os.Build;
@ -270,6 +271,12 @@ public class TopicActivity extends BaseActivity implements TopicAdapter.OnPostFo
emojiKeyboard.setVisibility(View.GONE); emojiKeyboard.setVisibility(View.GONE);
return; return;
} else if (viewModel.isWritingReply()) { } 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); topicItems.remove(topicItems.size() - 1);
topicAdapter.notifyItemRemoved(topicItems.size()); topicAdapter.notifyItemRemoved(topicItems.size());
topicAdapter.setBackButtonHidden(); topicAdapter.setBackButtonHidden();
@ -303,6 +310,18 @@ public class TopicActivity extends BaseActivity implements TopicAdapter.OnPostFo
viewModel.stopLoading(); 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 @Override
public void onPostFocusChange(int position) { public void onPostFocusChange(int position) {
recyclerView.scrollToPosition(position); recyclerView.scrollToPosition(position);

11
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.annotation.TargetApi;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color; import android.graphics.Color;
import android.graphics.Typeface; import android.graphics.Typeface;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
@ -632,6 +633,10 @@ class TopicAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
holder.itemView.setEnabled(false); holder.itemView.setEnabled(false);
emojiKeyboard.hide(); 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(), viewModel.postReply(context, holder.quickReplySubject.getText().toString(),
holder.replyEditor.getText().toString()); holder.replyEditor.getText().toString());
}); });
@ -639,8 +644,12 @@ class TopicAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
if (reply.getContent() != null) { if (reply.getContent() != null) {
holder.replyEditor.setText(reply.getContent()); holder.replyEditor.setText(reply.getContent());
} else { } else if (viewModel.getBuildedQuotes() != null && !viewModel.getBuildedQuotes().isEmpty()) {
holder.replyEditor.setText(viewModel.getBuildedQuotes()); 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) { if (backPressHidden) {

6
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----------------> // <-------------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) { public void setRemoveVoteTaskStartedListener(ExternalAsyncTask.OnTaskStartedListener removeVoteTaskStartedListener) {
this.removeVoteTaskStartedListener = removeVoteTaskStartedListener; this.removeVoteTaskStartedListener = removeVoteTaskStartedListener;

1
app/src/main/res/values/strings.xml

@ -75,6 +75,7 @@
<item quantity="other">You may only select %d options</item> <item quantity="other">You may only select %d options</item>
</plurals> </plurals>
<string name="show_vote_options_button">hide results</string> <string name="show_vote_options_button">hide results</string>
<string name="pref_topic_drafts_key">preference-topic-drafts-key</string>
<!--Profile Activity--> <!--Profile Activity-->
<string name="username">Username</string> <string name="username">Username</string>

Loading…
Cancel
Save