4 Commits

5 changed files with 323 additions and 271 deletions

View File

@@ -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
controller.nameController.text.trim().isNotEmpty && .where((controller) =>
(double.tryParse(controller.amountController.text) ?? 0) > 0 controller.nameController.text.trim().isNotEmpty &&
).map((controller) => Ingredient( (double.tryParse(controller.amountController.text) ?? 0) > 0)
name: controller.nameController.text.trim(), .map((controller) => Ingredient(
amount: double.tryParse(controller.amountController.text) ?? 0.0, name: controller.nameController.text.trim(),
unit: controller.selectedUnit, amount: double.tryParse(controller.amountController.text) ?? 0.0,
syncId: const Uuid().v4(), unit: controller.selectedUnit,
lastModified: DateTime.now(), syncId: const Uuid().v4(),
)).toList(); lastModified: DateTime.now(),
))
.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>();

View File

@@ -272,125 +272,140 @@ class _SimpleSyncSettingsScreenState extends State<SimpleSyncSettingsScreen> {
} }
Widget _buildConfigurationSection(SimpleSyncProvider syncProvider) { Widget _buildConfigurationSection(SimpleSyncProvider syncProvider) {
return Card( return Column(
child: Padding( crossAxisAlignment: CrossAxisAlignment.stretch,
padding: const EdgeInsets.all(16.0), children: [
child: Column( Card(
crossAxisAlignment: CrossAxisAlignment.start, child: Padding(
children: [ padding: const EdgeInsets.all(12.0),
Text( child: Column(
'Sync Configuration', crossAxisAlignment: CrossAxisAlignment.start,
style: Theme.of(context).textTheme.titleLarge, children: [
), Text(
const SizedBox(height: 16), 'Sync Configuration',
_buildAutoSyncSection(), style: Theme.of(context).textTheme.titleLarge,
const SizedBox(height: 24),
Text(
'WebDAV Settings',
style: Theme.of(context).textTheme.titleMedium,
),
const SizedBox(height: 16),
TextFormField(
controller: _serverUrlController,
decoration: const InputDecoration(
labelText: 'Server URL',
hintText: 'your-nextcloud.com',
helperText: 'Enter just the hostname. We\'ll auto-detect the full WebDAV path.',
border: OutlineInputBorder(),
),
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter a server URL';
}
return null;
},
),
const SizedBox(height: 16),
TextFormField(
controller: _usernameController,
decoration: const InputDecoration(
labelText: 'Username',
border: OutlineInputBorder(),
),
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter a username';
}
return null;
},
),
if (_previewUrl.isNotEmpty) ...[
const SizedBox(height: 8),
Container(
padding: const EdgeInsets.all(12),
decoration: BoxDecoration(
color: Theme.of(context).colorScheme.surfaceContainerHighest.withValues(alpha: 0.3),
borderRadius: BorderRadius.circular(8),
border: Border.all(
color: Theme.of(context).colorScheme.outline.withValues(alpha: 0.5),
),
), ),
child: Column( const SizedBox(height: 8),
crossAxisAlignment: CrossAxisAlignment.start, _buildAutoSyncSection(),
children: [ ],
Text( ),
'WebDAV URL Preview:', ),
style: Theme.of(context).textTheme.labelMedium, ),
), const SizedBox(height: 12),
const SizedBox(height: 4), Card(
SelectableText( child: Padding(
_previewUrl, padding: const EdgeInsets.all(12.0),
style: Theme.of(context).textTheme.bodySmall?.copyWith( child: Column(
fontFamily: 'monospace', crossAxisAlignment: CrossAxisAlignment.start,
color: Theme.of(context).colorScheme.primary, children: [
Text(
'WebDAV Settings',
style: Theme.of(context).textTheme.titleMedium,
),
const SizedBox(height: 8),
TextFormField(
controller: _serverUrlController,
decoration: const InputDecoration(
labelText: 'Server URL',
hintText: 'your-nextcloud.com',
helperText: 'Enter just the hostname. We\'ll auto-detect the full WebDAV path.',
border: OutlineInputBorder(),
),
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter a server URL';
}
return null;
},
),
const SizedBox(height: 8),
TextFormField(
controller: _usernameController,
decoration: const InputDecoration(
labelText: 'Username',
border: OutlineInputBorder(),
),
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter a username';
}
return null;
},
),
if (_previewUrl.isNotEmpty) ...[
const SizedBox(height: 6),
Container(
padding: const EdgeInsets.all(8),
decoration: BoxDecoration(
color: Theme.of(context).colorScheme.surfaceContainerHighest.withValues(alpha: 0.3),
borderRadius: BorderRadius.circular(8),
border: Border.all(
color: Theme.of(context).colorScheme.outline.withValues(alpha: 0.5),
), ),
), ),
const SizedBox(height: 8), child: Column(
Row( crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.end,
children: [ children: [
ElevatedButton.icon( Text(
onPressed: syncProvider.isSyncing ? null : _testConnection, 'WebDAV URL Preview:',
icon: const Icon(Icons.link), style: Theme.of(context).textTheme.labelMedium,
label: const Text('Test'), ),
style: ElevatedButton.styleFrom( const SizedBox(height: 4),
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(8)), SelectableText(
elevation: 0, _previewUrl,
style: Theme.of(context).textTheme.bodySmall?.copyWith(
fontFamily: 'monospace',
color: Theme.of(context).colorScheme.primary,
), ),
), ),
const SizedBox(height: 6),
Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
ElevatedButton.icon(
onPressed: syncProvider.isSyncing ? null : _testConnection,
icon: const Icon(Icons.link),
label: const Text('Test'),
style: ElevatedButton.styleFrom(
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(8)),
elevation: 0,
),
),
],
),
], ],
), ),
], ),
],
const SizedBox(height: 8),
TextFormField(
controller: _passwordController,
decoration: const InputDecoration(
labelText: 'Password',
border: OutlineInputBorder(),
),
obscureText: true,
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter a password';
}
return null;
},
), ),
), const SizedBox(height: 8),
], TextFormField(
const SizedBox(height: 16), controller: _remotePathController,
TextFormField( decoration: const InputDecoration(
controller: _passwordController, labelText: 'Remote Path (optional)',
decoration: const InputDecoration( hintText: 'Supplements/',
labelText: 'Password', border: OutlineInputBorder(),
border: OutlineInputBorder(), ),
), ),
obscureText: true, ],
validator: (value) {
if (value == null || value.isEmpty) {
return 'Please enter a password';
}
return null;
},
), ),
const SizedBox(height: 16), ),
TextFormField(
controller: _remotePathController,
decoration: const InputDecoration(
labelText: 'Remote Path (optional)',
hintText: 'Supplements/',
border: OutlineInputBorder(),
),
),
],
), ),
), ],
); );
} }

