adds syncing

This commit is contained in:
2025-08-27 16:17:21 +02:00
parent 1191d06e53
commit 709cf2cbd9
24 changed files with 3809 additions and 226 deletions

View File

@@ -1,6 +1,10 @@
import 'ingredient.dart';
import 'dart:convert';
import 'package:uuid/uuid.dart';
import 'ingredient.dart';
import 'sync_enums.dart';
class Supplement {
final int? id;
final String name;
@@ -14,6 +18,12 @@ class Supplement {
final DateTime createdAt;
final bool isActive;
// Sync metadata
final String syncId;
final DateTime lastModified;
final SyncStatus syncStatus;
final bool isDeleted;
Supplement({
this.id,
required this.name,
@@ -26,7 +36,12 @@ class Supplement {
this.notes,
required this.createdAt,
this.isActive = true,
});
String? syncId,
DateTime? lastModified,
this.syncStatus = SyncStatus.pending,
this.isDeleted = false,
}) : syncId = syncId ?? const Uuid().v4(),
lastModified = lastModified ?? DateTime.now();
// Helper getters
double get totalDosagePerIntake {
@@ -40,7 +55,7 @@ class Supplement {
if (ingredients.isEmpty) {
return 'No ingredients specified';
}
return ingredients.map((ingredient) =>
return ingredients.map((ingredient) =>
'${ingredient.amount * numberOfUnits}${ingredient.unit} ${ingredient.name}'
).join(', ');
}
@@ -66,12 +81,16 @@ class Supplement {
'notes': notes,
'createdAt': createdAt.toIso8601String(),
'isActive': isActive ? 1 : 0,
'syncId': syncId,
'lastModified': lastModified.toIso8601String(),
'syncStatus': syncStatus.name,
'isDeleted': isDeleted ? 1 : 0,
};
}
factory Supplement.fromMap(Map<String, dynamic> map) {
List<Ingredient> ingredients = [];
// Try to parse ingredients if they exist
if (map['ingredients'] != null && map['ingredients'].isNotEmpty) {
try {
@@ -98,6 +117,17 @@ class Supplement {
notes: map['notes'],
createdAt: DateTime.parse(map['createdAt']),
isActive: map['isActive'] == 1,
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,
);
}
@@ -113,6 +143,10 @@ class Supplement {
String? notes,
DateTime? createdAt,
bool? isActive,
String? syncId,
DateTime? lastModified,
SyncStatus? syncStatus,
bool? isDeleted,
}) {
return Supplement(
id: id ?? this.id,
@@ -126,6 +160,34 @@ class Supplement {
notes: notes ?? this.notes,
createdAt: createdAt ?? this.createdAt,
isActive: isActive ?? this.isActive,
syncId: syncId ?? this.syncId,
lastModified: lastModified ?? this.lastModified,
syncStatus: syncStatus ?? this.syncStatus,
isDeleted: isDeleted ?? this.isDeleted,
);
}
/// Create a new supplement with updated sync status and timestamp
Supplement markAsModified() {
return copyWith(
lastModified: DateTime.now(),
syncStatus: SyncStatus.modified,
);
}
/// Create a new supplement marked as synced
Supplement markAsSynced() {
return copyWith(
syncStatus: SyncStatus.synced,
);
}
/// Create a new supplement marked for deletion
Supplement markAsDeleted() {
return copyWith(
isDeleted: true,
lastModified: DateTime.now(),
syncStatus: SyncStatus.modified,
);
}
}