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,3 +1,7 @@
|
||||
import 'package:uuid/uuid.dart';
|
||||
|
||||
import 'sync_enums.dart';
|
||||
|
||||
class SupplementIntake {
|
||||
final int? id;
|
||||
final int supplementId;
|
||||
@@ -6,6 +10,12 @@ class SupplementIntake {
|
||||
final double unitsTaken; // Number of units taken (can be fractional)
|
||||
final String? notes;
|
||||
|
||||
// Sync metadata
|
||||
final String syncId;
|
||||
final DateTime lastModified;
|
||||
final SyncStatus syncStatus;
|
||||
final bool isDeleted;
|
||||
|
||||
SupplementIntake({
|
||||
this.id,
|
||||
required this.supplementId,
|
||||
@@ -13,7 +23,12 @@ class SupplementIntake {
|
||||
required this.dosageTaken,
|
||||
required this.unitsTaken,
|
||||
this.notes,
|
||||
});
|
||||
String? syncId,
|
||||
DateTime? lastModified,
|
||||
this.syncStatus = SyncStatus.pending,
|
||||
this.isDeleted = false,
|
||||
}) : syncId = syncId ?? const Uuid().v4(),
|
||||
lastModified = lastModified ?? DateTime.now();
|
||||
|
||||
Map<String, dynamic> toMap() {
|
||||
return {
|
||||
@@ -23,6 +38,10 @@ class SupplementIntake {
|
||||
'dosageTaken': dosageTaken,
|
||||
'unitsTaken': unitsTaken,
|
||||
'notes': notes,
|
||||
'syncId': syncId,
|
||||
'lastModified': lastModified.toIso8601String(),
|
||||
'syncStatus': syncStatus.name,
|
||||
'isDeleted': isDeleted ? 1 : 0,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -34,6 +53,17 @@ class SupplementIntake {
|
||||
dosageTaken: map['dosageTaken'],
|
||||
unitsTaken: (map['unitsTaken'] ?? 1).toDouble(), // Default for backwards compatibility
|
||||
notes: map['notes'],
|
||||
syncId: map['syncId'] ?? const Uuid().v4(),
|
||||
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,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -44,6 +74,10 @@ class SupplementIntake {
|
||||
double? dosageTaken,
|
||||
double? unitsTaken,
|
||||
String? notes,
|
||||
String? syncId,
|
||||
DateTime? lastModified,
|
||||
SyncStatus? syncStatus,
|
||||
bool? isDeleted,
|
||||
}) {
|
||||
return SupplementIntake(
|
||||
id: id ?? this.id,
|
||||
@@ -52,6 +86,26 @@ class SupplementIntake {
|
||||
dosageTaken: dosageTaken ?? this.dosageTaken,
|
||||
unitsTaken: unitsTaken ?? this.unitsTaken,
|
||||
notes: notes ?? this.notes,
|
||||
syncId: syncId ?? this.syncId,
|
||||
lastModified: lastModified ?? this.lastModified,
|
||||
syncStatus: syncStatus ?? this.syncStatus,
|
||||
isDeleted: isDeleted ?? this.isDeleted,
|
||||
);
|
||||
}
|
||||
|
||||
/// Create a new intake marked as synced
|
||||
SupplementIntake markAsSynced() {
|
||||
return copyWith(
|
||||
syncStatus: SyncStatus.synced,
|
||||
);
|
||||
}
|
||||
|
||||
/// Create a new intake marked for deletion
|
||||
SupplementIntake markAsDeleted() {
|
||||
return copyWith(
|
||||
isDeleted: true,
|
||||
lastModified: DateTime.now(),
|
||||
syncStatus: SyncStatus.modified,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user