fixed space model updates

This commit is contained in:
hannathkadher
2025-01-25 01:29:21 +04:00
parent 4258ccdfbd
commit 9167c8da29
4 changed files with 121 additions and 116 deletions

View File

@ -251,32 +251,15 @@ class AssignTagModelsDialog extends StatelessWidget {
builder: (buttonContext) => CancelButton( builder: (buttonContext) => CancelButton(
label: 'Add New Device', label: 'Add New Device',
onPressed: () async { onPressed: () async {
for (var tag in state.tags) { final updatedTags =
if (tag.location == null) { List<TagModel>.from(state.tags);
continue; final result =
} processTags(updatedTags, subspaces);
final previousTagSubspace = final processedTags =
checkTagExistInSubspace( result['updatedTags'] as List<TagModel>;
tag, subspaces ?? []); 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) { if (context.mounted) {
Navigator.of(context).pop(); Navigator.of(context).pop();
@ -293,15 +276,16 @@ class AssignTagModelsDialog extends StatelessWidget {
allTags: allTags, allTags: allTags,
spaceName: spaceName, spaceName: spaceName,
otherSpaceModels: otherSpaceModels, otherSpaceModels: otherSpaceModels,
spaceTagModels: state.tags, spaceTagModels: processedTags,
pageContext: pageContext, pageContext: pageContext,
spaceModel: SpaceTemplateModel( spaceModel: SpaceTemplateModel(
modelName: spaceName, modelName: spaceName,
tags: state.tags, tags: updatedTags,
uuid: spaceModel?.uuid, uuid: spaceModel?.uuid,
internalId: internalId:
spaceModel?.internalId, spaceModel?.internalId,
subspaceModels: subspaces)), subspaceModels:
processedSubspaces)),
); );
} }
}, },
@ -318,33 +302,17 @@ class AssignTagModelsDialog extends StatelessWidget {
foregroundColor: ColorsManager.whiteColors, foregroundColor: ColorsManager.whiteColors,
onPressed: state.isSaveEnabled onPressed: state.isSaveEnabled
? () async { ? () async {
for (var tag in state.tags) { final updatedTags =
if (tag.location == null || List<TagModel>.from(state.tags);
subspaces == null) { final result =
continue; processTags(updatedTags, subspaces);
}
final previousTagSubspace = final processedTags =
checkTagExistInSubspace( result['updatedTags'] as List<TagModel>;
tag, subspaces ?? []); final processedSubspaces =
result['subspaces']
as List<SubspaceTemplateModel>;
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) Navigator.of(context)
.popUntil((route) => route.isFirst); .popUntil((route) => route.isFirst);
@ -359,11 +327,12 @@ class AssignTagModelsDialog extends StatelessWidget {
otherSpaceModels: otherSpaceModels, otherSpaceModels: otherSpaceModels,
spaceModel: SpaceTemplateModel( spaceModel: SpaceTemplateModel(
modelName: spaceName, modelName: spaceName,
tags: state.tags, tags: processedTags,
uuid: spaceModel?.uuid, uuid: spaceModel?.uuid,
internalId: internalId:
spaceModel?.internalId, spaceModel?.internalId,
subspaceModels: subspaces), subspaceModels:
processedSubspaces),
); );
}, },
); );
@ -397,39 +366,100 @@ class AssignTagModelsDialog extends StatelessWidget {
.toList(); .toList();
} }
void removeTagFromSubspace(TagModel tag, SubspaceTemplateModel? subspace) { int? checkTagExistInSubspace(
subspace?.tags?.removeWhere((t) => t.internalId == tag.internalId);
}
SubspaceTemplateModel? checkTagExistInSubspace(
TagModel tag, List<SubspaceTemplateModel>? subspaces) { TagModel tag, List<SubspaceTemplateModel>? subspaces) {
if (subspaces == null) return null; if (subspaces == null) return null;
for (var subspace in subspaces) { for (int i = 0; i < subspaces.length; i++) {
if (subspace.tags == null) return null; final subspace = subspaces[i];
if (subspace.tags == null) continue;
for (var t in subspace.tags!) { for (var t in subspace.tags!) {
if (tag.internalId == t.internalId) return subspace; if (tag.internalId == t.internalId) {
return i;
}
} }
} }
return null; return null;
} }
void moveToNewSubspace(TagModel tag, List<SubspaceTemplateModel> subspaces) { Map<String, dynamic> processTags(
final targetSubspace = subspaces List<TagModel> updatedTags, List<SubspaceTemplateModel>? subspaces) {
.firstWhere((subspace) => subspace.subspaceName == tag.location); final modifiedTags = List<TagModel>.from(updatedTags);
final modifiedSubspaces = List<SubspaceTemplateModel>.from(subspaces ?? []);
targetSubspace.tags ??= []; for (var tag in modifiedTags.toList()) {
if (targetSubspace.tags?.any((t) => t.internalId == tag.internalId) != if (modifiedSubspaces.isEmpty) continue;
true) {
targetSubspace.tags?.add(tag);
}
}
void updateTagInSubspace(TagModel tag, SubspaceTemplateModel? subspace) { final prevIndice = checkTagExistInSubspace(tag, modifiedSubspaces);
final currentTag = subspace?.tags?.firstWhere(
(t) => t.internalId == tag.internalId, if ((tag.location == 'Main Space' || tag.location == null) &&
); (prevIndice == null ||
if (currentTag != null) { modifiedSubspaces[prevIndice].subspaceName == 'Main Space')) {
currentTag.tag = tag.tag; 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,
};
} }
} }

