From 9be03850a5a263081dd0aa31a62b832194a2416f Mon Sep 17 00:00:00 2001 From: hannathkadher Date: Thu, 21 Nov 2024 16:16:14 +0400 Subject: [PATCH] fixed delete --- lib/common/custom_expansion_tile.dart | 1 - .../bloc/space_management_bloc.dart | 21 +++++- .../spaces_management/model/space_model.dart | 15 +---- .../widgets/community_structure_widget.dart | 67 ++++++++++++++----- .../widgets/loaded_space_widget.dart | 4 +- .../widgets/sidebar_widget.dart | 6 +- 6 files changed, 74 insertions(+), 40 deletions(-) diff --git a/lib/common/custom_expansion_tile.dart b/lib/common/custom_expansion_tile.dart index a6412b3c..364ccdce 100644 --- a/lib/common/custom_expansion_tile.dart +++ b/lib/common/custom_expansion_tile.dart @@ -96,7 +96,6 @@ class CustomExpansionTileState extends State { if (widget.onItemSelected != null) { widget.onItemSelected!(); } - debugPrint('${widget.title} ${widget.isSelected} tapped for selection'); }, child: Text( _capitalizeFirstLetter(widget.title), diff --git a/lib/pages/spaces_management/bloc/space_management_bloc.dart b/lib/pages/spaces_management/bloc/space_management_bloc.dart index 1f413bd8..4b79ff1c 100644 --- a/lib/pages/spaces_management/bloc/space_management_bloc.dart +++ b/lib/pages/spaces_management/bloc/space_management_bloc.dart @@ -97,8 +97,7 @@ class SpaceManagementBloc extends Bloc emit, - ) { - } + ) {} void _onCreateCommunity( CreateCommunityEvent event, @@ -152,10 +151,26 @@ class SpaceManagementBloc extends Bloc spaces, String communityUuid) async { final orderedSpaces = flattenHierarchy(spaces); + final parentsToDelete = orderedSpaces.where((space) => + space.status == SpaceStatus.deleted && + (space.parent == null || space.parent?.status != SpaceStatus.deleted)); + + for (var parent in parentsToDelete) { + try { + // Ensure parent.uuid is not null before calling the API + if (parent.uuid != null) { + await _api.deleteSpace(communityUuid, parent.uuid!); + } + } catch (e) { + print( + 'Error deleting space ${parent.name} (UUID: ${parent.uuid}, Community UUID: $communityUuid): $e'); + rethrow; // Decide whether to stop execution or continue + } + } + for (var space in orderedSpaces) { try { if (space.uuid != null && space.uuid!.isNotEmpty) { - // Call update if the space already has a UUID final response = await _api.updateSpace( communityId: communityUuid, spaceId: space.uuid!, diff --git a/lib/pages/spaces_management/model/space_model.dart b/lib/pages/spaces_management/model/space_model.dart index 9996da6f..f8a5d412 100644 --- a/lib/pages/spaces_management/model/space_model.dart +++ b/lib/pages/spaces_management/model/space_model.dart @@ -3,7 +3,7 @@ import 'package:syncrow_web/pages/spaces_management/model/community_model.dart'; import 'package:syncrow_web/pages/spaces_management/model/connection_model.dart'; import 'package:syncrow_web/pages/spaces_management/model/selected_product_model.dart'; -enum SpaceStatus { newSpace, modified, unchanged } +enum SpaceStatus { newSpace, modified, unchanged, deleted } class SpaceModel { String? uuid; @@ -106,17 +106,4 @@ class SpaceModel { outgoingConnections.add(connection); } - @override - String toString() { - return ''' -SpaceModel( - uuid: $uuid, - name: $name, - icon: $icon, - isPrivate: $isPrivate, - position: $position, - selectedProducts: $selectedProducts -) -'''; - } } diff --git a/lib/pages/spaces_management/widgets/community_structure_widget.dart b/lib/pages/spaces_management/widgets/community_structure_widget.dart index 97265ca1..51bdccc2 100644 --- a/lib/pages/spaces_management/widgets/community_structure_widget.dart +++ b/lib/pages/spaces_management/widgets/community_structure_widget.dart @@ -66,6 +66,9 @@ class _CommunityStructureAreaState extends State { @override Widget build(BuildContext context) { + final visibleSpaces = flattenSpaces(widget.spaces); + final visibleConnections = createConnections(widget.spaces); + Size screenSize = MediaQuery.of(context).size; return Expanded( child: Container( @@ -81,7 +84,7 @@ class _CommunityStructureAreaState extends State { Flexible( child: Stack( children: [ - if (spaces.isNotEmpty) + if (visibleSpaces.isNotEmpty) InteractiveViewer( boundaryMargin: EdgeInsets.all(500), minScale: 0.5, @@ -94,7 +97,7 @@ class _CommunityStructureAreaState extends State { children: [ for (var connection in connections) CustomPaint(painter: CurvedLinePainter([connection])), - for (var entry in spaces.asMap().entries) + for (var entry in visibleSpaces.asMap().entries) Positioned( left: entry.value.position.dx, top: entry.value.position.dy, @@ -103,7 +106,7 @@ class _CommunityStructureAreaState extends State { onButtonTap: (int index, Offset newPosition, String direction) { _showCreateSpaceDialog( screenSize, - position: spaces[index].position + newPosition, + position: visibleSpaces[index].position + newPosition, parentIndex: index, direction: direction, ); @@ -119,10 +122,10 @@ class _CommunityStructureAreaState extends State { return SpaceContainerWidget( index: index, onDoubleTap: () { - _showEditSpaceDialog(spaces[index]); + _showEditSpaceDialog(visibleSpaces[index]); }, - icon: spaces[index].icon ?? '', - name: spaces[index].name, + icon: visibleSpaces[index].icon ?? '', + name: visibleSpaces[index].name, ); }, ), @@ -131,7 +134,7 @@ class _CommunityStructureAreaState extends State { ), ), ), - if (spaces.isEmpty) + if (visibleSpaces.isEmpty) Center( child: AddSpaceButton( onTap: () { @@ -180,7 +183,7 @@ class _CommunityStructureAreaState extends State { ], ), // Show "Save" button only if there are spaces - if (spaces.isNotEmpty && widget.selectedCommunity != null) + if (widget.spaces.isNotEmpty && widget.selectedCommunity != null) Row(children: [ ElevatedButton.icon( onPressed: () { @@ -358,16 +361,15 @@ class _CommunityStructureAreaState extends State { List result = []; void flatten(SpaceModel space) { - // Add the current space to the result + if (space.status == SpaceStatus.deleted) return; + 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); } @@ -379,7 +381,11 @@ class _CommunityStructureAreaState extends State { List connections = []; void addConnections(SpaceModel parent, String direction) { + if (parent.status == SpaceStatus.deleted) return; + for (var child in parent.children) { + if (child.status == SpaceStatus.deleted) continue; + // Create a connection object connections.add( Connection( @@ -395,7 +401,6 @@ class _CommunityStructureAreaState extends State { } } - // Process each top-level space for (var space in spaces) { addConnections(space, "down"); } @@ -405,16 +410,18 @@ class _CommunityStructureAreaState extends State { void _saveSpaces() { if (widget.selectedCommunity == null) { - print("No community selected for saving spaces."); + debugPrint("No community selected for saving spaces."); return; } List spacesToSave = spaces.where((space) { - return space.status == SpaceStatus.newSpace || space.status == SpaceStatus.modified; + return space.status == SpaceStatus.newSpace || + space.status == SpaceStatus.modified || + space.status == SpaceStatus.deleted; }).toList(); if (spacesToSave.isEmpty) { - print("No new or modified spaces to save."); + debugPrint("No new or modified spaces to save."); return; } @@ -427,11 +434,37 @@ class _CommunityStructureAreaState extends State { } void _onDelete() { - if (widget.selectedCommunity != null && widget.selectedCommunity?.uuid != null) { + if (widget.selectedCommunity != null && + widget.selectedCommunity?.uuid != null && + widget.selectedSpace == null) { context.read().add(DeleteCommunityEvent( communityUuid: widget.selectedCommunity!.uuid, )); - + } + if (widget.selectedSpace != null) { + setState(() { + for (var space in spaces) { + if (space.uuid == widget.selectedSpace?.uuid) { + space.status = SpaceStatus.deleted; + _markChildrenAsDeleted(space); + } + } + _removeConnectionsForDeletedSpaces(); + }); } } + + void _markChildrenAsDeleted(SpaceModel parent) { + for (var child in parent.children) { + child.status = SpaceStatus.deleted; + _markChildrenAsDeleted(child); + } + } + + void _removeConnectionsForDeletedSpaces() { + connections.removeWhere((connection) { + return connection.startSpace.status == SpaceStatus.deleted || + connection.endSpace.status == SpaceStatus.deleted; + }); + } } diff --git a/lib/pages/spaces_management/widgets/loaded_space_widget.dart b/lib/pages/spaces_management/widgets/loaded_space_widget.dart index 082224b6..79a493bb 100644 --- a/lib/pages/spaces_management/widgets/loaded_space_widget.dart +++ b/lib/pages/spaces_management/widgets/loaded_space_widget.dart @@ -10,8 +10,8 @@ class LoadedSpaceView extends StatefulWidget { final List communities; final CommunityModel? selectedCommunity; final SpaceModel? selectedSpace; - final ValueChanged onCommunitySelected; - final ValueChanged onSpaceSelected; + final ValueChanged? onCommunitySelected; + final ValueChanged? onSpaceSelected; final List? products; const LoadedSpaceView({ diff --git a/lib/pages/spaces_management/widgets/sidebar_widget.dart b/lib/pages/spaces_management/widgets/sidebar_widget.dart index 4e8a9046..41a9e649 100644 --- a/lib/pages/spaces_management/widgets/sidebar_widget.dart +++ b/lib/pages/spaces_management/widgets/sidebar_widget.dart @@ -15,7 +15,7 @@ import 'package:syncrow_web/utils/style.dart'; class SidebarWidget extends StatefulWidget { final Function(CommunityModel)? onCommunitySelected; - final Function(SpaceModel)? onSpaceSelected; + final Function(SpaceModel?)? onSpaceSelected; final List communities; const SidebarWidget( @@ -190,7 +190,8 @@ class _SidebarWidgetState extends State { }); if (widget.onCommunitySelected != null) { - widget.onCommunitySelected!(community); // Pass the entire community + widget.onCommunitySelected!(community); + widget.onSpaceSelected!(null); // Pass the entire community } }, onExpansionChanged: (String title, bool expanded) { @@ -204,7 +205,6 @@ class _SidebarWidgetState extends State { Widget _buildSpaceTile(SpaceModel space, CommunityModel community) { bool isExpandedSpace = _isSpaceOrChildSelected(space); - bool isSelectedSpace = _selectedSpaceUuid == space.uuid; // Check if space should be expanded return SpaceTile( title: space.name,