mirror of
https://github.com/SyncrowIOT/web.git
synced 2025-07-15 17:47:53 +00:00
save only if there is a change or new space
This commit is contained in:
@ -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/community_model.dart';
|
||||||
import 'package:syncrow_web/pages/spaces_management/model/connection_model.dart';
|
import 'package:syncrow_web/pages/spaces_management/model/connection_model.dart';
|
||||||
|
|
||||||
|
enum SpaceStatus { newSpace, modified, unchanged }
|
||||||
|
|
||||||
class SpaceModel {
|
class SpaceModel {
|
||||||
String? uuid;
|
String? uuid;
|
||||||
final String? icon;
|
final String? icon;
|
||||||
@ -14,23 +16,26 @@ class SpaceModel {
|
|||||||
List<SpaceModel> children;
|
List<SpaceModel> children;
|
||||||
Offset position;
|
Offset position;
|
||||||
bool isHovered;
|
bool isHovered;
|
||||||
|
SpaceStatus status;
|
||||||
|
|
||||||
List<Connection> outgoingConnections = []; // Connections from this space
|
List<Connection> outgoingConnections = []; // Connections from this space
|
||||||
Connection? incomingConnection; // Connections to this space
|
Connection? incomingConnection; // Connections to this space
|
||||||
|
|
||||||
SpaceModel(
|
SpaceModel({
|
||||||
{this.uuid,
|
this.uuid,
|
||||||
this.spaceTuyaUuid,
|
this.spaceTuyaUuid,
|
||||||
required this.icon,
|
required this.icon,
|
||||||
required this.name,
|
required this.name,
|
||||||
required this.isPrivate,
|
required this.isPrivate,
|
||||||
this.invitationCode,
|
this.invitationCode,
|
||||||
this.parent,
|
this.parent,
|
||||||
this.community,
|
this.community,
|
||||||
required this.children,
|
required this.children,
|
||||||
required this.position,
|
required this.position,
|
||||||
this.isHovered = false,
|
this.isHovered = false,
|
||||||
this.incomingConnection});
|
this.incomingConnection,
|
||||||
|
this.status = SpaceStatus.unchanged,
|
||||||
|
});
|
||||||
|
|
||||||
factory SpaceModel.fromJson(Map<String, dynamic> json) {
|
factory SpaceModel.fromJson(Map<String, dynamic> json) {
|
||||||
// Create SpaceModel instance first
|
// Create SpaceModel instance first
|
||||||
@ -55,7 +60,8 @@ class SpaceModel {
|
|||||||
// Add incomingConnection to the instance after creation
|
// Add incomingConnection to the instance after creation
|
||||||
if (json['incomingConnections'] != null &&
|
if (json['incomingConnections'] != null &&
|
||||||
json['incomingConnections'] is List &&
|
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];
|
final conn = json['incomingConnections'][0];
|
||||||
instance.incomingConnection = Connection(
|
instance.incomingConnection = Connection(
|
||||||
startSpace: instance.parent ?? instance, // Parent space
|
startSpace: instance.parent ?? instance, // Parent space
|
||||||
|
@ -45,6 +45,13 @@ class SpaceManagementPageState extends State<SpaceManagementPage> {
|
|||||||
if (state is SpaceManagementLoading) {
|
if (state is SpaceManagementLoading) {
|
||||||
return const Center(child: CircularProgressIndicator());
|
return const Center(child: CircularProgressIndicator());
|
||||||
} else if (state is SpaceManagementLoaded) {
|
} else if (state is SpaceManagementLoaded) {
|
||||||
|
int selectedIndex = state.communities.indexWhere(
|
||||||
|
(community) => community.uuid == selectedCommunity?.uuid,
|
||||||
|
);
|
||||||
|
if (selectedIndex != -1) {
|
||||||
|
selectedCommunity = state.communities[selectedIndex];
|
||||||
|
}
|
||||||
|
|
||||||
return LoadedSpaceView(
|
return LoadedSpaceView(
|
||||||
communities: state.communities,
|
communities: state.communities,
|
||||||
selectedCommunity: selectedCommunity,
|
selectedCommunity: selectedCommunity,
|
||||||
|
@ -193,6 +193,9 @@ class _CommunityStructureAreaState extends State<CommunityStructureArea> {
|
|||||||
void _updateNodePosition(SpaceModel node, Offset newPosition) {
|
void _updateNodePosition(SpaceModel node, Offset newPosition) {
|
||||||
setState(() {
|
setState(() {
|
||||||
node.position = newPosition;
|
node.position = newPosition;
|
||||||
|
if (node.status != SpaceStatus.newSpace) {
|
||||||
|
node.status = SpaceStatus.modified; // Mark as modified
|
||||||
|
}
|
||||||
if (node.position.dx >= canvasWidth - 200) {
|
if (node.position.dx >= canvasWidth - 200) {
|
||||||
canvasWidth += 200;
|
canvasWidth += 200;
|
||||||
}
|
}
|
||||||
@ -244,6 +247,7 @@ class _CommunityStructureAreaState extends State<CommunityStructureArea> {
|
|||||||
position: centerPosition,
|
position: centerPosition,
|
||||||
isPrivate: false,
|
isPrivate: false,
|
||||||
children: [],
|
children: [],
|
||||||
|
status: SpaceStatus.newSpace,
|
||||||
);
|
);
|
||||||
|
|
||||||
if (parentIndex != null && direction != null) {
|
if (parentIndex != null && direction != null) {
|
||||||
@ -330,14 +334,21 @@ class _CommunityStructureAreaState extends State<CommunityStructureArea> {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Prepare spaces and connections data
|
List<SpaceModel> spacesToSave = spaces.where((space) {
|
||||||
List<SpaceModel> spaceList = spaces;
|
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;
|
String communityUuid = widget.selectedCommunity!.uuid;
|
||||||
|
|
||||||
// Dispatch the save event
|
|
||||||
context.read<SpaceManagementBloc>().add(SaveSpacesEvent(
|
context.read<SpaceManagementBloc>().add(SaveSpacesEvent(
|
||||||
spaces: spaceList,
|
spaces: spacesToSave,
|
||||||
communityUuid: communityUuid,
|
communityUuid: communityUuid,
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user