Files
flux/lib/features/notes/models/note_model.dart

132 lines
3.4 KiB
Dart
Raw Permalink Normal View History

2026-05-20 12:08:10 +02:00
import 'package:equatable/equatable.dart';
import 'package:flutter/material.dart';
class NoteModel extends Equatable {
final String? id;
final DateTime? createdAt;
final DateTime? updatedAt;
2026-05-21 14:43:47 +02:00
final String companyId;
2026-05-20 12:08:10 +02:00
final String createdBy;
final String? title;
final String? content;
2026-05-21 14:43:47 +02:00
final String color;
2026-05-20 12:08:10 +02:00
final bool isPinned;
final bool isSharedAll;
2026-05-21 14:43:47 +02:00
final List<String> collaboratorIds;
2026-05-20 12:08:10 +02:00
// Campi di utilità per la UI e le relazioni
2026-05-21 14:43:47 +02:00
//final List<StaffMemberModel> collaborators;
2026-05-20 12:08:10 +02:00
const NoteModel({
this.id,
this.createdAt,
this.updatedAt,
2026-05-21 14:43:47 +02:00
required this.companyId,
2026-05-20 12:08:10 +02:00
required this.createdBy,
this.title,
this.content,
2026-05-21 14:43:47 +02:00
this.color = '#FFF59D',
2026-05-20 12:08:10 +02:00
this.isPinned = false,
this.isSharedAll = false,
2026-05-21 14:43:47 +02:00
required this.collaboratorIds,
2026-05-20 12:08:10 +02:00
});
/// 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,
2026-05-21 14:43:47 +02:00
String? companyId,
2026-05-20 12:08:10 +02:00
String? createdBy,
String? title,
String? content,
String? color,
bool? isPinned,
bool? isSharedAll,
List<String>? collaboratorIds,
}) {
return NoteModel(
id: id ?? this.id,
createdAt: createdAt ?? this.createdAt,
updatedAt: updatedAt ?? this.updatedAt,
2026-05-21 14:43:47 +02:00
companyId: companyId ?? this.companyId,
2026-05-20 12:08:10 +02:00
createdBy: createdBy ?? this.createdBy,
title: title ?? this.title,
content: content ?? this.content,
color: color ?? this.color,
isPinned: isPinned ?? this.isPinned,
isSharedAll: isSharedAll ?? this.isSharedAll,
collaboratorIds: collaboratorIds ?? this.collaboratorIds,
);
}
factory NoteModel.empty({
required String createdBy,
required String companyId,
}) {
2026-05-21 14:43:47 +02:00
return NoteModel(
createdBy: createdBy,
companyId: companyId,
collaboratorIds: [],
);
2026-05-20 12:08:10 +02:00
}
Map<String, dynamic> 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<String, dynamic> 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,
2026-05-21 14:43:47 +02:00
companyId: map['company_id'] as String,
2026-05-20 12:08:10 +02:00
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,
2026-05-21 14:43:47 +02:00
// TRUCCO NINJA: Castiamo l'array di Postgres in una List<String> pulita
collaboratorIds: map['collaborator_ids'] != null
? List<String>.from(map['collaborator_ids'] as List)
: [],
2026-05-20 12:08:10 +02:00
);
}
@override
List<Object?> get props => [
id,
createdAt,
updatedAt,
createdBy,
title,
content,
color,
isPinned,
isSharedAll,
companyId,
collaboratorIds,
2026-05-21 14:43:47 +02:00
//collaborators,
2026-05-20 12:08:10 +02:00
];
}