From d0b74ca68afdafe4acf9e391fd5da80f1bae0d14 Mon Sep 17 00:00:00 2001 From: hannathkadher Date: Mon, 18 Nov 2024 20:40:41 +0400 Subject: [PATCH] added incoming and outgoing connections --- .../model/connection_model.dart | 21 +++++-- .../spaces_management/model/space_model.dart | 31 ++++++---- .../widgets/community_structure_widget.dart | 57 ++++++++----------- 3 files changed, 62 insertions(+), 47 deletions(-) diff --git a/lib/pages/spaces_management/model/connection_model.dart b/lib/pages/spaces_management/model/connection_model.dart index a078e550..650781d6 100644 --- a/lib/pages/spaces_management/model/connection_model.dart +++ b/lib/pages/spaces_management/model/connection_model.dart @@ -5,8 +5,21 @@ class Connection { final SpaceModel endSpace; final String direction; - Connection( - {required this.startSpace, - required this.endSpace, - required this.direction}); + Connection({required this.startSpace, required this.endSpace, required this.direction}); + + Map toMap() { + return { + 'startUuid': startSpace.uuid ?? 'unsaved-start-space-${startSpace.name}', // Fallback for unsaved spaces + 'endUuid': endSpace.uuid ?? 'unsaved-end-space-${endSpace.name}', // Fallback for unsaved spaces + 'direction': direction, + }; + } + + static Connection fromMap(Map map, Map spaces) { + return Connection( + startSpace: spaces[map['startUuid']]!, + endSpace: spaces[map['endUuid']]!, + direction: map['direction'], + ); + } } diff --git a/lib/pages/spaces_management/model/space_model.dart b/lib/pages/spaces_management/model/space_model.dart index 71af8cce..6363a331 100644 --- a/lib/pages/spaces_management/model/space_model.dart +++ b/lib/pages/spaces_management/model/space_model.dart @@ -1,5 +1,6 @@ import 'dart:ui'; import 'package:syncrow_web/pages/spaces_management/model/community_model.dart'; +import 'package:syncrow_web/pages/spaces_management/model/connection_model.dart'; class SpaceModel { final String? uuid; @@ -8,12 +9,15 @@ class SpaceModel { final String name; final bool isPrivate; final String? invitationCode; - final SpaceModel? parent; + SpaceModel? parent; final CommunityModel? community; - final List children; + List children; Offset position; bool isHovered; + List outgoingConnections = []; // Connections from this space + List incomingConnections = []; // Connections to this space + SpaceModel({ this.uuid, this.spaceTuyaUuid, @@ -35,15 +39,10 @@ class SpaceModel { name: json['spaceName'], isPrivate: json['isPrivate'] ?? false, invitationCode: json['invitationCode'], - parent: - json['parent'] != null ? SpaceModel.fromJson(json['parent']) : null, - community: json['community'] != null - ? CommunityModel.fromJson(json['community']) - : null, + parent: json['parent'] != null ? SpaceModel.fromJson(json['parent']) : null, + community: json['community'] != null ? CommunityModel.fromJson(json['community']) : null, children: json['children'] != null - ? (json['children'] as List) - .map((child) => SpaceModel.fromJson(child)) - .toList() + ? (json['children'] as List).map((child) => SpaceModel.fromJson(child)).toList() : [], icon: json['icon'] as String?, position: json['position'] != null @@ -60,12 +59,22 @@ class SpaceModel { 'name': name, 'isPrivate': isPrivate, 'invitationCode': invitationCode, - 'parent': parent?.toMap(), + 'parent': parent?.uuid, 'community': community?.toMap(), 'children': children.map((child) => child.toMap()).toList(), 'icon': icon, 'position': {'dx': position.dx, 'dy': position.dy}, 'isHovered': isHovered, + 'outgoingConnections': outgoingConnections.map((c) => c.toMap()).toList(), + 'incomingConnections': incomingConnections.map((c) => c.toMap()).toList(), }; } + + void addOutgoingConnection(Connection connection) { + outgoingConnections.add(connection); + } + + void addIncomingConnection(Connection connection) { + incomingConnections.add(connection); + } } diff --git a/lib/pages/spaces_management/widgets/community_structure_widget.dart b/lib/pages/spaces_management/widgets/community_structure_widget.dart index 7c1c29e1..9eca6913 100644 --- a/lib/pages/spaces_management/widgets/community_structure_widget.dart +++ b/lib/pages/spaces_management/widgets/community_structure_widget.dart @@ -53,20 +53,17 @@ class _CommunityStructureAreaState extends State { child: Stack( children: [ for (var connection in connections) - CustomPaint( - painter: CurvedLinePainter([connection])), + CustomPaint(painter: CurvedLinePainter([connection])), for (var entry in spaces.asMap().entries) Positioned( left: entry.value.position.dx, top: entry.value.position.dy, child: SpaceCardWidget( index: entry.key, - onButtonTap: (int index, Offset newPosition, - String direction) { + onButtonTap: (int index, Offset newPosition, String direction) { _showCreateSpaceDialog( screenSize, - position: - spaces[index].position + newPosition, + position: spaces[index].position + newPosition, parentIndex: index, direction: direction, ); @@ -135,13 +132,12 @@ class _CommunityStructureAreaState extends State { if (widget.selectedCommunity != null) Text( widget.selectedCommunity!.name, - style: const TextStyle( - fontSize: 16, color: ColorsManager.blackColor), + style: const TextStyle(fontSize: 16, color: ColorsManager.blackColor), ), ], ), // Show "Save" button only if there are spaces - if (spaces.isNotEmpty && widget.selectedCommunity != null ) + if (spaces.isNotEmpty && widget.selectedCommunity != null) ElevatedButton.icon( onPressed: () { _saveSpaces(); @@ -149,14 +145,12 @@ class _CommunityStructureAreaState extends State { icon: const Icon(Icons.save, size: 18), label: const Text("Save"), style: ElevatedButton.styleFrom( - backgroundColor: ColorsManager.whiteColors, foregroundColor: Colors.black, shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(8.0), ), - padding: - const EdgeInsets.symmetric(horizontal: 12.0, vertical: 8.0), + padding: const EdgeInsets.symmetric(horizontal: 12.0, vertical: 8.0), side: BorderSide(color: Colors.grey.shade300), elevation: 0, ), @@ -169,29 +163,19 @@ class _CommunityStructureAreaState extends State { void _updateNodePosition(SpaceModel node, Offset newPosition) { setState(() { node.position = newPosition; - - // Expand canvas to the right when node approaches the right edge if (node.position.dx >= canvasWidth - 200) { canvasWidth += 200; } - - // Expand canvas downward when node approaches the bottom edge if (node.position.dy >= canvasHeight - 200) { canvasHeight += 200; } - - // Expand canvas to the left when node approaches the left edge if (node.position.dx <= 200) { double shiftAmount = 200; canvasWidth += shiftAmount; - - // Shift all nodes to the right by shiftAmount for (var n in spaces) { n.position = Offset(n.position.dx + shiftAmount, n.position.dy); } } - - // Prevent nodes from going out of bounds on top edge if (node.position.dy < 0) { node.position = Offset(node.position.dx, 0); } @@ -210,25 +194,30 @@ class _CommunityStructureAreaState extends State { Offset centerPosition = position ?? Offset( screenSize.width / 2 - 75, // Center horizontally - screenSize.height / 2 - - 50, // Slightly above the center vertically + screenSize.height / 2 - 50, // Slightly above the center vertically ); - SpaceModel newSpace = - SpaceModel(name: name, icon: icon, position: centerPosition, isPrivate: false, children: []); - spaces.add(newSpace); + SpaceModel newSpace = SpaceModel( + name: name, + icon: icon, + position: centerPosition, + isPrivate: false, + children: [], + ); - _updateNodePosition(newSpace, newSpace.position); - - // Add connection for down-button if (parentIndex != null && direction != null) { SpaceModel parentSpace = spaces[parentIndex]; + newSpace.parent = parentSpace; + parentSpace.children.add(newSpace); connections.add(Connection( startSpace: parentSpace, endSpace: newSpace, direction: direction, )); } + + spaces.add(newSpace); + _updateNodePosition(newSpace, newSpace.position); }); }, ); @@ -243,8 +232,12 @@ class _CommunityStructureAreaState extends State { } void _saveSpaces() { - // Implement your save functionality here + List> spaceData = spaces.map((space) => space.toMap()).toList(); + List> connectionData = + connections.map((connection) => connection.toMap()).toList(); - print("Spaces saved: ${spaces.length}"); + // Save to local storage, a file, or send to a backend + print('Spaces: ${spaceData}'); + print('Connections: ${connectionData}'); } }