mirror of
https://github.com/SyncrowIOT/web.git
synced 2025-07-10 07:07:19 +00:00
fixed space model updates
This commit is contained in:
@ -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<TagModel>.from(state.tags);
|
||||
final result =
|
||||
processTags(updatedTags, subspaces);
|
||||
|
||||
final previousTagSubspace =
|
||||
checkTagExistInSubspace(
|
||||
tag, subspaces ?? []);
|
||||
final processedTags =
|
||||
result['updatedTags'] as List<TagModel>;
|
||||
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<TagModel>.from(state.tags);
|
||||
final result =
|
||||
processTags(updatedTags, subspaces);
|
||||
|
||||
final previousTagSubspace =
|
||||
checkTagExistInSubspace(
|
||||
tag, subspaces ?? []);
|
||||
final processedTags =
|
||||
result['updatedTags'] as List<TagModel>;
|
||||
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)
|
||||
.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<SubspaceTemplateModel>? 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<SubspaceTemplateModel> subspaces) {
|
||||
final targetSubspace = subspaces
|
||||
.firstWhere((subspace) => subspace.subspaceName == tag.location);
|
||||
Map<String, dynamic> processTags(
|
||||
List<TagModel> updatedTags, List<SubspaceTemplateModel>? subspaces) {
|
||||
final modifiedTags = List<TagModel>.from(updatedTags);
|
||||
final modifiedSubspaces = List<SubspaceTemplateModel>.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,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
@ -89,7 +89,7 @@ class TagChipDisplay extends StatelessWidget {
|
||||
EditChip(onTap: () async {
|
||||
// Use the Navigator's context for showDialog
|
||||
Navigator.of(context).pop();
|
||||
|
||||
|
||||
await showDialog<bool>(
|
||||
barrierDismissible: false,
|
||||
context: context,
|
||||
|
@ -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<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;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user