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;
}
}

@ -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,20 +212,11 @@ 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();
}
});
}
});
return view;
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {

@ -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;
}
}

@ -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,7 +61,6 @@ 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
// Get controls
@ -96,6 +95,18 @@ 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;
@ -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> {

@ -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