added incoming and outgoing connections

This commit is contained in:
hannathkadher
2024-11-18 20:40:41 +04:00
parent 836c44fd95
commit d0b74ca68a
3 changed files with 62 additions and 47 deletions

View File

@ -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<String, dynamic> 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<String, dynamic> map, Map<String, SpaceModel> spaces) {
return Connection(
startSpace: spaces[map['startUuid']]!,
endSpace: spaces[map['endUuid']]!,
direction: map['direction'],
);
}
}

View File

@ -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<SpaceModel> children;
List<SpaceModel> children;
Offset position;
bool isHovered;
List<Connection> outgoingConnections = []; // Connections from this space
List<Connection> 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);
}
}

View File

@ -53,20 +53,17 @@ class _CommunityStructureAreaState extends State<CommunityStructureArea> {
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,8 +132,7 @@ class _CommunityStructureAreaState extends State<CommunityStructureArea> {
if (widget.selectedCommunity != null)
Text(
widget.selectedCommunity!.name,
style: const TextStyle(
fontSize: 16, color: ColorsManager.blackColor),
style: const TextStyle(fontSize: 16, color: ColorsManager.blackColor),
),
],
),
@ -149,14 +145,12 @@ class _CommunityStructureAreaState extends State<CommunityStructureArea> {
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<CommunityStructureArea> {
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<CommunityStructureArea> {
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<CommunityStructureArea> {
}
void _saveSpaces() {
// Implement your save functionality here
List<Map<String, dynamic>> spaceData = spaces.map((space) => space.toMap()).toList();
List<Map<String, dynamic>> 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}');
}
}