Commit 02f7c65b authored by Filipe de Lima Brito's avatar Filipe de Lima Brito

Update SidebarMainPresenter.java

parent 99604ed3
...@@ -3,8 +3,6 @@ package chat.rocket.android.fragment.sidebar; ...@@ -3,8 +3,6 @@ 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 com.hadisatrio.optional.Optional;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
...@@ -35,7 +33,7 @@ public class SidebarMainPresenter extends BasePresenter<SidebarMainContract.View ...@@ -35,7 +33,7 @@ public class SidebarMainPresenter extends BasePresenter<SidebarMainContract.View
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; private List<RoomSidebar> roomSidebarList;
public SidebarMainPresenter(String hostname, public SidebarMainPresenter(String hostname,
RoomInteractor roomInteractor, RoomInteractor roomInteractor,
...@@ -146,12 +144,14 @@ public class SidebarMainPresenter extends BasePresenter<SidebarMainContract.View ...@@ -146,12 +144,14 @@ public class SidebarMainPresenter extends BasePresenter<SidebarMainContract.View
} }
private void processRooms(List<Room> roomList) { private void processRooms(List<Room> roomList) {
ArrayList<RoomSidebar> roomSidebarList = new ArrayList<>(); roomSidebarList = new ArrayList<>();
List<String> userToObserverList = new ArrayList<>();
for (Room room : roomList) { for (Room room : roomList) {
String roomName = room.getName(); String roomName = room.getName();
String roomType = room.getType(); String roomType = room.getType();
roomSidebar = new RoomSidebar(); RoomSidebar roomSidebar = new RoomSidebar();
roomSidebar.setId(room.getId()); roomSidebar.setId(room.getId());
roomSidebar.setRoomId(room.getRoomId()); roomSidebar.setRoomId(room.getRoomId());
...@@ -164,26 +164,38 @@ public class SidebarMainPresenter extends BasePresenter<SidebarMainContract.View ...@@ -164,26 +164,38 @@ public class SidebarMainPresenter extends BasePresenter<SidebarMainContract.View
roomSidebar.setLastSeen(room.getLastSeen()); roomSidebar.setLastSeen(room.getLastSeen());
if (roomType.equals(Room.TYPE_DIRECT_MESSAGE)) { if (roomType.equals(Room.TYPE_DIRECT_MESSAGE)) {
getUserStatus(roomName); userToObserverList.add(roomName);
} }
roomSidebarList.add(roomSidebar); roomSidebarList.add(roomSidebar);
} }
view.showRoomSidebarList(roomSidebarList); if (userToObserverList.isEmpty()) {
view.showRoomSidebarList(roomSidebarList);
} else {
getUsersStatus();
}
} }
private void getUserStatus(String username) { private void getUsersStatus() {
final Disposable subscription = userRepository.getByUsername(username) // TODO Filter when Android Studion uses the java8 features (removeIf).
// .filter(userList -> userList.removeIf(user -> !userToObserverList.contains(user.getUsername())))
final Disposable subscription = userRepository.getAll()
.distinctUntilChanged() .distinctUntilChanged()
.filter(Optional::isPresent) .subscribeOn(AndroidSchedulers.from(BackgroundLooper.get()))
.map(Optional::get) .observeOn(AndroidSchedulers.mainThread())
.map(User::getStatus) .subscribe(this::processUsers, Logger::report);
.subscribe(this::setUserStatus);
addSubscription(subscription); addSubscription(subscription);
} }
private void setUserStatus(String status) { private void processUsers(List<User> userList) {
roomSidebar.setUserStatus(status); for (User user: userList) {
for(RoomSidebar roomSidebar: roomSidebarList) {
if (roomSidebar.getRoomName().equals(user.getUsername())) {
roomSidebar.setUserStatus(user.getStatus());
}
}
}
view.showRoomSidebarList(roomSidebarList);
} }
private void updateCurrentUserStatus(String 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