Skip to content

Commit

Permalink
Sub Spaces (#267)
Browse files Browse the repository at this point in the history
  • Loading branch information
Airyzz authored Jul 4, 2024
1 parent 14c999a commit 8a8037c
Show file tree
Hide file tree
Showing 20 changed files with 683 additions and 474 deletions.
2 changes: 1 addition & 1 deletion commet/lib/client/client_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ class ClientManager {
void _addSpace(Client client, int index) {
var space = client.spaces[index];
space.onUpdate.listen((_) => spaceUpdated(space));
space.onChildUpdated.listen((_) => spaceChildUpdated(space));
space.onChildRoomUpdated.listen((_) => spaceChildUpdated(space));
spaces.add(client.spaces[index]);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ extension MatrixExtensions on Client {
topic = topicState.first.content['topic'] as String?;

return GenericRoomPreview(roomId,
avatar: avatar, displayName: displayName!, topic: topic);
avatar: avatar,
displayName: displayName!,
type: RoomPreviewType.room,
topic: topic);
}

Future<void> addEmoticonRoomPack(String roomId, String packKey) async {
Expand Down
9 changes: 9 additions & 0 deletions commet/lib/client/matrix/matrix_room_preview.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,21 @@ class MatrixSpaceRoomChunkPreview implements RoomPreview {
@override
String? get topic => chunk.topic;

@override
late RoomPreviewType type;

MatrixSpaceRoomChunkPreview(this.chunk, this.matrixClient) {
avatar = chunk.avatarUrl != null
? NetworkImage(chunk.avatarUrl!
.getThumbnail(matrixClient, width: 60, height: 60)
.toString())
: null;

if (chunk.roomType == "m.space") {
type = RoomPreviewType.space;
} else {
type = RoomPreviewType.room;
}
}

@override
Expand Down
51 changes: 43 additions & 8 deletions commet/lib/client/matrix/matrix_space.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ class MatrixSpace extends Space {
final NotifyingList<RoomPreview> _previews =
NotifyingList.empty(growable: true);

@override
List<Space> get subspaces => List.from(_matrixRoom.spaceChildren
.map((child) => _client.getSpace(child.roomId!))
.where((e) => e != null));

final StreamController<Room> _onChildUpdated = StreamController.broadcast();

ImageProvider? _avatar;
Expand Down Expand Up @@ -94,19 +99,19 @@ class MatrixSpace extends Space {
String get identifier => _matrixRoom.id;

@override
Stream<int> get onChildPreviewAdded => _previews.onAdd;
Stream<int> get onChildRoomPreviewAdded => _previews.onAdd;

@override
Stream<int> get onChildPreviewRemoved => _previews.onRemove;
Stream<int> get onChildRoomPreviewRemoved => _previews.onRemove;

@override
Stream<void> get onChildPreviewsUpdated => _previews.onListUpdated;
Stream<void> get onChildRoomPreviewsUpdated => _previews.onListUpdated;

@override
Stream<Room> get onChildUpdated => _onChildUpdated.stream;
Stream<Room> get onChildRoomUpdated => _onChildUpdated.stream;

@override
Stream<void> get onChildrenUpdated => _rooms.onListUpdated;
Stream<void> get onChildRoomsUpdated => _rooms.onListUpdated;

@override
Stream<int> get onRoomAdded => _rooms.onAdd;
Expand All @@ -128,6 +133,32 @@ class MatrixSpace extends Space {

late List<StreamSubscription> _subscriptions;

@override
bool get isTopLevel {
var parents = _matrixRoom.spaceParents;

bool anyParentContainsRoom = false;
for (var parent in parents) {
if (parent.roomId == null) {
continue;
}

var room = _matrixClient.getRoomById(parent.roomId!);
if (room == null) {
continue;
}

var contains =
room.spaceChildren.any((child) => child.roomId == _matrixRoom.id);
if (contains) {
anyParentContainsRoom = true;
break;
}
}

return !anyParentContainsRoom;
}

MatrixSpace(
MatrixClient client, matrix.Room room, matrix.Client matrixClient) {
_matrixRoom = room;
Expand Down Expand Up @@ -199,10 +230,12 @@ class MatrixSpace extends Space {
var room = client.getRoom(child.roomId!);
if (room != null) {
_previews.removeWhere((element) => element.roomId == room.identifier);
if (!containsRoom(room.identifier)) {

if (!containsRoom(room.identifier) &&
!client.hasSpace(room.identifier)) {
_rooms.add(room);
}
} else {}
}
}
}

Expand Down Expand Up @@ -272,7 +305,9 @@ class MatrixSpace extends Space {
// read child rooms
response.rooms
.where((element) => element.roomId != identifier)
.where((element) => !containsRoom(element.roomId))
.where((element) =>
_matrixClient.getRoomById(element.roomId)?.membership !=
matrix.Membership.join)
.forEach((element) {
_previews.add(MatrixSpaceRoomChunkPreview(element, _matrixClient));
});
Expand Down
8 changes: 7 additions & 1 deletion commet/lib/client/room_preview.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import 'package:flutter/material.dart';

enum RoomPreviewType { room, space }

abstract class RoomPreview {
String get roomId;
ImageProvider? get avatar;
String get displayName;
String? get topic;
Color get color;
RoomPreviewType get type;
}

class GenericRoomPreview implements RoomPreview {
Expand All @@ -22,8 +25,11 @@ class GenericRoomPreview implements RoomPreview {
String? topic;

GenericRoomPreview(this.roomId,
{this.avatar, required this.displayName, this.topic});
{this.avatar, required this.displayName, required this.type, this.topic});

@override
Color get color => Colors.grey;

@override
RoomPreviewType type;
}
14 changes: 9 additions & 5 deletions commet/lib/client/space.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ abstract class Space {

List<Room> get rooms;

List<Space> get subspaces;

bool get isTopLevel;

String get topic;

PushRule get pushRule;
Expand All @@ -33,19 +37,19 @@ abstract class Space {

Stream<void> get onUpdate;

Stream<Room> get onChildUpdated;
Stream<Room> get onChildRoomUpdated;

Stream<int> get onRoomAdded;

Stream<int> get onRoomRemoved;

Stream<void> get onChildrenUpdated;
Stream<void> get onChildRoomsUpdated;

Stream<int> get onChildPreviewAdded;
Stream<int> get onChildRoomPreviewAdded;

Stream<int> get onChildPreviewRemoved;
Stream<int> get onChildRoomPreviewRemoved;

Stream<void> get onChildPreviewsUpdated;
Stream<void> get onChildRoomPreviewsUpdated;

List<RoomPreview> get childPreviews;

Expand Down
Loading

0 comments on commit 8a8037c

Please sign in to comment.