adds retry functionality

This commit is contained in:
2025-09-05 15:13:55 +02:00
parent 99711d56ec
commit 7828e48d9d
7 changed files with 414 additions and 47 deletions

View File

@@ -23,6 +23,11 @@ class SettingsProvider extends ChangeNotifier {
// Notifications
int _snoozeMinutes = 10;
// Notification retry settings
bool _notificationRetryEnabled = true;
int _notificationRetryCount = 3;
int _notificationRetryDelayMinutes = 5;
// Auto-sync settings
bool _autoSyncEnabled = false;
int _autoSyncDebounceSeconds = 5;
@@ -42,6 +47,11 @@ class SettingsProvider extends ChangeNotifier {
// Notifications
int get snoozeMinutes => _snoozeMinutes;
// Notification retry getters
bool get notificationRetryEnabled => _notificationRetryEnabled;
int get notificationRetryCount => _notificationRetryCount;
int get notificationRetryDelayMinutes => _notificationRetryDelayMinutes;
// Auto-sync getters
bool get autoSyncEnabled => _autoSyncEnabled;
int get autoSyncDebounceSeconds => _autoSyncDebounceSeconds;
@@ -85,6 +95,11 @@ class SettingsProvider extends ChangeNotifier {
// Load snooze setting
_snoozeMinutes = prefs.getInt('snooze_minutes') ?? 10;
// Load notification retry settings
_notificationRetryEnabled = prefs.getBool('notification_retry_enabled') ?? true;
_notificationRetryCount = prefs.getInt('notification_retry_count') ?? 3;
_notificationRetryDelayMinutes = prefs.getInt('notification_retry_delay_minutes') ?? 5;
// Load auto-sync settings
_autoSyncEnabled = prefs.getBool('auto_sync_enabled') ?? false;
_autoSyncDebounceSeconds = prefs.getInt('auto_sync_debounce_seconds') ?? 30;
@@ -259,6 +274,37 @@ class SettingsProvider extends ChangeNotifier {
await prefs.setInt('snooze_minutes', minutes);
}
Future<void> setNotificationRetryEnabled(bool enabled) async {
_notificationRetryEnabled = enabled;
notifyListeners();
final prefs = await SharedPreferences.getInstance();
await prefs.setBool('notification_retry_enabled', enabled);
}
Future<void> setNotificationRetryCount(int count) async {
if (count < 0 || count > 10) {
throw ArgumentError('Retry count must be between 0 and 10');
}
_notificationRetryCount = count;
notifyListeners();
final prefs = await SharedPreferences.getInstance();
await prefs.setInt('notification_retry_count', count);
}
Future<void> setNotificationRetryDelayMinutes(int minutes) async {
const allowed = [1, 2, 3, 5, 10, 15, 20, 30];
if (!allowed.contains(minutes)) {
throw ArgumentError('Retry delay must be one of ${allowed.join(", ")} minutes');
}
_notificationRetryDelayMinutes = minutes;
notifyListeners();
final prefs = await SharedPreferences.getInstance();
await prefs.setInt('notification_retry_delay_minutes', minutes);
}
// Auto-sync setters
Future<void> setAutoSyncEnabled(bool enabled) async {
_autoSyncEnabled = enabled;

View File

@@ -3,10 +3,12 @@ import 'dart:async';
import 'package:flutter/foundation.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:provider/provider.dart';
import 'package:supplements/logging.dart';
import '../models/supplement.dart';
import '../models/supplement_intake.dart';
import '../providers/settings_provider.dart';
import '../services/database_helper.dart';
import '../services/database_sync_service.dart';
import '../services/simple_notification_service.dart';
@@ -27,6 +29,9 @@ class SupplementProvider with ChangeNotifier, WidgetsBindingObserver {
// Callback for triggering sync when data changes
VoidCallback? _onDataChanged;
// Context for accessing other providers
BuildContext? _context;
List<Supplement> get supplements => _supplements;
List<Map<String, dynamic>> get todayIntakes => _todayIntakes;
List<Map<String, dynamic>> get monthlyIntakes => _monthlyIntakes;
@@ -42,11 +47,12 @@ class SupplementProvider with ChangeNotifier, WidgetsBindingObserver {
_onDataChanged?.call();
}
Future<void> initialize() async {
Future<void> initialize([BuildContext? context]) async {
if (_initialized) {
return;
}
_initialized = true;
_context = context;
// Add this provider as an observer for app lifecycle changes
WidgetsBinding.instance.addObserver(this);
@@ -135,7 +141,21 @@ class SupplementProvider with ChangeNotifier, WidgetsBindingObserver {
}
try {
await _notificationService.scheduleDailyGroupedRemindersSafe(_supplements);
SettingsProvider? settingsProvider;
if (_context != null && _context!.mounted) {
try {
settingsProvider = Provider.of<SettingsProvider>(_context!, listen: false);
} catch (e) {
if (kDebugMode) {
printLog('📱 Could not access SettingsProvider: $e');
}
}
}
await _notificationService.scheduleDailyGroupedRemindersSafe(
_supplements,
settingsProvider: settingsProvider,
);
await _notificationService.getPendingNotifications();
} catch (e) {
if (kDebugMode) {