mirror of
https://github.com/vleeuwenmenno/supplements.git
synced 2025-09-11 18:29:12 +02:00
feat: Implement snooze functionality for notifications
- Added snooze duration setting in SettingsScreen. - Created DebugNotificationsScreen to view pending notifications and logs. - Integrated notification logging with NotificationDebugStore. - Enhanced SimpleNotificationService to handle snooze actions and log notifications. - Removed ProfileSetupScreen as it is no longer needed. - Updated NotificationRouter to manage snooze actions without UI. - Refactored settings provider to include snooze duration management.
This commit is contained in:
@@ -10,6 +10,8 @@ import '../models/supplement.dart';
|
||||
import '../providers/supplement_provider.dart';
|
||||
import '../widgets/dialogs/bulk_take_dialog.dart';
|
||||
import '../widgets/dialogs/take_supplement_dialog.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import 'package:supplements/services/simple_notification_service.dart';
|
||||
|
||||
/// Centralizes routing from notification actions/taps to in-app UI.
|
||||
/// Handles both foreground/background taps and terminated-launch scenarios.
|
||||
@@ -27,7 +29,22 @@ class NotificationRouter {
|
||||
final payloadMap = _decodePayload(response.payload);
|
||||
final actionId = response.actionId;
|
||||
printLog('🔔 handleNotificationResponse: actionId=$actionId payload=${response.payload} map=$payloadMap');
|
||||
printLog('🔔 handleNotificationResponse: Received actionId: $actionId');
|
||||
printLog('🔔 handleNotificationResponse: Decoded payloadMap: $payloadMap');
|
||||
|
||||
// Handle Snooze actions without surfacing UI
|
||||
if (actionId == 'snooze_single' || actionId == 'snooze_group') {
|
||||
try {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
final minutes = prefs.getInt('snooze_minutes') ?? 10;
|
||||
await _scheduleSnoozeFromPayload(payloadMap, Duration(minutes: minutes));
|
||||
} catch (e) {
|
||||
printLog('⚠️ Failed to handle snooze action: $e');
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// Default: route to in-app UI for Take actions and normal taps
|
||||
await _routeFromPayload(payloadMap);
|
||||
}
|
||||
|
||||
@@ -134,14 +151,124 @@ class NotificationRouter {
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _scheduleSnoozeFromPayload(Map<String, dynamic>? payload, Duration delay) async {
|
||||
if (payload == null) {
|
||||
printLog('⚠️ Snooze requested but payload was null');
|
||||
return;
|
||||
}
|
||||
|
||||
// Try to wait for providers to be ready to build rich content.
|
||||
final ready = await _waitUntilReady(timeout: const Duration(seconds: 5));
|
||||
BuildContext? ctx = _navigatorKey?.currentContext;
|
||||
|
||||
SupplementProvider? provider;
|
||||
if (ready && ctx != null) {
|
||||
provider = Provider.of<SupplementProvider>(ctx, listen: false);
|
||||
}
|
||||
|
||||
String title = 'Supplement reminder';
|
||||
String body = 'Tap to see details';
|
||||
bool isSingle = false;
|
||||
// Start with a mutable copy of the payload to add meta information
|
||||
final Map<String, dynamic> mutablePayload = Map.from(payload);
|
||||
|
||||
final type = mutablePayload['type'];
|
||||
|
||||
if (type == 'single') {
|
||||
final id = payload['id'];
|
||||
isSingle = true;
|
||||
// Ensure the payload for single snooze is correctly formatted
|
||||
mutablePayload['type'] = 'single';
|
||||
mutablePayload['id'] = id;
|
||||
|
||||
if (id is int && provider != null) {
|
||||
Supplement? s;
|
||||
try {
|
||||
s = provider.supplements.firstWhere((el) => el.id == id);
|
||||
} catch (_) {
|
||||
s = null;
|
||||
}
|
||||
if (s != null) {
|
||||
title = 'Time for ${s.name}';
|
||||
body =
|
||||
'${s.name} — ${s.numberOfUnits} ${s.unitType} (${s.ingredientsPerUnit})';
|
||||
} else {
|
||||
body = 'Tap to take supplement';
|
||||
}
|
||||
}
|
||||
} else if (type == 'group') {
|
||||
final timeKey = mutablePayload['time'];
|
||||
if (timeKey is String) {
|
||||
if (provider != null) {
|
||||
final list = provider.supplements
|
||||
.where((s) =>
|
||||
s.isActive && s.reminderTimes.contains(timeKey))
|
||||
.toList();
|
||||
|
||||
if (list.length == 1) {
|
||||
final s = list.first;
|
||||
isSingle = true;
|
||||
title = 'Time for ${s.name}';
|
||||
body =
|
||||
'${s.name} — ${s.numberOfUnits} ${s.unitType} (${s.ingredientsPerUnit})';
|
||||
// If a group becomes a single, update the payload type
|
||||
mutablePayload['type'] = 'single';
|
||||
mutablePayload['id'] = s.id;
|
||||
mutablePayload.remove('time'); // Remove time key for single
|
||||
} else if (list.isNotEmpty) {
|
||||
isSingle = false;
|
||||
title = 'Time for ${list.length} supplements';
|
||||
final lines = list
|
||||
.map((s) =>
|
||||
'${s.name} — ${s.numberOfUnits} ${s.unitType} (${s.ingredientsPerUnit})')
|
||||
.toList();
|
||||
body = lines.join('\n');
|
||||
// Ensure payload type is group
|
||||
mutablePayload['type'] = 'group';
|
||||
mutablePayload['time'] = timeKey;
|
||||
} else {
|
||||
// Fallback generic group
|
||||
isSingle = false;
|
||||
title = 'Supplement reminder';
|
||||
body = 'Tap to see details';
|
||||
mutablePayload['type'] = 'group';
|
||||
mutablePayload['time'] = timeKey;
|
||||
}
|
||||
} else {
|
||||
// Provider not ready; schedule generic group payload
|
||||
isSingle = false;
|
||||
mutablePayload['type'] = 'group';
|
||||
mutablePayload['time'] = timeKey;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Ensure the payload always has the correct type and ID/time for logging
|
||||
// and re-scheduling. The SimpleNotificationService will add the 'meta' field.
|
||||
final payloadStr = jsonEncode(mutablePayload);
|
||||
|
||||
await SimpleNotificationService.instance.scheduleOneOffReminder(
|
||||
title: title,
|
||||
body: body,
|
||||
payload: payloadStr,
|
||||
isSingle: isSingle,
|
||||
delay: delay,
|
||||
);
|
||||
}
|
||||
|
||||
Future<bool> _waitUntilReady({required Duration timeout}) async {
|
||||
final start = DateTime.now();
|
||||
while (DateTime.now().difference(start) < timeout) {
|
||||
final ctx = _navigatorKey!.currentContext;
|
||||
final key = _navigatorKey;
|
||||
final ctx = key?.currentContext;
|
||||
if (ctx != null) {
|
||||
final provider = Provider.of<SupplementProvider>(ctx, listen: false);
|
||||
if (!provider.isLoading) {
|
||||
return true;
|
||||
try {
|
||||
final provider = Provider.of<SupplementProvider>(ctx, listen: false);
|
||||
if (!provider.isLoading) {
|
||||
return true;
|
||||
}
|
||||
} catch (_) {
|
||||
// Provider not available yet
|
||||
}
|
||||
}
|
||||
await Future.delayed(const Duration(milliseconds: 100));
|
||||
|
Reference in New Issue
Block a user