[Room] Add a onUpdate callback

This commit is contained in:
Christian Pauly 2019-09-03 13:24:44 +02:00
parent 902df33d50
commit e6a859d83c
3 changed files with 17 additions and 0 deletions

View File

@ -34,6 +34,8 @@ import './User.dart';
import 'Connection.dart'; import 'Connection.dart';
import 'Timeline.dart'; import 'Timeline.dart';
typedef onRoomUpdate = void Function();
/// Represents a Matrix room. /// Represents a Matrix room.
class Room { class Room {
/// The full qualified Matrix ID for the room in the format '!localid:server.abc'. /// The full qualified Matrix ID for the room in the format '!localid:server.abc'.
@ -64,6 +66,9 @@ class Room {
/// ID of the fully read marker event. /// ID of the fully read marker event.
String fullyRead; String fullyRead;
/// If something changes, this callback will be triggered.
onRoomUpdate onUpdate;
/// The name of the room if set by a participant. /// The name of the room if set by a participant.
String get name { String get name {
if (states["m.room.name"] != null && if (states["m.room.name"] != null &&

View File

@ -140,6 +140,7 @@ class RoomList {
if (chatUpdate.summary.mInvitedMemberCount != null) if (chatUpdate.summary.mInvitedMemberCount != null)
rooms[j].mInvitedMemberCount = chatUpdate.summary.mInvitedMemberCount; rooms[j].mInvitedMemberCount = chatUpdate.summary.mInvitedMemberCount;
} }
if (rooms[j].onUpdate != null) rooms[j].onUpdate();
} }
sortAndUpdate(); sortAndUpdate();
} }
@ -158,6 +159,7 @@ class RoomList {
if (rooms[j].states[stateEvent.key] != null && if (rooms[j].states[stateEvent.key] != null &&
rooms[j].states[stateEvent.key].time > stateEvent.time) return; rooms[j].states[stateEvent.key].time > stateEvent.time) return;
rooms[j].states[stateEvent.key] = stateEvent; rooms[j].states[stateEvent.key] = stateEvent;
if (rooms[j].onUpdate != null) rooms[j].onUpdate();
sortAndUpdate(); sortAndUpdate();
} }

View File

@ -129,6 +129,15 @@ void main() {
ChatTime now = ChatTime.now(); ChatTime now = ChatTime.now();
int roomUpdates = 0;
roomList.rooms[0].onUpdate = () {
roomUpdates++;
};
roomList.rooms[1].onUpdate = () {
roomUpdates++;
};
client.connection.onEvent.add(EventUpdate( client.connection.onEvent.add(EventUpdate(
type: "timeline", type: "timeline",
roomID: "1", roomID: "1",
@ -158,6 +167,7 @@ void main() {
await new Future.delayed(new Duration(milliseconds: 50)); await new Future.delayed(new Duration(milliseconds: 50));
expect(updateCount, 4); expect(updateCount, 4);
expect(roomUpdates, 2);
expect(insertList, [0, 1]); expect(insertList, [0, 1]);
expect(removeList, []); expect(removeList, []);