From a996601e278060e90537342865c52d84434fee64 Mon Sep 17 00:00:00 2001 From: M66B Date: Wed, 6 Mar 2019 16:51:44 +0000 Subject: [PATCH] Refactored tile unseen --- .../eu/faircode/email/ServiceTileUnseen.java | 51 ++++++++----------- 1 file changed, 21 insertions(+), 30 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/ServiceTileUnseen.java b/app/src/main/java/eu/faircode/email/ServiceTileUnseen.java index 98bde00e38..a9206ea888 100644 --- a/app/src/main/java/eu/faircode/email/ServiceTileUnseen.java +++ b/app/src/main/java/eu/faircode/email/ServiceTileUnseen.java @@ -29,72 +29,63 @@ import android.service.quicksettings.TileService; import java.util.List; -import androidx.lifecycle.LifecycleService; -import androidx.lifecycle.LiveData; import androidx.lifecycle.Observer; @TargetApi(Build.VERSION_CODES.N) public class ServiceTileUnseen extends TileService { - private LifecycleService owner = new LifecycleService(); - private LiveData> liveMessages; + private TwoStateOwner owner = new TwoStateOwner(); @Override public void onCreate() { - owner.onCreate(); super.onCreate(); + + DB.getInstance(this).message().liveUnseenNotify().observe(owner, new Observer>() { + @Override + public void onChanged(List messages) { + Log.i("Update tile unseen=" + messages.size()); + + Tile tile = getQsTile(); + if (tile != null) { + tile.setState(messages.size() > 0 ? Tile.STATE_ACTIVE : Tile.STATE_INACTIVE); + tile.setIcon(Icon.createWithResource(ServiceTileUnseen.this, + messages.size() > 0 ? R.drawable.baseline_mail_24 : R.drawable.baseline_mail_outline_24)); + tile.setLabel(getResources().getQuantityString( + R.plurals.title_tile_unseen, messages.size(), messages.size())); + tile.updateTile(); + } + } + }); } @Override public IBinder onBind(Intent intent) { - owner.onBind(intent); return super.onBind(intent); } @Override public int onStartCommand(Intent intent, int flags, int startId) { - owner.onStartCommand(intent, flags, startId); return super.onStartCommand(intent, flags, startId); } @Override public void onDestroy() { - owner.onDestroy(); super.onDestroy(); } public void onStartListening() { Log.i("Start tile unseen"); - liveMessages = DB.getInstance(this).message().liveUnseenNotify(); - liveMessages.observe(owner, new Observer>() { - @Override - public void onChanged(List messages) { - Log.i("Update tile unseen=" + messages.size()); - - Tile tile = getQsTile(); - if (tile != null) { - tile.setState(messages.size() > 0 ? Tile.STATE_ACTIVE : Tile.STATE_INACTIVE); - tile.setIcon(Icon.createWithResource(ServiceTileUnseen.this, - messages.size() > 0 ? R.drawable.baseline_mail_24 : R.drawable.baseline_mail_outline_24)); - tile.setLabel(getResources().getQuantityString( - R.plurals.title_tile_unseen, messages.size(), messages.size())); - tile.updateTile(); - } - } - }); + owner.start(); } public void onStopListening() { Log.i("Stop tile unseen"); - if (liveMessages != null) { - liveMessages.removeObservers(owner); - liveMessages = null; - } + owner.stop(); } public void onClick() { Log.i("Click tile unseen"); - Intent clear = new Intent(this, ServiceSynchronize.class); + Intent clear = new Intent(this, ServiceUI.class); clear.setAction("clear"); startService(clear); }