Added badge for unread messages

This commit is contained in:
Christian Basler 2016-02-23 07:06:34 +01:00
parent df121b25c6
commit 563085ed79
5 changed files with 60 additions and 8 deletions

View File

@ -31,7 +31,6 @@ import android.support.v7.widget.Toolbar;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.AdapterView; import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CompoundButton; import android.widget.CompoundButton;
import android.widget.RelativeLayout; import android.widget.RelativeLayout;
@ -58,6 +57,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.io.Serializable; import java.io.Serializable;
import java.lang.ref.WeakReference;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
@ -103,6 +103,8 @@ public class MainActivity extends AppCompatActivity
private static final int ADD_IDENTITY = 1; private static final int ADD_IDENTITY = 1;
private static final int MANAGE_IDENTITY = 2; private static final int MANAGE_IDENTITY = 2;
public static WeakReference<MainActivity> instance;
/** /**
* Whether or not the activity is in two-pane mode, i.e. running on a tablet * Whether or not the activity is in two-pane mode, i.e. running on a tablet
* device. * device.
@ -432,6 +434,19 @@ public class MainActivity extends AppCompatActivity
.build(); .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) { private void checkAndStartNode(final CompoundButton buttonView) {
if (service == null) return; 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() { private void showSelectedLabel() {
if (getSupportFragmentManager().findFragmentById(R.id.item_list) instanceof if (getSupportFragmentManager().findFragmentById(R.id.item_list) instanceof
MessageListFragment) { MessageListFragment) {
@ -537,4 +567,9 @@ public class MainActivity extends AppCompatActivity
} }
super.onStop(); super.onStop();
} }
public static MainActivity getInstance() {
if (instance == null) return null;
return instance.get();
}
} }

View File

@ -35,6 +35,7 @@ import com.mikepenz.google_material_typeface_library.GoogleMaterial;
import java.util.Iterator; import java.util.Iterator;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import ch.dissem.apps.abit.listener.ActionBarListener;
import ch.dissem.apps.abit.service.Singleton; import ch.dissem.apps.abit.service.Singleton;
import ch.dissem.apps.abit.util.Drawables; import ch.dissem.apps.abit.util.Drawables;
import ch.dissem.bitmessage.entity.BitmessageAddress; import ch.dissem.bitmessage.entity.BitmessageAddress;
@ -129,6 +130,9 @@ public class MessageDetailFragment extends Fragment {
} }
} }
if (removed) { if (removed) {
if (getActivity() instanceof ActionBarListener) {
((ActionBarListener) getActivity()).updateUnread();
}
Singleton.getMessageRepository(inflater.getContext()).save(item); Singleton.getMessageRepository(inflater.getContext()).save(item);
} }
} }
@ -180,8 +184,14 @@ public class MessageDetailFragment extends Fragment {
case R.id.mark_unread: case R.id.mark_unread:
item.addLabels(messageRepo.getLabels(Label.Type.UNREAD)); item.addLabels(messageRepo.getLabels(Label.Type.UNREAD));
messageRepo.save(item); messageRepo.save(item);
if (getActivity() instanceof ActionBarListener) {
((ActionBarListener) getActivity()).updateUnread();
}
return true; return true;
case R.id.archive: case R.id.archive:
if (item.isUnread() && getActivity() instanceof ActionBarListener) {
((ActionBarListener) getActivity()).updateUnread();
}
item.getLabels().clear(); item.getLabels().clear();
messageRepo.save(item); messageRepo.save(item);
return true; return true;

View File

@ -21,4 +21,6 @@ package ch.dissem.apps.abit.listener;
*/ */
public interface ActionBarListener { public interface ActionBarListener {
void updateTitle(CharSequence title); void updateTitle(CharSequence title);
void updateUnread();
} }

View File

@ -21,6 +21,7 @@ import android.content.Context;
import java.util.Deque; import java.util.Deque;
import java.util.LinkedList; import java.util.LinkedList;
import ch.dissem.apps.abit.MainActivity;
import ch.dissem.apps.abit.notification.NewMessageNotification; import ch.dissem.apps.abit.notification.NewMessageNotification;
import ch.dissem.bitmessage.BitmessageContext; import ch.dissem.bitmessage.BitmessageContext;
import ch.dissem.bitmessage.entity.Plaintext; import ch.dissem.bitmessage.entity.Plaintext;
@ -58,6 +59,12 @@ public class MessageListener implements BitmessageContext.Listener {
notification.multiNotification(unacknowledged, numberOfUnacknowledgedMessages); notification.multiNotification(unacknowledged, numberOfUnacknowledgedMessages);
} }
notification.show(); notification.show();
// If MainActivity is shown, update the sidebar badges
MainActivity main = MainActivity.getInstance();
if (main != null) {
main.updateUnread();
}
} }
public void resetNotification() { public void resetNotification() {

View File

@ -19,8 +19,10 @@ package ch.dissem.apps.abit.repository;
import android.content.ContentValues; import android.content.ContentValues;
import android.content.Context; import android.content.Context;
import android.database.Cursor; import android.database.Cursor;
import android.database.DatabaseUtils;
import android.database.sqlite.SQLiteConstraintException; import android.database.sqlite.SQLiteConstraintException;
import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase;
import android.support.v4.database.DatabaseUtilsCompat;
import ch.dissem.apps.abit.R; import ch.dissem.apps.abit.R;
import ch.dissem.bitmessage.InternalContext; import ch.dissem.bitmessage.InternalContext;
@ -171,14 +173,10 @@ public class AndroidMessageRepository implements MessageRepository, InternalCont
where = ""; where = "";
} }
SQLiteDatabase db = sql.getReadableDatabase(); SQLiteDatabase db = sql.getReadableDatabase();
try (Cursor c = db.query( return (int) DatabaseUtils.queryNumEntries(db, TABLE_NAME,
TABLE_NAME, new String[]{COLUMN_ID},
where + "id IN (SELECT message_id FROM Message_Label WHERE label_id IN (" + where + "id IN (SELECT message_id FROM Message_Label WHERE label_id IN (" +
"SELECT id FROM Label WHERE type = '" + Label.Type.UNREAD.name() + "'))", "SELECT id FROM Label WHERE type = '" + Label.Type.UNREAD.name() + "'))"
null, null, null, null );
)) {
return c.getColumnCount();
}
} }
@Override @Override