Commit 0af8de4c authored by Filipe de Lima Brito's avatar Filipe de Lima Brito

Update SidebarMainPresenter.java

parent da668d26
...@@ -3,12 +3,10 @@ package chat.rocket.android.fragment.sidebar; ...@@ -3,12 +3,10 @@ package chat.rocket.android.fragment.sidebar;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.v4.util.Pair; import android.support.v4.util.Pair;
import chat.rocket.core.models.Spotlight;
import chat.rocket.persistence.realm.repositories.RealmSpotlightRepository;
import com.hadisatrio.optional.Optional; import com.hadisatrio.optional.Optional;
import io.reactivex.Flowable;
import io.reactivex.android.schedulers.AndroidSchedulers; import java.util.ArrayList;
import io.reactivex.disposables.Disposable; import java.util.List;
import chat.rocket.android.BackgroundLooper; import chat.rocket.android.BackgroundLooper;
import chat.rocket.android.RocketChatCache; import chat.rocket.android.RocketChatCache;
...@@ -20,155 +18,175 @@ import chat.rocket.android.helper.TextUtils; ...@@ -20,155 +18,175 @@ import chat.rocket.android.helper.TextUtils;
import chat.rocket.android.shared.BasePresenter; import chat.rocket.android.shared.BasePresenter;
import chat.rocket.core.interactors.RoomInteractor; import chat.rocket.core.interactors.RoomInteractor;
import chat.rocket.core.models.Room; import chat.rocket.core.models.Room;
import chat.rocket.core.models.RoomSidebar;
import chat.rocket.core.models.Spotlight;
import chat.rocket.core.models.User; import chat.rocket.core.models.User;
import chat.rocket.core.repositories.UserRepository; import chat.rocket.core.repositories.UserRepository;
import java.util.List; import chat.rocket.persistence.realm.repositories.RealmSpotlightRepository;
import io.reactivex.Flowable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
public class SidebarMainPresenter extends BasePresenter<SidebarMainContract.View> implements SidebarMainContract.Presenter { public class SidebarMainPresenter extends BasePresenter<SidebarMainContract.View> implements SidebarMainContract.Presenter {
private final String hostname; private final String hostname;
private String userId; private final RoomInteractor roomInteractor;
private final RoomInteractor roomInteractor; private final UserRepository userRepository;
private final UserRepository userRepository; private final RocketChatCache rocketChatCache;
private final RocketChatCache rocketChatCache; private final AbsoluteUrlHelper absoluteUrlHelper;
private final AbsoluteUrlHelper absoluteUrlHelper; private final MethodCallHelper methodCallHelper;
private final MethodCallHelper methodCallHelper; private RealmSpotlightRepository realmSpotlightRepository;
private RealmSpotlightRepository realmSpotlightRepository; private RoomSidebar roomSidebar;
public SidebarMainPresenter(String hostname, public SidebarMainPresenter(String hostname,
RoomInteractor roomInteractor, RoomInteractor roomInteractor,
UserRepository userRepository, UserRepository userRepository,
RocketChatCache rocketChatCache, RocketChatCache rocketChatCache,
AbsoluteUrlHelper absoluteUrlHelper, AbsoluteUrlHelper absoluteUrlHelper,
MethodCallHelper methodCallHelper, MethodCallHelper methodCallHelper,
RealmSpotlightRepository realmSpotlightRepository) { RealmSpotlightRepository realmSpotlightRepository) {
this.hostname = hostname; this.hostname = hostname;
this.roomInteractor = roomInteractor; this.roomInteractor = roomInteractor;
this.userRepository = userRepository; this.userRepository = userRepository;
this.rocketChatCache = rocketChatCache; this.rocketChatCache = rocketChatCache;
this.absoluteUrlHelper = absoluteUrlHelper; this.absoluteUrlHelper = absoluteUrlHelper;
this.methodCallHelper = methodCallHelper; this.methodCallHelper = methodCallHelper;
this.realmSpotlightRepository = realmSpotlightRepository; this.realmSpotlightRepository = realmSpotlightRepository;
}
@Override
public void bindView(@NonNull SidebarMainContract.View view) {
super.bindView(view);
if (TextUtils.isEmpty(hostname)) {
view.showEmptyScreen();
return;
} }
view.showScreen(); @Override
public void bindView(@NonNull SidebarMainContract.View view) {
subscribeToRooms(); super.bindView(view);
final Disposable subscription = Flowable.combineLatest( if (TextUtils.isEmpty(hostname)) {
userRepository.getCurrent().distinctUntilChanged(), view.showEmptyScreen();
absoluteUrlHelper.getRocketChatAbsoluteUrl().toFlowable(), return;
Pair::new }
)
.subscribeOn(AndroidSchedulers.from(BackgroundLooper.get())) view.showScreen();
.observeOn(AndroidSchedulers.mainThread())
.subscribe( subscribeToRooms();
pair -> {
userId = pair.first.orNull().getId(); final Disposable subscription = Flowable.combineLatest(
view.show(pair.first.orNull()); userRepository.getCurrent().distinctUntilChanged(),
}, absoluteUrlHelper.getRocketChatAbsoluteUrl().toFlowable(),
Logger::report Pair::new
); )
.subscribeOn(AndroidSchedulers.from(BackgroundLooper.get()))
addSubscription(subscription); .observeOn(AndroidSchedulers.mainThread())
} .subscribe(pair -> view.show(pair.first.orNull()), Logger::report);
@Override addSubscription(subscription);
public void onRoomSelected(Room room) { }
rocketChatCache.setSelectedRoomId(room.getRoomId());
} @Override
public void onRoomSelected(RoomSidebar roomSidebar) {
@Override rocketChatCache.setSelectedRoomId(roomSidebar.getRoomId());
public Flowable<List<Spotlight>> searchSpotlight(String term) { }
methodCallHelper.searchSpotlight(term);
return realmSpotlightRepository.getSuggestionsFor(term, 10); @Override
} public Flowable<List<Spotlight>> searchSpotlight(String term) {
methodCallHelper.searchSpotlight(term);
@Override return realmSpotlightRepository.getSuggestionsFor(term, 10);
public void onSpotlightSelected(Spotlight spotlight) { }
if (spotlight.getType().equals(Room.TYPE_DIRECT_MESSAGE)) {
String username = spotlight.getName(); @Override
methodCallHelper.createDirectMessage(username) public void onSpotlightSelected(Spotlight spotlight) {
.continueWithTask(task -> { if (spotlight.getType().equals(Room.TYPE_DIRECT_MESSAGE)) {
if (task.isCompleted()) { String username = spotlight.getName();
rocketChatCache.setSelectedRoomId(task.getResult()); methodCallHelper.createDirectMessage(username)
} .continueWithTask(task -> {
return null; if (task.isCompleted()) {
}); rocketChatCache.setSelectedRoomId(task.getResult());
} else { }
methodCallHelper.joinRoom(spotlight.getId()) return null;
.continueWithTask(task -> { });
if (task.isCompleted()) { } else {
rocketChatCache.setSelectedRoomId(spotlight.getId()); methodCallHelper.joinRoom(spotlight.getId())
.continueWithTask(task -> {
if (task.isCompleted()) {
rocketChatCache.setSelectedRoomId(spotlight.getId());
}
return null;
});
}
}
@Override
public void onUserOnline() {
updateCurrentUserStatus(User.STATUS_ONLINE);
}
@Override
public void onUserAway() {
updateCurrentUserStatus(User.STATUS_AWAY);
}
@Override
public void onUserBusy() {
updateCurrentUserStatus(User.STATUS_BUSY);
}
@Override
public void onUserOffline() {
updateCurrentUserStatus(User.STATUS_OFFLINE);
}
@Override
public void onLogout() {
methodCallHelper.logout().continueWith(new LogIfError());
}
private void subscribeToRooms() {
final Disposable subscription = roomInteractor.getOpenRooms()
.distinctUntilChanged()
.subscribeOn(AndroidSchedulers.from(BackgroundLooper.get()))
.observeOn(AndroidSchedulers.mainThread())
.subscribe(this::processRooms, Logger::report);
addSubscription(subscription);
}
private void processRooms(List<Room> roomList) {
ArrayList<RoomSidebar> roomSidebarList = new ArrayList<>();
for (Room room : roomList) {
String roomName = room.getName();
String roomType = room.getType();
roomSidebar = new RoomSidebar();
roomSidebar.setId(room.getId());
roomSidebar.setRoomId(room.getRoomId());
roomSidebar.setRoomName(roomName);
roomSidebar.setType(roomType);
roomSidebar.setAlert(room.isAlert());
roomSidebar.setFavorite(room.isFavorite());
roomSidebar.setUnread(room.getUnread());
roomSidebar.setUpdateAt(room.getUpdatedAt());
roomSidebar.setLastSeen(room.getLastSeen());
if (roomType.equals(Room.TYPE_DIRECT_MESSAGE)) {
getUserStatus(roomName);
} }
return null;
}); roomSidebarList.add(roomSidebar);
}
view.showRoomSidebarList(roomSidebarList);
} }
}
private void getUserStatus(String username) {
@Override final Disposable subscription = userRepository.getByUsername(username)
public void onUserOnline() { .distinctUntilChanged()
updateCurrentUserStatus(User.STATUS_ONLINE); .filter(Optional::isPresent)
} .map(Optional::get)
.map(User::getStatus)
@Override .subscribe(this::setUserStatus);
public void onUserAway() { addSubscription(subscription);
updateCurrentUserStatus(User.STATUS_AWAY); }
}
private void setUserStatus(String status) {
@Override roomSidebar.setUserStatus(status);
public void onUserBusy() { }
updateCurrentUserStatus(User.STATUS_BUSY);
} private void updateCurrentUserStatus(String status) {
methodCallHelper.setUserStatus(status).continueWith(new LogIfError());
@Override
public void onUserOffline() {
updateCurrentUserStatus(User.STATUS_OFFLINE);
}
@Override
public void onLogout() {
methodCallHelper.logout().continueWith(new LogIfError());
}
private void subscribeToRooms() {
final Disposable subscription = roomInteractor.getOpenRooms()
.distinctUntilChanged()
.subscribeOn(AndroidSchedulers.from(BackgroundLooper.get()))
.observeOn(AndroidSchedulers.mainThread())
.subscribe(this::processRooms, Logger::report);
addSubscription(subscription);
}
private void processRooms(List<Room> roomList) {
view.showRoomList(roomList);
for (Room room: roomList) {
if (room.isDirectMessage()) {
subscribeToDirectMessages(room.getName());
}
} }
}
private void subscribeToDirectMessages(String username) {
final Disposable disposable = userRepository.getByUsername(username)
.distinctUntilChanged()
.filter(Optional::isPresent)
.map(Optional::get)
.subscribeOn(AndroidSchedulers.from(BackgroundLooper.get()))
.observeOn(AndroidSchedulers.mainThread())
.subscribe(view::showUserStatus, Logger::report);
addSubscription(disposable);
}
private void updateCurrentUserStatus(String status) {
methodCallHelper.setUserStatus(status).continueWith(new LogIfError());
}
} }
\ No newline at end of file
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