fixed issue in creating space

This commit is contained in:
hannathkadher
2025-02-02 21:02:58 +04:00
parent 916b606cb1
commit 91dfd53477
6 changed files with 110 additions and 57 deletions

View File

@ -195,7 +195,6 @@ class _CommunityStructureAreaState extends State<CommunityStructureArea> {
screenSize,
position:
spaces[index].position + newPosition,
parentIndex: index,
direction: direction,
);
@ -374,6 +373,15 @@ class _CommunityStructureAreaState extends State<CommunityStructureArea> {
widget.selectedSpace!.status =
SpaceStatus.modified; // Mark as modified
}
for (var space in spaces) {
if (space.internalId == widget.selectedSpace?.internalId) {
space.status = SpaceStatus.modified;
space.subspaces = subspaces;
space.tags = tags;
space.name = name;
}
}
});
},
key: Key(widget.selectedSpace!.name),
@ -452,7 +460,6 @@ class _CommunityStructureAreaState extends State<CommunityStructureArea> {
}).toList();
if (spacesToSave.isEmpty) {
debugPrint("No new or modified spaces to save.");
return;
}

View File

@ -457,7 +457,7 @@ class CreateSpaceDialogState extends State<CreateSpaceDialog> {
context: context,
builder: (context) => AssignTagDialog(
products: widget.products,
subspaces: widget.subspaces,
subspaces: subspaces,
addedProducts: TagHelper
.createInitialSelectedProductsForTags(
tags ?? [], subspaces),
@ -488,7 +488,6 @@ class CreateSpaceDialogState extends State<CreateSpaceDialog> {
enteredName,
widget.isEdit,
widget.products,
subspaces,
);
},
style: TextButton.styleFrom(
@ -617,9 +616,26 @@ class CreateSpaceDialogState extends State<CreateSpaceDialog> {
products: products,
existingSubSpaces: existingSubSpaces,
onSave: (slectedSubspaces) {
final List<Tag> tagsToAppendToSpace = [];
if (slectedSubspaces != null) {
final updatedIds =
slectedSubspaces.map((s) => s.internalId).toSet();
if (existingSubSpaces != null) {
final deletedSubspaces = existingSubSpaces
.where((s) => !updatedIds.contains(s.internalId))
.toList();
for (var s in deletedSubspaces) {
if (s.tags != null) {
tagsToAppendToSpace.addAll(s.tags!);
}
}
}
}
if (slectedSubspaces != null) {
setState(() {
subspaces = slectedSubspaces;
tags?.addAll(tagsToAppendToSpace);
selectedSpaceModel = null;
});
}
@ -629,7 +645,7 @@ class CreateSpaceDialogState extends State<CreateSpaceDialog> {
}
void _showTagCreateDialog(BuildContext context, String name, bool isEdit,
List<ProductModel>? products, List<SubspaceModel>? subspaces) {
List<ProductModel>? products) {
isEdit
? showDialog(
context: context,

View File

@ -11,7 +11,7 @@ import 'package:syncrow_web/pages/spaces_management/space_model/models/space_tem
import 'package:syncrow_web/pages/spaces_management/space_model/view/space_model_page.dart';
import 'package:syncrow_web/services/space_model_mang_api.dart';
class LoadedSpaceView extends StatelessWidget {
class LoadedSpaceView extends StatefulWidget {
final List<CommunityModel> communities;
final CommunityModel? selectedCommunity;
final SpaceModel? selectedSpace;
@ -26,41 +26,64 @@ class LoadedSpaceView extends StatelessWidget {
this.selectedSpace,
this.products,
this.spaceModels,
required this.shouldNavigateToSpaceModelPage
required this.shouldNavigateToSpaceModelPage,
});
@override
Widget build(BuildContext context) {
_LoadedSpaceViewState createState() => _LoadedSpaceViewState();
}
class _LoadedSpaceViewState extends State<LoadedSpaceView> {
late List<SpaceTemplateModel> _spaceModels;
@override
void initState() {
super.initState();
_spaceModels = widget.spaceModels ?? [];
}
void _onSpaceModelsUpdated(List<SpaceTemplateModel> updatedModels) {
WidgetsBinding.instance.addPostFrameCallback((_) {
setState(() {
_spaceModels = updatedModels;
});
});
}
@override
Widget build(BuildContext context) {
return Stack(
clipBehavior: Clip.none,
children: [
Row(
children: [
SidebarWidget(
communities: communities,
selectedSpaceUuid:
selectedSpace?.uuid ?? selectedCommunity?.uuid ?? '',
communities: widget.communities,
selectedSpaceUuid: widget.selectedSpace?.uuid ??
widget.selectedCommunity?.uuid ??
'',
),
shouldNavigateToSpaceModelPage
widget.shouldNavigateToSpaceModelPage
? Expanded(
child: BlocProvider(
create: (context) => SpaceModelBloc(
api: SpaceModelManagementApi(),
initialSpaceModels: spaceModels ?? [],
initialSpaceModels: _spaceModels,
),
child: SpaceModelPage(
products: products,
products: widget.products,
onSpaceModelsUpdated:
_onSpaceModelsUpdated, // Pass callback
),
),
)
: CommunityStructureArea(
selectedCommunity: selectedCommunity,
selectedSpace: selectedSpace,
spaces: selectedCommunity?.spaces ?? [],
products: products,
communities: communities,
spaceModels: spaceModels,
selectedCommunity: widget.selectedCommunity,
selectedSpace: widget.selectedSpace,
spaces: widget.selectedCommunity?.spaces ?? [],
products: widget.products,
communities: widget.communities,
spaceModels: _spaceModels,
),
],
),

View File

@ -233,7 +233,8 @@ class AssignTagDialog extends StatelessWidget {
label: 'Add New Device',
onPressed: () async {
final updatedTags = List<Tag>.from(state.tags);
final result = processTags(updatedTags, subspaces);
final result =
TagHelper.processTags(updatedTags, subspaces);
final processedTags =
result['updatedTags'] as List<Tag>;
@ -271,8 +272,8 @@ class AssignTagDialog extends StatelessWidget {
onPressed: state.isSaveEnabled
? () async {
final updatedTags = List<Tag>.from(state.tags);
final result =
processTags(updatedTags, subspaces);
final result = TagHelper.processTags(
updatedTags, subspaces);
final processedTags =
result['updatedTags'] as List<Tag>;
@ -311,33 +312,4 @@ class AssignTagDialog extends StatelessWidget {
);
return availableTagsForTagModel;
}
Map<String, dynamic> processTags(
List<Tag> updatedTags, List<SubspaceModel>? subspaces) {
return TagHelper.updateTags<Tag>(
updatedTags: updatedTags,
subspaces: subspaces,
getInternalId: (tag) => tag.internalId,
getLocation: (tag) => tag.location,
setLocation: (tag, location) => tag.location = location,
getSubspaceName: (subspace) => subspace.subspaceName,
getSubspaceTags: (subspace) => subspace.tags,
setSubspaceTags: (subspace, tags) => subspace.tags = tags,
checkTagExistInSubspace: checkTagExistInSubspace,
);
}
int? checkTagExistInSubspace(Tag tag, List<dynamic>? subspaces) {
if (subspaces == 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 i;
}
}
}
return null;
}
}

View File

@ -278,7 +278,8 @@ class TagHelper {
.toList();
}
static int? checkTagExistInSubspaceModels(TagModel tag, List<dynamic>? subspaces) {
static int? checkTagExistInSubspaceModels(
TagModel tag, List<dynamic>? subspaces) {
if (subspaces == null) return null;
for (int i = 0; i < subspaces.length; i++) {
@ -308,4 +309,32 @@ class TagHelper {
);
}
static int? checkTagExistInSubspace(Tag tag, List<dynamic>? subspaces) {
if (subspaces == 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 i;
}
}
}
return null;
}
static Map<String, dynamic> processTags(
List<Tag> updatedTags, List<SubspaceModel>? subspaces) {
return TagHelper.updateTags<Tag>(
updatedTags: updatedTags,
subspaces: subspaces,
getInternalId: (tag) => tag.internalId,
getLocation: (tag) => tag.location,
setLocation: (tag, location) => tag.location = location,
getSubspaceName: (subspace) => subspace.subspaceName,
getSubspaceTags: (subspace) => subspace.tags,
setSubspaceTags: (subspace, tags) => subspace.tags = tags,
checkTagExistInSubspace: checkTagExistInSubspace,
);
}
}

View File

@ -11,8 +11,10 @@ import 'package:syncrow_web/utils/color_manager.dart';
class SpaceModelPage extends StatelessWidget {
final List<ProductModel>? products;
final Function(List<SpaceTemplateModel>)? onSpaceModelsUpdated;
const SpaceModelPage({Key? key, this.products}) : super(key: key);
const SpaceModelPage({Key? key, this.products, this.onSpaceModelsUpdated})
: super(key: key);
@override
Widget build(BuildContext context) {
@ -25,6 +27,10 @@ class SpaceModelPage extends StatelessWidget {
final allTagValues = _getAllTagValues(spaceModels);
final allSpaceModelNames = _getAllSpaceModelName(spaceModels);
if (onSpaceModelsUpdated != null) {
onSpaceModelsUpdated!(spaceModels);
}
return Scaffold(
backgroundColor: ColorsManager.whiteColors,
body: Padding(