Moved observers and loaders to onActivityCreate

pull/30/head
M66B 6 years ago
parent b0d8bc9c10
commit ab5f432978

@ -188,6 +188,17 @@ public class FragmentAccount extends FragmentEx {
pbCheck.setVisibility(View.GONE);
ibDelete.setVisibility(id < 0 ? View.GONE : View.VISIBLE);
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// Get arguments
Bundle args = getArguments();
long id = (args == null ? -1 : args.getLong("id", -1));
// Observe
DB.getInstance(getContext()).account().liveAccount(id).observe(this, new Observer<EntityAccount>() {
@Override
@ -202,8 +213,6 @@ public class FragmentAccount extends FragmentEx {
cbPrimary.setEnabled(account == null ? true : account.synchronize);
}
});
return view;
}
private static class PutLoader extends AsyncTaskLoader<Throwable> {

@ -80,6 +80,13 @@ public class FragmentAccounts extends FragmentEx {
grpReady.setVisibility(View.GONE);
pbWait.setVisibility(View.VISIBLE);
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// Observe accounts
DB.getInstance(getContext()).account().liveAccounts().observe(this, new Observer<List<EntityAccount>>() {
@Override
@ -90,7 +97,5 @@ public class FragmentAccounts extends FragmentEx {
grpReady.setVisibility(View.VISIBLE);
}
});
return view;
}
}

