Refactored compose to create a draft on get

pull/50/head
M66B 7 years ago
parent 9766a76fab
commit 643b17397a

@ -45,12 +45,8 @@ public class ActivityCompose extends ActivityBase implements FragmentManager.OnB
getSupportFragmentManager().addOnBackStackChangedListener(this); getSupportFragmentManager().addOnBackStackChangedListener(this);
if (getSupportFragmentManager().getFragments().size() == 0) { if (getSupportFragmentManager().getFragments().size() == 0) {
Bundle args = getIntent().getExtras();
if (args == null)
args = new Bundle();
FragmentCompose fragment = new FragmentCompose(); FragmentCompose fragment = new FragmentCompose();
fragment.setArguments(args); fragment.setArguments(getIntent().getExtras());
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction(); FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.content_frame, fragment).addToBackStack("compose"); fragmentTransaction.replace(R.id.content_frame, fragment).addToBackStack("compose");

@ -64,15 +64,16 @@ public class ActivityView extends ActivityBase implements FragmentManager.OnBack
static final int LOADER_ACCOUNT_PUT = 2; static final int LOADER_ACCOUNT_PUT = 2;
static final int LOADER_IDENTITY_PUT = 3; static final int LOADER_IDENTITY_PUT = 3;
static final int LOADER_FOLDER_PUT = 4; static final int LOADER_FOLDER_PUT = 4;
static final int LOADER_MESSAGE_VIEW = 5; static final int LOADER_MESSAGE_ACCOUNT = 5;
static final int LOADER_MESSAGE_SEEN = 6; static final int LOADER_MESSAGE_VIEW = 6;
static final int LOADER_MESSAGE_EDIT = 7; static final int LOADER_MESSAGE_SEEN = 7;
static final int LOADER_MESSAGE_SPAM = 8; static final int LOADER_MESSAGE_EDIT = 8;
static final int LOADER_MESSAGE_TRASH = 9; static final int LOADER_MESSAGE_SPAM = 9;
static final int LOADER_MESSAGE_MOVE = 10; static final int LOADER_MESSAGE_TRASH = 10;
static final int LOADER_MESSAGE_ARCHIVE = 11; static final int LOADER_MESSAGE_MOVE = 11;
static final int LOADER_SEEN_UNTIL = 12; static final int LOADER_MESSAGE_ARCHIVE = 12;
static final int LOADER_DEBUG_INFO = 13; static final int LOADER_SEEN_UNTIL = 13;
static final int LOADER_DEBUG_INFO = 14;
static final int REQUEST_VIEW = 1; static final int REQUEST_VIEW = 1;
static final int REQUEST_UNSEEN = 2; static final int REQUEST_UNSEEN = 2;

@ -45,7 +45,7 @@ public class AdapterMessage extends PagedListAdapter<TupleMessageEx, AdapterMess
enum ViewType {FOLDER, THREAD} enum ViewType {FOLDER, THREAD}
public class ViewHolder extends RecyclerView.ViewHolder public class ViewHolder extends RecyclerView.ViewHolder
implements View.OnClickListener { implements View.OnClickListener, View.OnLongClickListener {
View itemView; View itemView;
TextView tvFrom; TextView tvFrom;
TextView tvTime; TextView tvTime;
@ -68,10 +68,12 @@ public class AdapterMessage extends PagedListAdapter<TupleMessageEx, AdapterMess
private void wire() { private void wire() {
itemView.setOnClickListener(this); itemView.setOnClickListener(this);
itemView.setOnLongClickListener(this);
} }
private void unwire() { private void unwire() {
itemView.setOnClickListener(null); itemView.setOnClickListener(null);
itemView.setOnLongClickListener(null);
} }
private void clear() { private void clear() {
@ -126,11 +128,12 @@ public class AdapterMessage extends PagedListAdapter<TupleMessageEx, AdapterMess
int pos = getAdapterPosition(); int pos = getAdapterPosition();
if (pos == RecyclerView.NO_POSITION) if (pos == RecyclerView.NO_POSITION)
return; return;
final TupleMessageEx message = getItem(pos); TupleMessageEx message = getItem(pos);
if (EntityFolder.DRAFTS.equals(message.folderType)) if (EntityFolder.DRAFTS.equals(message.folderType))
context.startActivity( context.startActivity(
new Intent(context, ActivityCompose.class) new Intent(context, ActivityCompose.class)
.putExtra("action", "edit")
.putExtra("id", message.id)); .putExtra("id", message.id));
else { else {
LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(context); LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(context);
@ -139,6 +142,21 @@ public class AdapterMessage extends PagedListAdapter<TupleMessageEx, AdapterMess
.putExtra("id", message.id)); .putExtra("id", message.id));
} }
} }
@Override
public boolean onLongClick(View view) {
int pos = getAdapterPosition();
if (pos == RecyclerView.NO_POSITION)
return false;
TupleMessageEx message = getItem(pos);
LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(context);
lbm.sendBroadcast(
new Intent(ActivityView.ACTION_VIEW_MESSAGE)
.putExtra("id", message.id));
return true;
}
} }
AdapterMessage(Context context, ViewType viewType) { AdapterMessage(Context context, ViewType viewType) {

@ -94,6 +94,7 @@ public class FragmentAbout extends FragmentEx {
if (result.ex == null) { if (result.ex == null) {
long id = (Long) result.data; long id = (Long) result.data;
startActivity(new Intent(getContext(), ActivityCompose.class) startActivity(new Intent(getContext(), ActivityCompose.class)
.putExtra("action", "edit")
.putExtra("id", id)); .putExtra("id", id));
} else } else
Toast.makeText(getContext(), executor.toString(), Toast.LENGTH_LONG).show(); Toast.makeText(getContext(), executor.toString(), Toast.LENGTH_LONG).show();

@ -78,10 +78,6 @@ import androidx.recyclerview.widget.RecyclerView;
import static android.app.Activity.RESULT_OK; import static android.app.Activity.RESULT_OK;
public class FragmentCompose extends FragmentEx { public class FragmentCompose extends FragmentEx {
private boolean once = false;
private String thread = null;
private long rid = -1;
private Spinner spFrom; private Spinner spFrom;
private ImageView ivIdentityAdd; private ImageView ivIdentityAdd;
private AutoCompleteTextView etTo; private AutoCompleteTextView etTo;
@ -98,6 +94,7 @@ public class FragmentCompose extends FragmentEx {
private Group grpAddresses; private Group grpAddresses;
private Group grpAttachments; private Group grpAttachments;
private Group grpReady; private Group grpReady;
private MenuItem menuAttachment = null;
@Override @Override
@Nullable @Nullable
@ -106,11 +103,6 @@ public class FragmentCompose extends FragmentEx {
View view = inflater.inflate(R.layout.fragment_compose, container, false); View view = inflater.inflate(R.layout.fragment_compose, container, false);
// Get arguments
Bundle args = getArguments();
String action = (args == null ? null : args.getString("action"));
final long id = (TextUtils.isEmpty(action) ? (args == null ? -1 : args.getLong("id", -1)) : -1);
// Get controls // Get controls
spFrom = view.findViewById(R.id.spFrom); spFrom = view.findViewById(R.id.spFrom);
ivIdentityAdd = view.findViewById(R.id.ivIdentityAdd); ivIdentityAdd = view.findViewById(R.id.ivIdentityAdd);
@ -173,17 +165,7 @@ public class FragmentCompose extends FragmentEx {
bottom_navigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() { bottom_navigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override @Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) { public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) { onAction(item.getItemId());
case R.id.action_trash:
actionPut(id, "trash");
return true;
case R.id.action_save:
actionPut(id, "save");
return true;
case R.id.action_send:
actionPut(id, "send");
return true;
}
return false; return false;
} }
@ -258,6 +240,7 @@ public class FragmentCompose extends FragmentEx {
DB.getInstance(getContext()).identity().liveIdentities(true).observe(getViewLifecycleOwner(), new Observer<List<EntityIdentity>>() { DB.getInstance(getContext()).identity().liveIdentities(true).observe(getViewLifecycleOwner(), new Observer<List<EntityIdentity>>() {
@Override @Override
public void onChanged(@Nullable final List<EntityIdentity> identities) { public void onChanged(@Nullable final List<EntityIdentity> identities) {
// Sort identities
Collections.sort(identities, new Comparator<EntityIdentity>() { Collections.sort(identities, new Comparator<EntityIdentity>() {
@Override @Override
public int compare(EntityIdentity i1, EntityIdentity i2) { public int compare(EntityIdentity i1, EntityIdentity i2) {
@ -265,6 +248,7 @@ public class FragmentCompose extends FragmentEx {
} }
}); });
// Show identities
ArrayAdapter<EntityIdentity> adapter = new ArrayAdapter<>(getContext(), R.layout.spinner_item, identities); ArrayAdapter<EntityIdentity> adapter = new ArrayAdapter<>(getContext(), R.layout.spinner_item, identities);
adapter.setDropDownViewResource(R.layout.spinner_dropdown_item); adapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
spFrom.setAdapter(adapter); spFrom.setAdapter(adapter);
@ -279,9 +263,16 @@ public class FragmentCompose extends FragmentEx {
spFrom.setVisibility(View.VISIBLE); spFrom.setVisibility(View.VISIBLE);
ivIdentityAdd.setVisibility(View.VISIBLE); ivIdentityAdd.setVisibility(View.VISIBLE);
// Get might select another identity // Get draft
LoaderManager.getInstance(FragmentCompose.this) new Handler().post(new Runnable() {
.restartLoader(ActivityCompose.LOADER_COMPOSE_GET, getArguments(), getLoaderCallbacks).forceLoad(); @Override
public void run() {
// Get might select another identity
LoaderManager.getInstance(FragmentCompose.this)
.restartLoader(ActivityCompose.LOADER_COMPOSE_GET, getArguments(), getLoaderCallbacks).forceLoad();
}
});
} }
}); });
} }
@ -292,6 +283,13 @@ public class FragmentCompose extends FragmentEx {
super.onCreateOptionsMenu(menu, inflater); super.onCreateOptionsMenu(menu, inflater);
} }
@Override
public void onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
menuAttachment = menu.findItem(R.id.menu_attachment);
menuAttachment.setEnabled(false);
}
@Override @Override
public boolean onOptionsItemSelected(MenuItem item) { public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) { switch (item.getItemId()) {
@ -421,18 +419,16 @@ public class FragmentCompose extends FragmentEx {
} }
} }
private void actionPut(long id, String action) { private void onAction(int action) {
Log.i(Helper.TAG, "Put id=" + id + " action=" + action);
bottom_navigation.getMenu().setGroupEnabled(0, false); bottom_navigation.getMenu().setGroupEnabled(0, false);
long id = getArguments().getLong("id", -1);
EntityIdentity identity = (EntityIdentity) spFrom.getSelectedItem(); EntityIdentity identity = (EntityIdentity) spFrom.getSelectedItem();
Bundle args = new Bundle(); Bundle args = new Bundle();
args.putString("action", action);
args.putLong("id", id); args.putLong("id", id);
args.putLong("iid", identity == null ? -1 : identity.id); args.putInt("action", action);
args.putString("thread", FragmentCompose.this.thread); args.putLong("identity", identity == null ? -1 : identity.id);
args.putLong("rid", FragmentCompose.this.rid);
args.putString("to", etTo.getText().toString()); args.putString("to", etTo.getText().toString());
args.putString("cc", etCc.getText().toString()); args.putString("cc", etCc.getText().toString());
args.putString("bcc", etBcc.getText().toString()); args.putString("bcc", etBcc.getText().toString());
@ -443,7 +439,7 @@ public class FragmentCompose extends FragmentEx {
.restartLoader(ActivityCompose.LOADER_COMPOSE_PUT, args, putLoaderCallbacks).forceLoad(); .restartLoader(ActivityCompose.LOADER_COMPOSE_PUT, args, putLoaderCallbacks).forceLoad();
} }
private static class GetLoader extends AsyncTaskLoader<Bundle> { private static class GetLoader extends AsyncTaskLoader<EntityMessage> {
private Bundle args; private Bundle args;
GetLoader(Context context) { GetLoader(Context context) {
@ -456,167 +452,139 @@ public class FragmentCompose extends FragmentEx {
@Nullable @Nullable
@Override @Override
public Bundle loadInBackground() { public EntityMessage loadInBackground() {
Bundle result = new Bundle(); EntityMessage draft = null;
try { try {
String action = args.getString("action"); String action = args.getString("action");
long id = args.getLong("id", -1); long id = args.getLong("id", -1);
long account = args.getLong("account", -1);
long reference = args.getLong("reference", -1);
Log.i(Helper.TAG, "Get action=" + action + " id=" + id + " account=" + account + " reference=" + reference);
result.putString("action", action); DB db = DB.getInstance(getContext());
EntityMessage msg = DB.getInstance(getContext()).message().getMessage(id);
if (msg != null) {
if (msg.identity != null)
result.putLong("iid", msg.identity);
if (msg.replying != null)
result.putLong("rid", msg.replying);
result.putSerializable("cc", msg.cc);
result.putSerializable("bcc", msg.bcc);
result.putString("thread", msg.thread);
result.putString("subject", msg.subject);
result.putString("body", msg.body);
}
if (TextUtils.isEmpty(action)) { draft = db.message().getMessage(id);
if (msg != null) { if (draft == null) {
result.putSerializable("from", msg.from); EntityMessage ref = DB.getInstance(getContext()).message().getMessage(reference);
result.putSerializable("to", msg.to); if (ref != null)
} account = ref.account;
} else if ("reply".equals(action)) {
Address[] to = null; EntityFolder drafts;
if (msg != null) drafts = db.folder().getFolderByType(account, EntityFolder.DRAFTS);
to = (msg.reply == null || msg.reply.length == 0 ? msg.from : msg.reply); if (drafts == null)
result.putLong("rid", msg.id); drafts = db.folder().getPrimaryDrafts();
result.putSerializable("from", msg.to);
result.putSerializable("to", to); draft = new EntityMessage();
} else if ("reply_all".equals(action)) { draft.account = account;
Address[] to = null; draft.folder = drafts.id;
if (msg != null) {
List<Address> addresses = new ArrayList<>(); if (ref != null) {
if (msg.reply != null) draft.thread = ref.thread;
addresses.addAll(Arrays.asList(msg.reply));
else if (msg.from != null) if ("reply".equals(action)) {
addresses.addAll(Arrays.asList(msg.from)); draft.replying = ref.id;
if (msg.cc != null) draft.to = (ref.reply == null || ref.reply.length == 0 ? ref.from : ref.reply);
addresses.addAll(Arrays.asList(msg.cc)); draft.from = ref.to;
to = addresses.toArray(new Address[0]);
} else if ("reply_all".equals(action)) {
draft.replying = ref.id;
List<Address> addresses = new ArrayList<>();
if (draft.reply != null && ref.reply.length > 0)
addresses.addAll(Arrays.asList(ref.reply));
else if (draft.from != null)
addresses.addAll(Arrays.asList(ref.from));
if (draft.cc != null)
addresses.addAll(Arrays.asList(ref.cc));
draft.to = addresses.toArray(new Address[0]);
draft.from = ref.to;
} else if ("forward".equals(action)) {
//msg.replying = ref.id;
draft.from = ref.to;
}
if ("reply".equals(action) || "reply_all".equals(action)) {
draft.subject = getContext().getString(R.string.title_subject_reply, ref.subject);
draft.body = String.format("<br><br>%s %s:<br><br>%s",
Html.escapeHtml(new Date().toString()),
Html.escapeHtml(TextUtils.join(", ", draft.to)),
HtmlHelper.sanitize(getContext(), ref.body, true));
} else if ("forward".equals(action)) {
draft.subject = getContext().getString(R.string.title_subject_forward, ref.subject);
draft.body = String.format("<br><br>%s %s:<br><br>%s",
Html.escapeHtml(new Date().toString()),
Html.escapeHtml(TextUtils.join(", ", ref.from)),
HtmlHelper.sanitize(getContext(), ref.body, true));
}
} }
result.putLong("rid", msg.id);
result.putSerializable("from", msg.to); draft.received = new Date().getTime();
result.putSerializable("to", to); draft.seen = false;
} else if ("forward".equals(action)) { draft.ui_seen = false;
Address[] to = null; draft.ui_hide = false;
if (msg != null)
to = (msg.reply == null || msg.reply.length == 0 ? msg.from : msg.reply); draft.id = DB.getInstance(getContext()).message().insertMessage(draft);
result.putSerializable("from", msg.to);
result.putSerializable("to", to);
} }
} catch (Throwable ex) { } catch (Throwable ex) {
Log.e(Helper.TAG, ex + "\n" + Log.getStackTraceString(ex)); Log.e(Helper.TAG, ex + "\n" + Log.getStackTraceString(ex));
} }
return result;
return draft;
} }
} }
private LoaderManager.LoaderCallbacks getLoaderCallbacks = new LoaderManager.LoaderCallbacks<Bundle>() { private LoaderManager.LoaderCallbacks getLoaderCallbacks = new LoaderManager.LoaderCallbacks<EntityMessage>() {
@NonNull @NonNull
@Override @Override
public Loader<Bundle> onCreateLoader(int id, @Nullable Bundle args) { public Loader<EntityMessage> onCreateLoader(int id, @Nullable Bundle args) {
GetLoader loader = new GetLoader(getContext()); GetLoader loader = new GetLoader(getContext());
loader.setArgs(args); loader.setArgs(args);
return loader; return loader;
} }
@Override @Override
public void onLoadFinished(@NonNull Loader<Bundle> loader, Bundle result) { public void onLoadFinished(@NonNull Loader<EntityMessage> loader, EntityMessage msg) {
LoaderManager.getInstance(FragmentCompose.this).destroyLoader(loader.getId()); LoaderManager.getInstance(FragmentCompose.this).destroyLoader(loader.getId());
long iid = result.getLong("iid", -1); getArguments().putLong("id", msg.id);
long rid = result.getLong("rid", -1); String action = getArguments().getString("action");
String thread = result.getString("thread");
Address[] from = (Address[]) result.getSerializable("from");
Address[] to = (Address[]) result.getSerializable("to");
Address[] cc = (Address[]) result.getSerializable("cc");
Address[] bcc = (Address[]) result.getSerializable("bcc");
String subject = result.getString("subject");
String body = result.getString("body");
String action = result.getString("action");
menuAttachment.setEnabled(true);
pbWait.setVisibility(View.GONE); pbWait.setVisibility(View.GONE);
grpAddresses.setVisibility("reply_all".equals(action) ? View.VISIBLE : View.GONE); grpAddresses.setVisibility("reply_all".equals(action) ? View.VISIBLE : View.GONE);
grpReady.setVisibility(View.VISIBLE); grpReady.setVisibility(View.VISIBLE);
FragmentCompose.this.thread = thread;
FragmentCompose.this.rid = rid;
ArrayAdapter adapter = (ArrayAdapter) spFrom.getAdapter(); ArrayAdapter adapter = (ArrayAdapter) spFrom.getAdapter();
if (adapter != null) { if (adapter != null) {
for (int pos = 0; pos < adapter.getCount(); pos++) { for (int pos = 0; pos < adapter.getCount(); pos++) {
EntityIdentity identity = (EntityIdentity) adapter.getItem(pos); EntityIdentity identity = (EntityIdentity) adapter.getItem(pos);
if (iid < 0 if (msg.identity == null
? from != null && from.length > 0 && ((InternetAddress) from[0]).getAddress().equals(identity.email) ? msg.from != null && msg.from.length > 0 && ((InternetAddress) msg.from[0]).getAddress().equals(identity.email)
: iid == identity.id) { : msg.identity.equals(identity.id)) {
spFrom.setSelection(pos); spFrom.setSelection(pos);
break; break;
} }
} }
} }
if (!once) { etTo.setText(msg.to == null ? null : TextUtils.join(", ", msg.to));
// Prevent changed fields from being overwritten etCc.setText(msg.cc == null ? null : TextUtils.join(", ", msg.cc));
once = true; etBcc.setText(msg.bcc == null ? null : TextUtils.join(", ", msg.bcc));
etSubject.setText(msg.subject);
Handler handler = new Handler(); etBody.setText(Html.fromHtml(msg.body));
etCc.setText(cc == null ? null : TextUtils.join(", ", cc)); if ("edit".equals(action))
etBcc.setText(bcc == null ? null : TextUtils.join(", ", bcc)); etTo.requestFocus();
else if ("reply".equals(action) || "reply_all".equals(action))
if (action == null) { etBody.requestFocus();
etTo.setText(to == null ? null : TextUtils.join(", ", to)); else if ("forward".equals(action))
etSubject.setText(subject); etTo.requestFocus();
if (body != null)
etBody.setText(Html.fromHtml(HtmlHelper.sanitize(getContext(), body, false)));
handler.post(new Runnable() {
@Override
public void run() {
etTo.requestFocus();
}
});
} else if ("reply".equals(action) || "reply_all".equals(action)) {
etTo.setText(to == null ? null : TextUtils.join(", ", to));
String text = String.format("<br><br>%s %s:<br><br>%s",
Html.escapeHtml(new Date().toString()),
Html.escapeHtml(to == null ? "" : TextUtils.join(", ", to)),
HtmlHelper.sanitize(getContext(), body, true));
etSubject.setText(getContext().getString(R.string.title_subject_reply, subject));
etBody.setText(Html.fromHtml(text));
handler.post(new Runnable() {
@Override
public void run() {
etBody.requestFocus();
}
});
} else if ("forward".equals(action)) {
String text = String.format("<br><br>%s %s:<br><br>%s",
Html.escapeHtml(new Date().toString()),
Html.escapeHtml(to == null ? "" : TextUtils.join(", ", to)),
HtmlHelper.sanitize(getContext(), body, true));
etSubject.setText(getContext().getString(R.string.title_subject_forward, subject));
etBody.setText(Html.fromHtml(text));
handler.post(new Runnable() {
@Override
public void run() {
etTo.requestFocus();
}
});
}
}
bottom_navigation.getMenu().setGroupEnabled(0, true); bottom_navigation.getMenu().setGroupEnabled(0, true);
} }
@Override @Override
public void onLoaderReset(@NonNull Loader<Bundle> loader) { public void onLoaderReset(@NonNull Loader<EntityMessage> loader) {
} }
}; };
@ -634,45 +602,29 @@ public class FragmentCompose extends FragmentEx {
@Override @Override
public Throwable loadInBackground() { public Throwable loadInBackground() {
try { try {
String action = args.getString("action");
long id = args.getLong("id");
Log.i(Helper.TAG, "Put load id=" + id + " action=" + action);
DB db = DB.getInstance(getContext());
DaoMessage message = db.message();
DaoIdentity identity = db.identity();
DaoFolder folder = db.folder();
// Get data // Get data
EntityMessage draft = message.getMessage(id); long id = args.getLong("id");
EntityIdentity ident = identity.getIdentity(args.getLong("iid")); int action = args.getInt("action");
if (ident == null) long iid = args.getLong("identity");
throw new IllegalArgumentException(getContext().getString(R.string.title_from_missing));
EntityFolder drafts = db.folder().getFolderByType(ident.account, EntityFolder.DRAFTS);
long rid = args.getLong("rid", -1);
String thread = args.getString("thread");
String to = args.getString("to"); String to = args.getString("to");
String cc = args.getString("cc"); String cc = args.getString("cc");
String bcc = args.getString("bcc"); String bcc = args.getString("bcc");
String body = args.getString("body");
String subject = args.getString("subject"); String subject = args.getString("subject");
String body = args.getString("body");
Address afrom[] = (ident == null ? null : new Address[]{new InternetAddress(ident.email, ident.name)}); // Get draft & selected identity
DB db = DB.getInstance(getContext());
EntityMessage draft = db.message().getMessage(id);
EntityIdentity identity = db.identity().getIdentity(iid);
// Convert data
Address afrom[] = (identity == null ? null : new Address[]{new InternetAddress(identity.email, identity.name)});
Address ato[] = (TextUtils.isEmpty(to) ? null : InternetAddress.parse(to)); Address ato[] = (TextUtils.isEmpty(to) ? null : InternetAddress.parse(to));
Address acc[] = (TextUtils.isEmpty(cc) ? null : InternetAddress.parse(cc)); Address acc[] = (TextUtils.isEmpty(cc) ? null : InternetAddress.parse(cc));
Address abcc[] = (TextUtils.isEmpty(bcc) ? null : InternetAddress.parse(bcc)); Address abcc[] = (TextUtils.isEmpty(bcc) ? null : InternetAddress.parse(bcc));
// Build draft // Update draft
boolean update = (draft != null); draft.identity = (identity == null ? null : identity.id);
if (draft == null)
draft = new EntityMessage();
draft.account = drafts.account;
draft.folder = drafts.id;
draft.identity = (ident == null ? null : ident.id);
draft.replying = (rid < 0 ? null : rid);
draft.thread = thread;
draft.from = afrom; draft.from = afrom;
draft.to = ato; draft.to = ato;
draft.cc = acc; draft.cc = acc;
@ -680,20 +632,15 @@ public class FragmentCompose extends FragmentEx {
draft.subject = subject; draft.subject = subject;
draft.body = "<pre>" + body.replaceAll("\\r?\\n", "<br />") + "</pre>"; draft.body = "<pre>" + body.replaceAll("\\r?\\n", "<br />") + "</pre>";
draft.received = new Date().getTime(); draft.received = new Date().getTime();
draft.seen = false;
draft.ui_seen = false;
draft.ui_hide = false;
// Store draft db.message().updateMessage(draft);
if (!update)
draft.id = message.insertMessage(draft);
// Check data // Check data
try { try {
db.beginTransaction(); db.beginTransaction();
if ("trash".equals(action)) { if (action == R.id.action_trash) {
EntityFolder trash = db.folder().getFolderByType(ident.account, EntityFolder.TRASH); EntityFolder trash = db.folder().getFolderByType(draft.account, EntityFolder.TRASH);
boolean move = (draft.uid != null); boolean move = (draft.uid != null);
if (move) if (move)
@ -706,7 +653,7 @@ public class FragmentCompose extends FragmentEx {
if (!move) if (!move)
EntityOperation.queue(db, draft, EntityOperation.ADD); EntityOperation.queue(db, draft, EntityOperation.ADD);
} else if ("save".equals(action)) { } else if (action == R.id.action_save) {
// Delete previous draft // Delete previous draft
draft.ui_hide = true; draft.ui_hide = true;
db.message().updateMessage(draft); db.message().updateMessage(draft);
@ -719,7 +666,11 @@ public class FragmentCompose extends FragmentEx {
draft.id = db.message().insertMessage(draft); draft.id = db.message().insertMessage(draft);
EntityOperation.queue(db, draft, EntityOperation.ADD); EntityOperation.queue(db, draft, EntityOperation.ADD);
} else if ("send".equals(action)) { } else if (action == R.id.action_send) {
// Check data
if (draft.identity == null)
throw new IllegalArgumentException(getContext().getString(R.string.title_from_missing));
if (draft.to == null && draft.cc == null && draft.bcc == null) if (draft.to == null && draft.cc == null && draft.bcc == null)
throw new IllegalArgumentException(getContext().getString(R.string.title_to_missing)); throw new IllegalArgumentException(getContext().getString(R.string.title_to_missing));
@ -730,7 +681,7 @@ public class FragmentCompose extends FragmentEx {
// Copy message to outbox // Copy message to outbox
draft.id = null; draft.id = null;
draft.folder = folder.getOutbox().id; draft.folder = db.folder().getOutbox().id;
draft.uid = null; draft.uid = null;
draft.ui_hide = false; draft.ui_hide = false;
draft.id = db.message().insertMessage(draft); draft.id = db.message().insertMessage(draft);
@ -766,19 +717,18 @@ public class FragmentCompose extends FragmentEx {
public void onLoadFinished(@NonNull Loader<Throwable> loader, Throwable ex) { public void onLoadFinished(@NonNull Loader<Throwable> loader, Throwable ex) {
LoaderManager.getInstance(FragmentCompose.this).destroyLoader(loader.getId()); LoaderManager.getInstance(FragmentCompose.this).destroyLoader(loader.getId());
Bundle args = ((PutLoader) loader).args;
String action = args.getString("action");
Log.i(Helper.TAG, "Put finished action=" + action + " ex=" + ex);
bottom_navigation.getMenu().setGroupEnabled(0, true); bottom_navigation.getMenu().setGroupEnabled(0, true);
if (ex == null) { if (ex == null) {
if ("trash".equals(action)) { Bundle args = ((PutLoader) loader).args;
int action = args.getInt("action");
if (action == R.id.action_trash) {
getFragmentManager().popBackStack(); getFragmentManager().popBackStack();
Toast.makeText(getContext(), R.string.title_draft_trashed, Toast.LENGTH_LONG).show(); Toast.makeText(getContext(), R.string.title_draft_trashed, Toast.LENGTH_LONG).show();
} else if ("save".equals(action)) } else if (action == R.id.action_save)
Toast.makeText(getContext(), R.string.title_draft_saved, Toast.LENGTH_LONG).show(); Toast.makeText(getContext(), R.string.title_draft_saved, Toast.LENGTH_LONG).show();
else if ("send".equals(action)) { else if (action == R.id.action_send) {
getFragmentManager().popBackStack(); getFragmentManager().popBackStack();
Toast.makeText(getContext(), R.string.title_queued, Toast.LENGTH_LONG).show(); Toast.makeText(getContext(), R.string.title_queued, Toast.LENGTH_LONG).show();
} }

@ -460,6 +460,7 @@ public class FragmentMessage extends FragmentEx {
if (result.ex == null) if (result.ex == null)
getContext().startActivity( getContext().startActivity(
new Intent(getContext(), ActivityCompose.class) new Intent(getContext(), ActivityCompose.class)
.putExtra("action", "edit")
.putExtra("id", (long) result.data)); .putExtra("id", (long) result.data));
else else
Toast.makeText(getContext(), result.ex.toString(), Toast.LENGTH_LONG).show(); Toast.makeText(getContext(), result.ex.toString(), Toast.LENGTH_LONG).show();
@ -469,14 +470,14 @@ public class FragmentMessage extends FragmentEx {
private void onActionForward(long id) { private void onActionForward(long id) {
startActivity(new Intent(getContext(), ActivityCompose.class) startActivity(new Intent(getContext(), ActivityCompose.class)
.putExtra("id", id) .putExtra("action", "forward")
.putExtra("action", "forward")); .putExtra("reference", id));
} }
private void onActionReplyAll(long id) { private void onActionReplyAll(long id) {
startActivity(new Intent(getContext(), ActivityCompose.class) startActivity(new Intent(getContext(), ActivityCompose.class)
.putExtra("id", id) .putExtra("action", "reply_all")
.putExtra("action", "reply_all")); .putExtra("reference", id));
} }
private void onActionSpam(final long id) { private void onActionSpam(final long id) {
@ -711,8 +712,8 @@ public class FragmentMessage extends FragmentEx {
private void onActionReply(long id) { private void onActionReply(long id) {
startActivity(new Intent(getContext(), ActivityCompose.class) startActivity(new Intent(getContext(), ActivityCompose.class)
.putExtra("id", id) .putExtra("action", "reply")
.putExtra("action", "reply")); .putExtra("reference", id));
} }
private static class MoveLoader extends AsyncTaskLoader<List<EntityFolder>> { private static class MoveLoader extends AsyncTaskLoader<List<EntityFolder>> {

@ -28,6 +28,7 @@ import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.ProgressBar; import android.widget.ProgressBar;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast;
import com.google.android.material.floatingactionbutton.FloatingActionButton; import com.google.android.material.floatingactionbutton.FloatingActionButton;
@ -83,7 +84,10 @@ public class FragmentMessages extends FragmentEx {
fab.setOnClickListener(new View.OnClickListener() { fab.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View view) { public void onClick(View view) {
startActivity(new Intent(getContext(), ActivityCompose.class)); startActivity(new Intent(getContext(), ActivityCompose.class)
.putExtra("action", "new")
.putExtra("account", (Long) fab.getTag())
);
} }
}); });
@ -91,6 +95,7 @@ public class FragmentMessages extends FragmentEx {
tvNoEmail.setVisibility(View.GONE); tvNoEmail.setVisibility(View.GONE);
grpReady.setVisibility(View.GONE); grpReady.setVisibility(View.GONE);
pbWait.setVisibility(View.VISIBLE); pbWait.setVisibility(View.VISIBLE);
fab.setVisibility(View.GONE);
return view; return view;
} }
@ -126,7 +131,6 @@ public class FragmentMessages extends FragmentEx {
messages = new LivePagedListBuilder<>(db.message().pagedThread(thread, debug), PAGE_SIZE).build(); messages = new LivePagedListBuilder<>(db.message().pagedThread(thread, debug), PAGE_SIZE).build();
} }
Log.i(Helper.TAG, "Observing messages");
messages.observe(getViewLifecycleOwner(), new Observer<PagedList<TupleMessageEx>>() { messages.observe(getViewLifecycleOwner(), new Observer<PagedList<TupleMessageEx>>() {
@Override @Override
public void onChanged(@Nullable PagedList<TupleMessageEx> messages) { public void onChanged(@Nullable PagedList<TupleMessageEx> messages) {
@ -145,5 +149,32 @@ public class FragmentMessages extends FragmentEx {
} }
} }
}); });
new SimpleLoader() {
@Override
public Object onLoad(Bundle args) throws Throwable {
long folder = (args == null ? -1 : args.getLong("folder", -1));
long thread = (args == null ? -1 : args.getLong("thread", -1)); // message ID
DB db = DB.getInstance(getContext());
if (thread < 0)
if (folder < 0)
return db.folder().getPrimaryDrafts().account;
else
return db.folder().getFolder(folder).account;
else
return db.message().getMessage(thread).account;
}
@Override
public void onLoaded(Bundle args, Result result) {
if (result.ex == null) {
fab.setTag(result.data);
fab.setVisibility(View.VISIBLE);
} else
Toast.makeText(getContext(), result.ex.toString(), Toast.LENGTH_LONG).show();
}
}.load(this, ActivityView.LOADER_MESSAGE_ACCOUNT, getArguments());
} }
} }

Loading…
Cancel
Save