From 8306b0eb506b979e08e14439d76948c3a16a3422 Mon Sep 17 00:00:00 2001 From: M66B Date: Fri, 25 Jan 2019 15:41:00 +0000 Subject: [PATCH] Fixed identity selection changing, refactoring --- .../java/eu/faircode/email/DaoIdentity.java | 16 +++++------ .../eu/faircode/email/FragmentCompose.java | 27 +++++++++++++------ .../eu/faircode/email/FragmentIdentities.java | 2 +- .../java/eu/faircode/email/FragmentSetup.java | 4 +-- .../eu/faircode/email/TupleIdentityEx.java | 10 +++++++ 5 files changed, 38 insertions(+), 21 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/DaoIdentity.java b/app/src/main/java/eu/faircode/email/DaoIdentity.java index fb45bc2075..41efc41b03 100644 --- a/app/src/main/java/eu/faircode/email/DaoIdentity.java +++ b/app/src/main/java/eu/faircode/email/DaoIdentity.java @@ -30,18 +30,14 @@ import androidx.room.Update; @Dao public interface DaoIdentity { @Query("SELECT identity.*, account.name AS accountName FROM identity" + - " JOIN account ON account.id = identity.account") - LiveData> liveIdentities(); - - @Query("SELECT identity.* FROM identity" + " JOIN account ON account.id = identity.account" + - " WHERE (:account IS NULL OR account.id = :account)" + - " AND account.synchronize = :synchronize" + - " AND identity.synchronize = :synchronize") - LiveData> liveIdentities(Long account, boolean synchronize); + " WHERE NOT :synchronize OR account.synchronize") + LiveData> liveIdentities(boolean synchronize); - @Query("SELECT * FROM identity") - List getIdentities(); + @Query("SELECT identity.*, account.name AS accountName FROM identity" + + " JOIN account ON account.id = identity.account" + + " WHERE NOT :synchronize OR account.synchronize") + List getIdentities(boolean synchronize); @Query("SELECT * FROM identity WHERE account = :account") List getIdentities(long account); diff --git a/app/src/main/java/eu/faircode/email/FragmentCompose.java b/app/src/main/java/eu/faircode/email/FragmentCompose.java index 0d773b9e04..05c844adbb 100644 --- a/app/src/main/java/eu/faircode/email/FragmentCompose.java +++ b/app/src/main/java/eu/faircode/email/FragmentCompose.java @@ -1379,7 +1379,7 @@ public class FragmentCompose extends FragmentBase { if ("edit".equals(action)) throw new IllegalStateException("Draft not found hide=" + (result.draft != null)); - List identities = db.identity().getIdentities(); + List identities = db.identity().getIdentities(true); EntityMessage ref = db.message().getMessage(reference); if (ref == null) { @@ -1640,16 +1640,14 @@ public class FragmentCompose extends FragmentBase { getActivity().invalidateOptionsMenu(); - DB db = DB.getInstance(getContext()); - - db.identity().liveIdentities().observe(getViewLifecycleOwner(), new Observer>() { + new SimpleTask>() { @Override - public void onChanged(List identities) { + protected List onExecute(Context context, Bundle args) { + DB db = DB.getInstance(context); + List identities = db.identity().getIdentities(true); if (identities == null) identities = new ArrayList<>(); - Log.i("Set identities=" + identities.size()); - // Sort identities Collections.sort(identities, new Comparator() { @Override @@ -1664,6 +1662,13 @@ public class FragmentCompose extends FragmentBase { } }); + return identities; + } + + @Override + protected void onExecuted(Bundle args, List identities) { + Log.i("Set identities=" + identities.size()); + // Show identities IdentityAdapter adapter = new IdentityAdapter(getContext(), identities); adapter.setDropDownViewResource(R.layout.spinner_item1_dropdown); @@ -1677,9 +1682,15 @@ public class FragmentCompose extends FragmentBase { break; } } + } + @Override + protected void onException(Bundle args, Throwable ex) { + Helper.unexpectedError(getContext(), getViewLifecycleOwner(), ex); } - }); + }.execute(FragmentCompose.this, new Bundle(), "compose:identities"); + + DB db = DB.getInstance(getContext()); db.attachment().liveAttachments(result.draft.id).observe(getViewLifecycleOwner(), new Observer>() { diff --git a/app/src/main/java/eu/faircode/email/FragmentIdentities.java b/app/src/main/java/eu/faircode/email/FragmentIdentities.java index d58a590a60..0eb5eb3a43 100644 --- a/app/src/main/java/eu/faircode/email/FragmentIdentities.java +++ b/app/src/main/java/eu/faircode/email/FragmentIdentities.java @@ -104,7 +104,7 @@ public class FragmentIdentities extends FragmentBase { super.onActivityCreated(savedInstanceState); // Observe identities - DB.getInstance(getContext()).identity().liveIdentities().observe(getViewLifecycleOwner(), new Observer>() { + DB.getInstance(getContext()).identity().liveIdentities(false).observe(getViewLifecycleOwner(), new Observer>() { @Override public void onChanged(@Nullable List identities) { if (identities == null) diff --git a/app/src/main/java/eu/faircode/email/FragmentSetup.java b/app/src/main/java/eu/faircode/email/FragmentSetup.java index f203592cfb..0513cff591 100644 --- a/app/src/main/java/eu/faircode/email/FragmentSetup.java +++ b/app/src/main/java/eu/faircode/email/FragmentSetup.java @@ -309,9 +309,9 @@ public class FragmentSetup extends FragmentBase { } }); - db.identity().liveIdentities(null, true).observe(getViewLifecycleOwner(), new Observer>() { + db.identity().liveIdentities(true).observe(getViewLifecycleOwner(), new Observer>() { @Override - public void onChanged(@Nullable List identities) { + public void onChanged(@Nullable List identities) { boolean done = (identities != null && identities.size() > 0); tvIdentityDone.setText(done ? R.string.title_setup_done : R.string.title_setup_to_do); tvIdentityDone.setTextColor(done ? textColorPrimary : colorWarning); diff --git a/app/src/main/java/eu/faircode/email/TupleIdentityEx.java b/app/src/main/java/eu/faircode/email/TupleIdentityEx.java index 9c322ce551..cec7c4bfbc 100644 --- a/app/src/main/java/eu/faircode/email/TupleIdentityEx.java +++ b/app/src/main/java/eu/faircode/email/TupleIdentityEx.java @@ -21,4 +21,14 @@ package eu.faircode.email; public class TupleIdentityEx extends EntityIdentity { public String accountName; + + @Override + public boolean equals(Object obj) { + if (obj instanceof TupleIdentityEx) { + TupleIdentityEx other = (TupleIdentityEx) obj; + return (super.equals(obj) && + (this.accountName == null ? other.accountName == null : accountName.equals(other.accountName))); + } else + return false; + } }