Commit 3eb9b754 authored by Tiago Cunha's avatar Tiago Cunha

Changes

parent 3678835c
......@@ -8,7 +8,6 @@ import android.support.v7.graphics.drawable.DrawerArrowDrawable;
import android.support.v7.widget.Toolbar;
import android.view.View;
import java.util.List;
import chat.rocket.android.LaunchUtil;
import chat.rocket.android.R;
import chat.rocket.android.api.MethodCallHelper;
......@@ -18,15 +17,16 @@ import chat.rocket.android.fragment.sidebar.SidebarMainFragment;
import chat.rocket.android.helper.LogcatIfError;
import chat.rocket.android.helper.TextUtils;
import chat.rocket.core.interactors.CanCreateRoomInteractor;
import chat.rocket.persistence.realm.models.ddp.RealmRoom;
import chat.rocket.core.interactors.RoomInteractor;
import chat.rocket.core.interactors.SessionInteractor;
import chat.rocket.persistence.realm.models.ddp.RealmUser;
import chat.rocket.persistence.realm.models.internal.RealmSession;
import chat.rocket.persistence.realm.RealmHelper;
import chat.rocket.persistence.realm.RealmListObserver;
import chat.rocket.persistence.realm.RealmObjectObserver;
import chat.rocket.persistence.realm.RealmStore;
import chat.rocket.android.service.ConnectivityManager;
import chat.rocket.android.widget.RoomToolbar;
import chat.rocket.persistence.realm.repositories.RealmRoomRepository;
import chat.rocket.persistence.realm.repositories.RealmSessionRepository;
import chat.rocket.persistence.realm.repositories.RealmUserRepository;
import hugo.weaving.DebugLog;
......@@ -37,7 +37,6 @@ import hugo.weaving.DebugLog;
public class MainActivity extends AbstractAuthedActivity implements MainContract.View {
private RealmObjectObserver<RealmSession> sessionObserver;
private RealmListObserver<RealmRoom> unreadRoomSubscriptionObserver;
private boolean isForeground;
private StatusTicker statusTicker;
......@@ -180,14 +179,19 @@ public class MainActivity extends AbstractAuthedActivity implements MainContract
if (presenter != null) {
presenter.release();
}
CanCreateRoomInteractor interactor = new CanCreateRoomInteractor(
RoomInteractor roomInteractor = new RoomInteractor(new RealmRoomRepository(hostname));
CanCreateRoomInteractor createRoomInteractor = new CanCreateRoomInteractor(
new RealmUserRepository(hostname),
new RealmSessionRepository(hostname)
new SessionInteractor(new RealmSessionRepository(hostname))
);
presenter = new MainPresenter(interactor);
presenter = new MainPresenter(
roomInteractor,
createRoomInteractor);
updateSessionObserver();
updateUnreadRoomSubscriptionObserver();
updateSidebarMainFragment();
}
......@@ -235,44 +239,6 @@ public class MainActivity extends AbstractAuthedActivity implements MainContract
}
}
private void updateUnreadRoomSubscriptionObserver() {
if (unreadRoomSubscriptionObserver != null) {
unreadRoomSubscriptionObserver.unsub();
unreadRoomSubscriptionObserver = null;
}
if (hostname == null) {
return;
}
RealmHelper realmHelper = RealmStore.get(hostname);
if (realmHelper == null) {
return;
}
unreadRoomSubscriptionObserver = realmHelper
.createListObserver(realm ->
realm.where(RealmRoom.class)
.equalTo(RealmRoom.ALERT, true)
.equalTo(RealmRoom.OPEN, true)
.findAll())
.setOnUpdateListener(this::updateRoomToolbarUnreadCount);
unreadRoomSubscriptionObserver.sub();
}
private void updateRoomToolbarUnreadCount(List<RealmRoom> unreadRooms) {
RoomToolbar toolbar = (RoomToolbar) findViewById(R.id.activity_main_toolbar);
if (toolbar != null) {
//ref: Rocket.Chat:client/startup/unread.js
final int numUnreadChannels = unreadRooms.size();
int numMentionsSum = 0;
for (RealmRoom room : unreadRooms) {
numMentionsSum += room.getUnread();
}
toolbar.setUnreadBudge(numUnreadChannels, numMentionsSum);
}
}
private void updateSidebarMainFragment() {
getSupportFragmentManager().beginTransaction()
.replace(R.id.sidebar_fragment_container, SidebarMainFragment.create(hostname))
......@@ -291,10 +257,6 @@ public class MainActivity extends AbstractAuthedActivity implements MainContract
sessionObserver.unsub();
sessionObserver = null;
}
if (unreadRoomSubscriptionObserver != null) {
unreadRoomSubscriptionObserver.unsub();
unreadRoomSubscriptionObserver = null;
}
super.onDestroy();
}
......@@ -314,6 +276,14 @@ public class MainActivity extends AbstractAuthedActivity implements MainContract
closeSidebarIfNeeded();
}
@Override
public void showUnreadCount(int roomsCount, int mentionsCount) {
RoomToolbar toolbar = (RoomToolbar) findViewById(R.id.activity_main_toolbar);
if (toolbar != null) {
toolbar.setUnreadBudge(roomsCount, mentionsCount);
}
}
//TODO: consider this class to define in layouthelper for more complicated operation.
private static class StatusTicker {
public static final int STATUS_DISMISS = 0;
......
......@@ -9,6 +9,8 @@ public interface MainContract {
void showHome();
void showRoom(String hostname, String roomId);
void showUnreadCount(int roomsCount, int mentionsCount);
}
interface Presenter extends BaseContract.Presenter<View> {
......
package chat.rocket.android.activity;
import android.support.annotation.NonNull;
import android.support.v4.util.Pair;
import chat.rocket.android.BackgroundLooper;
import chat.rocket.android.shared.BasePresenter;
import chat.rocket.core.interactors.CanCreateRoomInteractor;
import chat.rocket.core.interactors.RoomInteractor;
import rx.Observable;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
......@@ -10,11 +15,34 @@ public class MainPresenter extends BasePresenter<MainContract.View>
implements MainContract.Presenter {
private final CanCreateRoomInteractor canCreateRoomInteractor;
private final RoomInteractor roomInteractor;
public MainPresenter(CanCreateRoomInteractor canCreateRoomInteractor) {
public MainPresenter(RoomInteractor roomInteractor,
CanCreateRoomInteractor canCreateRoomInteractor) {
this.roomInteractor = roomInteractor;
this.canCreateRoomInteractor = canCreateRoomInteractor;
}
@Override
public void bindView(@NonNull MainContract.View view) {
super.bindView(view);
subscribeToUnreadCount();
}
private void subscribeToUnreadCount() {
final Subscription subscription = Observable.combineLatest(
roomInteractor.getTotalUnreadRoomsCount(),
roomInteractor.getTotalUnreadMentionsCount(),
(Pair::new)
)
.subscribeOn(AndroidSchedulers.from(BackgroundLooper.get()))
.observeOn(AndroidSchedulers.mainThread())
.subscribe(pair -> view.showUnreadCount(pair.first, pair.second));
addSubscription(subscription);
}
@Override
public void onOpenRoom(String hostname, String roomId) {
final Subscription subscription = canCreateRoomInteractor.canCreate(roomId)
......
......@@ -22,6 +22,7 @@ import chat.rocket.android.fragment.sidebar.dialog.AddChannelDialogFragment;
import chat.rocket.android.fragment.sidebar.dialog.AddDirectMessageDialogFragment;
import chat.rocket.android.helper.TextUtils;
import chat.rocket.android.layouthelper.chatroom.RoomListManager;
import chat.rocket.core.interactors.RoomInteractor;
import chat.rocket.core.models.Room;
import chat.rocket.core.models.User;
import chat.rocket.android.renderer.UserRenderer;
......@@ -64,7 +65,7 @@ public class SidebarMainFragment extends AbstractFragment implements SidebarMain
presenter = new SidebarMainPresenter(
hostname,
new RealmRoomRepository(hostname),
new RoomInteractor(new RealmRoomRepository(hostname)),
new RealmUserRepository(hostname),
TextUtils.isEmpty(hostname) ? null : new MethodCallHelper(getContext(), hostname)
);
......
......@@ -7,8 +7,8 @@ import chat.rocket.android.api.MethodCallHelper;
import chat.rocket.android.helper.LogcatIfError;
import chat.rocket.android.helper.TextUtils;
import chat.rocket.android.shared.BasePresenter;
import chat.rocket.core.interactors.RoomInteractor;
import chat.rocket.core.models.User;
import chat.rocket.core.repositories.RoomRepository;
import chat.rocket.core.repositories.UserRepository;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
......@@ -17,14 +17,14 @@ public class SidebarMainPresenter extends BasePresenter<SidebarMainContract.View
implements SidebarMainContract.Presenter {
private final String hostname;
private final RoomRepository roomRepository;
private final RoomInteractor roomInteractor;
private final UserRepository userRepository;
private final MethodCallHelper methodCallHelper;
public SidebarMainPresenter(String hostname, RoomRepository roomRepository,
public SidebarMainPresenter(String hostname, RoomInteractor roomInteractor,
UserRepository userRepository, MethodCallHelper methodCallHelper) {
this.hostname = hostname;
this.roomRepository = roomRepository;
this.roomInteractor = roomInteractor;
this.userRepository = userRepository;
this.methodCallHelper = methodCallHelper;
}
......@@ -72,7 +72,7 @@ public class SidebarMainPresenter extends BasePresenter<SidebarMainContract.View
}
private void subscribeToRooms() {
final Subscription subscription = roomRepository.getOpenRooms()
final Subscription subscription = roomInteractor.getOpenRooms()
.distinctUntilChanged()
.subscribeOn(AndroidSchedulers.from(BackgroundLooper.get()))
.observeOn(AndroidSchedulers.mainThread())
......
......@@ -32,7 +32,7 @@ public class RealmMessageRepository extends RealmRepository implements MessageRe
final Realm realm = RealmStore.getRealm(hostname);
final Looper looper = Looper.myLooper();
if (realm == null) {
if (realm == null || looper == null) {
return Single.just(null);
}
......@@ -63,7 +63,7 @@ public class RealmMessageRepository extends RealmRepository implements MessageRe
final Realm realm = RealmStore.getRealm(hostname);
final Looper looper = Looper.myLooper();
if (realm == null) {
if (realm == null || looper == null) {
return Single.just(false);
}
......@@ -110,7 +110,7 @@ public class RealmMessageRepository extends RealmRepository implements MessageRe
final Realm realm = RealmStore.getRealm(hostname);
final Looper looper = Looper.myLooper();
if (realm == null) {
if (realm == null || looper == null) {
return Single.just(false);
}
......@@ -143,7 +143,7 @@ public class RealmMessageRepository extends RealmRepository implements MessageRe
final Realm realm = RealmStore.getRealm(hostname);
final Looper looper = Looper.myLooper();
if (realm == null) {
if (realm == null || looper == null) {
return Observable.just(null);
}
......@@ -165,7 +165,7 @@ public class RealmMessageRepository extends RealmRepository implements MessageRe
final Realm realm = RealmStore.getRealm(hostname);
final Looper looper = Looper.myLooper();
if (realm == null) {
if (realm == null || looper == null) {
return Single.just(0);
}
......
......@@ -7,11 +7,6 @@ import io.realm.Realm;
public class RealmRepository {
protected void close(Realm realm, Looper looper) {
new Handler(looper).post(new Runnable() {
@Override
public void run() {
realm.close();
}
});
new Handler(looper).post(realm::close);
}
}
......@@ -25,24 +25,23 @@ public class RealmRoomRepository extends RealmRepository implements RoomReposito
}
@Override
public Observable<List<Room>> getOpenRooms() {
public Observable<List<Room>> getAll() {
return Observable.defer(() -> {
final Realm realm = RealmStore.getRealm(hostname);
final Looper looper = Looper.myLooper();
if (realm == null) {
if (realm == null || looper == null) {
return Observable.just(null);
}
return realm.where(RealmRoom.class)
.equalTo(RealmRoom.OPEN, true)
.findAll()
.asObservable()
.unsubscribeOn(AndroidSchedulers.from(looper))
.doOnUnsubscribe(() -> close(realm, looper))
.filter(roomSubscriptions -> roomSubscriptions != null && roomSubscriptions.isLoaded()
&& roomSubscriptions.isValid())
.map(roomSubscriptions -> toList(roomSubscriptions));
.map(this::toList);
});
}
......@@ -52,7 +51,7 @@ public class RealmRoomRepository extends RealmRepository implements RoomReposito
final Realm realm = RealmStore.getRealm(hostname);
final Looper looper = Looper.myLooper();
if (realm == null) {
if (realm == null || looper == null) {
return Observable.just(null);
}
......@@ -64,7 +63,7 @@ public class RealmRoomRepository extends RealmRepository implements RoomReposito
.doOnUnsubscribe(() -> close(realm, looper))
.filter(roomSubscription -> roomSubscription != null && roomSubscription.isLoaded()
&& roomSubscription.isValid())
.map(roomSubscription -> roomSubscription.asRoom());
.map(RealmRoom::asRoom);
});
}
......@@ -74,7 +73,7 @@ public class RealmRoomRepository extends RealmRepository implements RoomReposito
final Realm realm = RealmStore.getRealm(hostname);
final Looper looper = Looper.myLooper();
if (realm == null) {
if (realm == null || looper == null) {
return Observable.just(null);
}
......@@ -86,7 +85,7 @@ public class RealmRoomRepository extends RealmRepository implements RoomReposito
.doOnUnsubscribe(() -> close(realm, looper))
.filter(loadMessageProcedure -> loadMessageProcedure != null
&& loadMessageProcedure.isLoaded() && loadMessageProcedure.isValid())
.map(loadMessageProcedure -> loadMessageProcedure.asRoomHistoryState());
.map(LoadMessageProcedure::asRoomHistoryState);
});
}
......@@ -96,7 +95,7 @@ public class RealmRoomRepository extends RealmRepository implements RoomReposito
final Realm realm = RealmStore.getRealm(hostname);
final Looper looper = Looper.myLooper();
if (realm == null) {
if (realm == null || looper == null) {
return Single.just(false);
}
......
......@@ -19,17 +19,17 @@ public class RealmSessionRepository extends RealmRepository implements SessionRe
}
@Override
public Observable<Session> getDefault() {
public Observable<Session> getById(int id) {
return Observable.defer(() -> {
final Realm realm = RealmStore.getRealm(hostname);
final Looper looper = Looper.myLooper();
if (realm == null) {
if (realm == null || looper == null) {
return Observable.just(null);
}
return realm.where(RealmSession.class)
.equalTo(RealmSession.ID, RealmSession.DEFAULT_ID)
.equalTo(RealmSession.ID, id)
.findAll()
.<RealmSession>asObservable()
.unsubscribeOn(AndroidSchedulers.from(looper))
......
......@@ -24,7 +24,7 @@ public class RealmUserRepository extends RealmRepository implements UserReposito
final Realm realm = RealmStore.getRealm(hostname);
final Looper looper = Looper.myLooper();
if (realm == null) {
if (realm == null || looper == null) {
return Observable.just(null);
}
......@@ -42,7 +42,7 @@ public class RealmUserRepository extends RealmRepository implements UserReposito
.unsubscribeOn(AndroidSchedulers.from(looper))
.doOnUnsubscribe(() -> close(realm, looper))
.filter(it -> it != null && it.isLoaded() && it.isValid())
.map(it -> it.asUser());
.map(RealmUser::asUser);
});
}
}
package chat.rocket.core.interactors;
import chat.rocket.core.repositories.SessionRepository;
import chat.rocket.core.repositories.UserRepository;
import rx.Observable;
import rx.Single;
......@@ -8,18 +7,18 @@ import rx.Single;
public class CanCreateRoomInteractor {
private final UserRepository userRepository;
private final SessionRepository sessionRepository;
private final SessionInteractor sessionInteractor;
public CanCreateRoomInteractor(UserRepository userRepository,
SessionRepository sessionRepository) {
SessionInteractor sessionInteractor) {
this.userRepository = userRepository;
this.sessionRepository = sessionRepository;
this.sessionInteractor = sessionInteractor;
}
public Single<Boolean> canCreate(String roomId) {
return Observable.zip(
userRepository.getCurrent(),
sessionRepository.getDefault(),
sessionInteractor.getDefault(),
Observable.just(roomId),
(user, session, room) -> user != null && session != null && room != null
)
......
package chat.rocket.core.interactors;
import java.util.List;
import chat.rocket.core.models.Room;
import chat.rocket.core.repositories.RoomRepository;
import rx.Observable;
public class RoomInteractor {
private final RoomRepository roomRepository;
public RoomInteractor(RoomRepository roomRepository) {
this.roomRepository = roomRepository;
}
public Observable<Integer> getTotalUnreadMentionsCount() {
return roomRepository.getAll()
.flatMap(rooms -> Observable.from(rooms)
.filter(room -> room.isOpen() && room.isAlert())
.map(Room::getUnread)
.defaultIfEmpty(0)
.reduce((unreadCount, unreadCount2) -> unreadCount + unreadCount2));
}
public Observable<Integer> getTotalUnreadRoomsCount() {
return roomRepository.getAll()
.flatMap(rooms -> Observable.from(rooms)
.filter(room -> room.isOpen() && room.isAlert())
.count());
}
public Observable<List<Room>> getOpenRooms() {
return roomRepository.getAll()
.flatMap(rooms -> Observable.from(rooms)
.filter(Room::isOpen)
.toList());
}
}
......@@ -6,14 +6,20 @@ import rx.Observable;
public class SessionInteractor {
private static final int DEFAULT_ID = 0;
private final SessionRepository sessionRepository;
public SessionInteractor(SessionRepository sessionRepository) {
this.sessionRepository = sessionRepository;
}
public Observable<Session> getDefault() {
return sessionRepository.getById(DEFAULT_ID);
}
public Observable<Session.State> getSessionState() {
return sessionRepository.getDefault()
return getDefault()
.map(this::getStateFrom);
}
......
......@@ -8,7 +8,7 @@ import rx.Single;
public interface RoomRepository {
Observable<List<Room>> getOpenRooms();
Observable<List<Room>> getAll();
Observable<Room> getById(String roomId);
......
......@@ -5,5 +5,5 @@ import rx.Observable;
public interface SessionRepository {
Observable<Session> getDefault();
Observable<Session> getById(int id);
}
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