delete Space

This commit is contained in:
hannathkadher
2024-11-21 11:52:27 +04:00
parent 6bc6097a7e
commit 8a7f9ab2dc
11 changed files with 353 additions and 46 deletions
@@ -110,6 +110,7 @@ class SpaceManagementBloc extends Bloc<SpaceManagementEvent, SpaceManagementStat
}
}
void _onSaveSpaces(
SaveSpacesEvent event,
Emitter<SpaceManagementState> emit,
@@ -146,6 +147,7 @@ class SpaceManagementBloc extends Bloc<SpaceManagementEvent, SpaceManagementStat
position: space.position,
icon: space.icon,
direction: space.incomingConnection?.direction,
products: space.selectedProducts
);
} else {
// Call create if the space does not have a UUID
@@ -10,4 +10,9 @@ class SelectedProduct {
'count': count,
};
}
@override
String toString() {
return 'SelectedProduct(productId: $productId, count: $count)';
}
}
@@ -58,6 +58,14 @@ class SpaceModel {
? Offset(json['x'], json['y'])
: const Offset(0, 0),
isHovered: false,
selectedProducts: json['spaceProducts'] != null
? (json['spaceProducts'] as List).map((product) {
return SelectedProduct(
productId: product['product']['uuid'],
count: product['productCount'],
);
}).toList()
: [],
);
// Add incomingConnection to the instance after creation
@@ -97,4 +105,18 @@ class SpaceModel {
void addOutgoingConnection(Connection connection) {
outgoingConnections.add(connection);
}
@override
String toString() {
return '''
SpaceModel(
uuid: $uuid,
name: $name,
icon: $icon,
isPrivate: $isPrivate,
position: $position,
selectedProducts: $selectedProducts
)
''';
}
}
@@ -1,18 +1,21 @@
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_svg/svg.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/product_model.dart';
import 'package:syncrow_web/pages/spaces_management/model/selected_product_model.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/dialogs/create_space_dialog.dart';
import 'package:syncrow_web/pages/spaces_management/widgets/curved_line_painter.dart';
import 'package:syncrow_web/pages/spaces_management/widgets/dialogs/delete_dialogue.dart';
import 'package:syncrow_web/pages/spaces_management/widgets/space_card_widget.dart';
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/widgets/space_container_widget.dart';
import 'package:syncrow_web/utils/color_manager.dart';
import 'package:syncrow_web/utils/constants/assets.dart';
class CommunityStructureArea extends StatefulWidget {
final CommunityModel? selectedCommunity;
@@ -116,6 +119,7 @@ class _CommunityStructureAreaState extends State<CommunityStructureArea> {
return SpaceContainerWidget(
index: index,
onDoubleTap: () {
print(spaces[index].toString());
_showEditSpaceDialog(spaces[index]);
},
icon: spaces[index].icon ?? '',
@@ -178,23 +182,55 @@ class _CommunityStructureAreaState extends State<CommunityStructureArea> {
),
// Show "Save" button only if there are spaces
if (spaces.isNotEmpty && widget.selectedCommunity != null)
ElevatedButton.icon(
onPressed: () {
_saveSpaces();
},
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),
Row(children: [
ElevatedButton.icon(
onPressed: () {
_saveSpaces();
},
icon: const Icon(
Icons.save,
size: 18,
color: ColorsManager.spaceColor,
),
label: const Text("Save"),
style: ElevatedButton.styleFrom(
backgroundColor: ColorsManager.textFieldGreyColor,
foregroundColor: ColorsManager.blackColor,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8.0),
),
padding: const EdgeInsets.symmetric(horizontal: 16.0, vertical: 8.0),
side: BorderSide(color: Colors.grey.shade300),
elevation: 0,
),
padding: const EdgeInsets.symmetric(horizontal: 12.0, vertical: 8.0),
side: BorderSide(color: Colors.grey.shade300),
elevation: 0,
),
),
const SizedBox(width: 10),
ElevatedButton.icon(
onPressed: () {
showDeleteConfirmationDialog(context, () {
// Handle the delete action here
print("Delete confirmed");
Navigator.of(context).pop(); // Close the dialog after confirming
}, widget.selectedSpace != null);
},
icon: SvgPicture.asset(
Assets.acFanAuto, // Path to your SVG asset
width: 18, // Adjust width as needed
height: 18, // Adjust height as needed
),
label: const Text("Delete"),
style: ElevatedButton.styleFrom(
backgroundColor: ColorsManager.textFieldGreyColor,
foregroundColor: Colors.black,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8.0),
),
padding: const EdgeInsets.symmetric(horizontal: 16.0, vertical: 8.0),
side: BorderSide(color: ColorsManager.neutralGray),
elevation: 0,
),
),
])
],
),
);
@@ -284,35 +320,35 @@ class _CommunityStructureAreaState extends State<CommunityStructureArea> {
);
}
void _showEditSpaceDialog(SpaceModel space) {
showDialog(
context: context,
builder: (BuildContext context) {
return CreateSpaceDialog(
products: widget.products,
name: space.name,
icon: space.icon,
isEdit: true,
selectedProducts: space.selectedProducts,
onCreateSpace: (String name, String icon, List<SelectedProduct> selectedProducts) {
setState(() {
// Update the space's properties
space.name = name;
space.icon = icon;
space.selectedProducts = selectedProducts;
void _showEditSpaceDialog(SpaceModel space) {
showDialog(
context: context,
builder: (BuildContext context) {
return CreateSpaceDialog(
products: widget.products,
name: space.name,
icon: space.icon,
isEdit: true,
selectedProducts: space.selectedProducts,
onCreateSpace: (String name, String icon, List<SelectedProduct> selectedProducts) {
setState(() {
// Update the space's properties
space.name = name;
space.icon = icon;
space.selectedProducts = selectedProducts;
if (space.status != SpaceStatus.newSpace) {
space.status = SpaceStatus.modified; // Mark as modified
}
});
},
// Pre-fill the dialog with current space data
key: Key(space.name), // Add a unique key to ensure dialog refresh
);
},
);
}
if (space.status != SpaceStatus.newSpace) {
space.status = SpaceStatus.modified; // Mark as modified
}
});
},
// Pre-fill the dialog with current space data
key: Key(space.name), // Add a unique key to ensure dialog refresh
);
},
);
}
void _handleHoverChanged(int index, bool isHovered) {
setState(() {
spaces[index].isHovered = isHovered;
@@ -369,6 +369,7 @@ class CreateSpaceDialogState extends State<CreateSpaceDialog> {
),
);
return product.icon ?? Assets.presenceSensor;
}
@@ -0,0 +1,225 @@
import 'package:flutter/material.dart';
import 'package:syncrow_web/utils/color_manager.dart';
void showDeleteConfirmationDialog(BuildContext context, VoidCallback onConfirm, bool isSpace) {
showDialog(
context: context,
barrierDismissible: false, // Prevent dismissing by tapping outside
builder: (BuildContext context) {
return Dialog(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(16.0),
),
child: SizedBox(
width: 500, // Set the desired width
child: Container(
color: ColorsManager.whiteColors,
padding: const EdgeInsets.all(20.0),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
// Icon
Container(
width: 50,
height: 50,
decoration: BoxDecoration(
color: ColorsManager.warningRed,
shape: BoxShape.circle,
),
child: const Icon(
Icons.close,
color: Colors.white,
size: 40,
),
),
const SizedBox(height: 20),
// Title
isSpace
? const Text(
'Delete Space',
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
),
)
: const Text(
'Delete Community',
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 10),
// Subtitle
isSpace
? const Text(
'All the data in the space will be lost',
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 14,
color: Colors.grey,
),
)
: const Text(
'All the data in the community will be lost',
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 14,
color: Colors.grey,
),
),
const SizedBox(height: 20),
// Buttons
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
ElevatedButton(
onPressed: () {
Navigator.of(context).pop(); // Close the first dialog
// Trigger the second popup
},
style: ElevatedButton.styleFrom(
backgroundColor: Colors.white,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12),
side: const BorderSide(color: ColorsManager.boxColor), // Black border
),
),
child: const Text(
'Cancel',
style: TextStyle(color: Colors.black),
),
),
ElevatedButton(
onPressed: () {
Navigator.of(context).pop(); // Close the first dialog
showProcessingPopup(context, isSpace, onConfirm);
},
style: ElevatedButton.styleFrom(
backgroundColor: Colors.blue,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8.0),
),
),
child: const Text(
'Continue',
style: TextStyle(color: Colors.white),
),
),
],
),
],
),
),
),
);
},
);
}
void showProcessingPopup(BuildContext context, bool isSpace, VoidCallback onDelete) {
showDialog(
context: context,
barrierDismissible: false,
builder: (BuildContext context) {
return Dialog(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(16.0),
),
child: SizedBox(
width: 500,
child: Container(
color: ColorsManager.whiteColors,
padding: const EdgeInsets.all(20.0),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
// Icon
Container(
width: 50,
height: 50,
decoration: BoxDecoration(
color: ColorsManager.warningRed,
shape: BoxShape.circle,
),
child: const Icon(
Icons.close,
color: Colors.white,
size: 40,
),
),
const SizedBox(height: 20),
// Title
isSpace
? const Text(
'Delete Space',
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
),
)
: const Text(
'Delete Community',
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
),
),
const SizedBox(height: 10),
// Subtitle
const Text(
'Are you sure you want to Delete?',
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 14,
color: Colors.grey,
),
),
const SizedBox(height: 20),
// Buttons (Optional)
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
ElevatedButton(
onPressed: () {
// Trigger the second popup
onDelete();
},
style: ElevatedButton.styleFrom(
backgroundColor: ColorsManager.warningRed,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12),
side: const BorderSide(color: ColorsManager.boxColor), // Black border
),
),
child: const Text(
'Delete',
style: TextStyle(color: Colors.white),
),
),
ElevatedButton(
onPressed: () {
Navigator.of(context).pop(); // Close the first dialog
},
style: ElevatedButton.styleFrom(
backgroundColor: Colors.white,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8.0),
),
),
child: const Text(
'Cancel',
style: TextStyle(color: Colors.black),
),
),
],
),
],
),
),
),
);
},
);
}
@@ -7,7 +7,7 @@ import 'package:syncrow_web/pages/spaces_management/bloc/space_management_event.
import 'package:syncrow_web/pages/spaces_management/model/community_model.dart';
import 'package:syncrow_web/pages/spaces_management/model/space_model.dart';
import 'package:syncrow_web/pages/spaces_management/widgets/community_tile.dart';
import 'package:syncrow_web/pages/spaces_management/view/dialogs/create_community_dialog.dart';
import 'package:syncrow_web/pages/spaces_management/widgets/dialogs/create_community_dialog.dart';
import 'package:syncrow_web/pages/spaces_management/widgets/space_tile_widget.dart';
import 'package:syncrow_web/utils/color_manager.dart';
import 'package:syncrow_web/utils/constants/assets.dart';
+7 -2
View File
@@ -183,6 +183,7 @@ class CommunitySpaceManagementApi {
String? direction,
bool isPrivate = false,
required Offset position,
required List<SelectedProduct> products,
}) async {
try {
final body = {
@@ -191,7 +192,8 @@ class CommunitySpaceManagementApi {
'x': position.dx,
'y': position.dy,
'direction': direction,
'icon': icon
'icon': icon,
'products': products.map((product) => product.toJson()).toList(),
};
if (parentId != null) {
body['parentUuid'] = parentId;
@@ -235,7 +237,10 @@ class CommunitySpaceManagementApi {
final response = await HTTPService().get(
path: ApiEndpoints.getSpaceHierarchy.replaceAll('{communityId}', communityId),
expectedResponseModel: (json) {
return (json['data'] as List).map((spaceJson) => SpaceModel.fromJson(spaceJson)).toList();
final spaceModels =
(json['data'] as List).map((spaceJson) => SpaceModel.fromJson(spaceJson)).toList();
return spaceModels;
},
);
return response;
+2
View File
@@ -234,4 +234,6 @@ class Assets {
static const String garageDoor = 'assets/icons/garage_opener.svg';
static const String doorSensor = 'assets/icons/door_sensor.svg';
static const String delete = 'assets/icons/delete_svg';
}