creek

The AI Image Editor of 2030

lib/data/models/project_model.dart (1764B)


 1import 'dart:convert';
 2
 3class ProjectModel {
 4  final int? id;
 5  final String title;
 6  final String? description;
 7  final int? parentId; // null -> Project, int - Event
 8  final String? globalStylesheet; // Stored as JSON string
 9  final DateTime lastAccessedAt;
10  final DateTime createdAt;
11  final List<String> assetsPath;
12
13  ProjectModel({
14    this.id,
15    required this.title,
16    this.description,
17    this.parentId,
18    this.globalStylesheet,
19    required this.lastAccessedAt,
20    required this.createdAt,
21    this.assetsPath = const [],
22  });
23
24  bool get isEvent => parentId != null;
25
26  // Helper to get stylesheet as a JSON Object (Map)
27  Map<String, dynamic> get styleSheetMap {
28    if (globalStylesheet == null || globalStylesheet!.isEmpty) return {};
29    try {
30      return jsonDecode(globalStylesheet!) as Map<String, dynamic>;
31    } catch (e) {
32      return {};
33    }
34  }
35
36  Map<String, dynamic> toMap() {
37    return {
38      'id': id,
39      'title': title,
40      'description': description,
41      'parent_id': parentId,
42      'global_stylesheet': globalStylesheet,
43      'last_accessed_at': lastAccessedAt.toIso8601String(),
44      'created_at': createdAt.toIso8601String(),
45      'assets_path': jsonEncode(assetsPath),
46    };
47  }
48
49  factory ProjectModel.fromMap(Map<String, dynamic> map) {
50    return ProjectModel(
51      id: map['id'],
52      title: map['title'],
53      description: map['description'],
54      parentId: map['parent_id'],
55      globalStylesheet: map['global_stylesheet'],
56      lastAccessedAt: DateTime.parse(map['last_accessed_at']),
57      createdAt: DateTime.parse(map['created_at']),
58      assetsPath:
59          map['assets_path'] != null
60              ? List<String>.from(jsonDecode(map['assets_path']))
61              : [],
62    );
63  }
64}