diff --git a/app/src/main/java/ch/dissem/apps/abit/MainActivity.java b/app/src/main/java/ch/dissem/apps/abit/MainActivity.java index 6e850a2..1baf37e 100644 --- a/app/src/main/java/ch/dissem/apps/abit/MainActivity.java +++ b/app/src/main/java/ch/dissem/apps/abit/MainActivity.java @@ -31,7 +31,6 @@ import android.support.v7.widget.Toolbar; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; -import android.widget.Button; import android.widget.CompoundButton; import android.widget.RelativeLayout; @@ -58,6 +57,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.Serializable; +import java.lang.ref.WeakReference; import java.util.ArrayList; import java.util.Collection; import java.util.List; @@ -103,6 +103,8 @@ public class MainActivity extends AppCompatActivity private static final int ADD_IDENTITY = 1; private static final int MANAGE_IDENTITY = 2; + public static WeakReference instance; + /** * Whether or not the activity is in two-pane mode, i.e. running on a tablet * device. @@ -432,6 +434,19 @@ public class MainActivity extends AppCompatActivity .build(); } + @Override + protected void onResume() { + instance = new WeakReference<>(this); + updateUnread(); + super.onResume(); + } + + @Override + protected void onPause() { + super.onPause(); + instance = null; + } + private void checkAndStartNode(final CompoundButton buttonView) { if (service == null) return; @@ -456,6 +471,21 @@ public class MainActivity extends AppCompatActivity } } + @Override + public void updateUnread() { + for (IDrawerItem item : drawer.getDrawerItems()) { + if (item.getTag() instanceof Label) { + Label label = (Label) item.getTag(); + int unread = bmc.messages().countUnread(label); + if (unread > 0) { + ((PrimaryDrawerItem) item).withBadge(String.valueOf(unread)); + } else { + ((PrimaryDrawerItem) item).withBadge(null); + } + } + } + } + private void showSelectedLabel() { if (getSupportFragmentManager().findFragmentById(R.id.item_list) instanceof MessageListFragment) { @@ -537,4 +567,9 @@ public class MainActivity extends AppCompatActivity } super.onStop(); } + + public static MainActivity getInstance() { + if (instance == null) return null; + return instance.get(); + } } diff --git a/app/src/main/java/ch/dissem/apps/abit/MessageDetailFragment.java b/app/src/main/java/ch/dissem/apps/abit/MessageDetailFragment.java index c86b690..b67cee5 100644 --- a/app/src/main/java/ch/dissem/apps/abit/MessageDetailFragment.java +++ b/app/src/main/java/ch/dissem/apps/abit/MessageDetailFragment.java @@ -35,6 +35,7 @@ import com.mikepenz.google_material_typeface_library.GoogleMaterial; import java.util.Iterator; import java.util.regex.Matcher; +import ch.dissem.apps.abit.listener.ActionBarListener; import ch.dissem.apps.abit.service.Singleton; import ch.dissem.apps.abit.util.Drawables; import ch.dissem.bitmessage.entity.BitmessageAddress; @@ -129,6 +130,9 @@ public class MessageDetailFragment extends Fragment { } } if (removed) { + if (getActivity() instanceof ActionBarListener) { + ((ActionBarListener) getActivity()).updateUnread(); + } Singleton.getMessageRepository(inflater.getContext()).save(item); } } @@ -180,8 +184,14 @@ public class MessageDetailFragment extends Fragment { case R.id.mark_unread: item.addLabels(messageRepo.getLabels(Label.Type.UNREAD)); messageRepo.save(item); + if (getActivity() instanceof ActionBarListener) { + ((ActionBarListener) getActivity()).updateUnread(); + } return true; case R.id.archive: + if (item.isUnread() && getActivity() instanceof ActionBarListener) { + ((ActionBarListener) getActivity()).updateUnread(); + } item.getLabels().clear(); messageRepo.save(item); return true; diff --git a/app/src/main/java/ch/dissem/apps/abit/listener/ActionBarListener.java b/app/src/main/java/ch/dissem/apps/abit/listener/ActionBarListener.java index d1e1ea4..39b2f55 100644 --- a/app/src/main/java/ch/dissem/apps/abit/listener/ActionBarListener.java +++ b/app/src/main/java/ch/dissem/apps/abit/listener/ActionBarListener.java @@ -21,4 +21,6 @@ package ch.dissem.apps.abit.listener; */ public interface ActionBarListener { void updateTitle(CharSequence title); + + void updateUnread(); } diff --git a/app/src/main/java/ch/dissem/apps/abit/listener/MessageListener.java b/app/src/main/java/ch/dissem/apps/abit/listener/MessageListener.java index b25fdfc..ddb99f5 100644 --- a/app/src/main/java/ch/dissem/apps/abit/listener/MessageListener.java +++ b/app/src/main/java/ch/dissem/apps/abit/listener/MessageListener.java @@ -21,6 +21,7 @@ import android.content.Context; import java.util.Deque; import java.util.LinkedList; +import ch.dissem.apps.abit.MainActivity; import ch.dissem.apps.abit.notification.NewMessageNotification; import ch.dissem.bitmessage.BitmessageContext; import ch.dissem.bitmessage.entity.Plaintext; @@ -58,6 +59,12 @@ public class MessageListener implements BitmessageContext.Listener { notification.multiNotification(unacknowledged, numberOfUnacknowledgedMessages); } notification.show(); + + // If MainActivity is shown, update the sidebar badges + MainActivity main = MainActivity.getInstance(); + if (main != null) { + main.updateUnread(); + } } public void resetNotification() { diff --git a/app/src/main/java/ch/dissem/apps/abit/repository/AndroidMessageRepository.java b/app/src/main/java/ch/dissem/apps/abit/repository/AndroidMessageRepository.java index a186fd3..6b128b4 100644 --- a/app/src/main/java/ch/dissem/apps/abit/repository/AndroidMessageRepository.java +++ b/app/src/main/java/ch/dissem/apps/abit/repository/AndroidMessageRepository.java @@ -19,8 +19,10 @@ package ch.dissem.apps.abit.repository; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; +import android.database.DatabaseUtils; import android.database.sqlite.SQLiteConstraintException; import android.database.sqlite.SQLiteDatabase; +import android.support.v4.database.DatabaseUtilsCompat; import ch.dissem.apps.abit.R; import ch.dissem.bitmessage.InternalContext; @@ -171,14 +173,10 @@ public class AndroidMessageRepository implements MessageRepository, InternalCont where = ""; } SQLiteDatabase db = sql.getReadableDatabase(); - try (Cursor c = db.query( - TABLE_NAME, new String[]{COLUMN_ID}, + return (int) DatabaseUtils.queryNumEntries(db, TABLE_NAME, where + "id IN (SELECT message_id FROM Message_Label WHERE label_id IN (" + - "SELECT id FROM Label WHERE type = '" + Label.Type.UNREAD.name() + "'))", - null, null, null, null - )) { - return c.getColumnCount(); - } + "SELECT id FROM Label WHERE type = '" + Label.Type.UNREAD.name() + "'))" + ); } @Override