mirror of
https://github.com/SyncrowIOT/syncrow-app.git
synced 2025-07-15 09:45:22 +00:00
47 lines
1.4 KiB
Dart
47 lines
1.4 KiB
Dart
import 'package:syncrow_app/features/app_layout/model/community_model.dart';
|
|
import 'package:syncrow_app/features/devices/model/room_model.dart';
|
|
|
|
class SpaceModel {
|
|
final String id;
|
|
final String name;
|
|
final Community community;
|
|
final List<SubSpaceModel> subspaces;
|
|
|
|
SpaceModel({
|
|
required this.id,
|
|
required this.name,
|
|
required this.community,
|
|
this.subspaces = const [], // Default to an empty list
|
|
});
|
|
|
|
/// Converts the instance into JSON format.
|
|
Map<String, dynamic> toJson() {
|
|
return {
|
|
'id': id,
|
|
'spaceName': name,
|
|
'community': community.toJson(),
|
|
'subspaces': subspaces.map((room) => room.toJson()).toList(),
|
|
};
|
|
}
|
|
|
|
/// Factory constructor to create an instance from JSON.
|
|
factory SpaceModel.fromJson(Map<String, dynamic> json) {
|
|
final spaceData = json['space'] as Map<String, dynamic>? ?? {};
|
|
|
|
return SpaceModel(
|
|
id: json['uuid'] ?? '',
|
|
name: spaceData['spaceName'] ?? 'Unnamed Space',
|
|
community: Community.fromJson(spaceData['community'] ?? {}),
|
|
subspaces: (spaceData['subspaces'] as List<dynamic>?)
|
|
?.map((item) => SubSpaceModel.fromJson(item))
|
|
.toList() ??
|
|
[],
|
|
);
|
|
}
|
|
|
|
/// Helper method to parse a list of SpaceModel from JSON.
|
|
static List<SpaceModel> fromJsonList(List<dynamic> jsonList) {
|
|
return jsonList.map((item) => SpaceModel.fromJson(item)).toList();
|
|
}
|
|
}
|