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;
import android.support.annotation.NonNull;
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 io.reactivex.Flowable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import java.util.ArrayList;
import java.util.List;
import chat.rocket.android.BackgroundLooper;
import chat.rocket.android.RocketChatCache;
......@@ -20,19 +18,24 @@ import chat.rocket.android.helper.TextUtils;
import chat.rocket.android.shared.BasePresenter;
import chat.rocket.core.interactors.RoomInteractor;
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.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 {
private final String hostname;
private String userId;
private final RoomInteractor roomInteractor;
private final UserRepository userRepository;
private final RocketChatCache rocketChatCache;
private final AbsoluteUrlHelper absoluteUrlHelper;
private final MethodCallHelper methodCallHelper;
private RealmSpotlightRepository realmSpotlightRepository;
private RoomSidebar roomSidebar;
public SidebarMainPresenter(String hostname,
RoomInteractor roomInteractor,
......@@ -70,20 +73,14 @@ public class SidebarMainPresenter extends BasePresenter<SidebarMainContract.View
)
.subscribeOn(AndroidSchedulers.from(BackgroundLooper.get()))
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
pair -> {
userId = pair.first.orNull().getId();
view.show(pair.first.orNull());
},
Logger::report
);
.subscribe(pair -> view.show(pair.first.orNull()), Logger::report);
addSubscription(subscription);
}
@Override
public void onRoomSelected(Room room) {
rocketChatCache.setSelectedRoomId(room.getRoomId());
public void onRoomSelected(RoomSidebar roomSidebar) {
rocketChatCache.setSelectedRoomId(roomSidebar.getRoomId());
}
@Override
......@@ -149,23 +146,44 @@ public class SidebarMainPresenter extends BasePresenter<SidebarMainContract.View
}
private void processRooms(List<Room> roomList) {
view.showRoomList(roomList);
for (Room room: roomList) {
if (room.isDirectMessage()) {
subscribeToDirectMessages(room.getName());
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);
}
roomSidebarList.add(roomSidebar);
}
view.showRoomSidebarList(roomSidebarList);
}
private void subscribeToDirectMessages(String username) {
final Disposable disposable = userRepository.getByUsername(username)
private void getUserStatus(String username) {
final Disposable subscription = 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);
.map(User::getStatus)
.subscribe(this::setUserStatus);
addSubscription(subscription);
}
private void setUserStatus(String status) {
roomSidebar.setUserStatus(status);
}
private void updateCurrentUserStatus(String status) {
......
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