mirror of
https://github.com/vleeuwenmenno/supplements.git
synced 2025-09-11 18:29:12 +02:00
adds syncing
This commit is contained in:
@@ -1,14 +1,26 @@
|
||||
import 'sync_enums.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 SyncStatus 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 = SyncStatus.pending,
|
||||
this.isDeleted = false,
|
||||
});
|
||||
|
||||
Map<String, dynamic> toMap() {
|
||||
@@ -17,6 +29,10 @@ class Ingredient {
|
||||
'name': name,
|
||||
'amount': amount,
|
||||
'unit': unit,
|
||||
'syncId': syncId,
|
||||
'lastModified': lastModified.toIso8601String(),
|
||||
'syncStatus': syncStatus.name,
|
||||
'isDeleted': isDeleted ? 1 : 0,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -26,6 +42,17 @@ class Ingredient {
|
||||
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
|
||||
? SyncStatus.values.firstWhere(
|
||||
(e) => e.name == map['syncStatus'],
|
||||
orElse: () => SyncStatus.pending,
|
||||
)
|
||||
: SyncStatus.pending,
|
||||
isDeleted: (map['isDeleted'] ?? 0) == 1,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -34,12 +61,20 @@ class Ingredient {
|
||||
String? name,
|
||||
double? amount,
|
||||
String? unit,
|
||||
String? syncId,
|
||||
DateTime? lastModified,
|
||||
SyncStatus? 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,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -54,11 +89,12 @@ class Ingredient {
|
||||
return other is Ingredient &&
|
||||
other.name == name &&
|
||||
other.amount == amount &&
|
||||
other.unit == unit;
|
||||
other.unit == unit &&
|
||||
other.syncId == syncId;
|
||||
}
|
||||
|
||||
@override
|
||||
int get hashCode {
|
||||
return name.hashCode ^ amount.hashCode ^ unit.hashCode;
|
||||
return name.hashCode ^ amount.hashCode ^ unit.hashCode ^ syncId.hashCode;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user