From 8b8dd0a427f3de1b852fc50466508587ca878096 Mon Sep 17 00:00:00 2001 From: Mark M2 Macbook Date: Wed, 20 May 2026 12:08:10 +0200 Subject: [PATCH] i --- lib/features/notes/models/note_model.dart | 150 ++++++++++++++++++++++ 1 file changed, 150 insertions(+) create mode 100644 lib/features/notes/models/note_model.dart diff --git a/lib/features/notes/models/note_model.dart b/lib/features/notes/models/note_model.dart new file mode 100644 index 0000000..887f7b4 --- /dev/null +++ b/lib/features/notes/models/note_model.dart @@ -0,0 +1,150 @@ +import 'package:equatable/equatable.dart'; +import 'package:flutter/material.dart'; +import 'package:flux/features/master_data/staff/models/staff_member_model.dart'; + +class NoteModel extends Equatable { + final String? id; + final DateTime? createdAt; + final DateTime? updatedAt; + final String createdBy; + final String? title; + final String? content; + final String color; // Stringa Hex es. '#FFF59D' + final bool isPinned; + final bool isSharedAll; + final String companyId; + + // Campi di utilità per la UI e le relazioni + final List collaboratorIds; + final List collaborators; + + const NoteModel({ + this.id, + this.createdAt, + this.updatedAt, + required this.createdBy, + this.title, + this.content, + this.color = '#FFF59D', // Giallo Post-it di default + this.isPinned = false, + this.isSharedAll = false, + required this.companyId, + this.collaboratorIds = const [], + this.collaborators = const [], + }); + + /// Trasforma il colore Hex String in un oggetto Color di Flutter + Color get flutterColor { + final hexCode = color.replaceAll('#', ''); + return Color(int.parse('FF$hexCode', radix: 16)); + } + + NoteModel copyWith({ + String? id, + DateTime? createdAt, + DateTime? updatedAt, + String? createdBy, + String? title, + String? content, + String? color, + bool? isPinned, + bool? isSharedAll, + String? companyId, + List? collaboratorIds, + List? collaborators, + }) { + return NoteModel( + id: id ?? this.id, + createdAt: createdAt ?? this.createdAt, + updatedAt: updatedAt ?? this.updatedAt, + createdBy: createdBy ?? this.createdBy, + title: title ?? this.title, + content: content ?? this.content, + color: color ?? this.color, + isPinned: isPinned ?? this.isPinned, + isSharedAll: isSharedAll ?? this.isSharedAll, + companyId: companyId ?? this.companyId, + collaboratorIds: collaboratorIds ?? this.collaboratorIds, + collaborators: collaborators ?? this.collaborators, + ); + } + + factory NoteModel.empty({ + required String createdBy, + required String companyId, + }) { + return NoteModel(createdBy: createdBy, companyId: companyId); + } + + Map toMap() { + return { + if (id != null) 'id': id, + 'created_by': createdBy, + 'title': title, + 'content': content, + 'color': color, + 'is_pinned': isPinned, + 'is_shared_all': isSharedAll, + 'company_id': companyId, + // I collaboratori vanno in una tabella separata, quindi non li inseriamo qui + }; + } + + factory NoteModel.fromMap(Map map) { + // Estraiamo gli ID dei collaboratori se presenti dalla join nativa di Supabase + List collIds = []; + List collModels = []; + + if (map['note_collaborators'] != null) { + final List jsonList = map['note_collaborators'] as List; + for (var item in jsonList) { + if (item['staff_id'] != null) { + collIds.add(item['staff_id'].toString()); + } + // Se abbiamo fatto la join profonda per avere anche i dettagli dello staff member + if (item['staff_members'] != null) { + collModels.add( + StaffMemberModel.fromMap( + item['staff_members'] as Map, + ), + ); + } + } + } + + return NoteModel( + id: map['id'] as String?, + createdAt: map['created_at'] != null + ? DateTime.parse(map['created_at'] as String) + : null, + updatedAt: map['updated_at'] != null + ? DateTime.parse(map['updated_at'] as String) + : null, + createdBy: map['created_by'] as String, + title: map['title'] as String?, + content: map['content'] as String?, + color: map['color'] as String? ?? '#FFF59D', + isPinned: map['is_pinned'] as bool? ?? false, + isSharedAll: map['is_shared_all'] as bool? ?? false, + companyId: map['company_id'] as String, + collaboratorIds: collIds, + collaborators: collModels, + ); + } + + @override + List get props => [ + id, + createdAt, + updatedAt, + createdBy, + title, + content, + color, + isPinned, + isSharedAll, + companyId, + collaboratorIds, + collaborators, + ]; +}