@ -93,7 +93,7 @@ public class FragmentCompose extends FragmentEx {
// 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);
final long id = (TextUtils.isEmpty(action) ? (args == null ? -1 : args.getLong("id", -1)) : -1);
// Get controls
spFrom = view.findViewById(R.id.spFrom);
@ -117,7 +117,7 @@ public class FragmentCompose extends FragmentEx {
@Override
public void onClick(View view) {
Bundle args = new Bundle();
args.putLong("id" , -1);
args.putLong("id", -1);
FragmentIdentity fragment = new FragmentIdentity();
fragment.setArguments(args);
@ -181,7 +181,12 @@ public class FragmentCompose extends FragmentEx {
pbWait.setVisibility(View.VISIBLE);
bottom_navigation.getMenu().setGroupEnabled(0, false);
final Handler handler = new Handler();
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
DB.getInstance(getContext()).identity().liveIdentities(true).observe(FragmentCompose.this, new Observer<List<EntityIdentity>>() {
@Override
@ -207,19 +212,10 @@ public class FragmentCompose extends FragmentEx {
spFrom.setVisibility(View.VISIBLE);
ivIdentityAdd.setVisibility(View.VISIBLE);
// For some reason this is needed to make sure the loader starts
handler.post(new Runnable() {
@Override
public void run() {
// Get might select another identity
getLoaderManager().restartLoader(ActivityCompose.LOADER_COMPOSE_GET, getArguments(), getLoaderCallbacks).forceLoad();
}
});
// Get might select another identity
getLoaderManager().restartLoader(ActivityCompose.LOADER_COMPOSE_GET, getArguments(), getLoaderCallbacks).forceLoad();
}
});
return view;
}
@Override
@ -298,16 +294,16 @@ public class FragmentCompose extends FragmentEx {
EntityIdentity identity = (EntityIdentity) spFrom.getSelectedItem();
Bundle args = new Bundle();
args.putLong("id" , id);
args.putLong("iid" , identity == null ? -1 : identity.id);
args.putString("thread" , FragmentCompose.this.thread);
args.putLong("rid" , FragmentCompose.this.rid);
args.putString("to" , etTo.getText().toString());
args.putString("cc" , etCc.getText().toString());
args.putString("bcc" , etBcc.getText().toString());
args.putString("subject" , etSubject.getText().toString());
args.putString("body" , etBody.getText().toString());
args.putString("action" , action);
args.putLong("id", id);
args.putLong("iid", identity == null ? -1 : identity.id);
args.putString("thread", FragmentCompose.this.thread);
args.putLong("rid", FragmentCompose.this.rid);
args.putString("to", etTo.getText().toString());
args.putString("cc", etCc.getText().toString());
args.putString("bcc", etBcc.getText().toString());
args.putString("subject", etSubject.getText().toString());
args.putString("body", etBody.getText().toString());
args.putString("action", action);
getLoaderManager().restartLoader(ActivityCompose.LOADER_COMPOSE_PUT, args, putLoaderCallbacks).forceLoad();
}
@ -329,35 +325,35 @@ public class FragmentCompose extends FragmentEx {
Bundle result = new Bundle();
try {
String action = args.getString("action");
long id = args.getLong("id" , -1);
long id = args.getLong("id", -1);
EntityMessage msg = DB.getInstance(getContext()).message().getMessage(id);
result.putString("action" , action);
result.putString("action", action);
if (msg != null) {
if (msg.identity != null)
result.putLong("iid" , msg.identity);
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);
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)) {
if (msg != null) {
result.putSerializable("from" , msg.from);
result.putSerializable("to" , msg.to);
result.putSerializable("from", msg.from);
result.putSerializable("to", msg.to);
}
} else if ("reply".equals(action)) {
Address[] to = null;
if (msg != null)
to = (msg.reply == null || msg.reply.length == 0 ? msg.from : msg.reply);
result.putLong("rid" , msg.id);
result.putSerializable("from" , msg.to);
result.putSerializable("to" , to);
result.putLong("rid", msg.id);
result.putSerializable("from", msg.to);
result.putSerializable("to", to);
} else if ("reply_all".equals(action)) {
Address[] to = null;
if (msg != null) {
@ -370,15 +366,15 @@ public class FragmentCompose extends FragmentEx {
addresses.addAll(Arrays.asList(msg.cc));
to = addresses.toArray(new Address[0]);
}
result.putLong("rid" , msg.id);
result.putSerializable("from" , msg.to);
result.putSerializable("to" , to);
result.putLong("rid", msg.id);
result.putSerializable("from", msg.to);
result.putSerializable("to", to);
} else if ("forward".equals(action)) {
Address[] to = null;
if (msg != null)
to = (msg.reply == null || msg.reply.length == 0 ? msg.from : msg.reply);
result.putSerializable("from" , msg.to);
result.putSerializable("to" , to);
result.putSerializable("from", msg.to);
result.putSerializable("to", to);
}
} catch (Throwable ex) {
Log.e(Helper.TAG, ex + "\n" + Log.getStackTraceString(ex));
@ -400,8 +396,8 @@ public class FragmentCompose extends FragmentEx {
public void onLoadFinished(@NonNull Loader<Bundle> loader, Bundle result) {
getLoaderManager().destroyLoader(loader.getId());
long iid = result.getLong("iid" , -1);
long rid = result.getLong("rid" , -1);
long iid = result.getLong("iid", -1);
long rid = result.getLong("rid", -1);
String thread = result.getString("thread");
Address[] from = (Address[]) result.getSerializable("from");
Address[] to = (Address[]) result.getSerializable("to");
@ -437,11 +433,11 @@ public class FragmentCompose extends FragmentEx {
Handler handler = new Handler();
etCc.setText(cc == null ? null : TextUtils.join(", " , cc));
etBcc.setText(bcc == null ? null : TextUtils.join(", " , bcc));
etCc.setText(cc == null ? null : TextUtils.join(", ", cc));
etBcc.setText(bcc == null ? null : TextUtils.join(", ", bcc));
if (action == null) {
etTo.setText(to == null ? null : TextUtils.join(", " , to));
etTo.setText(to == null ? null : TextUtils.join(", ", to));
etSubject.setText(subject);
if (body != null)
etBody.setText(Html.fromHtml(HtmlHelper.sanitize(getContext(), body, false)));
@ -452,10 +448,10 @@ public class FragmentCompose extends FragmentEx {
}
});
} 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" ,
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)),
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));
@ -466,9 +462,9 @@ public class FragmentCompose extends FragmentEx {
}
});
} else if ("forward".equals(action)) {
String text = String.format("<br><br>%s %s:<br><br>%s" ,
String text = String.format("<br><br>%s %s:<br><br>%s",
Html.escapeHtml(new Date().toString()),
Html.escapeHtml(to == null ? "" : TextUtils.join(", " , to)),
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));
@ -519,7 +515,7 @@ public class FragmentCompose extends FragmentEx {
if (drafts == null)
throw new Throwable(getContext().getString(R.string.title_no_primary_drafts));
long rid = args.getLong("rid" , -1);
long rid = args.getLong("rid", -1);
String thread = args.getString("thread");
String to = args.getString("to");
String cc = args.getString("cc");
@ -546,7 +542,7 @@ public class FragmentCompose extends FragmentEx {
draft.cc = acc;
draft.bcc = abcc;
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.seen = false;
draft.ui_seen = false;

@ -73,9 +73,9 @@ public class FragmentFolder extends FragmentEx {
pbSave.setVisibility(View.VISIBLE);
Bundle args = new Bundle();
args.putLong("id" , id);
args.putBoolean("synchronize" , cbSynchronize.isChecked());
args.putString("after" , etAfter.getText().toString());
args.putLong("id", id);
args.putBoolean("synchronize", cbSynchronize.isChecked());
args.putString("after", etAfter.getText().toString());
getLoaderManager().restartLoader(ActivityView.LOADER_FOLDER_PUT, args, putLoaderCallbacks).forceLoad();
}
@ -86,6 +86,18 @@ public class FragmentFolder extends FragmentEx {
grpReady.setVisibility(View.GONE);
pbWait.setVisibility(View.VISIBLE);
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// Get arguments
Bundle args = getArguments();
long id = (args == null ? -1 : args.getLong("id"));
// Observe
DB.getInstance(getContext()).folder().liveFolder(id).observe(this, new Observer<EntityFolder>() {
@Override
@ -99,8 +111,6 @@ public class FragmentFolder extends FragmentEx {
grpReady.setVisibility(View.VISIBLE);
}
});
return view;
}
private static class PutLoader extends AsyncTaskLoader<Throwable> {

@ -48,10 +48,6 @@ public class FragmentFolders extends FragmentEx {
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_folders, container, false);
// Get arguments
Bundle args = getArguments();
long account = (args == null ? -1 : args.getLong("account"));
// Get controls
rvFolder = view.findViewById(R.id.rvFolder);
pbWait = view.findViewById(R.id.pbWait);
@ -78,6 +74,17 @@ public class FragmentFolders extends FragmentEx {
grpReady.setVisibility(View.GONE);
pbWait.setVisibility(View.VISIBLE);
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// Get arguments
Bundle args = getArguments();
long account = (args == null ? -1 : args.getLong("account"));
DB db = DB.getInstance(getContext());
// Observe account
@ -100,8 +107,7 @@ public class FragmentFolders extends FragmentEx {
});
// Show hint
// TODO: find better solution
Toast.makeText(getContext(), R.string.title_item_edit_hint, Toast.LENGTH_SHORT).show();
return view;
}
}

@ -80,6 +80,13 @@ public class FragmentIdentities extends FragmentEx {
grpReady.setVisibility(View.GONE);
pbWait.setVisibility(View.VISIBLE);
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// Observe identities
DB.getInstance(getContext()).identity().liveIdentities().observe(this, new Observer<List<EntityIdentity>>() {
@Override
@ -90,7 +97,5 @@ public class FragmentIdentities extends FragmentEx {
grpReady.setVisibility(View.VISIBLE);
}
});
return view;
}
}

@ -87,7 +87,7 @@ public class FragmentIdentity extends FragmentEx {
// Get arguments
Bundle args = getArguments();
final long id = (args == null ? -1 : args.getLong("id" , -1));
final long id = (args == null ? -1 : args.getLong("id", -1));
// Get providers
providers = Provider.loadProfiles(getContext());
@ -167,17 +167,17 @@ public class FragmentIdentity extends FragmentEx {
pbCheck.setVisibility(View.VISIBLE);
Bundle args = new Bundle();
args.putLong("id" , id);
args.putString("name" , etName.getText().toString());
args.putString("email" , etEmail.getText().toString());
args.putString("replyto" , etReplyTo.getText().toString());
args.putString("host" , etHost.getText().toString());
args.putBoolean("starttls" , cbStartTls.isChecked());
args.putString("port" , etPort.getText().toString());
args.putString("user" , etUser.getText().toString());
args.putString("password" , tilPassword.getEditText().getText().toString());
args.putBoolean("synchronize" , cbSynchronize.isChecked());
args.putBoolean("primary" , cbPrimary.isChecked());
args.putLong("id", id);
args.putString("name", etName.getText().toString());
args.putString("email", etEmail.getText().toString());
args.putString("replyto", etReplyTo.getText().toString());
args.putString("host", etHost.getText().toString());
args.putBoolean("starttls", cbStartTls.isChecked());
args.putString("port", etPort.getText().toString());
args.putString("user", etUser.getText().toString());
args.putString("password", tilPassword.getEditText().getText().toString());
args.putBoolean("synchronize", cbSynchronize.isChecked());
args.putBoolean("primary", cbPrimary.isChecked());
getLoaderManager().restartLoader(ActivityView.LOADER_IDENTITY_PUT, args, putLoaderCallbacks).forceLoad();
}
@ -216,6 +216,17 @@ public class FragmentIdentity extends FragmentEx {
pbCheck.setVisibility(View.GONE);
ibDelete.setVisibility(id < 0 ? View.GONE : View.VISIBLE);
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// Get arguments
Bundle args = getArguments();
long id = (args == null ? -1 : args.getLong("id", -1));
// Observer
DB.getInstance(getContext()).identity().liveIdentity(id).observe(this, new Observer<EntityIdentity>() {
@Override
@ -233,8 +244,6 @@ public class FragmentIdentity extends FragmentEx {
cbPrimary.setEnabled(identity == null ? true : identity.synchronize);
}
});
return view;
}
private static class PutLoader extends AsyncTaskLoader<Throwable> {

@ -222,6 +222,17 @@ public class FragmentMessage extends FragmentEx {
adapter = new AdapterAttachment(getContext());
rvAttachment.setAdapter(adapter);
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// Get arguments
Bundle args = getArguments();
final long id = (args == null ? -1 : args.getLong("id"));
final DB db = DB.getInstance(getContext());
// Observe message
@ -254,8 +265,8 @@ public class FragmentMessage extends FragmentEx {
tvFrom.setTextColor(colorUnseen);
tvTime.setTextColor(colorUnseen);
DB.getInstance(getContext()).attachment().liveAttachments(id).removeObservers(FragmentMessage.this);
DB.getInstance(getContext()).attachment().liveAttachments(id).observe(FragmentMessage.this,
db.attachment().liveAttachments(id).removeObservers(FragmentMessage.this);
db.attachment().liveAttachments(id).observe(FragmentMessage.this,
new Observer<List<TupleAttachment>>() {
@Override
public void onChanged(@Nullable List<TupleAttachment> attachments) {
@ -313,8 +324,6 @@ public class FragmentMessage extends FragmentEx {
grpReady.setVisibility(View.VISIBLE);
}
});
return view;
}
@Override

@ -61,8 +61,7 @@ public class FragmentMessages extends FragmentEx {
// Get arguments
Bundle args = getArguments();
long folder = (args == null ? -1 : args.getLong("folder" , -1));
long thread = (args == null ? -1 : args.getLong("thread" , -1)); // message ID
long thread = (args == null ? -1 : args.getLong("thread", -1)); // message ID
// Get controls
rvMessage = view.findViewById(R.id.rvFolder);
@ -96,10 +95,22 @@ public class FragmentMessages extends FragmentEx {
pbWait.setVisibility(View.VISIBLE);
fab.setVisibility(View.GONE);
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// Get arguments
Bundle args = getArguments();
long folder = (args == null ? -1 : args.getLong("folder", -1));
long thread = (args == null ? -1 : args.getLong("thread", -1)); // message ID
// Observe folder/messages
DB db = DB.getInstance(getContext());
LiveData<PagedList<TupleMessageEx>> messages;
boolean debug = PreferenceManager.getDefaultSharedPreferences(getContext()).getBoolean("debug" , false);
boolean debug = PreferenceManager.getDefaultSharedPreferences(getContext()).getBoolean("debug", false);
if (thread < 0)
if (folder < 0) {
setSubtitle(R.string.title_folder_unified);
@ -118,9 +129,11 @@ public class FragmentMessages extends FragmentEx {
messages = new LivePagedListBuilder<>(db.message().pagedThread(thread, debug), PAGE_SIZE).build();
}
Log.i(Helper.TAG, "Observing messages");
messages.observe(this, new Observer<PagedList<TupleMessageEx>>() {
@Override
public void onChanged(@Nullable PagedList<TupleMessageEx> messages) {
Log.i(Helper.TAG, "Submit messages=" + messages.size());
adapter.submitList(messages);
pbWait.setVisibility(View.GONE);
@ -137,8 +150,6 @@ public class FragmentMessages extends FragmentEx {
});
getLoaderManager().restartLoader(ActivityView.LOADER_MESSAGES_INIT, new Bundle(), initLoaderCallbacks).forceLoad();
return view;
}
private static class InitLoader extends AsyncTaskLoader<Bundle> {
@ -152,10 +163,10 @@ public class FragmentMessages extends FragmentEx {
Bundle result = new Bundle();
try {
EntityFolder drafts = DB.getInstance(getContext()).folder().getPrimaryFolder(EntityFolder.TYPE_DRAFTS);
result.putBoolean("drafts" , drafts != null);
result.putBoolean("drafts", drafts != null);
} catch (Throwable ex) {
Log.e(Helper.TAG, ex + "\n" + Log.getStackTraceString(ex));
result.putBoolean("drafts" , false);
result.putBoolean("drafts", false);
}
return result;
}
@ -170,7 +181,7 @@ public class FragmentMessages extends FragmentEx {
@Override
public void onLoadFinished(@NonNull Loader<Bundle> loader, Bundle data) {
fab.setVisibility(data.getBoolean("drafts" , false) ? View.VISIBLE : View.GONE);
fab.setVisibility(data.getBoolean("drafts", false) ? View.VISIBLE : View.GONE);
}
@Override

@ -114,7 +114,7 @@ public class FragmentSetup extends FragmentEx {
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
String theme = prefs.getString("theme" , "light");
String theme = prefs.getString("theme", "light");
boolean dark = "dark".equals(theme);
cbDarkTheme.setTag(dark);
cbDarkTheme.setChecked(dark);
@ -124,16 +124,16 @@ public class FragmentSetup extends FragmentEx {
if (checked != (Boolean) button.getTag()) {
button.setTag(checked);
cbDarkTheme.setChecked(checked);
prefs.edit().putString("theme" , checked ? "dark" : "light").apply();
prefs.edit().putString("theme", checked ? "dark" : "light").apply();
}
}
});
cbDebug.setChecked(prefs.getBoolean("debug" , false));
cbDebug.setChecked(prefs.getBoolean("debug", false));
cbDebug.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("debug" , checked).apply();
prefs.edit().putBoolean("debug", checked).apply();
}
});
@ -145,22 +145,6 @@ public class FragmentSetup extends FragmentEx {
tvIdentityDone.setVisibility(View.INVISIBLE);
tvPermissionsDone.setVisibility(View.INVISIBLE);
final DB db = DB.getInstance(getContext());
db.account().liveAccounts(true).observe(this, new Observer<List<EntityAccount>>() {
@Override
public void onChanged(@Nullable List<EntityAccount> accounts) {
tvAccountDone.setVisibility(accounts.size() > 0 ? View.VISIBLE : View.INVISIBLE);
}
});
db.identity().liveIdentities(true).observe(this, new Observer<List<EntityIdentity>>() {
@Override
public void onChanged(@Nullable List<EntityIdentity> identities) {
tvIdentityDone.setVisibility(identities.size() > 0 ? View.VISIBLE : View.INVISIBLE);
}
});
int[] grantResults = new int[permissions.length];
for (int i = 0; i < permissions.length; i++)
grantResults[i] = ContextCompat.checkSelfPermission(getActivity(), permissions[i]);
@ -171,6 +155,7 @@ public class FragmentSetup extends FragmentEx {
executor.submit(new Runnable() {
@Override
public void run() {
DB db = DB.getInstance(getContext());
EntityFolder outbox = db.folder().getOutbox();
if (outbox == null) {
outbox = new EntityFolder();
@ -186,6 +171,27 @@ public class FragmentSetup extends FragmentEx {
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
DB db = DB.getInstance(getContext());
db.account().liveAccounts(true).observe(this, new Observer<List<EntityAccount>>() {
@Override
public void onChanged(@Nullable List<EntityAccount> accounts) {
tvAccountDone.setVisibility(accounts.size() > 0 ? View.VISIBLE : View.INVISIBLE);
}
});
db.identity().liveIdentities(true).observe(this, new Observer<List<EntityIdentity>>() {
@Override
public void onChanged(@Nullable List<EntityIdentity> identities) {
tvIdentityDone.setVisibility(identities.size() > 0 ? View.VISIBLE : View.INVISIBLE);
}
});
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
boolean has = (grantResults.length > 0);

Loading…
Cancel
Save