mirror of
https://github.com/SyncrowIOT/web.git
synced 2025-07-10 15:17:31 +00:00
117 lines
3.9 KiB
Dart
117 lines
3.9 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';
|
|
import 'package:syncrow_web/utils/constants/assets.dart';
|
|
import 'package:uuid/uuid.dart';
|
|
|
|
enum SpaceStatus { newSpace, modified, unchanged, deleted }
|
|
|
|
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;
|
|
String internalId;
|
|
|
|
List<Connection> outgoingConnections = []; // Connections from this space
|
|
Connection? incomingConnection; // Connections to this space
|
|
|
|
SpaceModel({
|
|
this.uuid,
|
|
String? internalId,
|
|
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 [],
|
|
}) : internalId = internalId ?? const Uuid().v4();
|
|
|
|
factory SpaceModel.fromJson(Map<String, dynamic> json, {String? parentInternalId}) {
|
|
final String internalId = json['internalId'] ?? const Uuid().v4();
|
|
|
|
final List<SpaceModel> children = json['children'] != null
|
|
? (json['children'] as List).map((childJson) {
|
|
return SpaceModel.fromJson(
|
|
childJson,
|
|
parentInternalId: internalId,
|
|
);
|
|
}).toList()
|
|
: [];
|
|
|
|
return SpaceModel(
|
|
internalId: internalId,
|
|
uuid: json['uuid'] ?? '',
|
|
spaceTuyaUuid: json['spaceTuyaUuid'],
|
|
name: json['spaceName'],
|
|
isPrivate: json['isPrivate'] ?? false,
|
|
invitationCode: json['invitationCode'],
|
|
parent: parentInternalId != null
|
|
? SpaceModel(
|
|
internalId: parentInternalId,
|
|
uuid: json['parent']?['uuid'],
|
|
spaceTuyaUuid: json['parent']?['spaceTuyaUuid'],
|
|
name: json['parent']?['spaceName'] ?? '',
|
|
isPrivate: json['parent']?['isPrivate'] ?? false,
|
|
invitationCode: json['parent']?['invitationCode'],
|
|
children: [],
|
|
position: Offset(json['parent']?['x'] ?? 0, json['parent']?['y'] ?? 0),
|
|
icon: json['parent']?['icon'] ?? Assets.location,
|
|
)
|
|
: null,
|
|
community: json['community'] != null ? CommunityModel.fromJson(json['community']) : null,
|
|
children: children,
|
|
icon: json['icon'] ?? Assets.location,
|
|
position: Offset(json['x'] ?? 0, json['y'] ?? 0),
|
|
isHovered: false,
|
|
selectedProducts: json['spaceProducts'] != null
|
|
? (json['spaceProducts'] as List).map((product) {
|
|
return SelectedProduct(
|
|
productId: product['product']['uuid'],
|
|
count: product['productCount'],
|
|
);
|
|
}).toList()
|
|
: [],
|
|
);
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|