fix: prioritize realtime reminder archive with cold-start fallback
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
import 'dart:math';
|
||||
|
||||
import 'package:flutter/widgets.dart';
|
||||
|
||||
import '../data/services/calendar_service.dart';
|
||||
import '../../../core/notifications/local_notification_service.dart';
|
||||
import 'models/reminder_action.dart';
|
||||
@@ -11,16 +13,23 @@ class ReminderActionExecutor {
|
||||
final LocalNotificationService _notificationService;
|
||||
final ReminderOutboxStore _outboxStore;
|
||||
final Random _random;
|
||||
final bool Function() _isAppActive;
|
||||
|
||||
ReminderActionExecutor({
|
||||
required CalendarService calendarService,
|
||||
required LocalNotificationService notificationService,
|
||||
required ReminderOutboxStore outboxStore,
|
||||
Random? random,
|
||||
bool Function()? isAppActive,
|
||||
}) : _calendarService = calendarService,
|
||||
_notificationService = notificationService,
|
||||
_outboxStore = outboxStore,
|
||||
_random = random ?? Random();
|
||||
_random = random ?? Random(),
|
||||
_isAppActive =
|
||||
isAppActive ??
|
||||
(() =>
|
||||
WidgetsBinding.instance.lifecycleState ==
|
||||
AppLifecycleState.resumed);
|
||||
|
||||
Future<void> handleAction({
|
||||
required ReminderAction action,
|
||||
@@ -86,6 +95,11 @@ class ReminderActionExecutor {
|
||||
}
|
||||
|
||||
Future<void> _archiveEvent(String eventId, ReminderAction action) async {
|
||||
if (_isAppActive()) {
|
||||
await _calendarService.archiveEvent(eventId);
|
||||
return;
|
||||
}
|
||||
|
||||
final opId =
|
||||
'${DateTime.now().millisecondsSinceEpoch}-${_random.nextInt(1 << 32)}';
|
||||
final outboxItem = ReminderOutboxItem(
|
||||
@@ -96,11 +110,5 @@ class ReminderActionExecutor {
|
||||
occurredAt: DateTime.now(),
|
||||
);
|
||||
await _outboxStore.enqueue(outboxItem);
|
||||
try {
|
||||
await _calendarService.archiveEvent(eventId);
|
||||
await _outboxStore.markDone(opId);
|
||||
} catch (error) {
|
||||
await _outboxStore.markRetry(opId, error.toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user