mirror of
https://github.com/vleeuwenmenno/supplements.git
synced 2025-09-11 18:29:12 +02:00
bugfix: changing times for supplements now still allows for proper
syncing
This commit is contained in:
@@ -557,21 +557,24 @@ class _AddSupplementScreenState extends State<AddSupplementScreen> {
|
|||||||
void _saveSupplement() async {
|
void _saveSupplement() async {
|
||||||
if (_formKey.currentState!.validate()) {
|
if (_formKey.currentState!.validate()) {
|
||||||
// Validate that we have at least one ingredient with name and amount
|
// Validate that we have at least one ingredient with name and amount
|
||||||
final validIngredients = _ingredientControllers.where((controller) =>
|
final validIngredients = _ingredientControllers
|
||||||
|
.where((controller) =>
|
||||||
controller.nameController.text.trim().isNotEmpty &&
|
controller.nameController.text.trim().isNotEmpty &&
|
||||||
(double.tryParse(controller.amountController.text) ?? 0) > 0
|
(double.tryParse(controller.amountController.text) ?? 0) > 0)
|
||||||
).map((controller) => Ingredient(
|
.map((controller) => Ingredient(
|
||||||
name: controller.nameController.text.trim(),
|
name: controller.nameController.text.trim(),
|
||||||
amount: double.tryParse(controller.amountController.text) ?? 0.0,
|
amount: double.tryParse(controller.amountController.text) ?? 0.0,
|
||||||
unit: controller.selectedUnit,
|
unit: controller.selectedUnit,
|
||||||
syncId: const Uuid().v4(),
|
syncId: const Uuid().v4(),
|
||||||
lastModified: DateTime.now(),
|
lastModified: DateTime.now(),
|
||||||
)).toList();
|
))
|
||||||
|
.toList();
|
||||||
|
|
||||||
if (validIngredients.isEmpty) {
|
if (validIngredients.isEmpty) {
|
||||||
ScaffoldMessenger.of(context).showSnackBar(
|
ScaffoldMessenger.of(context).showSnackBar(
|
||||||
const SnackBar(
|
const SnackBar(
|
||||||
content: Text('Please add at least one ingredient with name and amount'),
|
content:
|
||||||
|
Text('Please add at least one ingredient with name and amount'),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
return;
|
return;
|
||||||
@@ -580,14 +583,20 @@ class _AddSupplementScreenState extends State<AddSupplementScreen> {
|
|||||||
final supplement = Supplement(
|
final supplement = Supplement(
|
||||||
id: widget.supplement?.id,
|
id: widget.supplement?.id,
|
||||||
name: _nameController.text.trim(),
|
name: _nameController.text.trim(),
|
||||||
brand: _brandController.text.trim().isNotEmpty ? _brandController.text.trim() : null,
|
brand: _brandController.text.trim().isNotEmpty
|
||||||
|
? _brandController.text.trim()
|
||||||
|
: null,
|
||||||
ingredients: validIngredients,
|
ingredients: validIngredients,
|
||||||
numberOfUnits: int.parse(_numberOfUnitsController.text),
|
numberOfUnits: int.parse(_numberOfUnitsController.text),
|
||||||
unitType: _selectedUnitType,
|
unitType: _selectedUnitType,
|
||||||
frequencyPerDay: _frequencyPerDay,
|
frequencyPerDay: _frequencyPerDay,
|
||||||
reminderTimes: _reminderTimes,
|
reminderTimes: _reminderTimes,
|
||||||
notes: _notesController.text.trim().isNotEmpty ? _notesController.text.trim() : null,
|
notes: _notesController.text.trim().isNotEmpty
|
||||||
|
? _notesController.text.trim()
|
||||||
|
: null,
|
||||||
createdAt: widget.supplement?.createdAt ?? DateTime.now(),
|
createdAt: widget.supplement?.createdAt ?? DateTime.now(),
|
||||||
|
syncId: widget.supplement?.syncId, // Preserve syncId on update
|
||||||
|
lastModified: DateTime.now(), // Always update lastModified on save
|
||||||
);
|
);
|
||||||
|
|
||||||
final provider = context.read<SupplementProvider>();
|
final provider = context.read<SupplementProvider>();
|
||||||
|
@@ -289,19 +289,23 @@ class DatabaseSyncService {
|
|||||||
|
|
||||||
// Get all supplements from remote database
|
// Get all supplements from remote database
|
||||||
final remoteMaps = await remoteDb.query('supplements');
|
final remoteMaps = await remoteDb.query('supplements');
|
||||||
final remoteSupplements = remoteMaps.map((map) => Supplement.fromMap(map)).toList();
|
final remoteSupplements =
|
||||||
|
remoteMaps.map((map) => Supplement.fromMap(map)).toList();
|
||||||
|
|
||||||
if (kDebugMode) {
|
if (kDebugMode) {
|
||||||
print('SupplementsLog: Found ${remoteSupplements.length} supplements in remote database');
|
print(
|
||||||
|
'SupplementsLog: Found ${remoteSupplements.length} supplements in remote database');
|
||||||
for (final supplement in remoteSupplements) {
|
for (final supplement in remoteSupplements) {
|
||||||
print('SupplementsLog: Remote supplement: ${supplement.name} (syncId: ${supplement.syncId}, deleted: ${supplement.isDeleted})');
|
print(
|
||||||
|
'SupplementsLog: Remote supplement: ${supplement.name} (syncId: ${supplement.syncId}, deleted: ${supplement.isDeleted})');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (final remoteSupplement in remoteSupplements) {
|
for (final remoteSupplement in remoteSupplements) {
|
||||||
if (remoteSupplement.syncId.isEmpty) {
|
if (remoteSupplement.syncId.isEmpty) {
|
||||||
if (kDebugMode) {
|
if (kDebugMode) {
|
||||||
print('SupplementsLog: Skipping supplement ${remoteSupplement.name} - no syncId');
|
print(
|
||||||
|
'SupplementsLog: Skipping supplement ${remoteSupplement.name} - no syncId');
|
||||||
}
|
}
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@@ -316,22 +320,28 @@ class DatabaseSyncService {
|
|||||||
if (existingMaps.isEmpty) {
|
if (existingMaps.isEmpty) {
|
||||||
// New supplement from remote - insert it
|
// New supplement from remote - insert it
|
||||||
if (!remoteSupplement.isDeleted) {
|
if (!remoteSupplement.isDeleted) {
|
||||||
final supplementToInsert = remoteSupplement.copyWith(id: null);
|
// Manually create a new map without the id to ensure it's null
|
||||||
await localDb.insert('supplements', supplementToInsert.toMap());
|
final mapToInsert = remoteSupplement.toMap();
|
||||||
|
mapToInsert.remove('id');
|
||||||
|
await localDb.insert('supplements', mapToInsert);
|
||||||
if (kDebugMode) {
|
if (kDebugMode) {
|
||||||
print('SupplementsLog: ✓ Inserted new supplement: ${remoteSupplement.name}');
|
print(
|
||||||
|
'SupplementsLog: ✓ Inserted new supplement: ${remoteSupplement.name}');
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (kDebugMode) {
|
if (kDebugMode) {
|
||||||
print('SupplementsLog: Skipping deleted supplement: ${remoteSupplement.name}');
|
print(
|
||||||
|
'SupplementsLog: Skipping deleted supplement: ${remoteSupplement.name}');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Existing supplement - update if remote is newer
|
// Existing supplement - update if remote is newer
|
||||||
final existingSupplement = Supplement.fromMap(existingMaps.first);
|
final existingSupplement = Supplement.fromMap(existingMaps.first);
|
||||||
|
|
||||||
if (remoteSupplement.lastModified.isAfter(existingSupplement.lastModified)) {
|
if (remoteSupplement.lastModified
|
||||||
final supplementToUpdate = remoteSupplement.copyWith(id: existingSupplement.id);
|
.isAfter(existingSupplement.lastModified)) {
|
||||||
|
final supplementToUpdate =
|
||||||
|
remoteSupplement.copyWith(id: existingSupplement.id);
|
||||||
await localDb.update(
|
await localDb.update(
|
||||||
'supplements',
|
'supplements',
|
||||||
supplementToUpdate.toMap(),
|
supplementToUpdate.toMap(),
|
||||||
@@ -339,11 +349,13 @@ class DatabaseSyncService {
|
|||||||
whereArgs: [existingSupplement.id],
|
whereArgs: [existingSupplement.id],
|
||||||
);
|
);
|
||||||
if (kDebugMode) {
|
if (kDebugMode) {
|
||||||
print('SupplementsLog: ✓ Updated supplement: ${remoteSupplement.name}');
|
print(
|
||||||
|
'SupplementsLog: ✓ Updated supplement: ${remoteSupplement.name}');
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (kDebugMode) {
|
if (kDebugMode) {
|
||||||
print('SupplementsLog: Local supplement ${remoteSupplement.name} is newer, keeping local version');
|
print(
|
||||||
|
'SupplementsLog: Local supplement ${remoteSupplement.name} is newer, keeping local version');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user