Commit d0dc70b7 authored by Yusuke Iwaki's avatar Yusuke Iwaki

implement unread count budge

parent af2cdc34
......@@ -17,11 +17,14 @@ import chat.rocket.android.fragment.sidebar.SidebarMainFragment;
import chat.rocket.android.helper.LogcatIfError;
import chat.rocket.android.helper.TextUtils;
import chat.rocket.android.model.ServerConfig;
import chat.rocket.android.model.ddp.RoomSubscription;
import chat.rocket.android.model.ddp.User;
import chat.rocket.android.model.internal.Session;
import chat.rocket.android.realm_helper.RealmHelper;
import chat.rocket.android.realm_helper.RealmListObserver;
import chat.rocket.android.realm_helper.RealmObjectObserver;
import chat.rocket.android.realm_helper.RealmStore;
import chat.rocket.android.widget.RoomToolbar;
import hugo.weaving.DebugLog;
/**
......@@ -30,6 +33,7 @@ import hugo.weaving.DebugLog;
public class MainActivity extends AbstractAuthedActivity {
private RealmObjectObserver<Session> sessionObserver;
private RealmListObserver<RoomSubscription> unreadRoomSubscriptionObserver;
private boolean isForeground;
private StatusTicker statusTicker;
......@@ -150,6 +154,7 @@ public class MainActivity extends AbstractAuthedActivity {
protected void onServerConfigIdUpdated() {
super.onServerConfigIdUpdated();
updateSessionObserver();
updateUnreadRoomSubscriptionObserver();
updateSidebarMainFragment();
}
......@@ -207,6 +212,38 @@ public class MainActivity extends AbstractAuthedActivity {
}
}
private void updateUnreadRoomSubscriptionObserver() {
if (unreadRoomSubscriptionObserver != null) {
unreadRoomSubscriptionObserver.unsub();
unreadRoomSubscriptionObserver = null;
}
if (serverConfigId == null) {
return;
}
RealmHelper realmHelper = RealmStore.get(serverConfigId);
if (realmHelper == null) {
return;
}
unreadRoomSubscriptionObserver = realmHelper
.createListObserver(realm ->
realm.where(RoomSubscription.class)
.equalTo(RoomSubscription.ALERT, true)
.equalTo(RoomSubscription.OPEN, true)
.findAll())
.setOnUpdateListener(results -> updateRoomToolbarUnreadCount(results.size()));
unreadRoomSubscriptionObserver.sub();
}
private void updateRoomToolbarUnreadCount(int unreadCount) {
RoomToolbar toolbar = (RoomToolbar) findViewById(R.id.activity_main_toolbar);
if (toolbar != null) {
toolbar.setUnreadBudge(unreadCount);
}
}
private void updateSidebarMainFragment() {
getSupportFragmentManager().beginTransaction()
.replace(R.id.sidebar_fragment_container, SidebarMainFragment.create(serverConfigId))
......@@ -231,6 +268,10 @@ public class MainActivity extends AbstractAuthedActivity {
sessionObserver.unsub();
sessionObserver = null;
}
if (unreadRoomSubscriptionObserver != null) {
unreadRoomSubscriptionObserver.unsub();
unreadRoomSubscriptionObserver = null;
}
super.onDestroy();
}
......
......@@ -46,6 +46,5 @@ abstract class AbstractChatRoomFragment extends AbstractFragment {
}
roomToolbar.setRoomIcon(drawableResId);
roomToolbar.setUnreadBudge((int) (System.currentTimeMillis() % 15)); //TODO: just example!
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment