Commit 78947080 authored by Filipe de Lima Brito's avatar Filipe de Lima Brito

Update RoomListAdapter.java

parent 296a06c0
...@@ -5,6 +5,7 @@ import android.support.v7.widget.RecyclerView; ...@@ -5,6 +5,7 @@ import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.ViewGroup; import android.view.ViewGroup;
import chat.rocket.core.models.RoomSidebar;
import chat.rocket.core.models.Spotlight; import chat.rocket.core.models.Spotlight;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
...@@ -12,7 +13,6 @@ import java.util.List; ...@@ -12,7 +13,6 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import chat.rocket.android.R; import chat.rocket.android.R;
import chat.rocket.android.widget.internal.RoomListItemView; import chat.rocket.android.widget.internal.RoomListItemView;
import chat.rocket.core.models.Room;
public class RoomListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { public class RoomListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
...@@ -22,7 +22,7 @@ public class RoomListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolde ...@@ -22,7 +22,7 @@ public class RoomListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolde
private static final int VIEW_TYPE_HEADER = 0; private static final int VIEW_TYPE_HEADER = 0;
private static final int VIEW_TYPE_ROOM = 1; private static final int VIEW_TYPE_ROOM = 1;
private List<Room> roomList = Collections.emptyList(); private List<RoomSidebar> roomSidebarList = Collections.emptyList();
private List<Spotlight> spotlightList = Collections.emptyList(); private List<Spotlight> spotlightList = Collections.emptyList();
private List<RoomListHeader> roomListHeaders = Collections.emptyList(); private List<RoomListHeader> roomListHeaders = Collections.emptyList();
private Map<Integer, RoomListHeader> headersPosition = new HashMap<>(); private Map<Integer, RoomListHeader> headersPosition = new HashMap<>();
...@@ -32,9 +32,9 @@ public class RoomListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolde ...@@ -32,9 +32,9 @@ public class RoomListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolde
private OnItemClickListener externalListener; private OnItemClickListener externalListener;
private OnItemClickListener listener = new OnItemClickListener() { private OnItemClickListener listener = new OnItemClickListener() {
@Override @Override
public void onItemClick(Room room) { public void onItemClick(RoomSidebar roomSidebar) {
if (externalListener != null) { if (externalListener != null) {
externalListener.onItemClick(room); externalListener.onItemClick(roomSidebar);
} }
} }
...@@ -48,17 +48,17 @@ public class RoomListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolde ...@@ -48,17 +48,17 @@ public class RoomListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolde
public void setRoomListHeaders(@NonNull List<RoomListHeader> roomListHeaders) { public void setRoomListHeaders(@NonNull List<RoomListHeader> roomListHeaders) {
this.roomListHeaders = roomListHeaders; this.roomListHeaders = roomListHeaders;
updateRoomList(); updateRoomSidebarList();
} }
public void setRooms(@NonNull List<Room> roomList) { public void setRoomSidebarList(@NonNull List<RoomSidebar> roomSidebarList) {
this.roomList = roomList; this.roomSidebarList = roomSidebarList;
updateRoomList(); updateRoomSidebarList();
} }
public void setSpotlightList(@NonNull List<Spotlight> spotlightList) { public void setSpotlightList(@NonNull List<Spotlight> spotlightList) {
this.spotlightList = spotlightList; this.spotlightList = spotlightList;
updateRoomList(); updateRoomSidebarList();
} }
public void setMode(int mode) { public void setMode(int mode) {
...@@ -77,9 +77,9 @@ public class RoomListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolde ...@@ -77,9 +77,9 @@ public class RoomListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolde
@Override @Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == VIEW_TYPE_HEADER) { if (viewType == VIEW_TYPE_HEADER) {
return new RoomListHeaderViewHolder( return new RoomListHeaderViewHolder(LayoutInflater
LayoutInflater.from(parent.getContext()) .from(parent.getContext())
.inflate(R.layout.room_list_header, parent, false) .inflate(R.layout.room_list_header, parent, false)
); );
} }
return new RoomListItemViewHolder(new RoomListItemView(parent.getContext()), listener); return new RoomListItemViewHolder(new RoomListItemView(parent.getContext()), listener);
...@@ -89,13 +89,12 @@ public class RoomListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolde ...@@ -89,13 +89,12 @@ public class RoomListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolde
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if (mode == MODE_ROOM) { if (mode == MODE_ROOM) {
if (getItemViewType(position) == VIEW_TYPE_HEADER) { if (getItemViewType(position) == VIEW_TYPE_HEADER) {
((RoomListHeaderViewHolder) holder) ((RoomListHeaderViewHolder) holder).bind(headersPosition.get(position));
.bind(headersPosition.get(position));
return; return;
} }
((RoomListItemViewHolder) holder) RoomSidebar roomSidebar = roomSidebarList.get(position - getTotalHeadersBeforePosition(position));
.bind(roomList.get(position - getTotalHeadersBeforePosition(position))); ((RoomListItemViewHolder) holder).bind(roomSidebar);
} else if (mode == MODE_SPOTLIGHT) { } else if (mode == MODE_SPOTLIGHT) {
((RoomListItemViewHolder) holder).bind(spotlightList.get(position)); ((RoomListItemViewHolder) holder).bind(spotlightList.get(position));
} }
...@@ -106,7 +105,7 @@ public class RoomListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolde ...@@ -106,7 +105,7 @@ public class RoomListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolde
if (mode == MODE_SPOTLIGHT) { if (mode == MODE_SPOTLIGHT) {
return spotlightList.size(); return spotlightList.size();
} }
return roomList.size() + headersPosition.size(); return roomSidebarList.size() + headersPosition.size();
} }
@Override @Override
...@@ -121,7 +120,7 @@ public class RoomListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolde ...@@ -121,7 +120,7 @@ public class RoomListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolde
return VIEW_TYPE_ROOM; return VIEW_TYPE_ROOM;
} }
private void updateRoomList() { private void updateRoomSidebarList() {
if (mode == MODE_ROOM) { if (mode == MODE_ROOM) {
sortRoomList(); sortRoomList();
calculateHeadersPosition(); calculateHeadersPosition();
...@@ -132,18 +131,18 @@ public class RoomListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolde ...@@ -132,18 +131,18 @@ public class RoomListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolde
private void sortRoomList() { private void sortRoomList() {
int totalHeaders = roomListHeaders.size(); int totalHeaders = roomListHeaders.size();
Collections.sort(roomList, (room, anotherRoom) -> { Collections.sort(roomSidebarList, (roomSidebar, anotherRoom) -> {
for (int i = 0; i < totalHeaders; i++) { for (int i = 0; i < totalHeaders; i++) {
final RoomListHeader header = roomListHeaders.get(i); final RoomListHeader header = roomListHeaders.get(i);
if (header.owns(room) && !header.owns(anotherRoom)) { if (header.owns(roomSidebar) && !header.owns(anotherRoom)) {
return -1; return -1;
} else if (!header.owns(room) && header.owns(anotherRoom)) { } else if (!header.owns(roomSidebar) && header.owns(anotherRoom)) {
return 1; return 1;
} }
} }
return room.getName().compareTo(anotherRoom.getName()); return roomSidebar.getRoomName().compareTo(anotherRoom.getRoomName());
}); });
} }
...@@ -151,19 +150,19 @@ public class RoomListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolde ...@@ -151,19 +150,19 @@ public class RoomListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolde
headersPosition.clear(); headersPosition.clear();
int roomIdx = 0; int roomIdx = 0;
int totalRooms = roomList.size(); int totalRooms = roomSidebarList.size();
int totalHeaders = roomListHeaders.size(); int totalHeaders = roomListHeaders.size();
for (int i = 0; i < totalHeaders; i++) { for (int i = 0; i < totalHeaders; i++) {
final RoomListHeader header = roomListHeaders.get(i); final RoomListHeader header = roomListHeaders.get(i);
if (!header.shouldShow(roomList)) { if (!header.shouldShow(roomSidebarList)) {
continue; continue;
} }
headersPosition.put(roomIdx + headersPosition.size(), header); headersPosition.put(roomIdx + headersPosition.size(), header);
for (; roomIdx < totalRooms; roomIdx++) { for (; roomIdx < totalRooms; roomIdx++) {
final Room room = roomList.get(roomIdx); final RoomSidebar roomSidebar = roomSidebarList.get(roomIdx);
if (!header.owns(room)) { if (!header.owns(roomSidebar)) {
break; break;
} }
} }
...@@ -185,7 +184,7 @@ public class RoomListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolde ...@@ -185,7 +184,7 @@ public class RoomListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolde
} }
public interface OnItemClickListener { public interface OnItemClickListener {
void onItemClick(Room room); void onItemClick(RoomSidebar roomSidebar);
void onItemClick(Spotlight spotlight); void onItemClick(Spotlight spotlight);
} }
......
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