View File

@@ -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');
} }
} }
} }

View File

@@ -173,21 +173,36 @@ class NotificationService {
// Call the callback to record the intake // Call the callback to record the intake
if (_onTakeSupplementCallback != null) { if (_onTakeSupplementCallback != null) {
print('SupplementsLog: 📱 Calling supplement callback...'); print('SupplementsLog: 📱 Calling supplement callback...');
_onTakeSupplementCallback!(supplementId, supplementName, units, unitType); _onTakeSupplementCallback!(
supplementId, supplementName, units, unitType);
print('SupplementsLog: 📱 Callback completed'); print('SupplementsLog: 📱 Callback completed');
} else { } else {
print('SupplementsLog: 📱 ERROR: No callback registered!'); print('SupplementsLog: 📱 ERROR: No callback registered!');
} }
// Mark notification as taken in database (this will cancel any pending retries) // For retry notifications, the original notification ID is in the payload
if (notificationId != null) { int originalNotificationId;
print('SupplementsLog: 📱 Marking notification $notificationId as taken'); if (parts.length > 4 && int.tryParse(parts[4]) != null) {
await DatabaseHelper.instance.markNotificationTaken(notificationId); originalNotificationId = int.parse(parts[4]);
print(
// Cancel any pending retry notifications for this notification 'SupplementsLog: 📱 Retry notification detected. Original ID: $originalNotificationId');
_cancelRetryNotifications(notificationId); } else if (notificationId != null) {
originalNotificationId = notificationId;
} else {
print(
'SupplementsLog: 📱 ERROR: Could not determine notification ID to cancel.');
return;
} }
// Mark notification as taken in database (this will cancel any pending retries)
print(
'SupplementsLog: 📱 Marking notification $originalNotificationId as taken');
await DatabaseHelper.instance
.markNotificationTaken(originalNotificationId);
// Cancel any pending retry notifications for this notification
_cancelRetryNotifications(originalNotificationId);
// Show a confirmation notification // Show a confirmation notification
print('SupplementsLog: 📱 Showing confirmation notification...'); print('SupplementsLog: 📱 Showing confirmation notification...');
showInstantNotification( showInstantNotification(
@@ -195,7 +210,8 @@ class NotificationService {
'$supplementName has been recorded at ${DateTime.now().hour.toString().padLeft(2, '0')}:${DateTime.now().minute.toString().padLeft(2, '0')}', '$supplementName has been recorded at ${DateTime.now().hour.toString().padLeft(2, '0')}:${DateTime.now().minute.toString().padLeft(2, '0')}',
); );
} else { } else {
print('SupplementsLog: 📱 ERROR: Invalid payload format - not enough parts'); print(
'SupplementsLog: 📱 ERROR: Invalid payload format - not enough parts');
} }
} catch (e) { } catch (e) {
print('SupplementsLog: 📱 ERROR in _handleTakeAction: $e'); print('SupplementsLog: 📱 ERROR in _handleTakeAction: $e');

View File

@@ -1,7 +1,7 @@
name: supplements name: supplements
description: "A supplement tracking app for managing your daily supplements" description: "A supplement tracking app for managing your daily supplements"
publish_to: "none" publish_to: "none"
version: 1.0.4+27082025 version: 1.0.5+27082025
environment: environment:
sdk: ^3.9.0 sdk: ^3.9.0