diff --git a/lib/pages/spaces_management/assign_tag_models/views/assign_tag_models_dialog.dart b/lib/pages/spaces_management/assign_tag_models/views/assign_tag_models_dialog.dart index 5eef92f8..8cff1b35 100644 --- a/lib/pages/spaces_management/assign_tag_models/views/assign_tag_models_dialog.dart +++ b/lib/pages/spaces_management/assign_tag_models/views/assign_tag_models_dialog.dart @@ -251,32 +251,15 @@ class AssignTagModelsDialog extends StatelessWidget { builder: (buttonContext) => CancelButton( label: 'Add New Device', onPressed: () async { - for (var tag in state.tags) { - if (tag.location == null) { - continue; - } + final updatedTags = + List.from(state.tags); + final result = + processTags(updatedTags, subspaces); - final previousTagSubspace = - checkTagExistInSubspace( - tag, subspaces ?? []); + final processedTags = + result['updatedTags'] as List; + final processedSubspaces = result['subspaces']; - if (tag.location == 'Main Space') { - removeTagFromSubspace( - tag, previousTagSubspace); - } else if (tag.location != - previousTagSubspace?.subspaceName) { - removeTagFromSubspace( - tag, previousTagSubspace); - moveToNewSubspace(tag, subspaces ?? []); - state.tags.removeWhere( - (t) => t.internalId == tag.internalId); - } else { - updateTagInSubspace( - tag, previousTagSubspace); - state.tags.removeWhere( - (t) => t.internalId == tag.internalId); - } - } if (context.mounted) { Navigator.of(context).pop(); @@ -293,15 +276,16 @@ class AssignTagModelsDialog extends StatelessWidget { allTags: allTags, spaceName: spaceName, otherSpaceModels: otherSpaceModels, - spaceTagModels: state.tags, + spaceTagModels: processedTags, pageContext: pageContext, spaceModel: SpaceTemplateModel( modelName: spaceName, - tags: state.tags, + tags: updatedTags, uuid: spaceModel?.uuid, internalId: spaceModel?.internalId, - subspaceModels: subspaces)), + subspaceModels: + processedSubspaces)), ); } }, @@ -318,33 +302,17 @@ class AssignTagModelsDialog extends StatelessWidget { foregroundColor: ColorsManager.whiteColors, onPressed: state.isSaveEnabled ? () async { - for (var tag in state.tags) { - if (tag.location == null || - subspaces == null) { - continue; - } + final updatedTags = + List.from(state.tags); + final result = + processTags(updatedTags, subspaces); - final previousTagSubspace = - checkTagExistInSubspace( - tag, subspaces ?? []); + final processedTags = + result['updatedTags'] as List; + final processedSubspaces = + result['subspaces'] + as List; - if (tag.location == 'Main Space') { - removeTagFromSubspace( - tag, previousTagSubspace); - } else if (tag.location != - previousTagSubspace?.subspaceName) { - removeTagFromSubspace( - tag, previousTagSubspace); - moveToNewSubspace(tag, subspaces ?? []); - state.tags.removeWhere((t) => - t.internalId == tag.internalId); - } else { - updateTagInSubspace( - tag, previousTagSubspace); - state.tags.removeWhere((t) => - t.internalId == tag.internalId); - } - } Navigator.of(context) .popUntil((route) => route.isFirst); @@ -359,11 +327,12 @@ class AssignTagModelsDialog extends StatelessWidget { otherSpaceModels: otherSpaceModels, spaceModel: SpaceTemplateModel( modelName: spaceName, - tags: state.tags, + tags: processedTags, uuid: spaceModel?.uuid, internalId: spaceModel?.internalId, - subspaceModels: subspaces), + subspaceModels: + processedSubspaces), ); }, ); @@ -397,39 +366,100 @@ class AssignTagModelsDialog extends StatelessWidget { .toList(); } - void removeTagFromSubspace(TagModel tag, SubspaceTemplateModel? subspace) { - subspace?.tags?.removeWhere((t) => t.internalId == tag.internalId); - } - - SubspaceTemplateModel? checkTagExistInSubspace( + int? checkTagExistInSubspace( TagModel tag, List? subspaces) { if (subspaces == null) return null; - for (var subspace in subspaces) { - if (subspace.tags == null) return null; + for (int i = 0; i < subspaces.length; i++) { + final subspace = subspaces[i]; + if (subspace.tags == null) continue; for (var t in subspace.tags!) { - if (tag.internalId == t.internalId) return subspace; + if (tag.internalId == t.internalId) { + return i; + } } } return null; } - void moveToNewSubspace(TagModel tag, List subspaces) { - final targetSubspace = subspaces - .firstWhere((subspace) => subspace.subspaceName == tag.location); + Map processTags( + List updatedTags, List? subspaces) { + final modifiedTags = List.from(updatedTags); + final modifiedSubspaces = List.from(subspaces ?? []); - targetSubspace.tags ??= []; - if (targetSubspace.tags?.any((t) => t.internalId == tag.internalId) != - true) { - targetSubspace.tags?.add(tag); - } - } + for (var tag in modifiedTags.toList()) { + if (modifiedSubspaces.isEmpty) continue; - void updateTagInSubspace(TagModel tag, SubspaceTemplateModel? subspace) { - final currentTag = subspace?.tags?.firstWhere( - (t) => t.internalId == tag.internalId, - ); - if (currentTag != null) { - currentTag.tag = tag.tag; + final prevIndice = checkTagExistInSubspace(tag, modifiedSubspaces); + + if ((tag.location == 'Main Space' || tag.location == null) && + (prevIndice == null || + modifiedSubspaces[prevIndice].subspaceName == 'Main Space')) { + continue; + } + + if ((tag.location == 'Main Space' || tag.location == null) && + prevIndice != null) { + modifiedSubspaces[prevIndice] + .tags + ?.removeWhere((t) => t.internalId == tag.internalId); + continue; + } + + if ((tag.location != 'Main Space' && tag.location != null) && + prevIndice == null) { + final newIndex = modifiedSubspaces + .indexWhere((subspace) => subspace.subspaceName == tag.location); + if (newIndex != -1) { + if (modifiedSubspaces[newIndex] + .tags + ?.any((t) => t.internalId == tag.internalId) != + true) { + tag.location = modifiedSubspaces[newIndex].subspaceName; + modifiedSubspaces[newIndex].tags?.add(tag); + } + } + modifiedTags.removeWhere((t) => t.internalId == tag.internalId); + continue; + } + + if ((tag.location != 'Main Space' && tag.location != null) && + tag.location != modifiedSubspaces[prevIndice!].subspaceName) { + modifiedSubspaces[prevIndice] + .tags + ?.removeWhere((t) => t.internalId == tag.internalId); + final newIndex = modifiedSubspaces + .indexWhere((subspace) => subspace.subspaceName == tag.location); + if (newIndex != -1) { + if (modifiedSubspaces[newIndex] + .tags + ?.any((t) => t.internalId == tag.internalId) != + true) { + tag.location = modifiedSubspaces[newIndex].subspaceName; + modifiedSubspaces[newIndex].tags?.add(tag); + } + } + + modifiedTags.removeWhere((t) => t.internalId == tag.internalId); + continue; + } + + if ((tag.location != 'Main Space' && tag.location != null) && + tag.location == modifiedSubspaces[prevIndice!].subspaceName) { + modifiedTags.removeWhere((t) => t.internalId == tag.internalId); + continue; + } + + if ((tag.location == 'Main Space' || tag.location == null) && + prevIndice != null) { + modifiedSubspaces[prevIndice] + .tags + ?.removeWhere((t) => t.internalId == tag.internalId); + } } + + return { + 'updatedTags': modifiedTags, + 'subspaces': modifiedSubspaces, + }; } } diff --git a/lib/pages/spaces_management/helper/tag_helper.dart b/lib/pages/spaces_management/helper/tag_helper.dart index c0213622..4fa86b88 100644 --- a/lib/pages/spaces_management/helper/tag_helper.dart +++ b/lib/pages/spaces_management/helper/tag_helper.dart @@ -18,19 +18,19 @@ class TagHelper { if (subspaces != null) { for (var subspace in subspaces) { if (subspace.tags != null) { - for (var existingTag in subspace.tags!) { - initialTags.addAll( - subspace.tags!.map( - (tag) => tag.copyWith( - location: subspace.subspaceName, - internalId: existingTag.internalId, - tag: existingTag.tag), + initialTags.addAll( + subspace.tags!.map( + (tag) => tag.copyWith( + location: subspace.subspaceName, + internalId: tag.internalId, + tag: tag.tag, ), - ); - } + ), + ); } } } + return initialTags; } diff --git a/lib/pages/spaces_management/space_model/widgets/tag_chips_display_widget.dart b/lib/pages/spaces_management/space_model/widgets/tag_chips_display_widget.dart index 76c65805..11c7ca5b 100644 --- a/lib/pages/spaces_management/space_model/widgets/tag_chips_display_widget.dart +++ b/lib/pages/spaces_management/space_model/widgets/tag_chips_display_widget.dart @@ -89,7 +89,7 @@ class TagChipDisplay extends StatelessWidget { EditChip(onTap: () async { // Use the Navigator's context for showDialog Navigator.of(context).pop(); - + await showDialog( barrierDismissible: false, context: context, diff --git a/lib/pages/spaces_management/tag_model/views/add_device_type_model_widget.dart b/lib/pages/spaces_management/tag_model/views/add_device_type_model_widget.dart index 1ef3da9b..9d0eac96 100644 --- a/lib/pages/spaces_management/tag_model/views/add_device_type_model_widget.dart +++ b/lib/pages/spaces_management/tag_model/views/add_device_type_model_widget.dart @@ -5,6 +5,7 @@ import 'package:syncrow_web/pages/common/buttons/default_button.dart'; import 'package:syncrow_web/pages/spaces_management/assign_tag_models/views/assign_tag_models_dialog.dart'; import 'package:syncrow_web/pages/spaces_management/all_spaces/model/product_model.dart'; import 'package:syncrow_web/pages/spaces_management/all_spaces/model/selected_product_model.dart'; +import 'package:syncrow_web/pages/spaces_management/helper/tag_helper.dart'; import 'package:syncrow_web/pages/spaces_management/space_model/models/space_template_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'; @@ -123,7 +124,7 @@ class AddDeviceTypeModelWidget extends StatelessWidget { }, ); } else { - final initialTags = generateInitialTags( + final initialTags = TagHelper.generateInitialTags( spaceTagModels: spaceTagModels, subspaces: subspaces, ); @@ -165,7 +166,8 @@ class AddDeviceTypeModelWidget extends StatelessWidget { : () async { if (state is AddDeviceModelLoaded && state.selectedProducts.isNotEmpty) { - final initialTags = generateInitialTags( + final initialTags = + TagHelper.generateInitialTags( spaceTagModels: spaceTagModels, subspaces: subspaces, ); @@ -204,31 +206,4 @@ class AddDeviceTypeModelWidget extends StatelessWidget { ), ); } - - List generateInitialTags({ - List? spaceTagModels, - List? subspaces, - }) { - final List initialTags = []; - - if (spaceTagModels != null) { - initialTags.addAll(spaceTagModels); - } - - if (subspaces != null) { - for (var subspace in subspaces) { - if (subspace.tags != null) { - initialTags.addAll( - subspace.tags!.map( - (tag) => tag.copyWith( - location: subspace.subspaceName, - tag: tag.tag, - internalId: tag.internalId), - ), - ); - } - } - } - return initialTags; - } }