View File

@ -18,19 +18,19 @@ class TagHelper {
if (subspaces != null) { if (subspaces != null) {
for (var subspace in subspaces) { for (var subspace in subspaces) {
if (subspace.tags != null) { if (subspace.tags != null) {
for (var existingTag in subspace.tags!) { initialTags.addAll(
initialTags.addAll( subspace.tags!.map(
subspace.tags!.map( (tag) => tag.copyWith(
(tag) => tag.copyWith( location: subspace.subspaceName,
location: subspace.subspaceName, internalId: tag.internalId,
internalId: existingTag.internalId, tag: tag.tag,
tag: existingTag.tag),
), ),
); ),
} );
} }
} }
} }
return initialTags; return initialTags;
} }

View File

@ -89,7 +89,7 @@ class TagChipDisplay extends StatelessWidget {
EditChip(onTap: () async { EditChip(onTap: () async {
// Use the Navigator's context for showDialog // Use the Navigator's context for showDialog
Navigator.of(context).pop(); Navigator.of(context).pop();
await showDialog<bool>( await showDialog<bool>(
barrierDismissible: false, barrierDismissible: false,
context: context, context: context,

View File

@ -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/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/product_model.dart';
import 'package:syncrow_web/pages/spaces_management/all_spaces/model/selected_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/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/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_model.dart';
@ -123,7 +124,7 @@ class AddDeviceTypeModelWidget extends StatelessWidget {
}, },
); );
} else { } else {
final initialTags = generateInitialTags( final initialTags = TagHelper.generateInitialTags(
spaceTagModels: spaceTagModels, spaceTagModels: spaceTagModels,
subspaces: subspaces, subspaces: subspaces,
); );
@ -165,7 +166,8 @@ class AddDeviceTypeModelWidget extends StatelessWidget {
: () async { : () async {
if (state is AddDeviceModelLoaded && if (state is AddDeviceModelLoaded &&
state.selectedProducts.isNotEmpty) { state.selectedProducts.isNotEmpty) {
final initialTags = generateInitialTags( final initialTags =
TagHelper.generateInitialTags(
spaceTagModels: spaceTagModels, spaceTagModels: spaceTagModels,
subspaces: subspaces, subspaces: subspaces,
); );
@ -204,31 +206,4 @@ class AddDeviceTypeModelWidget extends StatelessWidget {
), ),
); );
} }
List<TagModel> generateInitialTags({
List<TagModel>? spaceTagModels,
List<SubspaceTemplateModel>? subspaces,
}) {
final List<TagModel> 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;
}
} }