adjust the canvas if spaces are far positioned

This commit is contained in:
hannathkadher
2024-11-19 12:37:57 +04:00
parent 20f94e290d
commit 3147fc154f
2 changed files with 18 additions and 19 deletions

View File

@ -141,29 +141,25 @@ class SpaceManagementBloc extends Bloc<SpaceManagementEvent, SpaceManagementStat
}
List<SpaceModel> flattenHierarchy(List<SpaceModel> spaces) {
final result = <SpaceModel>{}; // Use a Set to avoid duplicates
// Collect all top-level spaces (those without a parent)
final result = <SpaceModel>{};
final topLevelSpaces = spaces.where((space) => space.parent == null);
void visit(SpaceModel space) {
if (!result.contains(space)) {
result.add(space); // Add the space
result.add(space);
for (var child in spaces.where((s) => s.parent == space)) {
visit(child); // Recursively add children based on parent
visit(child);
}
}
}
// Start with top-level spaces
for (var space in topLevelSpaces) {
visit(space);
}
// Add any spaces that were not part of the hierarchy
for (var space in spaces) {
if (!result.contains(space)) {
result.add(space); // Add standalone or orphan spaces
result.add(space);
}
}
return result.toList(); // Convert back to a list

View File

@ -38,6 +38,7 @@ class _CommunityStructureAreaState extends State<CommunityStructureArea> {
super.initState();
spaces = widget.spaces.isNotEmpty ? flattenSpaces(widget.spaces) : [];
connections = widget.spaces.isNotEmpty ? createConnections(widget.spaces) : [];
_adjustCanvasSizeForSpaces();
}
@override
@ -48,10 +49,8 @@ class _CommunityStructureAreaState extends State<CommunityStructureArea> {
setState(() {
spaces = widget.spaces.isNotEmpty ? flattenSpaces(widget.spaces) : [];
connections = widget.spaces.isNotEmpty ? createConnections(widget.spaces) : [];
_adjustCanvasSizeForSpaces();
});
for (var space in spaces) {
print('InitState - Space Position ${space.name}: ${space.incomingConnection?.direction}');
}
}
}
@ -213,6 +212,18 @@ class _CommunityStructureAreaState extends State<CommunityStructureArea> {
});
}
void _adjustCanvasSizeForSpaces() {
for (var space in spaces) {
if (space.position.dx >= canvasWidth - 200) {
canvasWidth = space.position.dx + 200;
}
if (space.position.dy >= canvasHeight - 200) {
canvasHeight = space.position.dy + 200;
}
}
}
void _showCreateSpaceDialog(Size screenSize,
{Offset? position, int? parentIndex, String? direction}) {
showDialog(
@ -323,14 +334,6 @@ class _CommunityStructureAreaState extends State<CommunityStructureArea> {
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,