From bdbdaba8399dc01d90f203a8cc02275f42559a90 Mon Sep 17 00:00:00 2001 From: M66B Date: Mon, 23 Sep 2019 10:57:02 +0200 Subject: [PATCH] Fixed widget init --- .../java/eu/faircode/email/DaoMessage.java | 29 +++++++------------ .../eu/faircode/email/ServiceSynchronize.java | 4 +-- .../main/java/eu/faircode/email/Widget.java | 13 ++++++++- 3 files changed, 25 insertions(+), 21 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/DaoMessage.java b/app/src/main/java/eu/faircode/email/DaoMessage.java index 7b387fafa3..50e05bc98f 100644 --- a/app/src/main/java/eu/faircode/email/DaoMessage.java +++ b/app/src/main/java/eu/faircode/email/DaoMessage.java @@ -248,14 +248,19 @@ public interface DaoMessage { " WHERE message.id = :id") LiveData liveMessage(long id); - @Query("SELECT COUNT(message.id) AS unseen, SUM(ABS(notifying)) AS notifying" + + String widget = "SELECT COUNT(message.id) AS unseen, SUM(ABS(notifying)) AS notifying" + " FROM message" + " JOIN account ON account.id = message.account" + " JOIN folder ON folder.id = message.folder" + " WHERE account.`synchronize`" + " AND folder.notify" + - " AND NOT (message.ui_seen OR message.ui_hide <> 0)") - LiveData liveUnseen(); + " AND NOT (message.ui_seen OR message.ui_hide <> 0)"; + + @Query(widget) + LiveData liveUnseenWidget(); + + @Query(widget) + TupleMessageStats getUnseenWidget(); @Query("SELECT message.*" + ", account.pop AS accountPop, account.name AS accountName, IFNULL(identity.color, account.color) AS accountColor, account.notify AS accountNotify" + @@ -279,7 +284,7 @@ public interface DaoMessage { " ORDER BY message.received") LiveData> liveUnseenNotify(); - String widget = "SELECT message.*, account.name AS accountName" + + String widget_unified = "SELECT message.*, account.name AS accountName" + ", SUM(1 - message.ui_seen) AS unseen" + ", COUNT(message.id) - SUM(message.ui_flagged) AS unflagged" + ", MAX(message.received) AS dummy" + @@ -297,26 +302,14 @@ public interface DaoMessage { " ORDER BY message.received DESC" + " LIMIT 100"; - @Query(widget) + @Query(widget_unified) @SuppressWarnings(RoomWarnings.CURSOR_MISMATCH) LiveData> liveWidgetUnified(boolean threading, boolean unseen, boolean flagged); - @Query(widget) + @Query(widget_unified) @SuppressWarnings(RoomWarnings.CURSOR_MISMATCH) List getWidgetUnified(boolean threading, boolean unseen, boolean flagged); - @Query("SELECT COUNT(message.id) FROM message" + - " JOIN account ON account.id = message.account" + - " JOIN folder ON folder.id = message.folder" + - " WHERE account.`synchronize`" + - " AND folder.unified" + - " AND (account.created IS NULL OR message.received > account.created)" + - " AND NOT message.ui_seen" + - " AND NOT message.ui_ignored" + - " AND message.ui_hide = 0" + - " ORDER BY message.received") - int getUnseenUnified(); - @Query("SELECT uid FROM message" + " WHERE folder = :folder" + " AND (:received IS NULL OR received >= :received)" + diff --git a/app/src/main/java/eu/faircode/email/ServiceSynchronize.java b/app/src/main/java/eu/faircode/email/ServiceSynchronize.java index aff1b45352..cf98c1cc01 100644 --- a/app/src/main/java/eu/faircode/email/ServiceSynchronize.java +++ b/app/src/main/java/eu/faircode/email/ServiceSynchronize.java @@ -147,9 +147,9 @@ public class ServiceSynchronize extends ServiceBase { } }); - SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); + final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); - db.message().liveUnseen().observe(this, new Observer() { + db.message().liveUnseenWidget().observe(this, new Observer() { private Integer lastUnseen = null; @Override diff --git a/app/src/main/java/eu/faircode/email/Widget.java b/app/src/main/java/eu/faircode/email/Widget.java index 28d01a508e..8895ece94a 100644 --- a/app/src/main/java/eu/faircode/email/Widget.java +++ b/app/src/main/java/eu/faircode/email/Widget.java @@ -25,8 +25,11 @@ import android.appwidget.AppWidgetProvider; import android.content.ComponentName; import android.content.Context; import android.content.Intent; +import android.content.SharedPreferences; import android.widget.RemoteViews; +import androidx.preference.PreferenceManager; + import java.text.NumberFormat; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; @@ -41,7 +44,15 @@ public class Widget extends AppWidgetProvider { @Override public void run() { DB db = DB.getInstance(context); - update(context, appWidgetManager, appWidgetIds, db.message().getUnseenUnified()); + TupleMessageStats stats = db.message().getUnseenWidget(); + + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); + boolean unseen_ignored = prefs.getBoolean("unseen_ignored", false); + Integer unseen = (unseen_ignored ? stats.notifying : stats.unseen); + if (unseen == null) + unseen = 0; + + update(context, appWidgetManager, appWidgetIds, unseen); } }); }