save only if there is a change or new space

This commit is contained in:
hannathkadher
2024-11-19 13:11:57 +04:00
parent 3147fc154f
commit 3a333188f8
3 changed files with 42 additions and 18 deletions

View File

@ -2,6 +2,8 @@ import 'dart:ui';
import 'package:syncrow_web/pages/spaces_management/model/community_model.dart';
import 'package:syncrow_web/pages/spaces_management/model/connection_model.dart';
enum SpaceStatus { newSpace, modified, unchanged }
class SpaceModel {
String? uuid;
final String? icon;
@ -14,23 +16,26 @@ class SpaceModel {
List<SpaceModel> children;
Offset position;
bool isHovered;
SpaceStatus status;
List<Connection> outgoingConnections = []; // Connections from this space
Connection? incomingConnection; // Connections to this space
SpaceModel(
{this.uuid,
this.spaceTuyaUuid,
required this.icon,
required this.name,
required this.isPrivate,
this.invitationCode,
this.parent,
this.community,
required this.children,
required this.position,
this.isHovered = false,
this.incomingConnection});
SpaceModel({
this.uuid,
this.spaceTuyaUuid,
required this.icon,
required this.name,
required this.isPrivate,
this.invitationCode,
this.parent,
this.community,
required this.children,
required this.position,
this.isHovered = false,
this.incomingConnection,
this.status = SpaceStatus.unchanged,
});
factory SpaceModel.fromJson(Map<String, dynamic> json) {
// Create SpaceModel instance first
@ -55,7 +60,8 @@ class SpaceModel {
// Add incomingConnection to the instance after creation
if (json['incomingConnections'] != null &&
json['incomingConnections'] is List &&
(json['incomingConnections'] as List).isNotEmpty && instance.parent != null ) {
(json['incomingConnections'] as List).isNotEmpty &&
instance.parent != null) {
final conn = json['incomingConnections'][0];
instance.incomingConnection = Connection(
startSpace: instance.parent ?? instance, // Parent space

View File

@ -45,6 +45,13 @@ class SpaceManagementPageState extends State<SpaceManagementPage> {
if (state is SpaceManagementLoading) {
return const Center(child: CircularProgressIndicator());
} else if (state is SpaceManagementLoaded) {
int selectedIndex = state.communities.indexWhere(
(community) => community.uuid == selectedCommunity?.uuid,
);
if (selectedIndex != -1) {
selectedCommunity = state.communities[selectedIndex];
}
return LoadedSpaceView(
communities: state.communities,
selectedCommunity: selectedCommunity,

View File

@ -193,6 +193,9 @@ class _CommunityStructureAreaState extends State<CommunityStructureArea> {
void _updateNodePosition(SpaceModel node, Offset newPosition) {
setState(() {
node.position = newPosition;
if (node.status != SpaceStatus.newSpace) {
node.status = SpaceStatus.modified; // Mark as modified
}
if (node.position.dx >= canvasWidth - 200) {
canvasWidth += 200;
}
@ -244,6 +247,7 @@ class _CommunityStructureAreaState extends State<CommunityStructureArea> {
position: centerPosition,
isPrivate: false,
children: [],
status: SpaceStatus.newSpace,
);
if (parentIndex != null && direction != null) {
@ -330,14 +334,21 @@ class _CommunityStructureAreaState extends State<CommunityStructureArea> {
return;
}
// Prepare spaces and connections data
List<SpaceModel> spaceList = spaces;
List<SpaceModel> spacesToSave = spaces.where((space) {
return space.status == SpaceStatus.newSpace || space.status == SpaceStatus.modified;
}).toList();
if (spacesToSave.isEmpty) {
print("No new or modified spaces to save.");
return;
}
String communityUuid = widget.selectedCommunity!.uuid;
// Dispatch the save event
context.read<SpaceManagementBloc>().add(SaveSpacesEvent(
spaces: spaceList,
spaces: spacesToSave,
communityUuid: communityUuid,
));
}
}