Merge branch 'roomlist-enhance-onlyLeftLists' into 'master'

[RoomList] Enhance onlyLeft lists and add more tests.

See merge request famedly/famedlysdk!21
This commit is contained in:
Marcel 2019-06-27 17:34:15 +00:00
commit 968b95e5c3
2 changed files with 54 additions and 3 deletions

View file

@ -78,9 +78,10 @@ class RoomList {
if (rooms[j].id == chatUpdate.id) break; if (rooms[j].id == chatUpdate.id) break;
} }
final bool found = (j < rooms.length - 1 && rooms[j].id == chatUpdate.id); final bool found = (j < rooms.length - 1 && rooms[j].id == chatUpdate.id);
final bool isLeftRoom = chatUpdate.membership == "leave";
// Does the chat already exist in the list rooms? // Does the chat already exist in the list rooms?
if (!found && chatUpdate.membership != "leave") { if (!found && ((!onlyLeft && !isLeftRoom) || (onlyLeft && isLeftRoom))) {
num position = chatUpdate.membership == "invite" ? 0 : j; num position = chatUpdate.membership == "invite" ? 0 : j;
// Add the new chat to the list // Add the new chat to the list
Room newRoom = Room( Room newRoom = Room(
@ -93,8 +94,9 @@ class RoomList {
rooms.insert(position, newRoom); rooms.insert(position, newRoom);
if (onInsert != null) onInsert(position); if (onInsert != null) onInsert(position);
} }
// If the membership is "leave" then remove the item and stop here // If the membership is "leave" or not "leave" but onlyLeft=true then remove the item and stop here
else if (found && chatUpdate.membership == "leave") { else if (found &&
((!onlyLeft && isLeftRoom) || (onlyLeft && !isLeftRoom))) {
rooms.removeAt(j); rooms.removeAt(j);
if (onRemove != null) onRemove(j); if (onRemove != null) onRemove(j);
} }

View file

@ -168,5 +168,54 @@ void main() {
expect(roomList.rooms[0].lastMessage, "Testcase 2"); expect(roomList.rooms[0].lastMessage, "Testcase 2");
expect(roomList.rooms[0].timeCreated, now); expect(roomList.rooms[0].timeCreated, now);
}); });
test("onlyLeft", () async {
final Client client = Client("testclient");
client.homeserver = "https://testserver.abc";
int updateCount = 0;
List<int> insertList = [];
List<int> removeList = [];
RoomList roomList = RoomList(
client: client,
onlyLeft: true,
rooms: [],
onUpdate: () {
updateCount++;
},
onInsert: (int insertID) {
insertList.add(insertID);
},
onRemove: (int removeID) {
insertList.add(removeID);
});
client.connection.onRoomUpdate.add(RoomUpdate(
id: "1",
membership: "join",
notification_count: 2,
highlight_count: 1,
limitedTimeline: false,
prev_batch: "1234",
));
client.connection.onRoomUpdate.add(RoomUpdate(
id: "2",
membership: "leave",
notification_count: 2,
highlight_count: 1,
limitedTimeline: false,
prev_batch: "1234",
));
await new Future.delayed(new Duration(milliseconds: 50));
expect(roomList.eventSub != null, true);
expect(roomList.roomSub != null, true);
expect(roomList.rooms[0].id, "2");
expect(updateCount, 2);
expect(insertList, [0]);
expect(removeList, []);
});
}); });
} }