mirror of
https://github.com/SyncrowIOT/web.git
synced 2025-07-10 15:17:31 +00:00
fixed issues in create space and duplicate
This commit is contained in:
@ -179,6 +179,7 @@ class SpaceManagementBloc
|
||||
final updatedCommunities =
|
||||
await Future.wait(communities.map((community) async {
|
||||
final spaces = await _fetchSpacesForCommunity(community.uuid);
|
||||
|
||||
return CommunityModel(
|
||||
uuid: community.uuid,
|
||||
createdAt: community.createdAt,
|
||||
@ -313,6 +314,7 @@ class SpaceManagementBloc
|
||||
SelectSpaceEvent event,
|
||||
Emitter<SpaceManagementState> emit,
|
||||
) {
|
||||
|
||||
_handleCommunitySpaceStateUpdate(
|
||||
emit: emit,
|
||||
selectedCommunity: event.selectedCommunity,
|
||||
|
@ -95,6 +95,9 @@ class SpaceModel {
|
||||
icon: json['icon'] ?? Assets.location,
|
||||
position: Offset(json['x'] ?? 0, json['y'] ?? 0),
|
||||
isHovered: false,
|
||||
spaceModel: json['spaceModel'] != null
|
||||
? SpaceTemplateModel.fromJson(json['spaceModel'])
|
||||
: null,
|
||||
tags: (json['tags'] as List<dynamic>?)
|
||||
?.where((item) => item is Map<String, dynamic>) // Validate type
|
||||
.map((item) => Tag.fromJson(item as Map<String, dynamic>))
|
||||
|
@ -22,6 +22,7 @@ import 'package:syncrow_web/pages/spaces_management/all_spaces/widgets/curved_li
|
||||
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/helper/space_helper.dart';
|
||||
import 'package:syncrow_web/pages/spaces_management/space_model/models/space_template_model.dart';
|
||||
import 'package:syncrow_web/utils/color_manager.dart';
|
||||
|
||||
@ -341,6 +342,7 @@ class _CommunityStructureAreaState extends State<CommunityStructureArea> {
|
||||
}
|
||||
|
||||
void _showEditSpaceDialog() {
|
||||
print("parentsdf space is ${widget.selectedSpace?.parent?.name}");
|
||||
if (widget.selectedSpace != null) {
|
||||
showDialog(
|
||||
context: context,
|
||||
@ -350,7 +352,10 @@ class _CommunityStructureAreaState extends State<CommunityStructureArea> {
|
||||
spaceModels: widget.spaceModels,
|
||||
name: widget.selectedSpace!.name,
|
||||
icon: widget.selectedSpace!.icon,
|
||||
parentSpace: SpaceHelper.findSpaceByInternalId(
|
||||
widget.selectedSpace?.parent?.internalId, spaces),
|
||||
editSpace: widget.selectedSpace,
|
||||
currentSpaceModel: widget.selectedSpace?.spaceModel,
|
||||
tags: widget.selectedSpace?.tags,
|
||||
subspaces: widget.selectedSpace?.subspaces,
|
||||
isEdit: true,
|
||||
@ -650,12 +655,6 @@ class _CommunityStructureAreaState extends State<CommunityStructureArea> {
|
||||
|
||||
final Map<String, int> nameCounters = {};
|
||||
|
||||
String _generateCopyName(String originalName) {
|
||||
final baseName = originalName.replaceAll(RegExp(r'\(\d+\)$'), '').trim();
|
||||
nameCounters[baseName] = (nameCounters[baseName] ?? 0) + 1;
|
||||
return "$baseName(${nameCounters[baseName]})";
|
||||
}
|
||||
|
||||
SpaceModel duplicateRecursive(SpaceModel original, Offset parentPosition,
|
||||
SpaceModel? duplicatedParent) {
|
||||
Offset newPosition = parentPosition + Offset(horizontalGap, 0);
|
||||
@ -666,7 +665,8 @@ class _CommunityStructureAreaState extends State<CommunityStructureArea> {
|
||||
newPosition += Offset(horizontalGap, 0);
|
||||
}
|
||||
|
||||
final duplicatedName = _generateCopyName(original.name);
|
||||
final duplicatedName =
|
||||
SpaceHelper.generateUniqueSpaceName(original.name, spaces);
|
||||
|
||||
final duplicated = SpaceModel(
|
||||
name: duplicatedName,
|
||||
|
@ -40,6 +40,7 @@ class CreateSpaceDialog extends StatefulWidget {
|
||||
final List<SubspaceModel>? subspaces;
|
||||
final List<Tag>? tags;
|
||||
final List<String>? allTags;
|
||||
final SpaceTemplateModel? currentSpaceModel;
|
||||
|
||||
const CreateSpaceDialog(
|
||||
{super.key,
|
||||
@ -54,7 +55,8 @@ class CreateSpaceDialog extends StatefulWidget {
|
||||
this.selectedProducts = const [],
|
||||
this.spaceModels,
|
||||
this.subspaces,
|
||||
this.tags});
|
||||
this.tags,
|
||||
this.currentSpaceModel});
|
||||
|
||||
@override
|
||||
CreateSpaceDialogState createState() => CreateSpaceDialogState();
|
||||
@ -83,6 +85,7 @@ class CreateSpaceDialogState extends State<CreateSpaceDialog> {
|
||||
enteredName.isNotEmpty || nameController.text.isNotEmpty;
|
||||
tags = widget.tags ?? [];
|
||||
subspaces = widget.subspaces ?? [];
|
||||
selectedSpaceModel = widget.currentSpaceModel;
|
||||
}
|
||||
|
||||
@override
|
||||
@ -570,12 +573,23 @@ class CreateSpaceDialogState extends State<CreateSpaceDialog> {
|
||||
}
|
||||
|
||||
bool _isNameConflict(String value) {
|
||||
return (widget.parentSpace?.children.any((child) => child.name == value) ??
|
||||
false) ||
|
||||
(widget.parentSpace?.name == value) ||
|
||||
(widget.editSpace?.parent?.name == value) ||
|
||||
(widget.editSpace?.children.any((child) => child.name == value) ??
|
||||
false);
|
||||
final parentSpace = widget.parentSpace;
|
||||
final editSpace = widget.editSpace;
|
||||
final siblings = parentSpace?.children
|
||||
.where((child) => child.uuid != editSpace?.uuid)
|
||||
.toList() ??
|
||||
[];
|
||||
final siblingConflict = siblings.any((child) => child.name == value);
|
||||
final parentConflict =
|
||||
parentSpace?.name == value && parentSpace?.uuid != editSpace?.uuid;
|
||||
final parentOfEditSpaceConflict = editSpace?.parent?.name == value &&
|
||||
editSpace?.parent?.uuid != editSpace?.uuid;
|
||||
final childConflict =
|
||||
editSpace?.children.any((child) => child.name == value) ?? false;
|
||||
return siblingConflict ||
|
||||
parentConflict ||
|
||||
parentOfEditSpaceConflict ||
|
||||
childConflict;
|
||||
}
|
||||
|
||||
void _showLinkSpaceModelDialog(BuildContext context) {
|
||||
|
@ -39,15 +39,25 @@ class _LoadedSpaceViewState extends State<LoadedSpaceView> {
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_spaceModels = widget.spaceModels ?? [];
|
||||
_spaceModels = List.from(widget.spaceModels ?? []);
|
||||
}
|
||||
|
||||
@override
|
||||
void didUpdateWidget(covariant LoadedSpaceView oldWidget) {
|
||||
super.didUpdateWidget(oldWidget);
|
||||
if (widget.spaceModels != oldWidget.spaceModels) {
|
||||
setState(() {
|
||||
_spaceModels = List.from(widget.spaceModels ?? []);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
void _onSpaceModelsUpdated(List<SpaceTemplateModel> updatedModels) {
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
if (mounted && updatedModels != _spaceModels) {
|
||||
setState(() {
|
||||
_spaceModels = updatedModels;
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
@ -72,8 +82,7 @@ class _LoadedSpaceViewState extends State<LoadedSpaceView> {
|
||||
),
|
||||
child: SpaceModelPage(
|
||||
products: widget.products,
|
||||
onSpaceModelsUpdated:
|
||||
_onSpaceModelsUpdated, // Pass callback
|
||||
onSpaceModelsUpdated: _onSpaceModelsUpdated,
|
||||
),
|
||||
),
|
||||
)
|
||||
@ -91,13 +100,4 @@ class _LoadedSpaceViewState extends State<LoadedSpaceView> {
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
SpaceModel? findSpaceByUuid(String? uuid, List<CommunityModel> communities) {
|
||||
for (var community in communities) {
|
||||
for (var space in community.spaces) {
|
||||
if (space.uuid == uuid) return space;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
43
lib/pages/spaces_management/helper/space_helper.dart
Normal file
43
lib/pages/spaces_management/helper/space_helper.dart
Normal file
@ -0,0 +1,43 @@
|
||||
import 'package:syncrow_web/pages/spaces_management/all_spaces/model/community_model.dart';
|
||||
import 'package:syncrow_web/pages/spaces_management/all_spaces/model/space_model.dart';
|
||||
|
||||
class SpaceHelper {
|
||||
static SpaceModel? findSpaceByUuid(
|
||||
String? uuid, List<CommunityModel> communities) {
|
||||
for (var community in communities) {
|
||||
for (var space in community.spaces) {
|
||||
if (space.uuid == uuid) return space;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
static SpaceModel? findSpaceByInternalId(
|
||||
String? internalId, List<SpaceModel> spaces) {
|
||||
if (internalId != null) {
|
||||
for (var space in spaces) {
|
||||
if (space.internalId == internalId) return space;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
static String generateUniqueSpaceName(
|
||||
String originalName, List<SpaceModel> spaces) {
|
||||
final baseName = originalName.replaceAll(RegExp(r'\(\d+\)$'), '').trim();
|
||||
int maxNumber = 0;
|
||||
|
||||
for (var space in spaces) {
|
||||
final match = RegExp(r'^(.*?)\((\d+)\)$').firstMatch(space.name);
|
||||
if (match != null && match.group(1)?.trim() == baseName) {
|
||||
int existingNumber = int.parse(match.group(2)!);
|
||||
if (existingNumber > maxNumber) {
|
||||
maxNumber = existingNumber;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return "$baseName(${maxNumber + 1})";
|
||||
}
|
||||
}
|
@ -1,5 +1,4 @@
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:syncrow_web/pages/spaces_management/all_spaces/model/product_model.dart';
|
||||
import 'package:syncrow_web/pages/spaces_management/space_model/models/subspace_template_model.dart';
|
||||
import 'package:syncrow_web/pages/spaces_management/space_model/models/tag_model.dart';
|
||||
import 'package:syncrow_web/pages/spaces_management/space_model/models/tag_update_model.dart';
|
||||
|
Reference in New Issue
Block a user