Files
flux/lib/features/attachments/models/attachment_model.dart
T
brontomark 6d2c563ce2
Build and Release FLUX (Multi-Platform) / build-web (push) Successful in 1m37s
Build and Release FLUX (Multi-Platform) / build-linux (push) Successful in 1m38s
Build and Release FLUX (Multi-Platform) / build-macos (push) Successful in 2m24s
Build and Release FLUX (Multi-Platform) / build-windows (push) Successful in 10m50s
feat: add customer orders feature with management, forms, and PDF export
2026-12-02 18:26:45 +01:00

203 lines
5.8 KiB
Dart

import 'dart:typed_data';
import 'package:equatable/equatable.dart';
enum AttachmentViewMode {
cropped('cropped'),
original('original');
final String value;
const AttachmentViewMode(this.value);
static AttachmentViewMode? fromString(String? val) {
if (val == null) return null;
final clean = val.trim().toLowerCase();
if (clean == 'cropped') return AttachmentViewMode.cropped;
if (clean == 'original') return AttachmentViewMode.original;
return null;
}
}
class AttachmentModel extends Equatable {
final String? id;
final DateTime? createdAt;
final String? customerId;
final String? operationId;
final String? ticketId;
final String? shippingDocumentId;
final String? noteId;
final String? customerOrderId;
final String name;
final String extension;
final String? storagePath;
final String? thumbnailPath;
final String? croppedStoragePath;
final AttachmentViewMode? viewMode;
final int fileSize;
final Uint8List? localBytes;
final String companyId;
const AttachmentModel({
this.id,
this.createdAt,
this.customerId,
this.operationId,
this.ticketId,
this.shippingDocumentId,
this.noteId,
this.customerOrderId,
required this.name,
required this.extension,
this.storagePath,
this.thumbnailPath,
this.croppedStoragePath,
this.viewMode,
required this.fileSize,
this.localBytes,
required this.companyId,
});
factory AttachmentModel.empty() => const AttachmentModel(
name: '',
extension: '',
fileSize: 0,
companyId: '',
);
@override
List<Object?> get props => [
id,
createdAt,
customerId,
operationId,
ticketId,
shippingDocumentId,
noteId,
customerOrderId,
name,
extension,
storagePath,
thumbnailPath,
croppedStoragePath,
viewMode,
fileSize,
localBytes,
companyId,
];
bool get isLocal => localBytes != null;
bool get isPdf => extension.toLowerCase().replaceAll('.', '') == 'pdf';
String? get effectiveStoragePath {
if (viewMode == AttachmentViewMode.cropped) {
if (croppedStoragePath != null && croppedStoragePath!.isNotEmpty) {
return croppedStoragePath;
}
return storagePath;
} else if (viewMode == AttachmentViewMode.original) {
if (storagePath != null && storagePath!.isNotEmpty) {
return storagePath;
}
return croppedStoragePath;
}
// Default fallback quando viewMode non è specificato
if (croppedStoragePath != null && croppedStoragePath!.isNotEmpty) {
return croppedStoragePath;
}
return storagePath;
}
String get sizeFormatted {
if (fileSize <= 0) return "0 B";
const suffixes = ["B", "KB", "MB", "GB", "TB"];
var i = (fileSize.toString().length - 1) ~/ 3;
if (i >= suffixes.length) i = suffixes.length - 1;
double num = fileSize / (1 << (i * 10));
return "${num.toStringAsFixed(i == 0 ? 0 : 1)} ${suffixes[i]}";
}
AttachmentModel copyWith({
String? id,
DateTime? createdAt,
String? customerId,
String? operationId,
String? ticketId,
String? shippingDocumentId,
String? noteId,
String? customerOrderId,
String? name,
String? extension,
String? storagePath,
String? thumbnailPath,
String? croppedStoragePath,
AttachmentViewMode? viewMode,
int? fileSize,
Uint8List? localBytes,
String? companyId,
}) => AttachmentModel(
id: id ?? this.id,
createdAt: createdAt ?? this.createdAt,
customerId: customerId ?? this.customerId,
operationId: operationId ?? this.operationId,
ticketId: ticketId ?? this.ticketId,
shippingDocumentId: shippingDocumentId ?? this.shippingDocumentId,
noteId: noteId ?? this.noteId,
customerOrderId: customerOrderId ?? this.customerOrderId,
name: name ?? this.name,
extension: extension ?? this.extension,
storagePath: storagePath ?? this.storagePath,
thumbnailPath: thumbnailPath ?? this.thumbnailPath,
croppedStoragePath: croppedStoragePath ?? this.croppedStoragePath,
viewMode: viewMode ?? this.viewMode,
fileSize: fileSize ?? this.fileSize,
localBytes: localBytes ?? this.localBytes,
companyId: companyId ?? this.companyId,
);
factory AttachmentModel.fromMap(Map<String, dynamic> map) {
return AttachmentModel(
id: map['id'] as String,
createdAt: map['created_at'] != null
? DateTime.parse(map['created_at'])
: null,
customerId: map['customer_id'] as String?,
operationId: map['operation_id'] as String?,
ticketId: map['ticket_id'] as String?,
shippingDocumentId: map['shipping_document_id'] as String?,
noteId: map['note_id'] as String?,
customerOrderId: map['customer_order_id'] as String?,
name: map['name'] as String,
extension: map['extension'] as String,
storagePath: map['storage_path'] as String?,
thumbnailPath: map['thumbnail_path'] as String?,
croppedStoragePath: map['cropped_storage_path'] as String?,
viewMode: AttachmentViewMode.fromString(map['view_mode'] as String?),
fileSize: map['file_size'] is int
? map['file_size']
: int.tryParse(map['file_size']?.toString() ?? '0') ?? 0,
companyId: map['company_id'] as String,
);
}
Map<String, dynamic> toMap() {
return {
if (id != null) 'id': id,
'name': name,
'extension': extension,
'storage_path': storagePath,
'customer_id': customerId,
'operation_id': operationId,
'ticket_id': ticketId,
'shipping_document_id': shippingDocumentId,
'note_id': noteId,
'customer_order_id': customerOrderId,
'file_size': fileSize,
'company_id': companyId,
'thumbnail_path': thumbnailPath,
'cropped_storage_path': croppedStoragePath,
'view_mode': (viewMode ?? AttachmentViewMode.original).value,
};
}
}