Files
supplements/lib/models/ingredient.dart
2025-08-27 20:51:29 +02:00

101 lines
2.5 KiB
Dart

import '../services/database_sync_service.dart';
class Ingredient {
final int? id;
final String name; // e.g., "Vitamin K2", "Vitamin D3"
final double amount; // e.g., 75, 20
final String unit; // e.g., "mcg", "mg", "IU"
// Sync metadata
final String syncId;
final DateTime lastModified;
final RecordSyncStatus syncStatus;
final bool isDeleted;
const Ingredient({
this.id,
required this.name,
required this.amount,
required this.unit,
required this.syncId,
required this.lastModified,
this.syncStatus = RecordSyncStatus.pending,
this.isDeleted = false,
});
Map<String, dynamic> toMap() {
return {
'id': id,
'name': name,
'amount': amount,
'unit': unit,
'syncId': syncId,
'lastModified': lastModified.toIso8601String(),
'syncStatus': syncStatus.name,
'isDeleted': isDeleted ? 1 : 0,
};
}
factory Ingredient.fromMap(Map<String, dynamic> map) {
return Ingredient(
id: map['id'],
name: map['name'],
amount: map['amount']?.toDouble() ?? 0.0,
unit: map['unit'],
syncId: map['syncId'] ?? '',
lastModified: map['lastModified'] != null
? DateTime.parse(map['lastModified'])
: DateTime.now(),
syncStatus: map['syncStatus'] != null
? RecordSyncStatus.values.firstWhere(
(e) => e.name == map['syncStatus'],
orElse: () => RecordSyncStatus.pending,
)
: RecordSyncStatus.pending,
isDeleted: (map['isDeleted'] ?? 0) == 1,
);
}
Ingredient copyWith({
int? id,
String? name,
double? amount,
String? unit,
String? syncId,
DateTime? lastModified,
RecordSyncStatus? syncStatus,
bool? isDeleted,
}) {
return Ingredient(
id: id ?? this.id,
name: name ?? this.name,
amount: amount ?? this.amount,
unit: unit ?? this.unit,
syncId: syncId ?? this.syncId,
lastModified: lastModified ?? this.lastModified,
syncStatus: syncStatus ?? this.syncStatus,
isDeleted: isDeleted ?? this.isDeleted,
);
}
@override
String toString() {
return '$amount$unit $name';
}
@override
bool operator ==(Object other) {
if (identical(this, other)) return true;
return other is Ingredient &&
other.name == name &&
other.amount == amount &&
other.unit == unit &&
other.syncId == syncId;
}
@override
int get hashCode {
return name.hashCode ^ amount.hashCode ^ unit.hashCode ^ syncId.hashCode;
}
}