Draft support for compose screen

This commit is contained in:
Moez Bhatti
2016-02-28 14:06:47 -05:00
parent 0b1bd25d1e
commit aca7fefc6e
3 changed files with 44 additions and 8 deletions
@@ -93,7 +93,10 @@ public enum QKPreference {
QK_COMPOSE("pref_key_quickcompose", false), QK_COMPOSE("pref_key_quickcompose", false),
// LiveViews // LiveViews
CONVERSATION_THEME("conversation_theme"); CONVERSATION_THEME("conversation_theme"),
// Storage
COMPOSE_DRAFT("compose_draft", "");
private String mKey; private String mKey;
private Object mDefaultValue; private Object mDefaultValue;
@@ -104,6 +104,7 @@ public class ComposeFragment extends QKContentFragment implements ActivityLaunch
view.findViewById(R.id.compose_view_stub).setVisibility(View.VISIBLE); view.findViewById(R.id.compose_view_stub).setVisibility(View.VISIBLE);
mComposeView = (ComposeView) view.findViewById(R.id.compose_view); mComposeView = (ComposeView) view.findViewById(R.id.compose_view);
mComposeView.onOpenConversation(null, null);
mComposeView.setActivityLauncher(this); mComposeView.setActivityLauncher(this);
mComposeView.setRecipientProvider(this); mComposeView.setRecipientProvider(this);
mComposeView.setOnSendListener(this); mComposeView.setOnSendListener(this);
@@ -168,6 +169,22 @@ public class ComposeFragment extends QKContentFragment implements ActivityLaunch
} }
} }
@Override
public void onContentClosed() {
super.onContentClosed();
if (mComposeView != null) {
mComposeView.saveDraft();
}
}
@Override
public void onDestroy() {
super.onDestroy();
if (mComposeView != null) {
mComposeView.saveDraft();
}
}
@Override @Override
public void onMenuChanging(float percentOpen) { public void onMenuChanging(float percentOpen) {
@@ -715,7 +715,7 @@ public class ComposeView extends LinearLayout implements View.OnClickListener {
mConversationLegacy = conversationLegacy; mConversationLegacy = conversationLegacy;
// If the conversation was different, set up the draft here. // If the conversation was different, set up the draft here.
if (threadId != newThreadId) { if (threadId != newThreadId || newThreadId == -1) {
setupDraft(); setupDraft();
} }
} }
@@ -735,15 +735,27 @@ public class ComposeView extends LinearLayout implements View.OnClickListener {
public void saveDraft() { public void saveDraft() {
// If the conversation_reply view is null, then we won't worry about saving drafts at all. We also don't save // If the conversation_reply view is null, then we won't worry about saving drafts at all. We also don't save
// drafts if a message is about to be sent (delayed) // drafts if a message is about to be sent (delayed)
if (mReplyText != null && mConversation != null && mButtonState != SendButtonState.CANCEL) { if (mReplyText != null && mButtonState != SendButtonState.CANCEL) {
String draft = mReplyText.getText().toString(); String draft = mReplyText.getText().toString();
if (mConversationLegacy.hasDraft() && TextUtils.isEmpty(draft)) { if (mConversation != null) {
mConversationLegacy.clearDrafts(); if (mConversationLegacy.hasDraft() && TextUtils.isEmpty(draft)) {
mConversationLegacy.clearDrafts();
} else if (!TextUtils.isEmpty(draft) && } else if (!TextUtils.isEmpty(draft) &&
(!mConversationLegacy.hasDraft() || !draft.equals(mConversationLegacy.getDraft()))) { (!mConversationLegacy.hasDraft() || !draft.equals(mConversationLegacy.getDraft()))) {
mConversationLegacy.saveDraft(draft); mConversationLegacy.saveDraft(draft);
}
} else {
String oldDraft = mPrefs.getString(QKPreference.COMPOSE_DRAFT.getKey(), "");
if (!draft.equals(oldDraft)) {
mPrefs.edit().putString(QKPreference.COMPOSE_DRAFT.getKey(), draft).apply();
// Only show the draft if we saved text, not if we just cleared some
if (!TextUtils.isEmpty(draft)) {
Toast.makeText(mContext, R.string.toast_draft, Toast.LENGTH_SHORT).show();
}
}
} }
} }
@@ -767,6 +779,10 @@ public class ComposeView extends LinearLayout implements View.OnClickListener {
mReplyText.setText(""); mReplyText.setText("");
clearAttachment(); clearAttachment();
} }
} else {
String draft = mPrefs.getString(QKPreference.COMPOSE_DRAFT.getKey(), "");
mReplyText.setText(draft);
mReplyText.setSelection(draft.length());
} }
} }