mirror of
https://github.com/SyncrowIOT/web.git
synced 2025-07-10 07:07:19 +00:00
105 lines
3.3 KiB
Dart
105 lines
3.3 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';
|
|
|
|
class SpaceModel {
|
|
String? uuid;
|
|
final String? icon;
|
|
final String? spaceTuyaUuid;
|
|
final String name;
|
|
final bool isPrivate;
|
|
final String? invitationCode;
|
|
SpaceModel? parent;
|
|
final CommunityModel? community;
|
|
List<SpaceModel> children;
|
|
Offset position;
|
|
bool isHovered;
|
|
|
|
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});
|
|
|
|
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,
|
|
);
|
|
|
|
// 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;
|
|
}
|
|
@override
|
|
String toString() {
|
|
return '''
|
|
SpaceModel {
|
|
uuid: $uuid,
|
|
name: $name,
|
|
isPrivate: $isPrivate,
|
|
position: {dx: ${position.dx}, dy: ${position.dy}},
|
|
parentUuid: ${parent?.uuid},
|
|
children: ${children.map((child) => child.name).toList()},
|
|
isHovered: $isHovered
|
|
}''';
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|