mirror of
https://github.com/SyncrowIOT/web.git
synced 2025-07-14 09:17:37 +00:00
fixed page load for space
This commit is contained in:
@ -1,5 +1,8 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:syncrow_web/pages/common/buttons/add_space_button.dart';
|
||||
import 'package:syncrow_web/pages/spaces_management/bloc/space_management_bloc.dart';
|
||||
import 'package:syncrow_web/pages/spaces_management/bloc/space_management_event.dart';
|
||||
import 'package:syncrow_web/pages/spaces_management/model/space_model.dart';
|
||||
import 'package:syncrow_web/pages/spaces_management/view/dialogs/create_space_dialog.dart';
|
||||
import 'package:syncrow_web/pages/spaces_management/widgets/curved_line_painter.dart';
|
||||
@ -11,8 +14,14 @@ import 'package:syncrow_web/utils/color_manager.dart';
|
||||
|
||||
class CommunityStructureArea extends StatefulWidget {
|
||||
final CommunityModel? selectedCommunity;
|
||||
final List<SpaceModel> spaces;
|
||||
final List<Connection> connections;
|
||||
|
||||
CommunityStructureArea({this.selectedCommunity});
|
||||
CommunityStructureArea({
|
||||
this.selectedCommunity,
|
||||
required this.spaces,
|
||||
required this.connections,
|
||||
});
|
||||
|
||||
@override
|
||||
_CommunityStructureAreaState createState() => _CommunityStructureAreaState();
|
||||
@ -24,6 +33,28 @@ class _CommunityStructureAreaState extends State<CommunityStructureArea> {
|
||||
List<SpaceModel> spaces = [];
|
||||
List<Connection> connections = [];
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
spaces = widget.spaces.isNotEmpty ? flattenSpaces(widget.spaces) : [];
|
||||
connections = widget.spaces.isNotEmpty ? createConnections(widget.spaces) : [];
|
||||
}
|
||||
|
||||
@override
|
||||
void didUpdateWidget(covariant CommunityStructureArea oldWidget) {
|
||||
super.didUpdateWidget(oldWidget);
|
||||
|
||||
if (oldWidget.spaces != widget.spaces || oldWidget.connections != widget.connections) {
|
||||
setState(() {
|
||||
spaces = widget.spaces.isNotEmpty ? flattenSpaces(widget.spaces) : [];
|
||||
connections = widget.spaces.isNotEmpty ? createConnections(widget.spaces) : [];
|
||||
});
|
||||
for (var space in spaces) {
|
||||
print('InitState - Space Position ${space.name}: ${space.incomingConnection?.direction}');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
Size screenSize = MediaQuery.of(context).size;
|
||||
@ -196,7 +227,6 @@ class _CommunityStructureAreaState extends State<CommunityStructureArea> {
|
||||
screenSize.width / 2 - 75, // Center horizontally
|
||||
screenSize.height / 2 - 50, // Slightly above the center vertically
|
||||
);
|
||||
|
||||
SpaceModel newSpace = SpaceModel(
|
||||
name: name,
|
||||
icon: icon,
|
||||
@ -208,15 +238,15 @@ class _CommunityStructureAreaState extends State<CommunityStructureArea> {
|
||||
if (parentIndex != null && direction != null) {
|
||||
SpaceModel parentSpace = spaces[parentIndex];
|
||||
newSpace.parent = parentSpace;
|
||||
parentSpace.children.add(newSpace);
|
||||
final newConnection = Connection(
|
||||
startSpace: parentSpace,
|
||||
endSpace: newSpace,
|
||||
direction: direction,
|
||||
);
|
||||
connections.add(newConnection);
|
||||
newSpace.addIncomingConnection(newConnection);
|
||||
newSpace.incomingConnection = newConnection;
|
||||
parentSpace.addOutgoingConnection(newConnection);
|
||||
parentSpace.children.add(newSpace);
|
||||
}
|
||||
|
||||
spaces.add(newSpace);
|
||||
@ -234,14 +264,77 @@ class _CommunityStructureAreaState extends State<CommunityStructureArea> {
|
||||
});
|
||||
}
|
||||
|
||||
void _saveSpaces() {
|
||||
List<Map<String, dynamic>> spaceData = spaces.map((space) => space.toMap()).toList();
|
||||
List<Map<String, dynamic>> connectionData =
|
||||
connections.map((connection) => connection.toMap()).toList();
|
||||
List<SpaceModel> flattenSpaces(List<SpaceModel> spaces) {
|
||||
List<SpaceModel> result = [];
|
||||
|
||||
// Save to local storage, a file, or send to a backend
|
||||
print('Spaces: ${spaceData}');
|
||||
print('Connections: ${connectionData}');
|
||||
|
||||
void flatten(SpaceModel space) {
|
||||
// Add the current space to the result
|
||||
result.add(space);
|
||||
|
||||
// Recursively flatten child spaces
|
||||
for (var child in space.children) {
|
||||
flatten(child);
|
||||
}
|
||||
}
|
||||
|
||||
// Process each top-level space
|
||||
for (var space in spaces) {
|
||||
flatten(space);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
List<Connection> createConnections(List<SpaceModel> spaces) {
|
||||
List<Connection> connections = [];
|
||||
|
||||
void addConnections(SpaceModel parent, String direction) {
|
||||
for (var child in parent.children) {
|
||||
// Create a connection object
|
||||
connections.add(
|
||||
Connection(
|
||||
startSpace: parent,
|
||||
endSpace: child,
|
||||
direction: child.incomingConnection?.direction ??
|
||||
"down", // Assuming "down" for all direct children
|
||||
),
|
||||
);
|
||||
|
||||
// Recursively process the child's children
|
||||
addConnections(child, direction);
|
||||
}
|
||||
}
|
||||
|
||||
// Process each top-level space
|
||||
for (var space in spaces) {
|
||||
addConnections(space, "down");
|
||||
}
|
||||
|
||||
return connections;
|
||||
}
|
||||
|
||||
void _saveSpaces() {
|
||||
if (widget.selectedCommunity == null) {
|
||||
print("No community selected for saving spaces.");
|
||||
return;
|
||||
}
|
||||
|
||||
// Prepare spaces and connections data
|
||||
List<SpaceModel> spaceList = spaces;
|
||||
String communityUuid = widget.selectedCommunity!.uuid;
|
||||
|
||||
for (var space in spaceList) {
|
||||
print("Processing space: ${space.name}, UUID: ${space.uuid}");
|
||||
// Example: Log connections
|
||||
print("Parent UUID: ${space.parent?.uuid}");
|
||||
print("Incoming connection: ${space.incomingConnection?.direction}");
|
||||
print("Outgoing connections: ${space.outgoingConnections.map((c) => c.direction).toList()}");
|
||||
}
|
||||
|
||||
// Dispatch the save event
|
||||
context.read<SpaceManagementBloc>().add(SaveSpacesEvent(
|
||||
spaces: spaceList,
|
||||
communityUuid: communityUuid,
|
||||
));
|
||||
}
|
||||
}
|
||||
|
@ -32,7 +32,11 @@ class _LoadedStateViewState extends State<LoadedSpaceView> {
|
||||
communities: widget.communities,
|
||||
onCommunitySelected: widget.onCommunitySelected,
|
||||
),
|
||||
CommunityStructureArea(selectedCommunity: widget.selectedCommunity),
|
||||
CommunityStructureArea(
|
||||
selectedCommunity: widget.selectedCommunity,
|
||||
spaces: widget.selectedCommunity?.spaces ?? [],
|
||||
connections: [],
|
||||
),
|
||||
],
|
||||
),
|
||||
const GradientCanvasBorderWidget(),
|
||||
|
Reference in New Issue
Block a user