i
This commit is contained in:
150
lib/features/notes/models/note_model.dart
Normal file
150
lib/features/notes/models/note_model.dart
Normal file
@@ -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<String> collaboratorIds;
|
||||
final List<StaffMemberModel> 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<String>? collaboratorIds,
|
||||
List<StaffMemberModel>? 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<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) {
|
||||
// Estraiamo gli ID dei collaboratori se presenti dalla join nativa di Supabase
|
||||
List<String> collIds = [];
|
||||
List<StaffMemberModel> 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<String, dynamic>,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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<Object?> get props => [
|
||||
id,
|
||||
createdAt,
|
||||
updatedAt,
|
||||
createdBy,
|
||||
title,
|
||||
content,
|
||||
color,
|
||||
isPinned,
|
||||
isSharedAll,
|
||||
companyId,
|
||||
collaboratorIds,
|
||||
collaborators,
|
||||
];
|
||||
}
|
||||
Reference in New Issue
Block a user