mirror of
https://github.com/vleeuwenmenno/supplements.git
synced 2025-09-11 18:29:12 +02:00
adds retry functionality
This commit is contained in:
@@ -4,14 +4,14 @@ import 'dart:convert';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import 'package:supplements/logging.dart';
|
||||
import 'package:supplements/services/simple_notification_service.dart';
|
||||
|
||||
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.
|
||||
@@ -32,6 +32,9 @@ class NotificationRouter {
|
||||
printLog('🔔 handleNotificationResponse: Received actionId: $actionId');
|
||||
printLog('🔔 handleNotificationResponse: Decoded payloadMap: $payloadMap');
|
||||
|
||||
// Cancel retry notifications for any interaction (take, snooze, or tap)
|
||||
await _cancelRetryNotificationsForResponse(payloadMap);
|
||||
|
||||
// Handle Snooze actions without surfacing UI
|
||||
if (actionId == 'snooze_single' || actionId == 'snooze_group') {
|
||||
try {
|
||||
@@ -162,13 +165,14 @@ class NotificationRouter {
|
||||
}
|
||||
|
||||
// Try to wait for providers to be ready to build rich content.
|
||||
BuildContext? ctx = _navigatorKey?.currentContext;
|
||||
final ready = await _waitUntilReady(timeout: const Duration(seconds: 5));
|
||||
if (ctx != null && !ctx.mounted) ctx = null;
|
||||
|
||||
SupplementProvider? provider;
|
||||
if (ready && ctx != null) {
|
||||
provider = Provider.of<SupplementProvider>(ctx, listen: false);
|
||||
if (ready) {
|
||||
final ctx = _navigatorKey?.currentContext;
|
||||
if (ctx != null && ctx.mounted) {
|
||||
provider = Provider.of<SupplementProvider>(ctx, listen: false);
|
||||
}
|
||||
}
|
||||
|
||||
String title = 'Supplement reminder';
|
||||
@@ -295,4 +299,44 @@ class NotificationRouter {
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
/// Cancel retry notifications when user responds to any notification.
|
||||
/// This prevents redundant notifications after user interaction.
|
||||
Future<void> _cancelRetryNotificationsForResponse(Map<String, dynamic>? payloadMap) async {
|
||||
if (payloadMap == null) return;
|
||||
|
||||
try {
|
||||
final type = payloadMap['type'];
|
||||
|
||||
if (type == 'single') {
|
||||
// For single notifications, we need to find the time slot to cancel retries
|
||||
// We can extract this from the meta if it's a retry, or find it from the supplement
|
||||
final meta = payloadMap['meta'] as Map<String, dynamic>?;
|
||||
if (meta != null && meta['originalTime'] != null) {
|
||||
// This is a retry notification, cancel remaining retries for the original time
|
||||
final originalTime = meta['originalTime'] as String;
|
||||
await SimpleNotificationService.instance.cancelRetryNotificationsForTimeSlot(originalTime);
|
||||
printLog('🚫 Cancelled retries for original time slot: $originalTime');
|
||||
} else {
|
||||
// This is an original notification, find the time slot from the supplement
|
||||
final supplementId = payloadMap['id'];
|
||||
if (supplementId is int) {
|
||||
// We need to find which time slots this supplement has and cancel retries for all of them
|
||||
// For now, we'll use the broader cancellation method
|
||||
await SimpleNotificationService.instance.cancelRetryNotificationsForSupplement(supplementId);
|
||||
printLog('🚫 Cancelled retries for supplement ID: $supplementId');
|
||||
}
|
||||
}
|
||||
} else if (type == 'group') {
|
||||
// For group notifications, we have the time key directly
|
||||
final timeKey = payloadMap['time'] as String?;
|
||||
if (timeKey != null) {
|
||||
await SimpleNotificationService.instance.cancelRetryNotificationsForTimeSlot(timeKey);
|
||||
printLog('🚫 Cancelled retries for time slot: $timeKey');
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
printLog('⚠️ Failed to cancel retry notifications: $e');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user