diff --git a/commet/lib/client/matrix/matrix_room.dart b/commet/lib/client/matrix/matrix_room.dart index 6441a0a69..7bf012dec 100644 --- a/commet/lib/client/matrix/matrix_room.dart +++ b/commet/lib/client/matrix/matrix_room.dart @@ -117,6 +117,7 @@ class MatrixRoom extends Room { String get developerInfo => const JsonEncoder.withIndent(' ').convert(_matrixRoom.states); + Color? hashColor; @override Color get defaultColor { var comp = client.getComponent(); @@ -130,12 +131,22 @@ class MatrixRoom extends Room { } } - return getColorOfUser(identifier); + if (hashColor != null) return hashColor!; + + hashColor = MatrixPeer.hashColor(identifier); + + return hashColor!; } + // cache the result of push rule because this was becoming an expensive operation for ui stuff + matrix.PushRuleState? _pushRule; @override PushRule get pushRule { - switch (_matrixRoom.pushRuleState) { + if (_pushRule == null) { + _pushRule = _matrixRoom.pushRuleState; + } + + switch (_pushRule!) { case matrix.PushRuleState.notify: return PushRule.notify; case matrix.PushRuleState.mentionsOnly: @@ -145,6 +156,27 @@ class MatrixRoom extends Room { } } + @override + Future setPushRule(PushRule rule) async { + var newRule = _matrixRoom.pushRuleState; + + switch (rule) { + case PushRule.notify: + newRule = matrix.PushRuleState.notify; + break; + case PushRule.mentionsOnly: + newRule = matrix.PushRuleState.mentionsOnly; + break; + case PushRule.dontNotify: + newRule = matrix.PushRuleState.dontNotify; + break; + } + + await _matrixRoom.setPushRuleState(newRule); + _pushRule = _matrixRoom.pushRuleState; + _onUpdate.add(null); + } + @override ImageProvider? get avatar { final comp = client.getComponent(); @@ -521,26 +553,6 @@ class MatrixRoom extends Room { await _matrixRoom.enableEncryption(); } - @override - Future setPushRule(PushRule rule) async { - var newRule = _matrixRoom.pushRuleState; - - switch (rule) { - case PushRule.notify: - newRule = matrix.PushRuleState.notify; - break; - case PushRule.mentionsOnly: - newRule = matrix.PushRuleState.mentionsOnly; - break; - case PushRule.dontNotify: - newRule = matrix.PushRuleState.dontNotify; - break; - } - - await _matrixRoom.setPushRuleState(newRule); - _onUpdate.add(null); - } - @override Future setDisplayName(String newName) async { _displayName = newName; @@ -762,6 +774,7 @@ class MatrixRoom extends Room { void onRoomSyncUpdate(matrix.SyncUpdate event) { var update = event.rooms?.join?[_matrixRoom.id]; + if (update == null) return; _onUpdate.add(null); diff --git a/commet/lib/client/matrix/matrix_space.dart b/commet/lib/client/matrix/matrix_space.dart index 1f6c89a43..0d5b1e72d 100644 --- a/commet/lib/client/matrix/matrix_space.dart +++ b/commet/lib/client/matrix/matrix_space.dart @@ -56,18 +56,46 @@ class MatrixSpace extends Space { @override Color get color => MatrixPeer.hashColor(_matrixRoom.id); + + // cache the result of push rule because this was becoming an expensive operation for ui stuff + matrix.PushRuleState? _pushRule; @override PushRule get pushRule { - switch (_matrixRoom.pushRuleState) { + if (_pushRule == null) { + _pushRule = _matrixRoom.pushRuleState; + } + + switch (_pushRule!) { case matrix.PushRuleState.notify: return PushRule.notify; case matrix.PushRuleState.mentionsOnly: - return PushRule.notify; + return PushRule.mentionsOnly; case matrix.PushRuleState.dontNotify: return PushRule.dontNotify; } } + @override + Future setPushRule(PushRule rule) async { + var newRule = _matrixRoom.pushRuleState; + + switch (rule) { + case PushRule.notify: + newRule = matrix.PushRuleState.notify; + break; + case PushRule.mentionsOnly: + newRule = matrix.PushRuleState.mentionsOnly; + break; + case PushRule.dontNotify: + newRule = matrix.PushRuleState.dontNotify; + break; + } + + await _matrixRoom.setPushRuleState(newRule); + _pushRule = _matrixRoom.pushRuleState; + _onUpdate.add(null); + } + @override RoomVisibility get visibility { switch (_matrixRoom.joinRules) { @@ -147,14 +175,18 @@ class MatrixSpace extends Space { late List _subscriptions; + bool _isTopLevel = false; @override - bool get isTopLevel { + bool get isTopLevel => _isTopLevel; + + void _updateTopLevelStatus() { for (var room in _matrixClient.rooms.where((r) => r.isSpace)) { if (room.spaceChildren.any((child) => child.roomId == _matrixRoom.id)) { - return false; + _isTopLevel = false; + return; } } - return true; + _isTopLevel = true; } MatrixSpace( @@ -173,6 +205,9 @@ class MatrixSpace extends Space { client.onRoomAdded.listen((_) => updateRoomsList()), client.onRoomRemoved.listen(onClientRoomRemoved), client.matrixClient.onSync.stream.listen(onMatrixSync), + client.matrixClient.onRoomState.stream + .where((i) => i.roomId == room.id) + .listen(onStateChanged), // Subscribe to all child update events _rooms.onAdd.listen(_onRoomAdded), @@ -187,6 +222,12 @@ class MatrixSpace extends Space { } updateRoomsList(); + _updateTopLevelStatus(); + } + + void onStateChanged( + ({String roomId, matrix.StrippedStateEvent state}) event) { + refresh(); } @override @@ -313,26 +354,6 @@ class MatrixSpace extends Space { _onUpdate.add(null); } - @override - Future setPushRule(PushRule rule) async { - var newRule = _matrixRoom.pushRuleState; - - switch (rule) { - case PushRule.notify: - newRule = matrix.PushRuleState.notify; - break; - case PushRule.mentionsOnly: - newRule = matrix.PushRuleState.mentionsOnly; - break; - case PushRule.dontNotify: - newRule = matrix.PushRuleState.dontNotify; - break; - } - - await _matrixRoom.setPushRuleState(newRule); - _onUpdate.add(null); - } - @override Future loadExtra() async { var response = @@ -405,6 +426,7 @@ class MatrixSpace extends Space { for (var id in update.keys) { if (roomsWithChildren.any((i) => i.identifier == id)) { + _updateTopLevelStatus(); _onUpdate.add(null); } }