added duplicate

This commit is contained in:
hannathkadher
2025-01-27 00:33:50 +04:00
parent 9167c8da29
commit 4907eebc42
22 changed files with 1025 additions and 455 deletions

View File

@ -5,19 +5,23 @@ import 'package:syncrow_web/utils/color_manager.dart';
import 'package:syncrow_web/utils/constants/assets.dart';
class CommunityStructureHeaderActionButtons extends StatelessWidget {
const CommunityStructureHeaderActionButtons({
super.key,
required this.theme,
required this.isSave,
required this.onSave,
required this.onDelete,
required this.selectedSpace,
});
const CommunityStructureHeaderActionButtons(
{super.key,
required this.theme,
required this.isSave,
required this.onSave,
required this.onDelete,
required this.selectedSpace,
required this.onDuplicate,
required this.onEdit});
final ThemeData theme;
final bool isSave;
final VoidCallback onSave;
final VoidCallback onDelete;
final VoidCallback onDuplicate;
final VoidCallback onEdit;
final SpaceModel? selectedSpace;
@override
@ -42,13 +46,18 @@ class CommunityStructureHeaderActionButtons extends StatelessWidget {
CommunityStructureHeaderButton(
label: "Edit",
svgAsset: Assets.editSpace,
onPressed: () => {},
onPressed: onEdit,
theme: theme,
),
CommunityStructureHeaderButton(
label: "Duplicate",
svgAsset: Assets.duplicate,
onPressed: onDuplicate,
theme: theme,
),
CommunityStructureHeaderButton(
label: "Delete",
icon: const Icon(Icons.delete,
size: 18, color: ColorsManager.warningRed),
svgAsset: Assets.spaceDelete,
onPressed: onDelete,
theme: theme,
),

View File

@ -24,12 +24,12 @@ class CommunityStructureHeaderButton extends StatelessWidget {
const double buttonHeight = 40;
return ConstrainedBox(
constraints: const BoxConstraints(
maxWidth: 100,
maxWidth: 130,
minHeight: buttonHeight,
),
child: DefaultButton(
onPressed: onPressed,
borderWidth: 3,
borderWidth: 2,
backgroundColor: ColorsManager.textFieldGreyColor,
foregroundColor: ColorsManager.blackColor,
borderRadius: 12.0,
@ -44,8 +44,8 @@ class CommunityStructureHeaderButton extends StatelessWidget {
if (svgAsset != null)
SvgPicture.asset(
svgAsset!,
width: 30,
height: 30,
width: 20,
height: 20,
),
const SizedBox(width: 10),
Flexible(

View File

@ -14,6 +14,9 @@ class CommunityStructureHeader extends StatefulWidget {
final TextEditingController nameController;
final VoidCallback onSave;
final VoidCallback onDelete;
final VoidCallback onEdit;
final VoidCallback onDuplicate;
final VoidCallback onEditName;
final ValueChanged<String> onNameSubmitted;
final List<CommunityModel> communities;
@ -32,7 +35,9 @@ class CommunityStructureHeader extends StatefulWidget {
required this.onNameSubmitted,
this.community,
required this.communities,
this.selectedSpace});
this.selectedSpace,
required this.onDuplicate,
required this.onEdit});
@override
State<CommunityStructureHeader> createState() =>
@ -146,6 +151,8 @@ class _CommunityStructureHeaderState extends State<CommunityStructureHeader> {
isSave: widget.isSave,
onSave: widget.onSave,
onDelete: widget.onDelete,
onDuplicate: widget.onDuplicate,
onEdit: widget.onEdit,
selectedSpace: widget.selectedSpace,
),
],

View File

@ -4,6 +4,8 @@ import 'package:flutter_bloc/flutter_bloc.dart';
// Syncrow project imports
import 'package:syncrow_web/pages/common/buttons/add_space_button.dart';
import 'package:syncrow_web/pages/common/buttons/cancel_button.dart';
import 'package:syncrow_web/pages/common/buttons/default_button.dart';
import 'package:syncrow_web/pages/spaces_management/all_spaces/bloc/space_management_bloc.dart';
import 'package:syncrow_web/pages/spaces_management/all_spaces/bloc/space_management_event.dart';
import 'package:syncrow_web/pages/spaces_management/all_spaces/model/product_model.dart';
@ -17,6 +19,7 @@ import 'package:syncrow_web/pages/spaces_management/all_spaces/widgets/blank_com
import 'package:syncrow_web/pages/spaces_management/all_spaces/widgets/community_structure_header_widget.dart';
import 'package:syncrow_web/pages/spaces_management/all_spaces/widgets/dialogs/create_space_dialog.dart';
import 'package:syncrow_web/pages/spaces_management/all_spaces/widgets/curved_line_painter.dart';
import 'package:syncrow_web/pages/spaces_management/all_spaces/widgets/dialogs/duplicate_process_dialog.dart';
import 'package:syncrow_web/pages/spaces_management/all_spaces/widgets/space_card_widget.dart';
import 'package:syncrow_web/pages/spaces_management/all_spaces/widgets/space_container_widget.dart';
import 'package:syncrow_web/pages/spaces_management/space_model/models/space_template_model.dart';
@ -133,6 +136,8 @@ class _CommunityStructureAreaState extends State<CommunityStructureArea> {
onSave: _saveSpaces,
selectedSpace: widget.selectedSpace,
onDelete: _onDelete,
onDuplicate: () => {_onDuplicate(context)},
onEdit: () => {},
onEditName: () {
setState(() {
isEditingName = !isEditingName;
@ -328,7 +333,6 @@ class _CommunityStructureAreaState extends State<CommunityStructureArea> {
parentSpace.addOutgoingConnection(newConnection);
parentSpace.children.add(newSpace);
}
spaces.add(newSpace);
_updateNodePosition(newSpace, newSpace.position);
});
@ -546,4 +550,175 @@ class _CommunityStructureAreaState extends State<CommunityStructureArea> {
space.status == SpaceStatus.modified ||
space.status == SpaceStatus.deleted);
}
void _onDuplicate(BuildContext parentContext) {
final screenWidth = MediaQuery.of(context).size.width;
if (widget.selectedSpace != null) {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
backgroundColor: ColorsManager.whiteColors,
title: Text(
"Duplicate Space",
textAlign: TextAlign.center,
style: Theme.of(context)
.textTheme
.headlineLarge
?.copyWith(color: ColorsManager.blackColor),
),
content: SizedBox(
width: screenWidth * 0.4,
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Text("Are you sure you want to duplicate?",
textAlign: TextAlign.center,
style: Theme.of(context).textTheme.headlineSmall),
const SizedBox(height: 15),
Text("All the child spaces will be duplicated.",
textAlign: TextAlign.center,
style: Theme.of(context)
.textTheme
.bodyMedium
?.copyWith(color: ColorsManager.lightGrayColor)),
const SizedBox(width: 15),
],
),
),
actions: [
Row(mainAxisAlignment: MainAxisAlignment.center, children: [
SizedBox(
width: 200,
child: CancelButton(
onPressed: () {
Navigator.of(context).pop();
},
label: "Cancel",
),
),
const SizedBox(width: 10),
SizedBox(
width: 200,
child: DefaultButton(
onPressed: () {
Navigator.of(context).pop();
showDialog(
context: context,
barrierDismissible: false,
builder: (BuildContext context) {
return DuplicateProcessDialog(
onDuplicate: () {
_duplicateSpace(widget.selectedSpace!);
_deselectSpace(parentContext);
},
);
},
);
},
backgroundColor: ColorsManager.secondaryColor,
borderRadius: 10,
foregroundColor: ColorsManager.whiteColors,
child: const Text('OK'),
),
),
])
],
);
},
);
}
}
void _duplicateSpace(SpaceModel space) {
final Map<SpaceModel, SpaceModel> originalToDuplicate = {};
const double horizontalGap = 150.0;
const double verticalGap = 100.0;
SpaceModel duplicateRecursive(SpaceModel original, Offset parentPosition) {
// Find a new position for the duplicated space
Offset newPosition = parentPosition + Offset(horizontalGap, 0);
// Avoid overlapping with existing spaces
while (spaces.any((s) =>
(s.position - newPosition).distance < horizontalGap &&
s.status != SpaceStatus.deleted)) {
newPosition += Offset(horizontalGap, 0);
}
// Create the duplicated space
final duplicated = SpaceModel(
name: "${original.name} (Copy)",
icon: original.icon,
position: newPosition,
isPrivate: original.isPrivate,
children: [],
status: SpaceStatus.newSpace,
parent: original.parent,
spaceModel: original.spaceModel,
subspaces: original.subspaces,
tags: original.tags,
);
originalToDuplicate[original] = duplicated;
// Copy the children of the original space to the duplicated space
Offset childStartPosition = newPosition + Offset(0, verticalGap);
for (final child in original.children) {
final duplicatedChild = duplicateRecursive(child, childStartPosition);
duplicated.children.add(duplicatedChild);
duplicatedChild.parent =
duplicated; // Set the parent for the duplicated child
childStartPosition += Offset(0, verticalGap);
}
return duplicated;
}
// Duplicate the selected space and its children
final duplicatedSpace = duplicateRecursive(space, space.position);
// Ensure the duplicated space has the same parent as the original
if (space.parent != null) {
final parentSpace = space.parent!;
final duplicatedParent = originalToDuplicate[parentSpace] ?? parentSpace;
duplicatedSpace.parent = duplicatedParent;
duplicatedParent.children.add(duplicatedSpace);
}
// Flatten the hierarchy of the duplicated spaces
List<SpaceModel> flattenHierarchy(SpaceModel root) {
final List<SpaceModel> result = [];
void traverse(SpaceModel node) {
result.add(node);
for (final child in node.children) {
traverse(child);
}
}
traverse(root);
return result;
}
final duplicatedSpacesList = flattenHierarchy(duplicatedSpace);
setState(() {
spaces.addAll(duplicatedSpacesList);
// Duplicate the connections
for (final connection in connections) {
if (originalToDuplicate.containsKey(connection.startSpace) &&
originalToDuplicate.containsKey(connection.endSpace)) {
connections.add(
Connection(
startSpace: originalToDuplicate[connection.startSpace]!,
endSpace: originalToDuplicate[connection.endSpace]!,
direction: connection.direction,
),
);
}
}
});
}
}

