From 3b2ee071f72243c60ff718bdf4c0d83b72e8895c Mon Sep 17 00:00:00 2001 From: M66B Date: Sun, 16 Dec 2018 17:39:15 +0100 Subject: [PATCH] Show unread count per account --- .../java/eu/faircode/email/ActivityMain.java | 4 +-- .../java/eu/faircode/email/ActivitySetup.java | 4 +-- .../java/eu/faircode/email/ActivityView.java | 22 ++++++++---- .../java/eu/faircode/email/AdapterFolder.java | 2 +- .../java/eu/faircode/email/DaoAccount.java | 14 ++++++-- .../eu/faircode/email/FragmentCompose.java | 10 +++--- .../eu/faircode/email/FragmentMessages.java | 4 +-- .../java/eu/faircode/email/FragmentSetup.java | 4 +-- .../eu/faircode/email/TupleAccountEx.java | 34 +++++++++++++++++++ app/src/main/res/values/strings.xml | 2 +- 10 files changed, 77 insertions(+), 23 deletions(-) create mode 100644 app/src/main/java/eu/faircode/email/TupleAccountEx.java diff --git a/app/src/main/java/eu/faircode/email/ActivityMain.java b/app/src/main/java/eu/faircode/email/ActivityMain.java index 271982f14f..7d26a05cd4 100644 --- a/app/src/main/java/eu/faircode/email/ActivityMain.java +++ b/app/src/main/java/eu/faircode/email/ActivityMain.java @@ -48,9 +48,9 @@ public class ActivityMain extends AppCompatActivity implements FragmentManager.O if (prefs.getBoolean("eula", false)) { super.onCreate(savedInstanceState); - DB.getInstance(this).account().liveAccounts(true).observe(this, new Observer>() { + DB.getInstance(this).account().liveAccounts(true).observe(this, new Observer>() { @Override - public void onChanged(@Nullable List accounts) { + public void onChanged(@Nullable List accounts) { if (accounts == null || accounts.size() == 0) startActivity(new Intent(ActivityMain.this, ActivitySetup.class)); else { diff --git a/app/src/main/java/eu/faircode/email/ActivitySetup.java b/app/src/main/java/eu/faircode/email/ActivitySetup.java index b75ed2303e..6763d7b9f4 100644 --- a/app/src/main/java/eu/faircode/email/ActivitySetup.java +++ b/app/src/main/java/eu/faircode/email/ActivitySetup.java @@ -65,9 +65,9 @@ public class ActivitySetup extends ActivityBilling implements FragmentManager.On fragmentTransaction.commit(); } - DB.getInstance(this).account().liveAccounts(true).observe(this, new Observer>() { + DB.getInstance(this).account().liveAccounts(true).observe(this, new Observer>() { @Override - public void onChanged(List accounts) { + public void onChanged(List accounts) { hasAccount = (accounts != null && accounts.size() > 0); } }); diff --git a/app/src/main/java/eu/faircode/email/ActivityView.java b/app/src/main/java/eu/faircode/email/ActivityView.java index d64187d917..3c2550c423 100644 --- a/app/src/main/java/eu/faircode/email/ActivityView.java +++ b/app/src/main/java/eu/faircode/email/ActivityView.java @@ -248,9 +248,9 @@ public class ActivityView extends ActivityBilling implements FragmentManager.OnB getSupportFragmentManager().addOnBackStackChangedListener(this); - DB.getInstance(this).account().liveAccounts(true).observe(this, new Observer>() { + DB.getInstance(this).account().liveAccounts(true).observe(this, new Observer>() { @Override - public void onChanged(@Nullable List accounts) { + public void onChanged(@Nullable List accounts) { if (accounts == null) accounts = new ArrayList<>(); @@ -266,11 +266,14 @@ public class ActivityView extends ActivityBilling implements FragmentManager.OnB } }); - for (EntityAccount account : accounts) + for (TupleAccountEx account : accounts) drawerArray.add(new DrawerItem( R.layout.item_drawer, -1, "connected".equals(account.state) ? R.drawable.baseline_folder_24 : R.drawable.baseline_folder_open_24, - account.color, account.name, account.id)); + account.color, + account.unseen > 0 ? getString(R.string.title_unseen_count, account.name, account.unseen) : account.name, + account.unseen > 0, + account.id)); drawerArray.add(new DrawerItem(R.layout.item_drawer_separator)); @@ -1129,6 +1132,7 @@ public class ActivityView extends ActivityBilling implements FragmentManager.OnB private int icon; private Integer color; private String title; + private boolean highlight; private Object data; DrawerItem(int layout) { @@ -1143,12 +1147,13 @@ public class ActivityView extends ActivityBilling implements FragmentManager.OnB this.title = context.getString(title); } - DrawerItem(int layout, int id, int icon, Integer color, String title, Object data) { + DrawerItem(int layout, int id, int icon, Integer color, String title, boolean highlight, Object data) { this.layout = layout; this.id = id; this.icon = icon; this.color = color; this.title = title; + this.highlight = highlight; this.data = data; } @@ -1179,9 +1184,14 @@ public class ActivityView extends ActivityBilling implements FragmentManager.OnB if (item.color != null) iv.setColorFilter(item.color); } - if (tv != null) + + if (tv != null) { tv.setText(item.title); + tv.setTextColor(Helper.resolveColor(getContext(), + item.highlight ? R.attr.colorUnread : android.R.attr.textColorSecondary)); + } + return row; } } diff --git a/app/src/main/java/eu/faircode/email/AdapterFolder.java b/app/src/main/java/eu/faircode/email/AdapterFolder.java index 5df8cfea4f..539ebbd94e 100644 --- a/app/src/main/java/eu/faircode/email/AdapterFolder.java +++ b/app/src/main/java/eu/faircode/email/AdapterFolder.java @@ -155,7 +155,7 @@ public class AdapterFolder extends RecyclerView.Adapter 0) - tvName.setText(context.getString(R.string.title_folder_unseen, name, folder.unseen)); + tvName.setText(context.getString(R.string.title_unseen_count, name, folder.unseen)); else tvName.setText(name); tvName.setTypeface(null, folder.unseen > 0 ? Typeface.BOLD : Typeface.NORMAL); diff --git a/app/src/main/java/eu/faircode/email/DaoAccount.java b/app/src/main/java/eu/faircode/email/DaoAccount.java index 95d13510b1..67c2839711 100644 --- a/app/src/main/java/eu/faircode/email/DaoAccount.java +++ b/app/src/main/java/eu/faircode/email/DaoAccount.java @@ -41,8 +41,18 @@ public interface DaoAccount { @Query("SELECT * FROM account") LiveData> liveAccounts(); - @Query("SELECT * FROM account WHERE synchronize = :synchronize") - LiveData> liveAccounts(boolean synchronize); + @Query("SELECT *" + + ", (SELECT COUNT(message.id)" + + " FROM message" + + " JOIN folder ON folder.id = message.folder" + + " WHERE message.account = account.id" + + " AND folder.type <> '" + EntityFolder.ARCHIVE + "'" + + " AND folder.type <> '" + EntityFolder.TRASH + "'" + + " AND folder.type <> '" + EntityFolder.DRAFTS + "'" + + " AND NOT ui_seen) AS unseen" + + " FROM account" + + " WHERE synchronize = :synchronize") + LiveData> liveAccounts(boolean synchronize); @Query("SELECT * FROM account WHERE id = :id") EntityAccount getAccount(long id); diff --git a/app/src/main/java/eu/faircode/email/FragmentCompose.java b/app/src/main/java/eu/faircode/email/FragmentCompose.java index 64a918e149..949efc8c7d 100644 --- a/app/src/main/java/eu/faircode/email/FragmentCompose.java +++ b/app/src/main/java/eu/faircode/email/FragmentCompose.java @@ -1366,11 +1366,11 @@ public class FragmentCompose extends FragmentEx { final DB db = DB.getInstance(getContext()); - db.account().liveAccounts(true).observe(getViewLifecycleOwner(), new Observer>() { + db.account().liveAccounts(true).observe(getViewLifecycleOwner(), new Observer>() { private LiveData> liveIdentities = null; @Override - public void onChanged(List accounts) { + public void onChanged(List accounts) { if (accounts == null) accounts = new ArrayList<>(); @@ -1780,11 +1780,11 @@ public class FragmentCompose extends FragmentEx { EntityAccount account; } - public class AccountAdapter extends ArrayAdapter { + public class AccountAdapter extends ArrayAdapter { private Context context; - private List accounts; + private List accounts; - AccountAdapter(@NonNull Context context, List accounts) { + AccountAdapter(@NonNull Context context, List accounts) { super(context, 0, accounts); this.context = context; this.accounts = accounts; diff --git a/app/src/main/java/eu/faircode/email/FragmentMessages.java b/app/src/main/java/eu/faircode/email/FragmentMessages.java index 562222ed84..944fa5afdd 100644 --- a/app/src/main/java/eu/faircode/email/FragmentMessages.java +++ b/app/src/main/java/eu/faircode/email/FragmentMessages.java @@ -1239,7 +1239,7 @@ public class FragmentMessages extends FragmentEx { unseen += folder.unseen; String name = getString(R.string.title_folder_unified); if (unseen > 0) - setSubtitle(getString(R.string.title_folder_unseen, name, unseen)); + setSubtitle(getString(R.string.title_unseen_count, name, unseen)); else setSubtitle(name); @@ -1264,7 +1264,7 @@ public class FragmentMessages extends FragmentEx { else { String name = folder.getDisplayName(getContext()); if (folder.unseen > 0) - setSubtitle(getString(R.string.title_folder_unseen, name, folder.unseen)); + setSubtitle(getString(R.string.title_unseen_count, name, folder.unseen)); else setSubtitle(name); diff --git a/app/src/main/java/eu/faircode/email/FragmentSetup.java b/app/src/main/java/eu/faircode/email/FragmentSetup.java index 71810ff081..335523aa38 100644 --- a/app/src/main/java/eu/faircode/email/FragmentSetup.java +++ b/app/src/main/java/eu/faircode/email/FragmentSetup.java @@ -354,13 +354,13 @@ public class FragmentSetup extends FragmentEx { final DB db = DB.getInstance(getContext()); - db.account().liveAccounts(true).observe(getViewLifecycleOwner(), new Observer>() { + db.account().liveAccounts(true).observe(getViewLifecycleOwner(), new Observer>() { private boolean done = false; private LiveData livePrimaryDrafts = null; private LiveData livePrimaryArchive = null; @Override - public void onChanged(@Nullable List accounts) { + public void onChanged(@Nullable List accounts) { done = (accounts != null && accounts.size() > 0); btnIdentity.setEnabled(done); diff --git a/app/src/main/java/eu/faircode/email/TupleAccountEx.java b/app/src/main/java/eu/faircode/email/TupleAccountEx.java new file mode 100644 index 0000000000..067872fece --- /dev/null +++ b/app/src/main/java/eu/faircode/email/TupleAccountEx.java @@ -0,0 +1,34 @@ +package eu.faircode.email; + +/* + This file is part of FairEmail. + + FairEmail is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + FairEmail is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with FairEmail. If not, see . + + Copyright 2018 by Marcel Bokhorst (M66B) +*/ + +public class TupleAccountEx extends EntityAccount { + public int unseen; + + @Override + public boolean equals(Object obj) { + if (obj instanceof TupleAccountEx) { + TupleAccountEx other = (TupleAccountEx) obj; + return (super.equals(obj) && + this.unseen == other.unseen); + } else + return false; + } +} diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 272be5aec8..952232f1d8 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -189,7 +189,7 @@ Delete this identity permanently? POP is not supported - %1$s (%2$d) + %1$s (%2$d) Synchronize now Delete local messages