mirror of
https://github.com/SyncrowIOT/web.git
synced 2025-07-10 07:07:19 +00:00
123 lines
3.7 KiB
Dart
123 lines
3.7 KiB
Dart
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';
|
|
import 'package:syncrow_web/pages/spaces_management/model/selected_product_model.dart';
|
|
|
|
enum SpaceStatus { newSpace, modified, unchanged }
|
|
|
|
class SpaceModel {
|
|
String? uuid;
|
|
String? icon;
|
|
final String? spaceTuyaUuid;
|
|
String name;
|
|
final bool isPrivate;
|
|
final String? invitationCode;
|
|
SpaceModel? parent;
|
|
final CommunityModel? community;
|
|
List<SpaceModel> children;
|
|
Offset position;
|
|
bool isHovered;
|
|
SpaceStatus status;
|
|
List<SelectedProduct> selectedProducts;
|
|
|
|
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,
|
|
this.status = SpaceStatus.unchanged,
|
|
this.selectedProducts = const [],
|
|
});
|
|
|
|
factory SpaceModel.fromJson(Map<String, dynamic> json) {
|
|
// Create SpaceModel instance first
|
|
final instance = SpaceModel(
|
|
uuid: json['uuid'] ?? '',
|
|
spaceTuyaUuid: json['spaceTuyaUuid'],
|
|
name: json['spaceName'],
|
|
isPrivate: json['isPrivate'] ?? false,
|
|
invitationCode: json['invitationCode'],
|
|
parent: json['parent'] != null ? SpaceModel.fromJson(json['parent']) : null,
|
|
community: json['community'] != null ? CommunityModel.fromJson(json['community']) : null,
|
|
children: json['children'] != null
|
|
? (json['children'] as List).map((child) => SpaceModel.fromJson(child)).toList()
|
|
: [],
|
|
icon: json['icon'] as String?,
|
|
position: json['x'] != null && json['y'] != null
|
|
? Offset(json['x'], json['y'])
|
|
: const Offset(0, 0),
|
|
isHovered: false,
|
|
selectedProducts: json['spaceProducts'] != null
|
|
? (json['spaceProducts'] as List).map((product) {
|
|
return SelectedProduct(
|
|
productId: product['product']['uuid'],
|
|
count: product['productCount'],
|
|
);
|
|
}).toList()
|
|
: [],
|
|
);
|
|
|
|
// Add incomingConnection to the instance after creation
|
|
if (json['incomingConnections'] != null &&
|
|
json['incomingConnections'] is List &&
|
|
(json['incomingConnections'] as List).isNotEmpty &&
|
|
instance.parent != null) {
|
|
final conn = json['incomingConnections'][0];
|
|
instance.incomingConnection = Connection(
|
|
startSpace: instance.parent ?? instance, // Parent space
|
|
endSpace: instance, // This space instance
|
|
direction: conn['direction'],
|
|
);
|
|
}
|
|
|
|
return instance;
|
|
}
|
|
|
|
Map<String, dynamic> toMap() {
|
|
return {
|
|
'uuid': uuid ?? '',
|
|
'spaceTuyaUuid': spaceTuyaUuid,
|
|
'name': name,
|
|
'isPrivate': isPrivate,
|
|
'invitationCode': invitationCode,
|
|
'parent': parent?.uuid,
|
|
'community': community?.toMap(),
|
|
'children': children.map((child) => child.toMap()).toList(),
|
|
'icon': icon,
|
|
'position': {'dx': position.dx, 'dy': position.dy},
|
|
'isHovered': isHovered,
|
|
'outgoingConnections': outgoingConnections.map((c) => c.toMap()).toList(),
|
|
'incomingConnection': incomingConnection?.toMap(),
|
|
};
|
|
}
|
|
|
|
void addOutgoingConnection(Connection connection) {
|
|
outgoingConnections.add(connection);
|
|
}
|
|
|
|
@override
|
|
String toString() {
|
|
return '''
|
|
SpaceModel(
|
|
uuid: $uuid,
|
|
name: $name,
|
|
icon: $icon,
|
|
isPrivate: $isPrivate,
|
|
position: $position,
|
|
selectedProducts: $selectedProducts
|
|
)
|
|
''';
|
|
}
|
|
}
|