View File

@ -10,6 +10,7 @@ import 'package:syncrow_web/pages/spaces_management/all_spaces/model/space_model
import 'package:syncrow_web/pages/spaces_management/all_spaces/model/subspace_model.dart';
import 'package:syncrow_web/pages/spaces_management/all_spaces/model/tag.dart';
import 'package:syncrow_web/pages/spaces_management/all_spaces/widgets/dialogs/icon_selection_dialog.dart';
import 'package:syncrow_web/pages/spaces_management/assign_tag/views/assign_tag_dialog.dart';
import 'package:syncrow_web/pages/spaces_management/create_subspace/views/create_subspace_model_dialog.dart';
import 'package:syncrow_web/pages/spaces_management/helper/tag_helper.dart';
import 'package:syncrow_web/pages/spaces_management/link_space_model/view/link_space_model_dialog.dart';
@ -409,7 +410,9 @@ class CreateSpaceDialogState extends State<CreateSpaceDialog> {
_showTagCreateDialog(
context,
enteredName,
widget.isEdit,
widget.products,
subspaces,
);
// Edit action
})
@ -420,7 +423,12 @@ class CreateSpaceDialogState extends State<CreateSpaceDialog> {
: TextButton(
onPressed: () {
_showTagCreateDialog(
context, enteredName, widget.products);
context,
enteredName,
widget.isEdit,
widget.products,
subspaces,
);
},
style: TextButton.styleFrom(
padding: EdgeInsets.zero,
@ -558,85 +566,57 @@ class CreateSpaceDialogState extends State<CreateSpaceDialog> {
);
}
void _showTagCreateDialog(
BuildContext context, String name, List<ProductModel>? products) {
showDialog(
context: context,
builder: (BuildContext context) {
return AddDeviceTypeWidget(
spaceName: name,
products: products,
subspaces: subspaces,
spaceTags: tags,
allTags: [],
initialSelectedProducts:
createInitialSelectedProducts(tags, subspaces),
onSave: (selectedSpaceTags, selectedSubspaces) {
setState(() {
tags = selectedSpaceTags;
selectedSpaceModel = null;
void _showTagCreateDialog(BuildContext context, String name, bool isEdit,
List<ProductModel>? products, List<SubspaceModel>? subspaces) {
isEdit
? showDialog(
context: context,
builder: (BuildContext context) {
return AssignTagDialog(
title: 'Edit Device',
addedProducts: TagHelper.createInitialSelectedProductsForTags(
tags, subspaces),
spaceName: name,
products: products,
subspaces: subspaces,
allTags: [],
onSave: (selectedSpaceTags, selectedSubspaces) {},
);
},
)
: showDialog(
context: context,
builder: (BuildContext context) {
return AddDeviceTypeWidget(
spaceName: name,
products: products,
subspaces: subspaces,
spaceTags: tags,
allTags: [],
initialSelectedProducts:
TagHelper.createInitialSelectedProductsForTags(
tags, subspaces),
onSave: (selectedSpaceTags, selectedSubspaces) {
setState(() {
tags = selectedSpaceTags;
selectedSpaceModel = null;
if (selectedSubspaces != null) {
if (subspaces != null) {
for (final subspace in subspaces!) {
for (final selectedSubspace in selectedSubspaces) {
if (subspace.subspaceName ==
selectedSubspace.subspaceName) {
subspace.tags = selectedSubspace.tags;
if (selectedSubspaces != null) {
if (subspaces != null) {
for (final subspace in subspaces!) {
for (final selectedSubspace in selectedSubspaces) {
if (subspace.subspaceName ==
selectedSubspace.subspaceName) {
subspace.tags = selectedSubspace.tags;
}
}
}
}
}
}
}
}
});
},
);
},
);
}
List<SelectedProduct> createInitialSelectedProducts(
List<Tag>? tags, List<SubspaceModel>? subspaces) {
final Map<ProductModel, int> productCounts = {};
if (tags != null) {
for (var tag in tags) {
if (tag.product != null) {
productCounts[tag.product!] = (productCounts[tag.product!] ?? 0) + 1;
}
}
}
if (subspaces != null) {
for (var subspace in subspaces) {
if (subspace.tags != null) {
for (var tag in subspace.tags!) {
if (tag.product != null) {
productCounts[tag.product!] =
(productCounts[tag.product!] ?? 0) + 1;
}
}
}
}
}
return productCounts.entries
.map((entry) => SelectedProduct(
productId: entry.key.uuid,
count: entry.value,
productName: entry.key.name ?? 'Unnamed',
product: entry.key,
))
.toList();
}
Map<ProductModel, int> _groupTags(List<Tag> tags) {
final Map<ProductModel, int> groupedTags = {};
for (var tag in tags) {
if (tag.product != null) {
groupedTags[tag.product!] = (groupedTags[tag.product!] ?? 0) + 1;
}
}
return groupedTags;
});
},
);
},
);
}
}

View File

@ -0,0 +1,86 @@
import 'package:flutter/material.dart';
import 'package:syncrow_web/utils/color_manager.dart';
class DuplicateProcessDialog extends StatefulWidget {
final VoidCallback onDuplicate;
const DuplicateProcessDialog({required this.onDuplicate, Key? key})
: super(key: key);
@override
_DuplicateProcessDialogState createState() => _DuplicateProcessDialogState();
}
class _DuplicateProcessDialogState extends State<DuplicateProcessDialog> {
bool isDuplicating = true;
@override
void initState() {
super.initState();
_startDuplication();
}
void _startDuplication() async {
WidgetsBinding.instance.addPostFrameCallback((_) {
widget.onDuplicate();
});
await Future.delayed(const Duration(seconds: 2));
setState(() {
isDuplicating = false;
});
await Future.delayed(const Duration(seconds: 2));
if (mounted) {
Navigator.of(context).pop();
}
}
@override
Widget build(BuildContext context) {
final screenWidth = MediaQuery.of(context).size.width;
return AlertDialog(
backgroundColor: Colors.white,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20),
),
content: SizedBox(
width: screenWidth * 0.4,
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
if (isDuplicating) ...[
const CircularProgressIndicator(
color: ColorsManager.vividBlue,
),
const SizedBox(height: 15),
Text(
"Duplicating in progress",
style: Theme.of(context)
.textTheme
.headlineSmall
?.copyWith(color: ColorsManager.primaryColor),
textAlign: TextAlign.center,
),
] else ...[
const Icon(
Icons.check_circle,
color: ColorsManager.vividBlue,
size: 50,
),
const SizedBox(height: 15),
Text(
"Duplicating successful",
textAlign: TextAlign.center,
style: Theme.of(context)
.textTheme
.headlineSmall
?.copyWith(color: ColorsManager.primaryColor),
),
],
],
),
),
);
}
}