From 5e5213f5b810ba6f5fd04bcd83a14a414193a8db Mon Sep 17 00:00:00 2001 From: M66B Date: Sat, 28 Mar 2020 10:02:00 +0100 Subject: [PATCH] Refactoring --- .../java/eu/faircode/email/DaoMessage.java | 46 +++++----- .../eu/faircode/email/ServiceSynchronize.java | 88 +++++++++---------- .../main/java/eu/faircode/email/Widget.java | 2 +- 3 files changed, 68 insertions(+), 68 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/DaoMessage.java b/app/src/main/java/eu/faircode/email/DaoMessage.java index 0e68a90393..30a2560d0a 100644 --- a/app/src/main/java/eu/faircode/email/DaoMessage.java +++ b/app/src/main/java/eu/faircode/email/DaoMessage.java @@ -350,29 +350,6 @@ public interface DaoMessage { " WHERE message.id = :id") LiveData liveMessageKeywords(long id); - @Transaction - @Query("SELECT account.id AS account, COUNT(message.id) AS unseen, SUM(ABS(notifying)) AS notifying" + - " FROM message" + - " JOIN account_view AS account ON account.id = message.account" + - " JOIN folder_view AS folder ON folder.id = message.folder" + - " WHERE (:account IS NULL OR account.id = :account)" + - " AND account.`synchronize`" + - " AND folder.notify" + - " AND NOT (message.ui_seen OR message.ui_hide)" + - " GROUP BY account.id" + - " ORDER BY account.id") - LiveData> liveUnseenWidget(Long account); - - @Query("SELECT :account AS account, COUNT(message.id) AS unseen, SUM(ABS(notifying)) AS notifying" + - " FROM message" + - " JOIN account_view AS account ON account.id = message.account" + - " JOIN folder_view AS folder ON folder.id = message.folder" + - " WHERE (:account IS NULL OR account.id = :account)" + - " AND account.`synchronize`" + - " AND folder.notify" + - " AND NOT (message.ui_seen OR message.ui_hide)") - TupleMessageStats getUnseenWidget(Long account); - @Transaction @Query("SELECT message.*" + ", account.pop AS accountProtocol, account.name AS accountName, COALESCE(identity.color, folder.color, account.color) AS accountColor" + @@ -401,6 +378,29 @@ public interface DaoMessage { " ORDER BY message.received") LiveData> liveUnseenNotify(); + @Transaction + @Query("SELECT account.id AS account, COUNT(message.id) AS unseen, SUM(ABS(notifying)) AS notifying" + + " FROM message" + + " JOIN account_view AS account ON account.id = message.account" + + " JOIN folder_view AS folder ON folder.id = message.folder" + + " WHERE (:account IS NULL OR account.id = :account)" + + " AND account.`synchronize`" + + " AND folder.notify" + + " AND NOT (message.ui_seen OR message.ui_hide)" + + " GROUP BY account.id" + + " ORDER BY account.id") + LiveData> liveWidgetUnseen(Long account); + + @Query("SELECT :account AS account, COUNT(message.id) AS unseen, SUM(ABS(notifying)) AS notifying" + + " FROM message" + + " JOIN account_view AS account ON account.id = message.account" + + " JOIN folder_view AS folder ON folder.id = message.folder" + + " WHERE (:account IS NULL OR account.id = :account)" + + " AND account.`synchronize`" + + " AND folder.notify" + + " AND NOT (message.ui_seen OR message.ui_hide)") + TupleMessageStats getWidgetUnseen(Long account); + @Transaction @Query("SELECT folder, COUNT(*) AS total" + ", SUM(ui_seen) AS seen" + diff --git a/app/src/main/java/eu/faircode/email/ServiceSynchronize.java b/app/src/main/java/eu/faircode/email/ServiceSynchronize.java index 1d7cbf8fd0..dea5527aaf 100644 --- a/app/src/main/java/eu/faircode/email/ServiceSynchronize.java +++ b/app/src/main/java/eu/faircode/email/ServiceSynchronize.java @@ -433,50 +433,6 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences } }); - db.message().liveUnseenWidget(null).observe(cowner, new Observer>() { - private List last = null; - - @Override - public void onChanged(List stats) { - if (stats == null) - stats = new ArrayList<>(); - - boolean changed = false; - if (last == null || last.size() != stats.size()) - changed = true; - else - for (int i = 0; i < stats.size(); i++) - if (!last.get(i).equals(stats.get(i))) { - changed = true; - break; - } - - if (!changed) - return; - - Widget.update(ServiceSynchronize.this); - - boolean badge = prefs.getBoolean("badge", true); - boolean unseen_ignored = prefs.getBoolean("unseen_ignored", false); - - int count = 0; - for (TupleMessageStats stat : stats) { - Integer unseen = (unseen_ignored ? stat.notifying : stat.unseen); - if (unseen != null) - count += unseen; - } - - try { - if (count == 0 || !badge) - ShortcutBadger.removeCount(ServiceSynchronize.this); - else - ShortcutBadger.applyCount(ServiceSynchronize.this, count); - } catch (Throwable ex) { - Log.e(ex); - } - } - }); - Map> groupNotifying = new HashMap<>(); // Get existing notifications @@ -535,6 +491,50 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences } }); + db.message().liveWidgetUnseen(null).observe(cowner, new Observer>() { + private List last = null; + + @Override + public void onChanged(List stats) { + if (stats == null) + stats = new ArrayList<>(); + + boolean changed = false; + if (last == null || last.size() != stats.size()) + changed = true; + else + for (int i = 0; i < stats.size(); i++) + if (!last.get(i).equals(stats.get(i))) { + changed = true; + break; + } + + if (!changed) + return; + + Widget.update(ServiceSynchronize.this); + + boolean badge = prefs.getBoolean("badge", true); + boolean unseen_ignored = prefs.getBoolean("unseen_ignored", false); + + int count = 0; + for (TupleMessageStats stat : stats) { + Integer unseen = (unseen_ignored ? stat.notifying : stat.unseen); + if (unseen != null) + count += unseen; + } + + try { + if (count == 0 || !badge) + ShortcutBadger.removeCount(ServiceSynchronize.this); + else + ShortcutBadger.applyCount(ServiceSynchronize.this, count); + } catch (Throwable ex) { + Log.e(ex); + } + } + }); + db.message().liveWidgetUnified().observe(cowner, new Observer>() { private List last = null; diff --git a/app/src/main/java/eu/faircode/email/Widget.java b/app/src/main/java/eu/faircode/email/Widget.java index 72c1634e2c..16dc1aa422 100644 --- a/app/src/main/java/eu/faircode/email/Widget.java +++ b/app/src/main/java/eu/faircode/email/Widget.java @@ -72,7 +72,7 @@ public class Widget extends AppWidgetProvider { pi = PendingIntent.getActivity(context, appWidgetId, view, PendingIntent.FLAG_UPDATE_CURRENT); } - TupleMessageStats stats = db.message().getUnseenWidget(account < 0 ? null : account); + TupleMessageStats stats = db.message().getWidgetUnseen(account < 0 ? null : account); Integer unseen = (unseen_ignored ? stats.notifying : stats.unseen); if (unseen == null) unseen = 0;