mirror of
https://github.com/vleeuwenmenno/supplements.git
synced 2025-09-11 18:29:12 +02:00
feat adds proper syncing feature
Signed-off-by: Menno van Leeuwen <menno@vleeuwen.me>
This commit is contained in:
@@ -46,31 +46,31 @@ class SupplementProvider with ChangeNotifier, WidgetsBindingObserver {
|
||||
await _notificationService.initialize();
|
||||
|
||||
// Set up the callback for handling supplement intake from notifications
|
||||
print('📱 Setting up notification callback...');
|
||||
print('SupplementsLog: 📱 Setting up notification callback...');
|
||||
_notificationService.setTakeSupplementCallback((supplementId, supplementName, units, unitType) {
|
||||
print('📱 === NOTIFICATION CALLBACK TRIGGERED ===');
|
||||
print('📱 Supplement ID: $supplementId');
|
||||
print('📱 Supplement Name: $supplementName');
|
||||
print('📱 Units: $units');
|
||||
print('📱 Unit Type: $unitType');
|
||||
print('SupplementsLog: 📱 === NOTIFICATION CALLBACK TRIGGERED ===');
|
||||
print('SupplementsLog: 📱 Supplement ID: $supplementId');
|
||||
print('SupplementsLog: 📱 Supplement Name: $supplementName');
|
||||
print('SupplementsLog: 📱 Units: $units');
|
||||
print('SupplementsLog: 📱 Unit Type: $unitType');
|
||||
|
||||
// Record the intake when user taps "Take" on notification
|
||||
recordIntake(supplementId, 0.0, unitsTaken: units);
|
||||
print('📱 Intake recorded successfully');
|
||||
print('📱 === CALLBACK COMPLETE ===');
|
||||
print('SupplementsLog: 📱 Intake recorded successfully');
|
||||
print('SupplementsLog: 📱 === CALLBACK COMPLETE ===');
|
||||
|
||||
if (kDebugMode) {
|
||||
print('📱 Recorded intake from notification: $supplementName ($units $unitType)');
|
||||
print('SupplementsLog: 📱 Recorded intake from notification: $supplementName ($units $unitType)');
|
||||
}
|
||||
});
|
||||
print('📱 Notification callback setup complete');
|
||||
print('SupplementsLog: 📱 Notification callback setup complete');
|
||||
|
||||
// Request permissions with error handling
|
||||
try {
|
||||
await _notificationService.requestPermissions();
|
||||
} catch (e) {
|
||||
if (kDebugMode) {
|
||||
print('Error requesting notification permissions: $e');
|
||||
print('SupplementsLog: Error requesting notification permissions: $e');
|
||||
}
|
||||
// Continue without notifications rather than crashing
|
||||
}
|
||||
@@ -99,7 +99,7 @@ class SupplementProvider with ChangeNotifier, WidgetsBindingObserver {
|
||||
await _checkPersistentReminders();
|
||||
} catch (e) {
|
||||
if (kDebugMode) {
|
||||
print('Error checking persistent reminders: $e');
|
||||
print('SupplementsLog: Error checking persistent reminders: $e');
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -120,8 +120,8 @@ class SupplementProvider with ChangeNotifier, WidgetsBindingObserver {
|
||||
|
||||
if (currentDate != lastCheckDate) {
|
||||
if (kDebugMode) {
|
||||
print('Date changed detected: ${lastCheckDate} -> ${currentDate}');
|
||||
print('Refreshing today\'s intakes for new day...');
|
||||
print('SupplementsLog: Date changed detected: ${lastCheckDate} -> ${currentDate}');
|
||||
print('SupplementsLog: Refreshing today\'s intakes for new day...');
|
||||
}
|
||||
|
||||
// Date has changed, refresh today's intakes
|
||||
@@ -129,7 +129,7 @@ class SupplementProvider with ChangeNotifier, WidgetsBindingObserver {
|
||||
await loadTodayIntakes();
|
||||
|
||||
if (kDebugMode) {
|
||||
print('Today\'s intakes refreshed for new day');
|
||||
print('SupplementsLog: Today\'s intakes refreshed for new day');
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -140,7 +140,7 @@ class SupplementProvider with ChangeNotifier, WidgetsBindingObserver {
|
||||
// For now, we'll check with default settings
|
||||
// In practice, the UI should call checkPersistentRemindersWithSettings
|
||||
if (kDebugMode) {
|
||||
print('📱 Checking persistent reminders with default settings');
|
||||
print('SupplementsLog: 📱 Checking persistent reminders with default settings');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -150,7 +150,7 @@ class SupplementProvider with ChangeNotifier, WidgetsBindingObserver {
|
||||
required int reminderRetryInterval,
|
||||
required int maxRetryAttempts,
|
||||
}) async {
|
||||
print('📱 🔄 MANUAL CHECK: Persistent reminders called from UI');
|
||||
print('SupplementsLog: 📱 🔄 MANUAL CHECK: Persistent reminders called from UI');
|
||||
await _notificationService.checkPersistentReminders(
|
||||
persistentReminders,
|
||||
reminderRetryInterval,
|
||||
@@ -160,7 +160,7 @@ class SupplementProvider with ChangeNotifier, WidgetsBindingObserver {
|
||||
|
||||
// Add a manual trigger method for testing
|
||||
Future<void> triggerRetryCheck() async {
|
||||
print('📱 🚨 MANUAL TRIGGER: Forcing retry check...');
|
||||
print('SupplementsLog: 📱 🚨 MANUAL TRIGGER: Forcing retry check...');
|
||||
await checkPersistentRemindersWithSettings(
|
||||
persistentReminders: true,
|
||||
reminderRetryInterval: 5, // Force 5 minute interval for testing
|
||||
@@ -183,7 +183,7 @@ class SupplementProvider with ChangeNotifier, WidgetsBindingObserver {
|
||||
if (state == AppLifecycleState.resumed) {
|
||||
// App came back to foreground, check if date changed
|
||||
if (kDebugMode) {
|
||||
print('App resumed, checking for date change...');
|
||||
print('SupplementsLog: App resumed, checking for date change...');
|
||||
}
|
||||
forceCheckDateChange();
|
||||
}
|
||||
@@ -191,7 +191,7 @@ class SupplementProvider with ChangeNotifier, WidgetsBindingObserver {
|
||||
|
||||
Future<void> _rescheduleAllNotifications() async {
|
||||
if (kDebugMode) {
|
||||
print('📱 Rescheduling notifications for all active supplements...');
|
||||
print('SupplementsLog: 📱 Rescheduling notifications for all active supplements...');
|
||||
}
|
||||
|
||||
for (final supplement in _supplements) {
|
||||
@@ -200,14 +200,14 @@ class SupplementProvider with ChangeNotifier, WidgetsBindingObserver {
|
||||
await _notificationService.scheduleSupplementReminders(supplement);
|
||||
} catch (e) {
|
||||
if (kDebugMode) {
|
||||
print('📱 Error rescheduling notifications for ${supplement.name}: $e');
|
||||
print('SupplementsLog: 📱 Error rescheduling notifications for ${supplement.name}: $e');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (kDebugMode) {
|
||||
print('📱 Finished rescheduling notifications');
|
||||
print('SupplementsLog: 📱 Finished rescheduling notifications');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -216,16 +216,16 @@ class SupplementProvider with ChangeNotifier, WidgetsBindingObserver {
|
||||
notifyListeners();
|
||||
|
||||
try {
|
||||
print('Loading supplements from database...');
|
||||
print('SupplementsLog: Loading supplements from database...');
|
||||
_supplements = await _databaseHelper.getAllSupplements();
|
||||
print('Loaded ${_supplements.length} supplements');
|
||||
print('SupplementsLog: Loaded ${_supplements.length} supplements');
|
||||
for (var supplement in _supplements) {
|
||||
print('Supplement: ${supplement.name}');
|
||||
print('SupplementsLog: Supplement: ${supplement.name}');
|
||||
}
|
||||
} catch (e) {
|
||||
print('Error loading supplements: $e');
|
||||
print('SupplementsLog: Error loading supplements: $e');
|
||||
if (kDebugMode) {
|
||||
print('Error loading supplements: $e');
|
||||
print('SupplementsLog: Error loading supplements: $e');
|
||||
}
|
||||
} finally {
|
||||
_isLoading = false;
|
||||
@@ -235,28 +235,28 @@ class SupplementProvider with ChangeNotifier, WidgetsBindingObserver {
|
||||
|
||||
Future<void> addSupplement(Supplement supplement) async {
|
||||
try {
|
||||
print('Adding supplement: ${supplement.name}');
|
||||
print('SupplementsLog: Adding supplement: ${supplement.name}');
|
||||
final id = await _databaseHelper.insertSupplement(supplement);
|
||||
print('Supplement inserted with ID: $id');
|
||||
print('SupplementsLog: Supplement inserted with ID: $id');
|
||||
final newSupplement = supplement.copyWith(id: id);
|
||||
|
||||
// Schedule notifications (skip if there's an error)
|
||||
try {
|
||||
await _notificationService.scheduleSupplementReminders(newSupplement);
|
||||
print('Notifications scheduled');
|
||||
print('SupplementsLog: Notifications scheduled');
|
||||
} catch (notificationError) {
|
||||
print('Warning: Could not schedule notifications: $notificationError');
|
||||
print('SupplementsLog: Warning: Could not schedule notifications: $notificationError');
|
||||
}
|
||||
|
||||
await loadSupplements();
|
||||
print('Supplements reloaded, count: ${_supplements.length}');
|
||||
print('SupplementsLog: Supplements reloaded, count: ${_supplements.length}');
|
||||
|
||||
// Trigger sync after adding supplement
|
||||
_triggerSyncIfEnabled();
|
||||
} catch (e) {
|
||||
print('Error adding supplement: $e');
|
||||
print('SupplementsLog: Error adding supplement: $e');
|
||||
if (kDebugMode) {
|
||||
print('Error adding supplement: $e');
|
||||
print('SupplementsLog: Error adding supplement: $e');
|
||||
}
|
||||
rethrow;
|
||||
}
|
||||
@@ -275,7 +275,7 @@ class SupplementProvider with ChangeNotifier, WidgetsBindingObserver {
|
||||
_triggerSyncIfEnabled();
|
||||
} catch (e) {
|
||||
if (kDebugMode) {
|
||||
print('Error updating supplement: $e');
|
||||
print('SupplementsLog: Error updating supplement: $e');
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -293,7 +293,7 @@ class SupplementProvider with ChangeNotifier, WidgetsBindingObserver {
|
||||
_triggerSyncIfEnabled();
|
||||
} catch (e) {
|
||||
if (kDebugMode) {
|
||||
print('Error deleting supplement: $e');
|
||||
print('SupplementsLog: Error deleting supplement: $e');
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -323,7 +323,7 @@ class SupplementProvider with ChangeNotifier, WidgetsBindingObserver {
|
||||
);
|
||||
} catch (e) {
|
||||
if (kDebugMode) {
|
||||
print('Error recording intake: $e');
|
||||
print('SupplementsLog: Error recording intake: $e');
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -332,22 +332,22 @@ class SupplementProvider with ChangeNotifier, WidgetsBindingObserver {
|
||||
try {
|
||||
final today = DateTime.now();
|
||||
if (kDebugMode) {
|
||||
print('Loading intakes for date: ${today.year}-${today.month}-${today.day}');
|
||||
print('SupplementsLog: Loading intakes for date: ${today.year}-${today.month}-${today.day}');
|
||||
}
|
||||
|
||||
_todayIntakes = await _databaseHelper.getIntakesWithSupplementsForDate(today);
|
||||
|
||||
if (kDebugMode) {
|
||||
print('Loaded ${_todayIntakes.length} intakes for today');
|
||||
print('SupplementsLog: Loaded ${_todayIntakes.length} intakes for today');
|
||||
for (var intake in _todayIntakes) {
|
||||
print(' - Supplement ID: ${intake['supplement_id']}, taken at: ${intake['takenAt']}');
|
||||
print('SupplementsLog: - Supplement ID: ${intake['supplement_id']}, taken at: ${intake['takenAt']}');
|
||||
}
|
||||
}
|
||||
|
||||
notifyListeners();
|
||||
} catch (e) {
|
||||
if (kDebugMode) {
|
||||
print('Error loading today\'s intakes: $e');
|
||||
print('SupplementsLog: Error loading today\'s intakes: $e');
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -358,7 +358,7 @@ class SupplementProvider with ChangeNotifier, WidgetsBindingObserver {
|
||||
notifyListeners();
|
||||
} catch (e) {
|
||||
if (kDebugMode) {
|
||||
print('Error loading monthly intakes: $e');
|
||||
print('SupplementsLog: Error loading monthly intakes: $e');
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -368,7 +368,7 @@ class SupplementProvider with ChangeNotifier, WidgetsBindingObserver {
|
||||
return await _databaseHelper.getIntakesWithSupplementsForDate(date);
|
||||
} catch (e) {
|
||||
if (kDebugMode) {
|
||||
print('Error loading intakes for date: $e');
|
||||
print('SupplementsLog: Error loading intakes for date: $e');
|
||||
}
|
||||
return [];
|
||||
}
|
||||
@@ -388,7 +388,26 @@ class SupplementProvider with ChangeNotifier, WidgetsBindingObserver {
|
||||
_triggerSyncIfEnabled();
|
||||
} catch (e) {
|
||||
if (kDebugMode) {
|
||||
print('Error deleting intake: $e');
|
||||
print('SupplementsLog: Error deleting intake: $e');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> permanentlyDeleteIntake(int intakeId) async {
|
||||
try {
|
||||
await _databaseHelper.permanentlyDeleteIntake(intakeId);
|
||||
await loadTodayIntakes();
|
||||
// Also refresh monthly intakes if they're loaded
|
||||
if (_monthlyIntakes.isNotEmpty) {
|
||||
await loadMonthlyIntakes(DateTime.now().year, DateTime.now().month);
|
||||
}
|
||||
notifyListeners();
|
||||
|
||||
// Trigger sync after permanently deleting intake
|
||||
_triggerSyncIfEnabled();
|
||||
} catch (e) {
|
||||
if (kDebugMode) {
|
||||
print('SupplementsLog: Error permanently deleting intake: $e');
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -404,7 +423,7 @@ class SupplementProvider with ChangeNotifier, WidgetsBindingObserver {
|
||||
// Method to manually refresh daily status (useful for testing or manual refresh)
|
||||
Future<void> refreshDailyStatus() async {
|
||||
if (kDebugMode) {
|
||||
print('Manually refreshing daily status...');
|
||||
print('SupplementsLog: Manually refreshing daily status...');
|
||||
}
|
||||
_lastDateCheck = DateTime.now();
|
||||
await loadTodayIntakes();
|
||||
@@ -417,20 +436,20 @@ class SupplementProvider with ChangeNotifier, WidgetsBindingObserver {
|
||||
final lastCheckDate = DateTime(_lastDateCheck.year, _lastDateCheck.month, _lastDateCheck.day);
|
||||
|
||||
if (kDebugMode) {
|
||||
print('Force checking date change...');
|
||||
print('Current date: $currentDate');
|
||||
print('Last check date: $lastCheckDate');
|
||||
print('SupplementsLog: Force checking date change...');
|
||||
print('SupplementsLog: Current date: $currentDate');
|
||||
print('SupplementsLog: Last check date: $lastCheckDate');
|
||||
}
|
||||
|
||||
if (currentDate != lastCheckDate) {
|
||||
if (kDebugMode) {
|
||||
print('Date change detected, refreshing intakes...');
|
||||
print('SupplementsLog: Date change detected, refreshing intakes...');
|
||||
}
|
||||
_lastDateCheck = now;
|
||||
await loadTodayIntakes();
|
||||
} else {
|
||||
if (kDebugMode) {
|
||||
print('No date change detected');
|
||||
print('SupplementsLog: No date change detected');
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -445,7 +464,7 @@ class SupplementProvider with ChangeNotifier, WidgetsBindingObserver {
|
||||
notifyListeners();
|
||||
} catch (e) {
|
||||
if (kDebugMode) {
|
||||
print('Error loading archived supplements: $e');
|
||||
print('SupplementsLog: Error loading archived supplements: $e');
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -460,7 +479,7 @@ class SupplementProvider with ChangeNotifier, WidgetsBindingObserver {
|
||||
_triggerSyncIfEnabled();
|
||||
} catch (e) {
|
||||
if (kDebugMode) {
|
||||
print('Error archiving supplement: $e');
|
||||
print('SupplementsLog: Error archiving supplement: $e');
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -475,21 +494,21 @@ class SupplementProvider with ChangeNotifier, WidgetsBindingObserver {
|
||||
_triggerSyncIfEnabled();
|
||||
} catch (e) {
|
||||
if (kDebugMode) {
|
||||
print('Error unarchiving supplement: $e');
|
||||
print('SupplementsLog: Error unarchiving supplement: $e');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> deleteArchivedSupplement(int supplementId) async {
|
||||
try {
|
||||
await _databaseHelper.deleteSupplement(supplementId);
|
||||
await _databaseHelper.permanentlyDeleteSupplement(supplementId);
|
||||
await loadArchivedSupplements(); // Refresh archived supplements
|
||||
|
||||
// Trigger sync after deleting archived supplement
|
||||
// Trigger sync after permanently deleting archived supplement
|
||||
_triggerSyncIfEnabled();
|
||||
} catch (e) {
|
||||
if (kDebugMode) {
|
||||
print('Error deleting archived supplement: $e');
|
||||
print('SupplementsLog: Error permanently deleting archived supplement: $e');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user