mirror of
https://github.com/SyncrowIOT/web.git
synced 2025-07-14 17:25:50 +00:00
added bloc for create community
This commit is contained in:
@ -0,0 +1,48 @@
|
||||
import 'package:syncrow_web/pages/auth/model/region_model.dart';
|
||||
import 'package:syncrow_web/pages/spaces_management/all_spaces/model/space_model.dart';
|
||||
|
||||
class CommunityModel {
|
||||
final String uuid;
|
||||
final DateTime createdAt;
|
||||
final DateTime updatedAt;
|
||||
String name;
|
||||
final String description;
|
||||
final RegionModel? region;
|
||||
List<SpaceModel> spaces;
|
||||
|
||||
CommunityModel({
|
||||
required this.uuid,
|
||||
required this.createdAt,
|
||||
required this.updatedAt,
|
||||
required this.name,
|
||||
required this.description,
|
||||
required this.spaces,
|
||||
this.region,
|
||||
});
|
||||
|
||||
factory CommunityModel.fromJson(Map<String, dynamic> json) {
|
||||
return CommunityModel(
|
||||
uuid: json['uuid'],
|
||||
createdAt: DateTime.parse(json['createdAt']),
|
||||
updatedAt: DateTime.parse(json['updatedAt']),
|
||||
name: json['name'],
|
||||
description: json['description'],
|
||||
region: json['region'] != null ? RegionModel.fromJson(json['region']) : null,
|
||||
spaces: json['spaces'] != null
|
||||
? (json['spaces'] as List).map((space) => SpaceModel.fromJson(space)).toList()
|
||||
: [],
|
||||
);
|
||||
}
|
||||
|
||||
Map<String, dynamic> toMap() {
|
||||
return {
|
||||
'uuid': uuid,
|
||||
'createdAt': createdAt.toIso8601String(),
|
||||
'updatedAt': updatedAt.toIso8601String(),
|
||||
'name': name,
|
||||
'description': description,
|
||||
'region': region?.toJson(),
|
||||
'spaces': spaces.map((space) => space.toMap()).toList(), // Convert spaces to Map
|
||||
};
|
||||
}
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
import 'package:syncrow_web/pages/spaces_management/all_spaces/model/space_model.dart';
|
||||
|
||||
class Connection {
|
||||
final SpaceModel startSpace;
|
||||
final SpaceModel endSpace;
|
||||
final String direction;
|
||||
|
||||
Connection({required this.startSpace, required this.endSpace, required this.direction});
|
||||
|
||||
Map<String, dynamic> toMap() {
|
||||
return {
|
||||
'startUuid': startSpace.uuid ?? 'unsaved-start-space-${startSpace.name}', // Fallback for unsaved spaces
|
||||
'endUuid': endSpace.uuid ?? 'unsaved-end-space-${endSpace.name}', // Fallback for unsaved spaces
|
||||
'direction': direction,
|
||||
};
|
||||
}
|
||||
|
||||
static Connection fromMap(Map<String, dynamic> map, Map<String, SpaceModel> spaces) {
|
||||
return Connection(
|
||||
startSpace: spaces[map['startUuid']]!,
|
||||
endSpace: spaces[map['endUuid']]!,
|
||||
direction: map['direction'],
|
||||
);
|
||||
}
|
||||
}
|
@ -0,0 +1,70 @@
|
||||
import 'package:syncrow_web/utils/constants/assets.dart';
|
||||
|
||||
class ProductModel {
|
||||
final String uuid;
|
||||
final String catName;
|
||||
String? name;
|
||||
final String prodId;
|
||||
final String prodType;
|
||||
String? icon;
|
||||
|
||||
ProductModel({
|
||||
required this.uuid,
|
||||
required this.catName,
|
||||
required this.prodId,
|
||||
required this.prodType,
|
||||
required this.name,
|
||||
this.icon,
|
||||
});
|
||||
|
||||
// Factory method to create a Product from JSON
|
||||
factory ProductModel.fromMap(Map<String, dynamic> json) {
|
||||
String icon = _mapIconToProduct(json['prodType']);
|
||||
return ProductModel(
|
||||
uuid: json['uuid'],
|
||||
catName: json['catName'],
|
||||
prodId: json['prodId'],
|
||||
prodType: json['prodType'],
|
||||
name: json['name'] ?? '',
|
||||
icon: _mapIconToProduct(json['prodType']));
|
||||
}
|
||||
|
||||
// Method to convert a Product to JSON
|
||||
Map<String, dynamic> toMap() {
|
||||
return {
|
||||
'uuid': uuid,
|
||||
'catName': catName,
|
||||
'prodId': prodId,
|
||||
'prodType': prodType,
|
||||
};
|
||||
}
|
||||
|
||||
static String _mapIconToProduct(String prodType) {
|
||||
const iconMapping = {
|
||||
'1G': Assets.Gang1SwitchIcon,
|
||||
'1GT': Assets.oneTouchSwitch,
|
||||
'2G': Assets.Gang2SwitchIcon,
|
||||
'2GT': Assets.twoTouchSwitch,
|
||||
'3G': Assets.Gang3SwitchIcon,
|
||||
'3GT': Assets.threeTouchSwitch,
|
||||
'CUR': Assets.curtain,
|
||||
'GD': Assets.garageDoor,
|
||||
'GW': Assets.SmartGatewayIcon,
|
||||
'DL': Assets.DoorLockIcon,
|
||||
'WL': Assets.waterLeakSensor,
|
||||
'WH': Assets.waterHeater,
|
||||
'AC': Assets.ac,
|
||||
'CPS': Assets.presenceSensor,
|
||||
'PC': Assets.powerClamp,
|
||||
'WPS': Assets.presenceSensor,
|
||||
'DS': Assets.doorSensor
|
||||
};
|
||||
|
||||
return iconMapping[prodType] ?? Assets.presenceSensor;
|
||||
}
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return 'ProductModel(uuid: $uuid, catName: $catName, prodId: $prodId, prodType: $prodType, name: $name, icon: $icon)';
|
||||
}
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
class SelectedProduct {
|
||||
final String productId;
|
||||
int count;
|
||||
|
||||
SelectedProduct({required this.productId, required this.count});
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
return {
|
||||
'productId': productId,
|
||||
'count': count,
|
||||
};
|
||||
}
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return 'SelectedProduct(productId: $productId, count: $count)';
|
||||
}
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
import 'dart:ui';
|
||||
|
||||
class SpaceData {
|
||||
final String name;
|
||||
final String icon;
|
||||
Offset position;
|
||||
bool isHovered;
|
||||
|
||||
SpaceData({
|
||||
required this.name,
|
||||
required this.icon,
|
||||
required this.position,
|
||||
this.isHovered = false,
|
||||
});
|
||||
}
|
134
lib/pages/spaces_management/all_spaces/model/space_model.dart
Normal file
134
lib/pages/spaces_management/all_spaces/model/space_model.dart
Normal file
@ -0,0 +1,134 @@
|
||||
import 'dart:ui';
|
||||
import 'package:syncrow_web/pages/spaces_management/all_spaces/model/community_model.dart';
|
||||
import 'package:syncrow_web/pages/spaces_management/all_spaces/model/connection_model.dart';
|
||||
import 'package:syncrow_web/pages/spaces_management/all_spaces/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()
|
||||
: [];
|
||||
|
||||
final instance = 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()
|
||||
: [],
|
||||
);
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
|
||||
import 'space_model.dart';
|
||||
|
||||
class SpacesResponse {
|
||||
final List<SpaceModel> data;
|
||||
final String message;
|
||||
final int page;
|
||||
final int size;
|
||||
final int totalItem;
|
||||
final int totalPage;
|
||||
final bool hasNext;
|
||||
final bool hasPrevious;
|
||||
|
||||
SpacesResponse({
|
||||
required this.data,
|
||||
required this.message,
|
||||
required this.page,
|
||||
required this.size,
|
||||
required this.totalItem,
|
||||
required this.totalPage,
|
||||
required this.hasNext,
|
||||
required this.hasPrevious,
|
||||
});
|
||||
|
||||
factory SpacesResponse.fromJson(Map<String, dynamic> json) {
|
||||
return SpacesResponse(
|
||||
data: (json['data'] as List)
|
||||
.map((jsonItem) => SpaceModel.fromJson(jsonItem))
|
||||
.toList(),
|
||||
message: json['message'],
|
||||
page: json['page'],
|
||||
size: json['size'],
|
||||
totalItem: json['totalItem'],
|
||||
totalPage: json['totalPage'],
|
||||
hasNext: json['hasNext'],
|
||||
hasPrevious: json['hasPrevious'],
|
||||
);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user