merge: integrate feature/reminder-overlay into dev
This commit is contained in:
@@ -19,7 +19,6 @@ import UserNotifications
|
|||||||
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
|
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
|
||||||
}
|
}
|
||||||
|
|
||||||
@available(iOS 10.0, *)
|
|
||||||
override func userNotificationCenter(
|
override func userNotificationCenter(
|
||||||
_ center: UNUserNotificationCenter,
|
_ center: UNUserNotificationCenter,
|
||||||
didReceive response: UNNotificationResponse,
|
didReceive response: UNNotificationResponse,
|
||||||
|
|||||||
@@ -20,7 +20,6 @@ import '../../features/calendar/data/calendar_api.dart';
|
|||||||
import '../../features/calendar/data/services/calendar_repository.dart';
|
import '../../features/calendar/data/services/calendar_repository.dart';
|
||||||
import '../../features/calendar/data/services/calendar_service.dart';
|
import '../../features/calendar/data/services/calendar_service.dart';
|
||||||
import '../../features/calendar/reminders/reminder_action_executor.dart';
|
import '../../features/calendar/reminders/reminder_action_executor.dart';
|
||||||
import '../../features/calendar/reminders/reminder_outbox_store.dart';
|
|
||||||
import '../../features/calendar/ui/calendar_state_manager.dart';
|
import '../../features/calendar/ui/calendar_state_manager.dart';
|
||||||
import '../../features/friends/data/friends_api.dart';
|
import '../../features/friends/data/friends_api.dart';
|
||||||
import '../../features/messages/data/inbox_api.dart';
|
import '../../features/messages/data/inbox_api.dart';
|
||||||
@@ -98,15 +97,11 @@ Future<void> configureDependencies() async {
|
|||||||
);
|
);
|
||||||
sl.registerSingleton<CalendarRepository>(calendarRepository);
|
sl.registerSingleton<CalendarRepository>(calendarRepository);
|
||||||
|
|
||||||
final reminderOutboxStore = ReminderOutboxStore(sharedPreferences);
|
|
||||||
sl.registerSingleton<ReminderOutboxStore>(reminderOutboxStore);
|
|
||||||
|
|
||||||
sl.registerSingleton<LocalNotificationService>(LocalNotificationService());
|
sl.registerSingleton<LocalNotificationService>(LocalNotificationService());
|
||||||
|
|
||||||
final reminderActionExecutor = ReminderActionExecutor(
|
final reminderActionExecutor = ReminderActionExecutor(
|
||||||
calendarService: calendarService,
|
calendarService: calendarService,
|
||||||
notificationService: sl<LocalNotificationService>(),
|
notificationService: sl<LocalNotificationService>(),
|
||||||
outboxStore: reminderOutboxStore,
|
|
||||||
);
|
);
|
||||||
sl.registerSingleton<ReminderActionExecutor>(reminderActionExecutor);
|
sl.registerSingleton<ReminderActionExecutor>(reminderActionExecutor);
|
||||||
|
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ import 'dart:convert';
|
|||||||
|
|
||||||
import 'package:flutter/foundation.dart';
|
import 'package:flutter/foundation.dart';
|
||||||
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
|
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
|
||||||
import 'package:shared_preferences/shared_preferences.dart';
|
|
||||||
import 'package:timezone/data/latest.dart' as tz_data;
|
import 'package:timezone/data/latest.dart' as tz_data;
|
||||||
import 'package:timezone/timezone.dart' as tz;
|
import 'package:timezone/timezone.dart' as tz;
|
||||||
|
|
||||||
@@ -10,8 +9,6 @@ import 'reminder_notification_callbacks.dart';
|
|||||||
import '../../features/calendar/data/models/schedule_item_model.dart';
|
import '../../features/calendar/data/models/schedule_item_model.dart';
|
||||||
import '../../features/calendar/reminders/models/reminder_action.dart';
|
import '../../features/calendar/reminders/models/reminder_action.dart';
|
||||||
import '../../features/calendar/reminders/models/reminder_payload.dart';
|
import '../../features/calendar/reminders/models/reminder_payload.dart';
|
||||||
import '../../features/calendar/reminders/reminder_action_dedupe_store.dart';
|
|
||||||
import '../../features/calendar/reminders/reminder_overlap_policy.dart';
|
|
||||||
|
|
||||||
typedef ReminderNotificationActionHandler =
|
typedef ReminderNotificationActionHandler =
|
||||||
Future<void> Function({
|
Future<void> Function({
|
||||||
@@ -25,18 +22,11 @@ class LocalNotificationService {
|
|||||||
static const String _actionSnooze = 'snooze10m';
|
static const String _actionSnooze = 'snooze10m';
|
||||||
|
|
||||||
final FlutterLocalNotificationsPlugin _plugin;
|
final FlutterLocalNotificationsPlugin _plugin;
|
||||||
final ReminderOverlapPolicy _overlapPolicy;
|
|
||||||
ReminderActionDedupeStore? _dedupeStore;
|
|
||||||
bool _initialized = false;
|
bool _initialized = false;
|
||||||
ReminderNotificationActionHandler? _actionHandler;
|
ReminderNotificationActionHandler? _actionHandler;
|
||||||
|
|
||||||
LocalNotificationService({
|
LocalNotificationService({FlutterLocalNotificationsPlugin? plugin})
|
||||||
FlutterLocalNotificationsPlugin? plugin,
|
: _plugin = plugin ?? FlutterLocalNotificationsPlugin();
|
||||||
ReminderOverlapPolicy? overlapPolicy,
|
|
||||||
ReminderActionDedupeStore? dedupeStore,
|
|
||||||
}) : _plugin = plugin ?? FlutterLocalNotificationsPlugin(),
|
|
||||||
_overlapPolicy = overlapPolicy ?? const ReminderOverlapPolicy(),
|
|
||||||
_dedupeStore = dedupeStore;
|
|
||||||
|
|
||||||
void bindActionHandler(ReminderNotificationActionHandler handler) {
|
void bindActionHandler(ReminderNotificationActionHandler handler) {
|
||||||
_actionHandler = handler;
|
_actionHandler = handler;
|
||||||
@@ -93,19 +83,9 @@ class LocalNotificationService {
|
|||||||
>();
|
>();
|
||||||
await iosImpl?.requestPermissions(alert: true, badge: true, sound: true);
|
await iosImpl?.requestPermissions(alert: true, badge: true, sound: true);
|
||||||
|
|
||||||
await _ensureDedupeStore();
|
|
||||||
|
|
||||||
_initialized = true;
|
_initialized = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> _ensureDedupeStore() async {
|
|
||||||
if (_dedupeStore != null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
final prefs = await SharedPreferences.getInstance();
|
|
||||||
_dedupeStore = ReminderActionDedupeStore(prefs);
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<void> upsertEventReminder(ScheduleItemModel event) async {
|
Future<void> upsertEventReminder(ScheduleItemModel event) async {
|
||||||
await initialize();
|
await initialize();
|
||||||
if (event.status != ScheduleStatus.active ||
|
if (event.status != ScheduleStatus.active ||
|
||||||
@@ -115,8 +95,9 @@ class LocalNotificationService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
final now = DateTime.now();
|
final now = DateTime.now();
|
||||||
final fireAt = _overlapPolicy.resolveFirstFireAt(event, now: now);
|
final reminderMinutes = event.metadata?.reminderMinutes ?? 0;
|
||||||
if (fireAt == null) {
|
final fireAt = event.startAt.subtract(Duration(minutes: reminderMinutes));
|
||||||
|
if (fireAt.isBefore(now)) {
|
||||||
await cancelEventReminder(event.id);
|
await cancelEventReminder(event.id);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -156,15 +137,8 @@ class LocalNotificationService {
|
|||||||
Iterable<ScheduleItemModel> events,
|
Iterable<ScheduleItemModel> events,
|
||||||
) async {
|
) async {
|
||||||
await initialize();
|
await initialize();
|
||||||
|
for (final event in events) {
|
||||||
final now = DateTime.now();
|
await upsertEventReminder(event);
|
||||||
final groups = _overlapPolicy.groupByMinute(events, now: now);
|
|
||||||
for (final group in groups) {
|
|
||||||
if (group.isAggregate) {
|
|
||||||
await _scheduleAggregateReminder(group.events, group.fireAt);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
await upsertEventReminder(group.events.first);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -184,10 +158,6 @@ class LocalNotificationService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Future<AndroidScheduleMode> _resolveAndroidScheduleMode() async {
|
Future<AndroidScheduleMode> _resolveAndroidScheduleMode() async {
|
||||||
if (defaultTargetPlatform != TargetPlatform.android) {
|
|
||||||
return AndroidScheduleMode.exactAllowWhileIdle;
|
|
||||||
}
|
|
||||||
|
|
||||||
final androidImpl = _plugin
|
final androidImpl = _plugin
|
||||||
.resolvePlatformSpecificImplementation<
|
.resolvePlatformSpecificImplementation<
|
||||||
AndroidFlutterLocalNotificationsPlugin
|
AndroidFlutterLocalNotificationsPlugin
|
||||||
@@ -326,56 +296,6 @@ class LocalNotificationService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> _scheduleAggregateReminder(
|
|
||||||
List<ScheduleItemModel> events,
|
|
||||||
DateTime fireAt,
|
|
||||||
) async {
|
|
||||||
if (events.isEmpty) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
final first = events.first;
|
|
||||||
final aggregateIds = events.map((event) => event.id).toList();
|
|
||||||
for (final id in aggregateIds) {
|
|
||||||
await cancelEventReminder(id);
|
|
||||||
}
|
|
||||||
|
|
||||||
final payload = ReminderPayload(
|
|
||||||
eventId: first.id,
|
|
||||||
title: '你有${events.length}个日程提醒',
|
|
||||||
startAt: first.startAt,
|
|
||||||
endAt: first.endAt,
|
|
||||||
timezone: first.timezone,
|
|
||||||
mode: ReminderPayloadMode.aggregate,
|
|
||||||
aggregateIds: aggregateIds,
|
|
||||||
fireTimeBucket:
|
|
||||||
fireAt.millisecondsSinceEpoch ~/
|
|
||||||
const Duration(minutes: 1).inMilliseconds,
|
|
||||||
version: 1,
|
|
||||||
);
|
|
||||||
|
|
||||||
final details = _buildNotificationDetails(fireAt);
|
|
||||||
final scheduledAt = tz.TZDateTime.from(fireAt, tz.local);
|
|
||||||
final mode = await _resolveAndroidScheduleMode();
|
|
||||||
final preview = events.take(3).map((item) => item.title).join('、');
|
|
||||||
|
|
||||||
await _plugin.zonedSchedule(
|
|
||||||
_notificationIdForEventCycle(
|
|
||||||
first.id,
|
|
||||||
fireAt,
|
|
||||||
ReminderPayloadMode.aggregate,
|
|
||||||
),
|
|
||||||
'你有${events.length}个日程提醒',
|
|
||||||
preview,
|
|
||||||
scheduledAt,
|
|
||||||
details,
|
|
||||||
payload: jsonEncode(payload.toJson()),
|
|
||||||
androidScheduleMode: mode,
|
|
||||||
uiLocalNotificationDateInterpretation:
|
|
||||||
UILocalNotificationDateInterpretation.absoluteTime,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
ReminderPayload? _decodePayload(String? raw) {
|
ReminderPayload? _decodePayload(String? raw) {
|
||||||
if (raw == null || raw.isEmpty) {
|
if (raw == null || raw.isEmpty) {
|
||||||
return null;
|
return null;
|
||||||
@@ -438,21 +358,6 @@ class LocalNotificationService {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
final dedupeStore = _dedupeStore;
|
|
||||||
if (dedupeStore != null) {
|
|
||||||
final notificationId = response.id?.toString() ?? payload.eventId;
|
|
||||||
final fireTimeBucket =
|
|
||||||
payload.fireTimeBucket ??
|
|
||||||
(payload.startAt.millisecondsSinceEpoch ~/
|
|
||||||
const Duration(minutes: 1).inMilliseconds);
|
|
||||||
final actionExecutionId =
|
|
||||||
'$notificationId|${action.value}|$fireTimeBucket';
|
|
||||||
final isNew = await dedupeStore.markIfNew(actionExecutionId);
|
|
||||||
if (!isNew) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
await handler(action: action, payload: payload);
|
await handler(action: action, payload: payload);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,8 +5,6 @@ import 'package:flutter/foundation.dart';
|
|||||||
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
|
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
|
||||||
import 'package:shared_preferences/shared_preferences.dart';
|
import 'package:shared_preferences/shared_preferences.dart';
|
||||||
|
|
||||||
import '../../features/calendar/reminders/reminder_cold_start_queue.dart';
|
|
||||||
|
|
||||||
typedef ReminderNotificationResponseHandler =
|
typedef ReminderNotificationResponseHandler =
|
||||||
Future<void> Function(NotificationResponse response);
|
Future<void> Function(NotificationResponse response);
|
||||||
|
|
||||||
@@ -15,8 +13,6 @@ class ReminderNotificationCallbacks {
|
|||||||
'calendar_reminder_pending_notification_responses_v1';
|
'calendar_reminder_pending_notification_responses_v1';
|
||||||
static ReminderNotificationResponseHandler? _responseHandler;
|
static ReminderNotificationResponseHandler? _responseHandler;
|
||||||
static Future<void> _pendingStorageLock = Future<void>.value();
|
static Future<void> _pendingStorageLock = Future<void>.value();
|
||||||
static final ReminderColdStartQueue _coldStartQueue =
|
|
||||||
ReminderColdStartQueue();
|
|
||||||
|
|
||||||
@visibleForTesting
|
@visibleForTesting
|
||||||
static Future<void> resetForTest() async {
|
static Future<void> resetForTest() async {
|
||||||
@@ -106,38 +102,35 @@ class ReminderNotificationCallbacks {
|
|||||||
|
|
||||||
final remaining = <String>[];
|
final remaining = <String>[];
|
||||||
for (final raw in pending) {
|
for (final raw in pending) {
|
||||||
_coldStartQueue.enqueue(() async {
|
Map<String, dynamic> parsed;
|
||||||
Map<String, dynamic> parsed;
|
try {
|
||||||
try {
|
parsed = Map<String, dynamic>.from(jsonDecode(raw) as Map);
|
||||||
parsed = Map<String, dynamic>.from(jsonDecode(raw) as Map);
|
} catch (_) {
|
||||||
} catch (_) {
|
continue;
|
||||||
return;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
final id = parsed['id'] as int?;
|
final id = parsed['id'] as int?;
|
||||||
final actionId = parsed['actionId'] as String?;
|
final actionId = parsed['actionId'] as String?;
|
||||||
final payload = parsed['payload'] as String?;
|
final payload = parsed['payload'] as String?;
|
||||||
final typeIndex = (parsed['type'] as int?) ?? 0;
|
final typeIndex = (parsed['type'] as int?) ?? 0;
|
||||||
final input = parsed['input'] as String?;
|
final input = parsed['input'] as String?;
|
||||||
final type = NotificationResponseType.values[typeIndex.clamp(0, 1)];
|
final type = NotificationResponseType.values[typeIndex.clamp(0, 1)];
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await handler(
|
await handler(
|
||||||
NotificationResponse(
|
NotificationResponse(
|
||||||
id: id,
|
id: id,
|
||||||
actionId: actionId,
|
actionId: actionId,
|
||||||
payload: payload,
|
payload: payload,
|
||||||
input: input,
|
input: input,
|
||||||
notificationResponseType: type,
|
notificationResponseType: type,
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
} catch (_) {
|
} catch (_) {
|
||||||
remaining.add(raw);
|
remaining.add(raw);
|
||||||
}
|
}
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
await _coldStartQueue.replay();
|
|
||||||
if (remaining.isEmpty) {
|
if (remaining.isEmpty) {
|
||||||
await prefs.remove(_pendingKey);
|
await prefs.remove(_pendingKey);
|
||||||
return;
|
return;
|
||||||
|
|||||||
@@ -1,20 +1,16 @@
|
|||||||
import '../../features/auth/presentation/bloc/auth_state.dart';
|
import '../../features/auth/presentation/bloc/auth_state.dart';
|
||||||
import '../../features/calendar/data/services/calendar_service.dart';
|
import '../../features/calendar/data/services/calendar_service.dart';
|
||||||
import '../../features/calendar/reminders/reminder_action_executor.dart';
|
|
||||||
import '../notifications/local_notification_service.dart';
|
import '../notifications/local_notification_service.dart';
|
||||||
|
|
||||||
class AuthSessionBootstrapper {
|
class AuthSessionBootstrapper {
|
||||||
AuthSessionBootstrapper({
|
AuthSessionBootstrapper({
|
||||||
required CalendarService calendarService,
|
required CalendarService calendarService,
|
||||||
required LocalNotificationService notificationService,
|
required LocalNotificationService notificationService,
|
||||||
required ReminderActionExecutor reminderActionExecutor,
|
|
||||||
}) : _calendarService = calendarService,
|
}) : _calendarService = calendarService,
|
||||||
_notificationService = notificationService,
|
_notificationService = notificationService;
|
||||||
_reminderActionExecutor = reminderActionExecutor;
|
|
||||||
|
|
||||||
final CalendarService _calendarService;
|
final CalendarService _calendarService;
|
||||||
final LocalNotificationService _notificationService;
|
final LocalNotificationService _notificationService;
|
||||||
final ReminderActionExecutor _reminderActionExecutor;
|
|
||||||
|
|
||||||
String? _syncedUserId;
|
String? _syncedUserId;
|
||||||
|
|
||||||
@@ -29,8 +25,6 @@ class AuthSessionBootstrapper {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await _reminderActionExecutor.replayPendingActions();
|
|
||||||
|
|
||||||
final now = DateTime.now();
|
final now = DateTime.now();
|
||||||
final start = now.subtract(const Duration(days: 90));
|
final start = now.subtract(const Duration(days: 90));
|
||||||
final end = now.add(const Duration(days: 90));
|
final end = now.add(const Duration(days: 90));
|
||||||
|
|||||||
@@ -1,52 +0,0 @@
|
|||||||
import 'dart:async';
|
|
||||||
|
|
||||||
import 'package:shared_preferences/shared_preferences.dart';
|
|
||||||
|
|
||||||
typedef SetStringListFn = Future<bool> Function(String key, List<String> value);
|
|
||||||
|
|
||||||
class ReminderActionDedupeStore {
|
|
||||||
static const String _key = 'calendar_reminder_action_dedupe_v1';
|
|
||||||
static const int _maxEntries = 512;
|
|
||||||
|
|
||||||
final SharedPreferences _prefs;
|
|
||||||
final SetStringListFn _setStringList;
|
|
||||||
Future<void> _queue = Future<void>.value();
|
|
||||||
|
|
||||||
ReminderActionDedupeStore(
|
|
||||||
SharedPreferences prefs, {
|
|
||||||
SetStringListFn? setStringList,
|
|
||||||
}) : _prefs = prefs,
|
|
||||||
_setStringList = setStringList ?? prefs.setStringList;
|
|
||||||
|
|
||||||
Future<bool> markIfNew(String actionExecutionId) async {
|
|
||||||
final completer = Completer<bool>();
|
|
||||||
_queue = _queue
|
|
||||||
.then((_) async {
|
|
||||||
completer.complete(await _markIfNewInternal(actionExecutionId));
|
|
||||||
})
|
|
||||||
.catchError((_) {
|
|
||||||
if (!completer.isCompleted) {
|
|
||||||
completer.complete(false);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
return completer.future;
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<bool> _markIfNewInternal(String actionExecutionId) async {
|
|
||||||
final current = List<String>.from(
|
|
||||||
_prefs.getStringList(_key) ?? const <String>[],
|
|
||||||
);
|
|
||||||
if (current.contains(actionExecutionId)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
current.add(actionExecutionId);
|
|
||||||
if (current.length > _maxEntries) {
|
|
||||||
current.removeRange(0, current.length - _maxEntries);
|
|
||||||
}
|
|
||||||
|
|
||||||
final saved = await _setStringList(_key, current);
|
|
||||||
return saved;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,26 +1,17 @@
|
|||||||
import 'dart:math';
|
|
||||||
|
|
||||||
import '../data/services/calendar_service.dart';
|
import '../data/services/calendar_service.dart';
|
||||||
import '../../../core/notifications/local_notification_service.dart';
|
import '../../../core/notifications/local_notification_service.dart';
|
||||||
import 'models/reminder_action.dart';
|
import 'models/reminder_action.dart';
|
||||||
import 'models/reminder_payload.dart';
|
import 'models/reminder_payload.dart';
|
||||||
import 'reminder_outbox_store.dart';
|
|
||||||
|
|
||||||
class ReminderActionExecutor {
|
class ReminderActionExecutor {
|
||||||
final CalendarService _calendarService;
|
final CalendarService _calendarService;
|
||||||
final LocalNotificationService _notificationService;
|
final LocalNotificationService _notificationService;
|
||||||
final ReminderOutboxStore _outboxStore;
|
|
||||||
final Random _random;
|
|
||||||
|
|
||||||
ReminderActionExecutor({
|
ReminderActionExecutor({
|
||||||
required CalendarService calendarService,
|
required CalendarService calendarService,
|
||||||
required LocalNotificationService notificationService,
|
required LocalNotificationService notificationService,
|
||||||
required ReminderOutboxStore outboxStore,
|
|
||||||
Random? random,
|
|
||||||
}) : _calendarService = calendarService,
|
}) : _calendarService = calendarService,
|
||||||
_notificationService = notificationService,
|
_notificationService = notificationService;
|
||||||
_outboxStore = outboxStore,
|
|
||||||
_random = random ?? Random();
|
|
||||||
|
|
||||||
Future<void> handleAction({
|
Future<void> handleAction({
|
||||||
required ReminderAction action,
|
required ReminderAction action,
|
||||||
@@ -35,7 +26,7 @@ class ReminderActionExecutor {
|
|||||||
if (action == ReminderAction.archive) {
|
if (action == ReminderAction.archive) {
|
||||||
for (final id in ids) {
|
for (final id in ids) {
|
||||||
await _notificationService.cancelEventReminder(id);
|
await _notificationService.cancelEventReminder(id);
|
||||||
await _archiveEvent(id, ReminderAction.archive);
|
await _archiveEvent(id);
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -47,21 +38,6 @@ class ReminderActionExecutor {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> replayPendingActions() async {
|
|
||||||
final pending = await _outboxStore.listPending();
|
|
||||||
for (final item in pending) {
|
|
||||||
if (item.targetStatus != 'archived') {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
await _calendarService.archiveEvent(item.eventId);
|
|
||||||
await _outboxStore.markDone(item.opId);
|
|
||||||
} catch (error) {
|
|
||||||
await _outboxStore.markRetry(item.opId, error.toString());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<void> _snoozeEvent(String eventId) async {
|
Future<void> _snoozeEvent(String eventId) async {
|
||||||
final event = await _calendarService.getEventById(eventId);
|
final event = await _calendarService.getEventById(eventId);
|
||||||
if (event == null) {
|
if (event == null) {
|
||||||
@@ -71,37 +47,21 @@ class ReminderActionExecutor {
|
|||||||
final endAt = event.endAt;
|
final endAt = event.endAt;
|
||||||
if (endAt != null && !now.isBefore(endAt)) {
|
if (endAt != null && !now.isBefore(endAt)) {
|
||||||
await _notificationService.cancelEventReminder(eventId);
|
await _notificationService.cancelEventReminder(eventId);
|
||||||
await _archiveEvent(eventId, ReminderAction.archive);
|
await _archiveEvent(eventId);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
final nextAt = now.add(const Duration(minutes: 10));
|
final nextAt = now.add(const Duration(minutes: 10));
|
||||||
if (endAt != null && !nextAt.isBefore(endAt)) {
|
if (endAt != null && !nextAt.isBefore(endAt)) {
|
||||||
await _notificationService.cancelEventReminder(eventId);
|
await _notificationService.cancelEventReminder(eventId);
|
||||||
await _archiveEvent(eventId, ReminderAction.archive);
|
await _archiveEvent(eventId);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
await _notificationService.scheduleReminderAt(event, nextAt);
|
await _notificationService.scheduleReminderAt(event, nextAt);
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> _archiveEvent(String eventId, ReminderAction action) async {
|
Future<void> _archiveEvent(String eventId) async {
|
||||||
try {
|
await _calendarService.archiveEvent(eventId);
|
||||||
await _calendarService.archiveEvent(eventId);
|
|
||||||
return;
|
|
||||||
} catch (_) {
|
|
||||||
// fall through to enqueue local outbox for retry
|
|
||||||
}
|
|
||||||
|
|
||||||
final opId =
|
|
||||||
'${DateTime.now().millisecondsSinceEpoch}-${_random.nextInt(1 << 32)}';
|
|
||||||
final outboxItem = ReminderOutboxItem(
|
|
||||||
opId: opId,
|
|
||||||
eventId: eventId,
|
|
||||||
action: action,
|
|
||||||
targetStatus: 'archived',
|
|
||||||
occurredAt: DateTime.now(),
|
|
||||||
);
|
|
||||||
await _outboxStore.enqueue(outboxItem);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,57 +0,0 @@
|
|||||||
import 'dart:async';
|
|
||||||
import 'dart:collection';
|
|
||||||
|
|
||||||
typedef ReminderColdStartReplayTask = Future<void> Function();
|
|
||||||
typedef ReminderColdStartTaskErrorHandler =
|
|
||||||
void Function(Object error, StackTrace stackTrace);
|
|
||||||
|
|
||||||
class ReminderColdStartQueue {
|
|
||||||
final Queue<ReminderColdStartReplayTask> _tasks =
|
|
||||||
Queue<ReminderColdStartReplayTask>();
|
|
||||||
final ReminderColdStartTaskErrorHandler? _onTaskError;
|
|
||||||
Future<void>? _inFlightReplay;
|
|
||||||
|
|
||||||
ReminderColdStartQueue({ReminderColdStartTaskErrorHandler? onTaskError})
|
|
||||||
: _onTaskError = onTaskError;
|
|
||||||
|
|
||||||
void enqueue(ReminderColdStartReplayTask task) {
|
|
||||||
_tasks.add(task);
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<void> replay() {
|
|
||||||
final inFlightReplay = _inFlightReplay;
|
|
||||||
if (inFlightReplay != null) {
|
|
||||||
return inFlightReplay;
|
|
||||||
}
|
|
||||||
|
|
||||||
final replayCompleter = Completer<void>();
|
|
||||||
final replayFuture = replayCompleter.future;
|
|
||||||
_inFlightReplay = replayFuture;
|
|
||||||
|
|
||||||
scheduleMicrotask(() async {
|
|
||||||
try {
|
|
||||||
await _replayInternal();
|
|
||||||
replayCompleter.complete();
|
|
||||||
} catch (error, stackTrace) {
|
|
||||||
replayCompleter.completeError(error, stackTrace);
|
|
||||||
} finally {
|
|
||||||
if (identical(_inFlightReplay, replayFuture)) {
|
|
||||||
_inFlightReplay = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
return replayFuture;
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<void> _replayInternal() async {
|
|
||||||
while (_tasks.isNotEmpty) {
|
|
||||||
final task = _tasks.removeFirst();
|
|
||||||
try {
|
|
||||||
await task();
|
|
||||||
} catch (error, stackTrace) {
|
|
||||||
_onTaskError?.call(error, stackTrace);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,202 +0,0 @@
|
|||||||
import 'dart:convert';
|
|
||||||
|
|
||||||
import 'package:shared_preferences/shared_preferences.dart';
|
|
||||||
|
|
||||||
import 'models/reminder_action.dart';
|
|
||||||
|
|
||||||
class ReminderOutboxItem {
|
|
||||||
final String opId;
|
|
||||||
final String eventId;
|
|
||||||
final ReminderAction action;
|
|
||||||
final String? targetStatus;
|
|
||||||
final DateTime occurredAt;
|
|
||||||
final int retryCount;
|
|
||||||
final DateTime? nextRetryAt;
|
|
||||||
final ReminderOutboxState state;
|
|
||||||
final String? lastError;
|
|
||||||
|
|
||||||
const ReminderOutboxItem({
|
|
||||||
required this.opId,
|
|
||||||
required this.eventId,
|
|
||||||
required this.action,
|
|
||||||
required this.occurredAt,
|
|
||||||
this.targetStatus,
|
|
||||||
this.retryCount = 0,
|
|
||||||
this.nextRetryAt,
|
|
||||||
this.state = ReminderOutboxState.pending,
|
|
||||||
this.lastError,
|
|
||||||
});
|
|
||||||
|
|
||||||
String get idempotencyBucket {
|
|
||||||
final bucket =
|
|
||||||
occurredAt.millisecondsSinceEpoch ~/
|
|
||||||
const Duration(minutes: 1).inMilliseconds;
|
|
||||||
return '$eventId|${action.value}|$bucket';
|
|
||||||
}
|
|
||||||
|
|
||||||
ReminderOutboxItem copyWith({
|
|
||||||
int? retryCount,
|
|
||||||
DateTime? nextRetryAt,
|
|
||||||
ReminderOutboxState? state,
|
|
||||||
String? lastError,
|
|
||||||
}) {
|
|
||||||
return ReminderOutboxItem(
|
|
||||||
opId: opId,
|
|
||||||
eventId: eventId,
|
|
||||||
action: action,
|
|
||||||
targetStatus: targetStatus,
|
|
||||||
occurredAt: occurredAt,
|
|
||||||
retryCount: retryCount ?? this.retryCount,
|
|
||||||
nextRetryAt: nextRetryAt ?? this.nextRetryAt,
|
|
||||||
state: state ?? this.state,
|
|
||||||
lastError: lastError ?? this.lastError,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
Map<String, dynamic> toJson() {
|
|
||||||
return {
|
|
||||||
'opId': opId,
|
|
||||||
'eventId': eventId,
|
|
||||||
'action': action.value,
|
|
||||||
'targetStatus': targetStatus,
|
|
||||||
'occurredAt': occurredAt.toIso8601String(),
|
|
||||||
'retryCount': retryCount,
|
|
||||||
'nextRetryAt': nextRetryAt?.toIso8601String(),
|
|
||||||
'state': state.value,
|
|
||||||
'lastError': lastError,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
factory ReminderOutboxItem.fromJson(Map<String, dynamic> json) {
|
|
||||||
return ReminderOutboxItem(
|
|
||||||
opId: (json['opId'] as String?) ?? '',
|
|
||||||
eventId: (json['eventId'] as String?) ?? '',
|
|
||||||
action: ReminderAction.fromValue(
|
|
||||||
(json['action'] as String?) ?? 'timeout_30s',
|
|
||||||
),
|
|
||||||
targetStatus: json['targetStatus'] as String?,
|
|
||||||
occurredAt: DateTime.parse(json['occurredAt'] as String),
|
|
||||||
retryCount: (json['retryCount'] as int?) ?? 0,
|
|
||||||
nextRetryAt: json['nextRetryAt'] != null
|
|
||||||
? DateTime.parse(json['nextRetryAt'] as String)
|
|
||||||
: null,
|
|
||||||
state: ReminderOutboxState.fromValue(
|
|
||||||
(json['state'] as String?) ?? 'pending',
|
|
||||||
),
|
|
||||||
lastError: json['lastError'] as String?,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
enum ReminderOutboxState {
|
|
||||||
pending('pending'),
|
|
||||||
done('done'),
|
|
||||||
dead('dead');
|
|
||||||
|
|
||||||
const ReminderOutboxState(this.value);
|
|
||||||
final String value;
|
|
||||||
|
|
||||||
static ReminderOutboxState fromValue(String raw) {
|
|
||||||
return ReminderOutboxState.values.firstWhere(
|
|
||||||
(item) => item.value == raw,
|
|
||||||
orElse: () => ReminderOutboxState.pending,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class ReminderOutboxStore {
|
|
||||||
static const String _key = 'calendar_reminder_outbox_v1';
|
|
||||||
final SharedPreferences _prefs;
|
|
||||||
|
|
||||||
ReminderOutboxStore(this._prefs);
|
|
||||||
|
|
||||||
Future<void> enqueue(ReminderOutboxItem item) async {
|
|
||||||
final current = await _readAll();
|
|
||||||
final duplicated = current.any(
|
|
||||||
(existing) =>
|
|
||||||
existing.state == ReminderOutboxState.pending &&
|
|
||||||
existing.idempotencyBucket == item.idempotencyBucket,
|
|
||||||
);
|
|
||||||
if (duplicated) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
current.add(item);
|
|
||||||
await _writeAll(current);
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<List<ReminderOutboxItem>> listPending() async {
|
|
||||||
final all = await _readAll();
|
|
||||||
final now = DateTime.now();
|
|
||||||
return all
|
|
||||||
.where((item) => item.state == ReminderOutboxState.pending)
|
|
||||||
.where(
|
|
||||||
(item) => item.nextRetryAt == null || !item.nextRetryAt!.isAfter(now),
|
|
||||||
)
|
|
||||||
.toList();
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<void> markDone(String opId) async {
|
|
||||||
final all = await _readAll();
|
|
||||||
final updated = all
|
|
||||||
.map(
|
|
||||||
(item) => item.opId == opId
|
|
||||||
? item.copyWith(
|
|
||||||
state: ReminderOutboxState.done,
|
|
||||||
nextRetryAt: null,
|
|
||||||
)
|
|
||||||
: item,
|
|
||||||
)
|
|
||||||
.toList();
|
|
||||||
await _writeAll(updated);
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<void> markRetry(String opId, String error) async {
|
|
||||||
final all = await _readAll();
|
|
||||||
final updated = all.map((item) {
|
|
||||||
if (item.opId != opId) {
|
|
||||||
return item;
|
|
||||||
}
|
|
||||||
final nextRetryCount = item.retryCount + 1;
|
|
||||||
if (nextRetryCount >= 8) {
|
|
||||||
return item.copyWith(
|
|
||||||
retryCount: nextRetryCount,
|
|
||||||
state: ReminderOutboxState.dead,
|
|
||||||
lastError: error,
|
|
||||||
nextRetryAt: null,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
final delayMinutes = nextRetryCount == 1 ? 0 : 1 << (nextRetryCount - 1);
|
|
||||||
return item.copyWith(
|
|
||||||
retryCount: nextRetryCount,
|
|
||||||
lastError: error,
|
|
||||||
nextRetryAt: DateTime.now().add(Duration(minutes: delayMinutes)),
|
|
||||||
);
|
|
||||||
}).toList();
|
|
||||||
await _writeAll(updated);
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<List<ReminderOutboxItem>> _readAll() async {
|
|
||||||
try {
|
|
||||||
final raw = _prefs.getString(_key);
|
|
||||||
if (raw == null || raw.isEmpty) {
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
final list = jsonDecode(raw) as List<dynamic>;
|
|
||||||
return list
|
|
||||||
.whereType<Map>()
|
|
||||||
.map(
|
|
||||||
(item) =>
|
|
||||||
ReminderOutboxItem.fromJson(Map<String, dynamic>.from(item)),
|
|
||||||
)
|
|
||||||
.toList();
|
|
||||||
} catch (_) {
|
|
||||||
await _prefs.remove(_key);
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<void> _writeAll(List<ReminderOutboxItem> items) async {
|
|
||||||
final raw = jsonEncode(items.map((item) => item.toJson()).toList());
|
|
||||||
await _prefs.setString(_key, raw);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,80 +0,0 @@
|
|||||||
import '../data/models/schedule_item_model.dart';
|
|
||||||
|
|
||||||
class ReminderOverlapGroup {
|
|
||||||
final DateTime fireAt;
|
|
||||||
final List<ScheduleItemModel> events;
|
|
||||||
|
|
||||||
const ReminderOverlapGroup({required this.fireAt, required this.events});
|
|
||||||
|
|
||||||
bool get isAggregate => events.length > 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
class ReminderOverlapPolicy {
|
|
||||||
const ReminderOverlapPolicy();
|
|
||||||
|
|
||||||
List<ReminderOverlapGroup> groupByMinute(
|
|
||||||
Iterable<ScheduleItemModel> events, {
|
|
||||||
required DateTime now,
|
|
||||||
}) {
|
|
||||||
final buckets = <String, List<ScheduleItemModel>>{};
|
|
||||||
final minuteToFireAt = <String, DateTime>{};
|
|
||||||
|
|
||||||
for (final event in events) {
|
|
||||||
final fireAt = resolveFirstFireAt(event, now: now);
|
|
||||||
if (fireAt == null) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
final minute = DateTime(
|
|
||||||
fireAt.year,
|
|
||||||
fireAt.month,
|
|
||||||
fireAt.day,
|
|
||||||
fireAt.hour,
|
|
||||||
fireAt.minute,
|
|
||||||
);
|
|
||||||
final key = minute.toIso8601String();
|
|
||||||
buckets.putIfAbsent(key, () => <ScheduleItemModel>[]).add(event);
|
|
||||||
minuteToFireAt[key] = minuteToFireAt[key] ?? fireAt;
|
|
||||||
}
|
|
||||||
|
|
||||||
final groups = buckets.entries
|
|
||||||
.map(
|
|
||||||
(entry) => ReminderOverlapGroup(
|
|
||||||
fireAt: minuteToFireAt[entry.key]!,
|
|
||||||
events: entry.value,
|
|
||||||
),
|
|
||||||
)
|
|
||||||
.toList();
|
|
||||||
|
|
||||||
groups.sort((left, right) => left.fireAt.compareTo(right.fireAt));
|
|
||||||
return groups;
|
|
||||||
}
|
|
||||||
|
|
||||||
DateTime? resolveFirstFireAt(
|
|
||||||
ScheduleItemModel event, {
|
|
||||||
required DateTime now,
|
|
||||||
}) {
|
|
||||||
if (event.status != ScheduleStatus.active) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
final reminderMinutes = event.metadata?.reminderMinutes;
|
|
||||||
if (reminderMinutes == null) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
final remindAt = event.startAt.subtract(Duration(minutes: reminderMinutes));
|
|
||||||
final endAt = event.endAt;
|
|
||||||
|
|
||||||
if (endAt != null && !now.isBefore(endAt)) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (now.isBefore(remindAt)) {
|
|
||||||
return remindAt;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (endAt != null && now.isBefore(endAt)) {
|
|
||||||
return now.add(const Duration(seconds: 5));
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,73 +0,0 @@
|
|||||||
import 'package:flutter/material.dart';
|
|
||||||
|
|
||||||
import '../../../../core/theme/design_tokens.dart';
|
|
||||||
import '../models/reminder_action.dart';
|
|
||||||
import '../models/reminder_payload.dart';
|
|
||||||
import '../reminder_action_executor.dart';
|
|
||||||
import 'reminder_presentation_coordinator.dart';
|
|
||||||
import 'widgets/reminder_action_sheet.dart';
|
|
||||||
|
|
||||||
class ReminderForegroundPresenter {
|
|
||||||
final GlobalKey<NavigatorState> _navigatorKey;
|
|
||||||
final ReminderActionExecutor _executor;
|
|
||||||
final ReminderPresentationCoordinator _coordinator;
|
|
||||||
bool _isPresenting = false;
|
|
||||||
|
|
||||||
ReminderForegroundPresenter({
|
|
||||||
required GlobalKey<NavigatorState> navigatorKey,
|
|
||||||
required ReminderActionExecutor executor,
|
|
||||||
ReminderPresentationCoordinator? coordinator,
|
|
||||||
}) : _navigatorKey = navigatorKey,
|
|
||||||
_executor = executor,
|
|
||||||
_coordinator = coordinator ?? ReminderPresentationCoordinator();
|
|
||||||
|
|
||||||
Future<void> present(ReminderPayload payload) async {
|
|
||||||
final context = _navigatorKey.currentContext;
|
|
||||||
if (context == null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
final lifecycleState = WidgetsBinding.instance.lifecycleState;
|
|
||||||
final isAppActive = lifecycleState == AppLifecycleState.resumed;
|
|
||||||
final shouldPresent = _coordinator.shouldPresent(
|
|
||||||
eventId: payload.eventId,
|
|
||||||
isAppActive: isAppActive,
|
|
||||||
);
|
|
||||||
if (!shouldPresent || _isPresenting) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
_isPresenting = true;
|
|
||||||
try {
|
|
||||||
final action = await showModalBottomSheet<ReminderAction>(
|
|
||||||
context: context,
|
|
||||||
useRootNavigator: true,
|
|
||||||
isScrollControlled: true,
|
|
||||||
backgroundColor: Colors.transparent,
|
|
||||||
builder: (sheetContext) {
|
|
||||||
return SafeArea(
|
|
||||||
child: Padding(
|
|
||||||
padding: const EdgeInsets.all(AppSpacing.md),
|
|
||||||
child: ReminderActionSheet(
|
|
||||||
onSnooze: () {
|
|
||||||
Navigator.of(sheetContext).pop(ReminderAction.snooze10m);
|
|
||||||
},
|
|
||||||
onArchive: () {
|
|
||||||
Navigator.of(sheetContext).pop(ReminderAction.archive);
|
|
||||||
},
|
|
||||||
),
|
|
||||||
),
|
|
||||||
);
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
if (action == null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
await _executor.handleAction(action: action, payload: payload);
|
|
||||||
} finally {
|
|
||||||
_isPresenting = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,12 +1,11 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:intl/intl.dart';
|
import 'package:intl/intl.dart';
|
||||||
|
import '../../../../core/theme/design_tokens.dart';
|
||||||
|
import '../../../../shared/widgets/app_button.dart';
|
||||||
|
import '../../reminders/reminder_queue_manager.dart';
|
||||||
|
import '../../reminders/models/reminder_payload.dart';
|
||||||
|
|
||||||
import 'package:social_app/core/theme/design_tokens.dart';
|
class ReminderOverlay extends StatefulWidget {
|
||||||
import 'package:social_app/shared/widgets/app_button.dart';
|
|
||||||
import 'package:social_app/features/calendar/reminders/reminder_queue_manager.dart';
|
|
||||||
import 'package:social_app/features/calendar/reminders/models/reminder_payload.dart';
|
|
||||||
|
|
||||||
class ReminderOverlay extends StatelessWidget {
|
|
||||||
const ReminderOverlay({
|
const ReminderOverlay({
|
||||||
super.key,
|
super.key,
|
||||||
required this.queueManager,
|
required this.queueManager,
|
||||||
@@ -21,162 +20,138 @@ class ReminderOverlay extends StatelessWidget {
|
|||||||
final VoidCallback onArchive;
|
final VoidCallback onArchive;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
State<ReminderOverlay> createState() => _ReminderOverlayState();
|
||||||
final payload = queueManager.currentPayload;
|
|
||||||
|
|
||||||
return Scaffold(
|
|
||||||
backgroundColor: AppColors.white,
|
|
||||||
body: SafeArea(
|
|
||||||
child: Padding(
|
|
||||||
padding: const EdgeInsets.symmetric(horizontal: AppSpacing.lg),
|
|
||||||
child: Column(
|
|
||||||
mainAxisAlignment: MainAxisAlignment.center,
|
|
||||||
crossAxisAlignment: CrossAxisAlignment.center,
|
|
||||||
children: [
|
|
||||||
const Spacer(),
|
|
||||||
Text(
|
|
||||||
payload?.title ?? '',
|
|
||||||
style: Theme.of(context).textTheme.headlineSmall,
|
|
||||||
textAlign: TextAlign.center,
|
|
||||||
),
|
|
||||||
const SizedBox(height: AppSpacing.md),
|
|
||||||
Text(
|
|
||||||
DateFormat('HH:mm').format(DateTime.now()),
|
|
||||||
style: Theme.of(context).textTheme.titleLarge,
|
|
||||||
),
|
|
||||||
const Spacer(),
|
|
||||||
_buildBottomButtons(context),
|
|
||||||
const SizedBox(height: AppSpacing.xxl),
|
|
||||||
],
|
|
||||||
),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
Widget _buildBottomButtons(BuildContext context) {
|
|
||||||
return Row(
|
|
||||||
children: [
|
|
||||||
Expanded(
|
|
||||||
child: _SnoozeButton(
|
|
||||||
onSnooze: (minutes) {
|
|
||||||
onSnooze(minutes);
|
|
||||||
queueManager.dequeueCurrent();
|
|
||||||
onComplete();
|
|
||||||
},
|
|
||||||
),
|
|
||||||
),
|
|
||||||
const SizedBox(width: AppSpacing.md),
|
|
||||||
Expanded(
|
|
||||||
child: AppButton(
|
|
||||||
text: '完成',
|
|
||||||
onPressed: () {
|
|
||||||
onArchive();
|
|
||||||
queueManager.dequeueCurrent();
|
|
||||||
onComplete();
|
|
||||||
},
|
|
||||||
),
|
|
||||||
),
|
|
||||||
],
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class _SnoozeButton extends StatefulWidget {
|
class _ReminderOverlayState extends State<ReminderOverlay> {
|
||||||
const _SnoozeButton({required this.onSnooze});
|
|
||||||
|
|
||||||
final void Function(int minutes) onSnooze;
|
|
||||||
|
|
||||||
@override
|
|
||||||
State<_SnoozeButton> createState() => _SnoozeButtonState();
|
|
||||||
}
|
|
||||||
|
|
||||||
class _SnoozeButtonState extends State<_SnoozeButton> {
|
|
||||||
final LayerLink _layerLink = LayerLink();
|
|
||||||
OverlayEntry? _overlayEntry;
|
OverlayEntry? _overlayEntry;
|
||||||
|
|
||||||
|
ReminderPayload? get _currentPayload => widget.queueManager.currentPayload;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void dispose() {
|
void dispose() {
|
||||||
_removeOverlay();
|
_hideSnoozeOptions();
|
||||||
super.dispose();
|
super.dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
void _removeOverlay() {
|
void _hideSnoozeOptions() {
|
||||||
_overlayEntry?.remove();
|
_overlayEntry?.remove();
|
||||||
_overlayEntry = null;
|
_overlayEntry = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
void _showOverlay() {
|
void _showSnoozeDropdown() {
|
||||||
_removeOverlay();
|
_hideSnoozeOptions();
|
||||||
|
|
||||||
final overlay = Overlay.of(context);
|
final box = context.findRenderObject() as RenderBox?;
|
||||||
final renderBox = context.findRenderObject() as RenderBox;
|
if (box == null) return;
|
||||||
final size = renderBox.size;
|
|
||||||
|
final button = box.localToGlobal(Offset.zero);
|
||||||
|
|
||||||
_overlayEntry = OverlayEntry(
|
_overlayEntry = OverlayEntry(
|
||||||
builder: (context) => Stack(
|
builder: (context) => Positioned(
|
||||||
children: [
|
left: button.dx,
|
||||||
Positioned(
|
top: button.dy + box.size.height + 4,
|
||||||
width: size.width,
|
width: 120,
|
||||||
child: CompositedTransformFollower(
|
child: Material(
|
||||||
link: _layerLink,
|
elevation: 4,
|
||||||
showWhenUnlinked: false,
|
borderRadius: BorderRadius.circular(8),
|
||||||
offset: Offset(0, size.height + AppSpacing.xs),
|
child: Container(
|
||||||
child: Material(
|
decoration: BoxDecoration(
|
||||||
elevation: 4,
|
color: AppColors.white,
|
||||||
borderRadius: BorderRadius.circular(AppRadius.md),
|
borderRadius: BorderRadius.circular(8),
|
||||||
color: AppColors.white,
|
border: Border.all(color: AppColors.borderSecondary),
|
||||||
child: Column(
|
),
|
||||||
mainAxisSize: MainAxisSize.min,
|
child: Column(
|
||||||
children: [
|
mainAxisSize: MainAxisSize.min,
|
||||||
_SnoozeOption(
|
children: [
|
||||||
label: '5 分钟',
|
_SnoozeOption(
|
||||||
onTap: () {
|
label: '5 分钟',
|
||||||
_removeOverlay();
|
onTap: () {
|
||||||
widget.onSnooze(5);
|
_hideSnoozeOptions();
|
||||||
},
|
_handleSnooze(5);
|
||||||
),
|
},
|
||||||
Divider(height: 1, color: AppColors.border),
|
|
||||||
_SnoozeOption(
|
|
||||||
label: '15 分钟',
|
|
||||||
onTap: () {
|
|
||||||
_removeOverlay();
|
|
||||||
widget.onSnooze(15);
|
|
||||||
},
|
|
||||||
),
|
|
||||||
],
|
|
||||||
),
|
),
|
||||||
),
|
const Divider(height: 1, color: AppColors.borderSecondary),
|
||||||
|
_SnoozeOption(
|
||||||
|
label: '15 分钟',
|
||||||
|
onTap: () {
|
||||||
|
_hideSnoozeOptions();
|
||||||
|
_handleSnooze(15);
|
||||||
|
},
|
||||||
|
),
|
||||||
|
],
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
Positioned.fill(
|
),
|
||||||
child: GestureDetector(
|
|
||||||
onTap: _removeOverlay,
|
|
||||||
behavior: HitTestBehavior.opaque,
|
|
||||||
child: Container(color: Colors.transparent),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
],
|
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
overlay.insert(_overlayEntry!);
|
Overlay.of(context).insert(_overlayEntry!);
|
||||||
|
}
|
||||||
|
|
||||||
|
void _handleComplete() {
|
||||||
|
widget.onArchive();
|
||||||
|
widget.queueManager.dequeueCurrent();
|
||||||
|
widget.onComplete();
|
||||||
|
}
|
||||||
|
|
||||||
|
void _handleSnooze(int minutes) {
|
||||||
|
widget.onSnooze(minutes);
|
||||||
|
widget.queueManager.dequeueCurrent();
|
||||||
|
widget.onComplete();
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return CompositedTransformTarget(
|
final payload = _currentPayload;
|
||||||
link: _layerLink,
|
if (payload == null) {
|
||||||
child: AppButton(
|
return const SizedBox.shrink();
|
||||||
text: '稍后提醒',
|
}
|
||||||
isOutlined: true,
|
|
||||||
onPressed: () {
|
return Container(
|
||||||
if (_overlayEntry == null) {
|
color: AppColors.white,
|
||||||
_showOverlay();
|
child: SafeArea(
|
||||||
} else {
|
child: Padding(
|
||||||
_removeOverlay();
|
padding: const EdgeInsets.all(AppSpacing.lg),
|
||||||
}
|
child: Column(
|
||||||
},
|
mainAxisAlignment: MainAxisAlignment.center,
|
||||||
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||||
|
children: [
|
||||||
|
Text(
|
||||||
|
payload.title,
|
||||||
|
style: Theme.of(context).textTheme.headlineSmall?.copyWith(
|
||||||
|
color: AppColors.slate900,
|
||||||
|
fontWeight: FontWeight.w600,
|
||||||
|
),
|
||||||
|
textAlign: TextAlign.center,
|
||||||
|
),
|
||||||
|
const SizedBox(height: AppSpacing.sm),
|
||||||
|
Text(
|
||||||
|
DateFormat('HH:mm').format(DateTime.now()),
|
||||||
|
style: Theme.of(
|
||||||
|
context,
|
||||||
|
).textTheme.titleLarge?.copyWith(color: AppColors.slate500),
|
||||||
|
textAlign: TextAlign.center,
|
||||||
|
),
|
||||||
|
const SizedBox(height: AppSpacing.xl),
|
||||||
|
Row(
|
||||||
|
children: [
|
||||||
|
Expanded(
|
||||||
|
child: AppButton(
|
||||||
|
text: '稍后提醒',
|
||||||
|
isOutlined: true,
|
||||||
|
onPressed: _showSnoozeDropdown,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
const SizedBox(width: AppSpacing.md),
|
||||||
|
Expanded(
|
||||||
|
child: AppButton(text: '完成', onPressed: _handleComplete),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -192,15 +167,16 @@ class _SnoozeOption extends StatelessWidget {
|
|||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return InkWell(
|
return InkWell(
|
||||||
onTap: onTap,
|
onTap: onTap,
|
||||||
borderRadius: BorderRadius.circular(AppRadius.md),
|
|
||||||
child: Padding(
|
child: Padding(
|
||||||
padding: const EdgeInsets.symmetric(
|
padding: const EdgeInsets.symmetric(
|
||||||
horizontal: AppSpacing.lg,
|
horizontal: AppSpacing.md,
|
||||||
vertical: AppSpacing.md,
|
vertical: AppSpacing.sm,
|
||||||
),
|
),
|
||||||
child: Text(
|
child: Text(
|
||||||
label,
|
label,
|
||||||
style: const TextStyle(fontSize: 15, fontWeight: FontWeight.w500),
|
style: Theme.of(
|
||||||
|
context,
|
||||||
|
).textTheme.bodyMedium?.copyWith(color: AppColors.slate900),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -1,29 +0,0 @@
|
|||||||
typedef ReminderPresentationNow = DateTime Function();
|
|
||||||
|
|
||||||
class ReminderPresentationCoordinator {
|
|
||||||
final Duration _dedupeWindow;
|
|
||||||
final ReminderPresentationNow _now;
|
|
||||||
final Map<String, DateTime> _lastPresentedAtByEventId = <String, DateTime>{};
|
|
||||||
|
|
||||||
ReminderPresentationCoordinator({
|
|
||||||
Duration dedupeWindow = const Duration(seconds: 30),
|
|
||||||
ReminderPresentationNow? now,
|
|
||||||
}) : _dedupeWindow = dedupeWindow,
|
|
||||||
_now = now ?? DateTime.now;
|
|
||||||
|
|
||||||
bool shouldPresent({required String eventId, required bool isAppActive}) {
|
|
||||||
if (!isAppActive) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
final currentTime = _now();
|
|
||||||
final lastPresentedAt = _lastPresentedAtByEventId[eventId];
|
|
||||||
if (lastPresentedAt != null &&
|
|
||||||
currentTime.difference(lastPresentedAt) < _dedupeWindow) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
_lastPresentedAtByEventId[eventId] = currentTime;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,58 +0,0 @@
|
|||||||
import 'package:flutter/material.dart';
|
|
||||||
|
|
||||||
import '../../../../../core/theme/design_tokens.dart';
|
|
||||||
import '../../../../../shared/widgets/app_button.dart';
|
|
||||||
|
|
||||||
class ReminderActionSheet extends StatelessWidget {
|
|
||||||
const ReminderActionSheet({
|
|
||||||
super.key,
|
|
||||||
required this.onSnooze,
|
|
||||||
required this.onArchive,
|
|
||||||
});
|
|
||||||
|
|
||||||
final VoidCallback onSnooze;
|
|
||||||
final VoidCallback onArchive;
|
|
||||||
|
|
||||||
@override
|
|
||||||
Widget build(BuildContext context) {
|
|
||||||
return Container(
|
|
||||||
width: double.infinity,
|
|
||||||
padding: const EdgeInsets.all(AppSpacing.lg),
|
|
||||||
decoration: BoxDecoration(
|
|
||||||
color: AppColors.white,
|
|
||||||
borderRadius: BorderRadius.circular(AppRadius.xl),
|
|
||||||
border: Border.all(color: AppColors.borderSecondary),
|
|
||||||
),
|
|
||||||
child: Column(
|
|
||||||
mainAxisSize: MainAxisSize.min,
|
|
||||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
||||||
children: [
|
|
||||||
Text(
|
|
||||||
'提醒操作',
|
|
||||||
textAlign: TextAlign.center,
|
|
||||||
style: Theme.of(
|
|
||||||
context,
|
|
||||||
).textTheme.titleMedium?.copyWith(color: AppColors.slate900),
|
|
||||||
),
|
|
||||||
const SizedBox(height: AppSpacing.lg),
|
|
||||||
Row(
|
|
||||||
crossAxisAlignment: CrossAxisAlignment.center,
|
|
||||||
children: [
|
|
||||||
Expanded(
|
|
||||||
child: AppButton(
|
|
||||||
text: '稍后提醒',
|
|
||||||
isOutlined: true,
|
|
||||||
onPressed: onSnooze,
|
|
||||||
),
|
|
||||||
),
|
|
||||||
const SizedBox(width: AppSpacing.md),
|
|
||||||
Expanded(
|
|
||||||
child: AppButton(text: '归档', onPressed: onArchive),
|
|
||||||
),
|
|
||||||
],
|
|
||||||
),
|
|
||||||
],
|
|
||||||
),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
+73
-53
@@ -7,9 +7,9 @@ import 'package:shared_preferences/shared_preferences.dart';
|
|||||||
import 'core/constants/app_constants.dart';
|
import 'core/constants/app_constants.dart';
|
||||||
import 'core/cache/cache_refresh_coordinator.dart';
|
import 'core/cache/cache_refresh_coordinator.dart';
|
||||||
import 'core/di/injection.dart';
|
import 'core/di/injection.dart';
|
||||||
import 'core/notifications/ios_notification_payload_bridge.dart';
|
|
||||||
import 'core/notifications/local_notification_service.dart';
|
import 'core/notifications/local_notification_service.dart';
|
||||||
import 'core/notifications/reminder_notification_callbacks.dart';
|
import 'core/notifications/reminder_notification_callbacks.dart';
|
||||||
|
import 'core/notifications/ios_notification_payload_bridge.dart';
|
||||||
import 'core/router/app_router.dart';
|
import 'core/router/app_router.dart';
|
||||||
import 'core/startup/auth_session_bootstrapper.dart';
|
import 'core/startup/auth_session_bootstrapper.dart';
|
||||||
import 'core/theme/app_theme.dart';
|
import 'core/theme/app_theme.dart';
|
||||||
@@ -20,8 +20,6 @@ import 'features/calendar/data/services/calendar_service.dart';
|
|||||||
import 'features/calendar/data/services/calendar_repository.dart';
|
import 'features/calendar/data/services/calendar_repository.dart';
|
||||||
import 'features/calendar/reminders/reminder_action_executor.dart';
|
import 'features/calendar/reminders/reminder_action_executor.dart';
|
||||||
import 'features/calendar/reminders/reminder_queue_manager.dart';
|
import 'features/calendar/reminders/reminder_queue_manager.dart';
|
||||||
import 'features/calendar/reminders/models/reminder_action.dart';
|
|
||||||
import 'features/calendar/reminders/models/reminder_payload.dart';
|
|
||||||
import 'features/calendar/reminders/ui/reminder_overlay.dart';
|
import 'features/calendar/reminders/ui/reminder_overlay.dart';
|
||||||
import 'features/calendar/ui/calendar_state_manager.dart';
|
import 'features/calendar/ui/calendar_state_manager.dart';
|
||||||
import 'features/chat/presentation/bloc/chat_bloc.dart';
|
import 'features/chat/presentation/bloc/chat_bloc.dart';
|
||||||
@@ -32,6 +30,7 @@ void main() async {
|
|||||||
WidgetsFlutterBinding.ensureInitialized();
|
WidgetsFlutterBinding.ensureInitialized();
|
||||||
await configureDependencies();
|
await configureDependencies();
|
||||||
await AppConstants.init();
|
await AppConstants.init();
|
||||||
|
|
||||||
final rootNavigatorKey = GlobalKey<NavigatorState>();
|
final rootNavigatorKey = GlobalKey<NavigatorState>();
|
||||||
sl<LocalNotificationService>().bindActionHandler(({
|
sl<LocalNotificationService>().bindActionHandler(({
|
||||||
required action,
|
required action,
|
||||||
@@ -44,15 +43,6 @@ void main() async {
|
|||||||
});
|
});
|
||||||
await sl<LocalNotificationService>().initialize();
|
await sl<LocalNotificationService>().initialize();
|
||||||
|
|
||||||
final prefs = sl<SharedPreferences>();
|
|
||||||
final payloadBridge = IOSNotificationPayloadBridge(prefs);
|
|
||||||
final queueManager = ReminderQueueManager();
|
|
||||||
final pendingPayload = await payloadBridge.getPendingPayload();
|
|
||||||
if (pendingPayload != null) {
|
|
||||||
queueManager.enqueueFromClick(pendingPayload);
|
|
||||||
await payloadBridge.clearPendingPayload();
|
|
||||||
}
|
|
||||||
|
|
||||||
final authBloc = sl<AuthBloc>();
|
final authBloc = sl<AuthBloc>();
|
||||||
authBloc.add(AuthStarted());
|
authBloc.add(AuthStarted());
|
||||||
|
|
||||||
@@ -75,6 +65,15 @@ void main() async {
|
|||||||
);
|
);
|
||||||
WidgetsBinding.instance.addObserver(cacheRefreshCoordinator);
|
WidgetsBinding.instance.addObserver(cacheRefreshCoordinator);
|
||||||
|
|
||||||
|
final prefs = await SharedPreferences.getInstance();
|
||||||
|
final payloadBridge = IOSNotificationPayloadBridge(prefs);
|
||||||
|
final pendingPayload = await payloadBridge.getPendingPayload();
|
||||||
|
final queueManager = ReminderQueueManager();
|
||||||
|
if (pendingPayload != null) {
|
||||||
|
queueManager.enqueueFromClick(pendingPayload);
|
||||||
|
await payloadBridge.clearPendingPayload();
|
||||||
|
}
|
||||||
|
|
||||||
runApp(
|
runApp(
|
||||||
LinksyApp(
|
LinksyApp(
|
||||||
authBloc: authBloc,
|
authBloc: authBloc,
|
||||||
@@ -82,10 +81,9 @@ void main() async {
|
|||||||
sessionBootstrapper: AuthSessionBootstrapper(
|
sessionBootstrapper: AuthSessionBootstrapper(
|
||||||
calendarService: sl<CalendarService>(),
|
calendarService: sl<CalendarService>(),
|
||||||
notificationService: sl<LocalNotificationService>(),
|
notificationService: sl<LocalNotificationService>(),
|
||||||
reminderActionExecutor: sl<ReminderActionExecutor>(),
|
|
||||||
),
|
),
|
||||||
pendingReminderPayload: pendingPayload,
|
queueManager: queueManager,
|
||||||
reminderQueueManager: queueManager,
|
payloadBridge: payloadBridge,
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -102,16 +100,16 @@ class LinksyApp extends StatefulWidget {
|
|||||||
final AuthBloc authBloc;
|
final AuthBloc authBloc;
|
||||||
final GlobalKey<NavigatorState> rootNavigatorKey;
|
final GlobalKey<NavigatorState> rootNavigatorKey;
|
||||||
final AuthSessionBootstrapper sessionBootstrapper;
|
final AuthSessionBootstrapper sessionBootstrapper;
|
||||||
final ReminderPayload? pendingReminderPayload;
|
final ReminderQueueManager queueManager;
|
||||||
final ReminderQueueManager reminderQueueManager;
|
final IOSNotificationPayloadBridge payloadBridge;
|
||||||
|
|
||||||
const LinksyApp({
|
const LinksyApp({
|
||||||
super.key,
|
super.key,
|
||||||
required this.authBloc,
|
required this.authBloc,
|
||||||
required this.rootNavigatorKey,
|
required this.rootNavigatorKey,
|
||||||
required this.sessionBootstrapper,
|
required this.sessionBootstrapper,
|
||||||
this.pendingReminderPayload,
|
required this.queueManager,
|
||||||
required this.reminderQueueManager,
|
required this.payloadBridge,
|
||||||
});
|
});
|
||||||
|
|
||||||
@override
|
@override
|
||||||
@@ -119,51 +117,73 @@ class LinksyApp extends StatefulWidget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class _LinksyAppState extends State<LinksyApp> {
|
class _LinksyAppState extends State<LinksyApp> {
|
||||||
OverlayEntry? _reminderOverlayEntry;
|
OverlayEntry? _reminderOverlay;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
super.initState();
|
super.initState();
|
||||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
_checkAndShowReminderOverlay();
|
||||||
_maybeShowReminderOverlay();
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void _maybeShowReminderOverlay() {
|
Future<void> _checkAndShowReminderOverlay() async {
|
||||||
if (widget.pendingReminderPayload == null) {
|
if (widget.queueManager.currentPayload != null) {
|
||||||
return;
|
_showReminderOverlay();
|
||||||
}
|
}
|
||||||
final context = widget.rootNavigatorKey.currentContext;
|
}
|
||||||
if (context == null) {
|
|
||||||
return;
|
void _showReminderOverlay() {
|
||||||
}
|
if (_reminderOverlay != null) return;
|
||||||
_reminderOverlayEntry = OverlayEntry(
|
|
||||||
builder: (context) => ReminderOverlay(
|
_reminderOverlay = OverlayEntry(
|
||||||
queueManager: widget.reminderQueueManager,
|
builder: (context) => Positioned.fill(
|
||||||
onComplete: _dismissReminderOverlay,
|
child: Material(
|
||||||
onSnooze: (minutes) {
|
color: Colors.black54,
|
||||||
final action = minutes >= 10
|
child: ReminderOverlay(
|
||||||
? ReminderAction.snooze10m
|
queueManager: widget.queueManager,
|
||||||
: ReminderAction.archive;
|
onComplete: _onReminderComplete,
|
||||||
sl<ReminderActionExecutor>().handleAction(
|
onSnooze: _onSnooze,
|
||||||
action: action,
|
onArchive: _onArchive,
|
||||||
payload: widget.pendingReminderPayload!,
|
),
|
||||||
);
|
),
|
||||||
},
|
|
||||||
onArchive: () {
|
|
||||||
sl<ReminderActionExecutor>().handleAction(
|
|
||||||
action: ReminderAction.archive,
|
|
||||||
payload: widget.pendingReminderPayload!,
|
|
||||||
);
|
|
||||||
},
|
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
Overlay.of(context).insert(_reminderOverlayEntry!);
|
|
||||||
|
Overlay.of(context).insert(_reminderOverlay!);
|
||||||
}
|
}
|
||||||
|
|
||||||
void _dismissReminderOverlay() {
|
void _onReminderComplete() {
|
||||||
_reminderOverlayEntry?.remove();
|
_reminderOverlay?.remove();
|
||||||
_reminderOverlayEntry = null;
|
_reminderOverlay = null;
|
||||||
|
|
||||||
|
if (!widget.queueManager.isEmpty) {
|
||||||
|
_showReminderOverlay();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> _onSnooze(int minutes) async {
|
||||||
|
final payload = widget.queueManager.currentPayload;
|
||||||
|
if (payload == null) return;
|
||||||
|
|
||||||
|
await sl<LocalNotificationService>().cancelEventReminder(payload.eventId);
|
||||||
|
final event = await sl<CalendarService>().getEventById(payload.eventId);
|
||||||
|
if (event != null) {
|
||||||
|
final snoozeTime = DateTime.now().add(Duration(minutes: minutes));
|
||||||
|
await sl<LocalNotificationService>().scheduleReminderAt(
|
||||||
|
event,
|
||||||
|
snoozeTime,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> _onArchive() async {
|
||||||
|
final payload = widget.queueManager.currentPayload;
|
||||||
|
if (payload == null) return;
|
||||||
|
|
||||||
|
try {
|
||||||
|
await sl<CalendarService>().archiveEvent(payload.eventId);
|
||||||
|
} catch (_) {
|
||||||
|
// archive failed, continue anyway
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
|
|||||||
@@ -1,94 +0,0 @@
|
|||||||
import 'package:flutter_test/flutter_test.dart';
|
|
||||||
import 'package:mocktail/mocktail.dart';
|
|
||||||
import 'package:social_app/core/notifications/local_notification_service.dart';
|
|
||||||
import 'package:social_app/core/startup/auth_session_bootstrapper.dart';
|
|
||||||
import 'package:social_app/features/auth/presentation/bloc/auth_state.dart';
|
|
||||||
import 'package:social_app/features/calendar/data/services/calendar_service.dart';
|
|
||||||
import 'package:social_app/features/calendar/reminders/reminder_action_executor.dart';
|
|
||||||
|
|
||||||
class MockCalendarService extends Mock implements CalendarService {}
|
|
||||||
|
|
||||||
class MockLocalNotificationService extends Mock
|
|
||||||
implements LocalNotificationService {}
|
|
||||||
|
|
||||||
class MockReminderActionExecutor extends Mock
|
|
||||||
implements ReminderActionExecutor {}
|
|
||||||
|
|
||||||
void main() {
|
|
||||||
late MockCalendarService calendarService;
|
|
||||||
late MockLocalNotificationService notificationService;
|
|
||||||
late MockReminderActionExecutor reminderActionExecutor;
|
|
||||||
late AuthSessionBootstrapper bootstrapper;
|
|
||||||
|
|
||||||
setUp(() {
|
|
||||||
calendarService = MockCalendarService();
|
|
||||||
notificationService = MockLocalNotificationService();
|
|
||||||
reminderActionExecutor = MockReminderActionExecutor();
|
|
||||||
bootstrapper = AuthSessionBootstrapper(
|
|
||||||
calendarService: calendarService,
|
|
||||||
notificationService: notificationService,
|
|
||||||
reminderActionExecutor: reminderActionExecutor,
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('does not fetch calendar events for unauthenticated state', () async {
|
|
||||||
await bootstrapper.syncForAuthState(AuthUnauthenticated());
|
|
||||||
|
|
||||||
verifyNever(() => calendarService.getEventsForRange(any(), any()));
|
|
||||||
verifyNever(() => notificationService.rebuildUpcomingReminders(any()));
|
|
||||||
verifyNever(() => reminderActionExecutor.replayPendingActions());
|
|
||||||
});
|
|
||||||
|
|
||||||
test('fetches upcoming events after authenticated state', () async {
|
|
||||||
when(
|
|
||||||
() => calendarService.getEventsForRange(any(), any()),
|
|
||||||
).thenAnswer((_) async => []);
|
|
||||||
when(
|
|
||||||
() => notificationService.rebuildUpcomingReminders(any()),
|
|
||||||
).thenAnswer((_) async {});
|
|
||||||
when(
|
|
||||||
() => reminderActionExecutor.replayPendingActions(),
|
|
||||||
).thenAnswer((_) async {});
|
|
||||||
|
|
||||||
await bootstrapper.syncForAuthState(
|
|
||||||
const AuthAuthenticated(
|
|
||||||
user: AuthUser(id: 'u1', phone: 'a@test.com'),
|
|
||||||
),
|
|
||||||
);
|
|
||||||
|
|
||||||
verify(() => calendarService.getEventsForRange(any(), any())).called(1);
|
|
||||||
verify(() => notificationService.rebuildUpcomingReminders(any())).called(1);
|
|
||||||
verify(() => reminderActionExecutor.replayPendingActions()).called(1);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('retries sync when previous bootstrap failed', () async {
|
|
||||||
when(
|
|
||||||
() => reminderActionExecutor.replayPendingActions(),
|
|
||||||
).thenThrow(Exception('offline'));
|
|
||||||
|
|
||||||
await bootstrapper.syncForAuthState(
|
|
||||||
const AuthAuthenticated(
|
|
||||||
user: AuthUser(id: 'u1', phone: 'a@test.com'),
|
|
||||||
),
|
|
||||||
);
|
|
||||||
|
|
||||||
when(
|
|
||||||
() => reminderActionExecutor.replayPendingActions(),
|
|
||||||
).thenAnswer((_) async {});
|
|
||||||
when(
|
|
||||||
() => calendarService.getEventsForRange(any(), any()),
|
|
||||||
).thenAnswer((_) async => []);
|
|
||||||
when(
|
|
||||||
() => notificationService.rebuildUpcomingReminders(any()),
|
|
||||||
).thenAnswer((_) async {});
|
|
||||||
|
|
||||||
await bootstrapper.syncForAuthState(
|
|
||||||
const AuthAuthenticated(
|
|
||||||
user: AuthUser(id: 'u1', phone: 'a@test.com'),
|
|
||||||
),
|
|
||||||
);
|
|
||||||
|
|
||||||
verify(() => reminderActionExecutor.replayPendingActions()).called(2);
|
|
||||||
verify(() => calendarService.getEventsForRange(any(), any())).called(1);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
@@ -1,78 +0,0 @@
|
|||||||
import 'package:flutter_test/flutter_test.dart';
|
|
||||||
import 'package:shared_preferences/shared_preferences.dart';
|
|
||||||
import 'package:social_app/features/calendar/reminders/reminder_action_dedupe_store.dart';
|
|
||||||
|
|
||||||
void main() {
|
|
||||||
const dedupeKey = 'calendar_reminder_action_dedupe_v1';
|
|
||||||
|
|
||||||
setUp(() {
|
|
||||||
SharedPreferences.setMockInitialValues({});
|
|
||||||
});
|
|
||||||
|
|
||||||
test('markIfNew returns true first and false for duplicate id', () async {
|
|
||||||
final prefs = await SharedPreferences.getInstance();
|
|
||||||
final store = ReminderActionDedupeStore(prefs);
|
|
||||||
|
|
||||||
expect(await store.markIfNew('action_1'), isTrue);
|
|
||||||
expect(await store.markIfNew('action_1'), isFalse);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('markIfNew dedupes after store re-initialization', () async {
|
|
||||||
final firstPrefs = await SharedPreferences.getInstance();
|
|
||||||
final firstStore = ReminderActionDedupeStore(firstPrefs);
|
|
||||||
|
|
||||||
expect(await firstStore.markIfNew('action_restart'), isTrue);
|
|
||||||
|
|
||||||
final secondPrefs = await SharedPreferences.getInstance();
|
|
||||||
final secondStore = ReminderActionDedupeStore(secondPrefs);
|
|
||||||
|
|
||||||
expect(await secondStore.markIfNew('action_restart'), isFalse);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('markIfNew trims history to max capacity', () async {
|
|
||||||
final prefs = await SharedPreferences.getInstance();
|
|
||||||
final store = ReminderActionDedupeStore(prefs);
|
|
||||||
|
|
||||||
for (var i = 0; i < 513; i++) {
|
|
||||||
expect(await store.markIfNew('action_$i'), isTrue);
|
|
||||||
}
|
|
||||||
|
|
||||||
final stored = prefs.getStringList(dedupeKey)!;
|
|
||||||
expect(stored.length, 512);
|
|
||||||
expect(stored.first, 'action_1');
|
|
||||||
expect(stored.last, 'action_512');
|
|
||||||
});
|
|
||||||
|
|
||||||
test(
|
|
||||||
'markIfNew is serialized and does not return true twice in parallel',
|
|
||||||
() async {
|
|
||||||
final prefs = await SharedPreferences.getInstance();
|
|
||||||
final store = ReminderActionDedupeStore(
|
|
||||||
prefs,
|
|
||||||
setStringList: (key, value) async {
|
|
||||||
await Future<void>.delayed(const Duration(milliseconds: 20));
|
|
||||||
return prefs.setStringList(key, value);
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
final results = await Future.wait<bool>([
|
|
||||||
store.markIfNew('parallel_action'),
|
|
||||||
store.markIfNew('parallel_action'),
|
|
||||||
]);
|
|
||||||
|
|
||||||
expect(results.where((item) => item).length, 1);
|
|
||||||
expect(results.where((item) => !item).length, 1);
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
test('markIfNew returns false when persistence write fails', () async {
|
|
||||||
final prefs = await SharedPreferences.getInstance();
|
|
||||||
final store = ReminderActionDedupeStore(
|
|
||||||
prefs,
|
|
||||||
setStringList: (key, value) async => false,
|
|
||||||
);
|
|
||||||
|
|
||||||
expect(await store.markIfNew('action_write_fail'), isFalse);
|
|
||||||
expect(prefs.getStringList(dedupeKey), isNull);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
@@ -1,13 +1,11 @@
|
|||||||
import 'package:flutter_test/flutter_test.dart';
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
import 'package:mocktail/mocktail.dart';
|
import 'package:mocktail/mocktail.dart';
|
||||||
import 'package:shared_preferences/shared_preferences.dart';
|
|
||||||
import 'package:social_app/core/notifications/local_notification_service.dart';
|
import 'package:social_app/core/notifications/local_notification_service.dart';
|
||||||
import 'package:social_app/features/calendar/data/models/schedule_item_model.dart';
|
import 'package:social_app/features/calendar/data/models/schedule_item_model.dart';
|
||||||
import 'package:social_app/features/calendar/data/services/calendar_service.dart';
|
import 'package:social_app/features/calendar/data/services/calendar_service.dart';
|
||||||
import 'package:social_app/features/calendar/reminders/models/reminder_action.dart';
|
import 'package:social_app/features/calendar/reminders/models/reminder_action.dart';
|
||||||
import 'package:social_app/features/calendar/reminders/models/reminder_payload.dart';
|
import 'package:social_app/features/calendar/reminders/models/reminder_payload.dart';
|
||||||
import 'package:social_app/features/calendar/reminders/reminder_action_executor.dart';
|
import 'package:social_app/features/calendar/reminders/reminder_action_executor.dart';
|
||||||
import 'package:social_app/features/calendar/reminders/reminder_outbox_store.dart';
|
|
||||||
|
|
||||||
class MockCalendarService extends Mock implements CalendarService {}
|
class MockCalendarService extends Mock implements CalendarService {}
|
||||||
|
|
||||||
@@ -17,19 +15,14 @@ class MockLocalNotificationService extends Mock
|
|||||||
void main() {
|
void main() {
|
||||||
late MockCalendarService calendarService;
|
late MockCalendarService calendarService;
|
||||||
late MockLocalNotificationService notificationService;
|
late MockLocalNotificationService notificationService;
|
||||||
late ReminderOutboxStore outboxStore;
|
|
||||||
late ReminderActionExecutor executor;
|
late ReminderActionExecutor executor;
|
||||||
|
|
||||||
setUp(() async {
|
setUp(() {
|
||||||
SharedPreferences.setMockInitialValues({});
|
|
||||||
final prefs = await SharedPreferences.getInstance();
|
|
||||||
calendarService = MockCalendarService();
|
calendarService = MockCalendarService();
|
||||||
notificationService = MockLocalNotificationService();
|
notificationService = MockLocalNotificationService();
|
||||||
outboxStore = ReminderOutboxStore(prefs);
|
|
||||||
executor = ReminderActionExecutor(
|
executor = ReminderActionExecutor(
|
||||||
calendarService: calendarService,
|
calendarService: calendarService,
|
||||||
notificationService: notificationService,
|
notificationService: notificationService,
|
||||||
outboxStore: outboxStore,
|
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -53,33 +46,6 @@ void main() {
|
|||||||
|
|
||||||
verify(() => notificationService.cancelEventReminder('evt_1')).called(1);
|
verify(() => notificationService.cancelEventReminder('evt_1')).called(1);
|
||||||
verify(() => calendarService.archiveEvent('evt_1')).called(1);
|
verify(() => calendarService.archiveEvent('evt_1')).called(1);
|
||||||
final pending = await outboxStore.listPending();
|
|
||||||
expect(pending, isEmpty);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('archive failure writes pending outbox item', () async {
|
|
||||||
when(
|
|
||||||
() => notificationService.cancelEventReminder('evt_1'),
|
|
||||||
).thenAnswer((_) async {});
|
|
||||||
when(
|
|
||||||
() => calendarService.archiveEvent('evt_1'),
|
|
||||||
).thenThrow(Exception('offline'));
|
|
||||||
|
|
||||||
await executor.handleAction(
|
|
||||||
action: ReminderAction.archive,
|
|
||||||
payload: ReminderPayload(
|
|
||||||
eventId: 'evt_1',
|
|
||||||
title: 'sync',
|
|
||||||
startAt: DateTime.parse('2026-03-18T16:00:00+08:00'),
|
|
||||||
timezone: 'Asia/Shanghai',
|
|
||||||
),
|
|
||||||
);
|
|
||||||
|
|
||||||
final pending = await outboxStore.listPending();
|
|
||||||
expect(pending.length, 1);
|
|
||||||
expect(pending.first.eventId, 'evt_1');
|
|
||||||
expect(pending.first.state, ReminderOutboxState.pending);
|
|
||||||
verify(() => calendarService.archiveEvent('evt_1')).called(1);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test('snooze reschedules +10m when event not expired', () async {
|
test('snooze reschedules +10m when event not expired', () async {
|
||||||
@@ -151,24 +117,4 @@ void main() {
|
|||||||
verify(() => calendarService.archiveEvent('evt_fallback')).called(1);
|
verify(() => calendarService.archiveEvent('evt_fallback')).called(1);
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
test('replay keeps pending item when targetStatus is not archived', () async {
|
|
||||||
const opId = 'op_non_archived';
|
|
||||||
await outboxStore.enqueue(
|
|
||||||
ReminderOutboxItem(
|
|
||||||
opId: opId,
|
|
||||||
eventId: 'evt_1',
|
|
||||||
action: ReminderAction.archive,
|
|
||||||
targetStatus: 'ignored',
|
|
||||||
occurredAt: DateTime.parse('2026-03-18T16:00:00+08:00'),
|
|
||||||
),
|
|
||||||
);
|
|
||||||
|
|
||||||
await executor.replayPendingActions();
|
|
||||||
|
|
||||||
final pending = await outboxStore.listPending();
|
|
||||||
expect(pending.length, 1);
|
|
||||||
expect(pending.first.opId, opId);
|
|
||||||
verifyNever(() => calendarService.archiveEvent(any()));
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,41 +0,0 @@
|
|||||||
import 'package:flutter/material.dart';
|
|
||||||
import 'package:flutter_test/flutter_test.dart';
|
|
||||||
import 'package:social_app/features/calendar/reminders/ui/widgets/reminder_action_sheet.dart';
|
|
||||||
|
|
||||||
void main() {
|
|
||||||
Future<void> pumpSheet(
|
|
||||||
WidgetTester tester, {
|
|
||||||
required VoidCallback onSnooze,
|
|
||||||
required VoidCallback onArchive,
|
|
||||||
}) {
|
|
||||||
return tester.pumpWidget(
|
|
||||||
MaterialApp(
|
|
||||||
home: Scaffold(
|
|
||||||
body: ReminderActionSheet(onSnooze: onSnooze, onArchive: onArchive),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
testWidgets('tap snooze button triggers onSnooze', (tester) async {
|
|
||||||
var snoozed = false;
|
|
||||||
|
|
||||||
await pumpSheet(tester, onSnooze: () => snoozed = true, onArchive: () {});
|
|
||||||
|
|
||||||
await tester.tap(find.text('稍后提醒'));
|
|
||||||
await tester.pump();
|
|
||||||
|
|
||||||
expect(snoozed, isTrue);
|
|
||||||
});
|
|
||||||
|
|
||||||
testWidgets('tap archive button triggers onArchive', (tester) async {
|
|
||||||
var archived = false;
|
|
||||||
|
|
||||||
await pumpSheet(tester, onSnooze: () {}, onArchive: () => archived = true);
|
|
||||||
|
|
||||||
await tester.tap(find.text('归档'));
|
|
||||||
await tester.pump();
|
|
||||||
|
|
||||||
expect(archived, isTrue);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
@@ -1,123 +0,0 @@
|
|||||||
import 'dart:async';
|
|
||||||
|
|
||||||
import 'package:flutter_test/flutter_test.dart';
|
|
||||||
import 'package:social_app/features/calendar/reminders/reminder_cold_start_queue.dart';
|
|
||||||
|
|
||||||
void main() {
|
|
||||||
test('replays queued actions in enqueue order', () async {
|
|
||||||
final queue = ReminderColdStartQueue();
|
|
||||||
final events = <String>[];
|
|
||||||
|
|
||||||
queue.enqueue(() async {
|
|
||||||
await Future<void>.delayed(const Duration(milliseconds: 20));
|
|
||||||
events.add('first');
|
|
||||||
});
|
|
||||||
queue.enqueue(() async {
|
|
||||||
events.add('second');
|
|
||||||
});
|
|
||||||
queue.enqueue(() async {
|
|
||||||
events.add('third');
|
|
||||||
});
|
|
||||||
|
|
||||||
await queue.replay();
|
|
||||||
|
|
||||||
expect(events, <String>['first', 'second', 'third']);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('single failure does not block following queued actions', () async {
|
|
||||||
final queue = ReminderColdStartQueue();
|
|
||||||
final events = <String>[];
|
|
||||||
final errors = <Object>[];
|
|
||||||
|
|
||||||
queue.enqueue(() async {
|
|
||||||
events.add('before');
|
|
||||||
});
|
|
||||||
queue.enqueue(() async {
|
|
||||||
throw StateError('boom');
|
|
||||||
});
|
|
||||||
queue.enqueue(() async {
|
|
||||||
events.add('after');
|
|
||||||
});
|
|
||||||
|
|
||||||
final observableQueue = ReminderColdStartQueue(
|
|
||||||
onTaskError: (Object error, StackTrace _) {
|
|
||||||
errors.add(error);
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
observableQueue.enqueue(() async {
|
|
||||||
throw StateError('boom_observable');
|
|
||||||
});
|
|
||||||
|
|
||||||
await queue.replay();
|
|
||||||
await observableQueue.replay();
|
|
||||||
|
|
||||||
expect(events, <String>['before', 'after']);
|
|
||||||
expect(errors.length, 1);
|
|
||||||
expect(errors.first, isA<StateError>());
|
|
||||||
});
|
|
||||||
|
|
||||||
test('concurrent replay calls join the same in-flight replay', () async {
|
|
||||||
final queue = ReminderColdStartQueue();
|
|
||||||
final taskGate = Completer<void>();
|
|
||||||
var runCount = 0;
|
|
||||||
var secondReplayCompleted = false;
|
|
||||||
|
|
||||||
queue.enqueue(() async {
|
|
||||||
runCount += 1;
|
|
||||||
await taskGate.future;
|
|
||||||
});
|
|
||||||
|
|
||||||
final firstReplay = queue.replay();
|
|
||||||
final secondReplay = queue.replay().then((_) {
|
|
||||||
secondReplayCompleted = true;
|
|
||||||
});
|
|
||||||
|
|
||||||
await Future<void>.delayed(const Duration(milliseconds: 10));
|
|
||||||
expect(secondReplayCompleted, isFalse);
|
|
||||||
|
|
||||||
taskGate.complete();
|
|
||||||
await Future.wait(<Future<void>>[firstReplay, secondReplay]);
|
|
||||||
|
|
||||||
expect(runCount, 1);
|
|
||||||
expect(secondReplayCompleted, isTrue);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('replay on empty queue does not block future enqueued tasks', () async {
|
|
||||||
final queue = ReminderColdStartQueue();
|
|
||||||
final events = <String>[];
|
|
||||||
|
|
||||||
await queue.replay();
|
|
||||||
|
|
||||||
queue.enqueue(() async {
|
|
||||||
events.add('after_empty_replay');
|
|
||||||
});
|
|
||||||
|
|
||||||
await queue.replay();
|
|
||||||
|
|
||||||
expect(events, <String>['after_empty_replay']);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('task-triggered replay reuses in-flight replay and completes', () async {
|
|
||||||
final queue = ReminderColdStartQueue();
|
|
||||||
final events = <String>[];
|
|
||||||
var nestedReplayCompleted = false;
|
|
||||||
|
|
||||||
queue.enqueue(() async {
|
|
||||||
events.add('first');
|
|
||||||
queue.replay().then((_) {
|
|
||||||
nestedReplayCompleted = true;
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
queue.enqueue(() async {
|
|
||||||
events.add('second');
|
|
||||||
});
|
|
||||||
|
|
||||||
await queue.replay();
|
|
||||||
await Future<void>.delayed(Duration.zero);
|
|
||||||
|
|
||||||
expect(events, <String>['first', 'second']);
|
|
||||||
expect(nestedReplayCompleted, isTrue);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
@@ -1,146 +0,0 @@
|
|||||||
import 'dart:convert';
|
|
||||||
|
|
||||||
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
|
|
||||||
import 'package:flutter_test/flutter_test.dart';
|
|
||||||
import 'package:mocktail/mocktail.dart';
|
|
||||||
import 'package:shared_preferences/shared_preferences.dart';
|
|
||||||
import 'package:social_app/core/notifications/local_notification_service.dart';
|
|
||||||
import 'package:social_app/core/notifications/reminder_notification_callbacks.dart';
|
|
||||||
import 'package:social_app/features/calendar/reminders/models/reminder_action.dart';
|
|
||||||
import 'package:social_app/features/calendar/reminders/models/reminder_payload.dart';
|
|
||||||
|
|
||||||
class MockFlutterLocalNotificationsPlugin extends Mock
|
|
||||||
implements FlutterLocalNotificationsPlugin {}
|
|
||||||
|
|
||||||
void main() {
|
|
||||||
setUpAll(() {
|
|
||||||
registerFallbackValue(
|
|
||||||
const InitializationSettings(
|
|
||||||
android: AndroidInitializationSettings('@mipmap/ic_launcher'),
|
|
||||||
iOS: DarwinInitializationSettings(),
|
|
||||||
),
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
late MockFlutterLocalNotificationsPlugin plugin;
|
|
||||||
late LocalNotificationService service;
|
|
||||||
late List<ReminderAction> handledActions;
|
|
||||||
late List<ReminderPayload> presentedPayloads;
|
|
||||||
DidReceiveNotificationResponseCallback? callback;
|
|
||||||
|
|
||||||
setUp(() async {
|
|
||||||
SharedPreferences.setMockInitialValues({});
|
|
||||||
plugin = MockFlutterLocalNotificationsPlugin();
|
|
||||||
service = LocalNotificationService(plugin: plugin);
|
|
||||||
handledActions = <ReminderAction>[];
|
|
||||||
presentedPayloads = <ReminderPayload>[];
|
|
||||||
|
|
||||||
when(
|
|
||||||
() => plugin.initialize(
|
|
||||||
any(),
|
|
||||||
onDidReceiveNotificationResponse: any(
|
|
||||||
named: 'onDidReceiveNotificationResponse',
|
|
||||||
),
|
|
||||||
onDidReceiveBackgroundNotificationResponse: any(
|
|
||||||
named: 'onDidReceiveBackgroundNotificationResponse',
|
|
||||||
),
|
|
||||||
),
|
|
||||||
).thenAnswer((invocation) async {
|
|
||||||
callback =
|
|
||||||
invocation.namedArguments[#onDidReceiveNotificationResponse]
|
|
||||||
as DidReceiveNotificationResponseCallback?;
|
|
||||||
return true;
|
|
||||||
});
|
|
||||||
|
|
||||||
when(
|
|
||||||
() => plugin
|
|
||||||
.resolvePlatformSpecificImplementation<
|
|
||||||
AndroidFlutterLocalNotificationsPlugin
|
|
||||||
>(),
|
|
||||||
).thenReturn(null);
|
|
||||||
when(
|
|
||||||
() => plugin
|
|
||||||
.resolvePlatformSpecificImplementation<
|
|
||||||
IOSFlutterLocalNotificationsPlugin
|
|
||||||
>(),
|
|
||||||
).thenReturn(null);
|
|
||||||
|
|
||||||
service.bindActionHandler(({required action, required payload}) async {
|
|
||||||
handledActions.add(action);
|
|
||||||
});
|
|
||||||
service.bindInAppReminderHandler((payload) async {
|
|
||||||
presentedPayloads.add(payload);
|
|
||||||
});
|
|
||||||
await ReminderNotificationCallbacks.bindResponseHandler(
|
|
||||||
service.handleNotificationResponse,
|
|
||||||
);
|
|
||||||
|
|
||||||
await service.initialize();
|
|
||||||
});
|
|
||||||
|
|
||||||
test('cancel action from system notification maps to archive', () async {
|
|
||||||
callback!(
|
|
||||||
NotificationResponse(
|
|
||||||
notificationResponseType:
|
|
||||||
NotificationResponseType.selectedNotificationAction,
|
|
||||||
id: 101,
|
|
||||||
actionId: 'cancel',
|
|
||||||
payload: jsonEncode(
|
|
||||||
ReminderPayload(
|
|
||||||
eventId: 'evt_1',
|
|
||||||
title: 'sync',
|
|
||||||
startAt: DateTime.parse('2026-03-19T10:00:00+08:00'),
|
|
||||||
timezone: 'Asia/Shanghai',
|
|
||||||
).toJson(),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
);
|
|
||||||
await Future<void>.delayed(Duration.zero);
|
|
||||||
|
|
||||||
expect(handledActions, [ReminderAction.archive]);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('duplicate notification response is handled only once', () async {
|
|
||||||
final response = NotificationResponse(
|
|
||||||
notificationResponseType:
|
|
||||||
NotificationResponseType.selectedNotificationAction,
|
|
||||||
id: 201,
|
|
||||||
actionId: 'cancel',
|
|
||||||
payload: jsonEncode(
|
|
||||||
ReminderPayload(
|
|
||||||
eventId: 'evt_2',
|
|
||||||
title: 'retro',
|
|
||||||
startAt: DateTime.parse('2026-03-19T11:00:00+08:00'),
|
|
||||||
timezone: 'Asia/Shanghai',
|
|
||||||
).toJson(),
|
|
||||||
),
|
|
||||||
);
|
|
||||||
|
|
||||||
callback!(response);
|
|
||||||
callback!(response);
|
|
||||||
await Future<void>.delayed(Duration.zero);
|
|
||||||
|
|
||||||
expect(handledActions, [ReminderAction.archive]);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('notification body tap forwards payload to in-app presenter', () async {
|
|
||||||
callback!(
|
|
||||||
NotificationResponse(
|
|
||||||
notificationResponseType: NotificationResponseType.selectedNotification,
|
|
||||||
id: 301,
|
|
||||||
payload: jsonEncode(
|
|
||||||
ReminderPayload(
|
|
||||||
eventId: 'evt_3',
|
|
||||||
title: 'daily sync',
|
|
||||||
startAt: DateTime.parse('2026-03-19T12:00:00+08:00'),
|
|
||||||
timezone: 'Asia/Shanghai',
|
|
||||||
).toJson(),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
);
|
|
||||||
await Future<void>.delayed(Duration.zero);
|
|
||||||
|
|
||||||
expect(presentedPayloads.map((item) => item.eventId), ['evt_3']);
|
|
||||||
expect(handledActions, isEmpty);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
@@ -1,48 +0,0 @@
|
|||||||
import 'package:flutter_test/flutter_test.dart';
|
|
||||||
import 'package:social_app/features/calendar/data/models/schedule_item_model.dart';
|
|
||||||
import 'package:social_app/features/calendar/reminders/reminder_overlap_policy.dart';
|
|
||||||
|
|
||||||
void main() {
|
|
||||||
final policy = ReminderOverlapPolicy();
|
|
||||||
|
|
||||||
test('groups reminders in same minute bucket', () {
|
|
||||||
final now = DateTime(2026, 3, 18, 15, 40, 0);
|
|
||||||
final eventA = ScheduleItemModel(
|
|
||||||
id: 'a',
|
|
||||||
ownerId: 'u1',
|
|
||||||
title: 'A',
|
|
||||||
startAt: DateTime(2026, 3, 18, 16, 0, 0),
|
|
||||||
endAt: DateTime(2026, 3, 18, 17, 0, 0),
|
|
||||||
metadata: ScheduleMetadata(reminderMinutes: 15),
|
|
||||||
);
|
|
||||||
final eventB = ScheduleItemModel(
|
|
||||||
id: 'b',
|
|
||||||
ownerId: 'u1',
|
|
||||||
title: 'B',
|
|
||||||
startAt: DateTime(2026, 3, 18, 16, 0, 20),
|
|
||||||
endAt: DateTime(2026, 3, 18, 17, 0, 0),
|
|
||||||
metadata: ScheduleMetadata(reminderMinutes: 15),
|
|
||||||
);
|
|
||||||
|
|
||||||
final groups = policy.groupByMinute([eventA, eventB], now: now);
|
|
||||||
expect(groups.length, 1);
|
|
||||||
expect(groups.first.events.length, 2);
|
|
||||||
expect(groups.first.isAggregate, isTrue);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('returns compensation fire time when remindAt already passed', () {
|
|
||||||
final now = DateTime(2026, 3, 18, 15, 50, 0);
|
|
||||||
final event = ScheduleItemModel(
|
|
||||||
id: 'a',
|
|
||||||
ownerId: 'u1',
|
|
||||||
title: 'A',
|
|
||||||
startAt: DateTime(2026, 3, 18, 16, 0, 0),
|
|
||||||
endAt: DateTime(2026, 3, 18, 16, 30, 0),
|
|
||||||
metadata: ScheduleMetadata(reminderMinutes: 15),
|
|
||||||
);
|
|
||||||
|
|
||||||
final fireAt = policy.resolveFirstFireAt(event, now: now);
|
|
||||||
expect(fireAt, isNotNull);
|
|
||||||
expect(fireAt!.isAfter(now), isTrue);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
@@ -1,392 +0,0 @@
|
|||||||
import 'package:flutter/foundation.dart';
|
|
||||||
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
|
|
||||||
import 'package:flutter_test/flutter_test.dart';
|
|
||||||
import 'package:mocktail/mocktail.dart';
|
|
||||||
import 'package:shared_preferences/shared_preferences.dart';
|
|
||||||
import 'package:social_app/core/notifications/local_notification_service.dart';
|
|
||||||
import 'package:social_app/features/calendar/data/models/schedule_item_model.dart';
|
|
||||||
import 'package:timezone/data/latest.dart' as tz_data;
|
|
||||||
import 'package:timezone/timezone.dart' as tz;
|
|
||||||
|
|
||||||
class MockFlutterLocalNotificationsPlugin extends Mock
|
|
||||||
implements FlutterLocalNotificationsPlugin {}
|
|
||||||
|
|
||||||
class MockAndroidFlutterLocalNotificationsPlugin extends Mock
|
|
||||||
implements AndroidFlutterLocalNotificationsPlugin {}
|
|
||||||
|
|
||||||
void main() {
|
|
||||||
TestWidgetsFlutterBinding.ensureInitialized();
|
|
||||||
|
|
||||||
setUpAll(() {
|
|
||||||
tz_data.initializeTimeZones();
|
|
||||||
registerFallbackValue(tz.TZDateTime.now(tz.local));
|
|
||||||
registerFallbackValue(const NotificationDetails());
|
|
||||||
registerFallbackValue(
|
|
||||||
const InitializationSettings(
|
|
||||||
android: AndroidInitializationSettings('@mipmap/ic_launcher'),
|
|
||||||
iOS: DarwinInitializationSettings(),
|
|
||||||
),
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
setUp(() {
|
|
||||||
SharedPreferences.setMockInitialValues({});
|
|
||||||
debugDefaultTargetPlatformOverride = TargetPlatform.android;
|
|
||||||
});
|
|
||||||
|
|
||||||
tearDown(() {
|
|
||||||
debugDefaultTargetPlatformOverride = null;
|
|
||||||
});
|
|
||||||
|
|
||||||
test(
|
|
||||||
'tracks fallback when Android notifications permission is denied',
|
|
||||||
() async {
|
|
||||||
final plugin = MockFlutterLocalNotificationsPlugin();
|
|
||||||
final androidImpl = MockAndroidFlutterLocalNotificationsPlugin();
|
|
||||||
final fallbackEvents = <Map<String, String>>[];
|
|
||||||
|
|
||||||
when(
|
|
||||||
() => plugin.initialize(
|
|
||||||
any(),
|
|
||||||
onDidReceiveNotificationResponse: any(
|
|
||||||
named: 'onDidReceiveNotificationResponse',
|
|
||||||
),
|
|
||||||
onDidReceiveBackgroundNotificationResponse: any(
|
|
||||||
named: 'onDidReceiveBackgroundNotificationResponse',
|
|
||||||
),
|
|
||||||
),
|
|
||||||
).thenAnswer((_) async => true);
|
|
||||||
when(
|
|
||||||
() => plugin
|
|
||||||
.resolvePlatformSpecificImplementation<
|
|
||||||
AndroidFlutterLocalNotificationsPlugin
|
|
||||||
>(),
|
|
||||||
).thenReturn(androidImpl);
|
|
||||||
when(
|
|
||||||
() => plugin
|
|
||||||
.resolvePlatformSpecificImplementation<
|
|
||||||
IOSFlutterLocalNotificationsPlugin
|
|
||||||
>(),
|
|
||||||
).thenReturn(null);
|
|
||||||
|
|
||||||
when(
|
|
||||||
() => androidImpl.requestNotificationsPermission(),
|
|
||||||
).thenAnswer((_) async => false);
|
|
||||||
when(
|
|
||||||
() => androidImpl.requestExactAlarmsPermission(),
|
|
||||||
).thenAnswer((_) async => true);
|
|
||||||
when(
|
|
||||||
() => androidImpl.requestFullScreenIntentPermission(),
|
|
||||||
).thenAnswer((_) async => true);
|
|
||||||
when(
|
|
||||||
() => androidImpl.areNotificationsEnabled(),
|
|
||||||
).thenAnswer((_) async => false);
|
|
||||||
|
|
||||||
final service = LocalNotificationService(
|
|
||||||
plugin: plugin,
|
|
||||||
permissionFallbackTracker:
|
|
||||||
({
|
|
||||||
required actionExecutionId,
|
|
||||||
required permissionState,
|
|
||||||
required appLifecycleState,
|
|
||||||
required platform,
|
|
||||||
}) {
|
|
||||||
fallbackEvents.add({
|
|
||||||
'actionExecutionId': actionExecutionId,
|
|
||||||
'permissionState': permissionState,
|
|
||||||
'appLifecycleState': appLifecycleState,
|
|
||||||
'platform': platform,
|
|
||||||
});
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
await service.initialize();
|
|
||||||
|
|
||||||
expect(fallbackEvents.length, 1);
|
|
||||||
expect(fallbackEvents.first['permissionState'], 'denied');
|
|
||||||
expect(fallbackEvents.first['platform'], 'android');
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
test(
|
|
||||||
'skips reminder scheduling when Android notifications are denied',
|
|
||||||
() async {
|
|
||||||
final plugin = MockFlutterLocalNotificationsPlugin();
|
|
||||||
final androidImpl = MockAndroidFlutterLocalNotificationsPlugin();
|
|
||||||
|
|
||||||
when(
|
|
||||||
() => plugin.initialize(
|
|
||||||
any(),
|
|
||||||
onDidReceiveNotificationResponse: any(
|
|
||||||
named: 'onDidReceiveNotificationResponse',
|
|
||||||
),
|
|
||||||
onDidReceiveBackgroundNotificationResponse: any(
|
|
||||||
named: 'onDidReceiveBackgroundNotificationResponse',
|
|
||||||
),
|
|
||||||
),
|
|
||||||
).thenAnswer((_) async => true);
|
|
||||||
when(
|
|
||||||
() => plugin
|
|
||||||
.resolvePlatformSpecificImplementation<
|
|
||||||
AndroidFlutterLocalNotificationsPlugin
|
|
||||||
>(),
|
|
||||||
).thenReturn(androidImpl);
|
|
||||||
when(
|
|
||||||
() => plugin
|
|
||||||
.resolvePlatformSpecificImplementation<
|
|
||||||
IOSFlutterLocalNotificationsPlugin
|
|
||||||
>(),
|
|
||||||
).thenReturn(null);
|
|
||||||
|
|
||||||
when(
|
|
||||||
() => androidImpl.requestNotificationsPermission(),
|
|
||||||
).thenAnswer((_) async => false);
|
|
||||||
when(
|
|
||||||
() => androidImpl.requestExactAlarmsPermission(),
|
|
||||||
).thenAnswer((_) async => true);
|
|
||||||
when(
|
|
||||||
() => androidImpl.requestFullScreenIntentPermission(),
|
|
||||||
).thenAnswer((_) async => true);
|
|
||||||
when(
|
|
||||||
() => androidImpl.areNotificationsEnabled(),
|
|
||||||
).thenAnswer((_) async => false);
|
|
||||||
|
|
||||||
final service = LocalNotificationService(plugin: plugin);
|
|
||||||
final event = ScheduleItemModel(
|
|
||||||
id: 'evt_1',
|
|
||||||
ownerId: 'u1',
|
|
||||||
title: 'sync',
|
|
||||||
startAt: DateTime.now().add(const Duration(minutes: 20)),
|
|
||||||
endAt: DateTime.now().add(const Duration(minutes: 50)),
|
|
||||||
metadata: ScheduleMetadata(reminderMinutes: 15),
|
|
||||||
);
|
|
||||||
|
|
||||||
await service.upsertEventReminder(event);
|
|
||||||
|
|
||||||
verifyNever(() => plugin.pendingNotificationRequests());
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
test(
|
|
||||||
'dispatches in-app reminder callback when notifications are denied',
|
|
||||||
() async {
|
|
||||||
final plugin = MockFlutterLocalNotificationsPlugin();
|
|
||||||
final androidImpl = MockAndroidFlutterLocalNotificationsPlugin();
|
|
||||||
final presentedEventIds = <String>[];
|
|
||||||
|
|
||||||
when(
|
|
||||||
() => plugin.initialize(
|
|
||||||
any(),
|
|
||||||
onDidReceiveNotificationResponse: any(
|
|
||||||
named: 'onDidReceiveNotificationResponse',
|
|
||||||
),
|
|
||||||
onDidReceiveBackgroundNotificationResponse: any(
|
|
||||||
named: 'onDidReceiveBackgroundNotificationResponse',
|
|
||||||
),
|
|
||||||
),
|
|
||||||
).thenAnswer((_) async => true);
|
|
||||||
when(
|
|
||||||
() => plugin
|
|
||||||
.resolvePlatformSpecificImplementation<
|
|
||||||
AndroidFlutterLocalNotificationsPlugin
|
|
||||||
>(),
|
|
||||||
).thenReturn(androidImpl);
|
|
||||||
when(
|
|
||||||
() => plugin
|
|
||||||
.resolvePlatformSpecificImplementation<
|
|
||||||
IOSFlutterLocalNotificationsPlugin
|
|
||||||
>(),
|
|
||||||
).thenReturn(null);
|
|
||||||
|
|
||||||
when(
|
|
||||||
() => androidImpl.requestNotificationsPermission(),
|
|
||||||
).thenAnswer((_) async => false);
|
|
||||||
when(
|
|
||||||
() => androidImpl.requestExactAlarmsPermission(),
|
|
||||||
).thenAnswer((_) async => true);
|
|
||||||
when(
|
|
||||||
() => androidImpl.requestFullScreenIntentPermission(),
|
|
||||||
).thenAnswer((_) async => true);
|
|
||||||
when(
|
|
||||||
() => androidImpl.areNotificationsEnabled(),
|
|
||||||
).thenAnswer((_) async => false);
|
|
||||||
|
|
||||||
final service = LocalNotificationService(plugin: plugin);
|
|
||||||
service.bindInAppReminderHandler((payload) async {
|
|
||||||
presentedEventIds.add(payload.eventId);
|
|
||||||
});
|
|
||||||
|
|
||||||
final event = ScheduleItemModel(
|
|
||||||
id: 'evt_2',
|
|
||||||
ownerId: 'u1',
|
|
||||||
title: 'retro',
|
|
||||||
startAt: DateTime.now().add(const Duration(minutes: 20)),
|
|
||||||
endAt: DateTime.now().add(const Duration(minutes: 50)),
|
|
||||||
metadata: ScheduleMetadata(reminderMinutes: 15),
|
|
||||||
);
|
|
||||||
|
|
||||||
await service.scheduleReminderAt(
|
|
||||||
event,
|
|
||||||
DateTime.now().add(const Duration(milliseconds: 20)),
|
|
||||||
);
|
|
||||||
await Future<void>.delayed(const Duration(milliseconds: 100));
|
|
||||||
|
|
||||||
expect(presentedEventIds, contains('evt_2'));
|
|
||||||
verifyNever(
|
|
||||||
() => plugin.zonedSchedule(
|
|
||||||
any(),
|
|
||||||
any(),
|
|
||||||
any(),
|
|
||||||
any(),
|
|
||||||
any(),
|
|
||||||
payload: any(named: 'payload'),
|
|
||||||
androidScheduleMode: any(named: 'androidScheduleMode'),
|
|
||||||
uiLocalNotificationDateInterpretation:
|
|
||||||
UILocalNotificationDateInterpretation.absoluteTime,
|
|
||||||
),
|
|
||||||
);
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
test('rebuild twice only dispatches one aggregate in-app fallback', () async {
|
|
||||||
final plugin = MockFlutterLocalNotificationsPlugin();
|
|
||||||
final androidImpl = MockAndroidFlutterLocalNotificationsPlugin();
|
|
||||||
final presentedPayloads = <String>[];
|
|
||||||
|
|
||||||
when(
|
|
||||||
() => plugin.initialize(
|
|
||||||
any(),
|
|
||||||
onDidReceiveNotificationResponse: any(
|
|
||||||
named: 'onDidReceiveNotificationResponse',
|
|
||||||
),
|
|
||||||
onDidReceiveBackgroundNotificationResponse: any(
|
|
||||||
named: 'onDidReceiveBackgroundNotificationResponse',
|
|
||||||
),
|
|
||||||
),
|
|
||||||
).thenAnswer((_) async => true);
|
|
||||||
when(
|
|
||||||
() => plugin
|
|
||||||
.resolvePlatformSpecificImplementation<
|
|
||||||
AndroidFlutterLocalNotificationsPlugin
|
|
||||||
>(),
|
|
||||||
).thenReturn(androidImpl);
|
|
||||||
when(
|
|
||||||
() => plugin
|
|
||||||
.resolvePlatformSpecificImplementation<
|
|
||||||
IOSFlutterLocalNotificationsPlugin
|
|
||||||
>(),
|
|
||||||
).thenReturn(null);
|
|
||||||
|
|
||||||
when(
|
|
||||||
() => androidImpl.requestNotificationsPermission(),
|
|
||||||
).thenAnswer((_) async => false);
|
|
||||||
when(
|
|
||||||
() => androidImpl.requestExactAlarmsPermission(),
|
|
||||||
).thenAnswer((_) async => true);
|
|
||||||
when(
|
|
||||||
() => androidImpl.requestFullScreenIntentPermission(),
|
|
||||||
).thenAnswer((_) async => true);
|
|
||||||
when(
|
|
||||||
() => androidImpl.areNotificationsEnabled(),
|
|
||||||
).thenAnswer((_) async => false);
|
|
||||||
|
|
||||||
final service = LocalNotificationService(plugin: plugin);
|
|
||||||
service.bindInAppReminderHandler((payload) async {
|
|
||||||
presentedPayloads.add(payload.title);
|
|
||||||
});
|
|
||||||
|
|
||||||
final startAt = DateTime.now().add(const Duration(milliseconds: 50));
|
|
||||||
final event1 = ScheduleItemModel(
|
|
||||||
id: 'evt_a',
|
|
||||||
ownerId: 'u1',
|
|
||||||
title: 'evt_a',
|
|
||||||
startAt: startAt,
|
|
||||||
endAt: startAt.add(const Duration(minutes: 30)),
|
|
||||||
metadata: ScheduleMetadata(reminderMinutes: 0),
|
|
||||||
);
|
|
||||||
final event2 = ScheduleItemModel(
|
|
||||||
id: 'evt_b',
|
|
||||||
ownerId: 'u1',
|
|
||||||
title: 'evt_b',
|
|
||||||
startAt: startAt,
|
|
||||||
endAt: startAt.add(const Duration(minutes: 30)),
|
|
||||||
metadata: ScheduleMetadata(reminderMinutes: 0),
|
|
||||||
);
|
|
||||||
|
|
||||||
await service.rebuildUpcomingReminders([event1, event2]);
|
|
||||||
await service.rebuildUpcomingReminders([event1, event2]);
|
|
||||||
await Future<void>.delayed(const Duration(milliseconds: 180));
|
|
||||||
|
|
||||||
expect(
|
|
||||||
presentedPayloads.where((title) => title.contains('你有2个日程提醒')).length,
|
|
||||||
1,
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
test(
|
|
||||||
'rebuild clears stale in-app fallback timers for removed events',
|
|
||||||
() async {
|
|
||||||
final plugin = MockFlutterLocalNotificationsPlugin();
|
|
||||||
final androidImpl = MockAndroidFlutterLocalNotificationsPlugin();
|
|
||||||
final presentedEventIds = <String>[];
|
|
||||||
|
|
||||||
when(
|
|
||||||
() => plugin.initialize(
|
|
||||||
any(),
|
|
||||||
onDidReceiveNotificationResponse: any(
|
|
||||||
named: 'onDidReceiveNotificationResponse',
|
|
||||||
),
|
|
||||||
onDidReceiveBackgroundNotificationResponse: any(
|
|
||||||
named: 'onDidReceiveBackgroundNotificationResponse',
|
|
||||||
),
|
|
||||||
),
|
|
||||||
).thenAnswer((_) async => true);
|
|
||||||
when(
|
|
||||||
() => plugin
|
|
||||||
.resolvePlatformSpecificImplementation<
|
|
||||||
AndroidFlutterLocalNotificationsPlugin
|
|
||||||
>(),
|
|
||||||
).thenReturn(androidImpl);
|
|
||||||
when(
|
|
||||||
() => plugin
|
|
||||||
.resolvePlatformSpecificImplementation<
|
|
||||||
IOSFlutterLocalNotificationsPlugin
|
|
||||||
>(),
|
|
||||||
).thenReturn(null);
|
|
||||||
|
|
||||||
when(
|
|
||||||
() => androidImpl.requestNotificationsPermission(),
|
|
||||||
).thenAnswer((_) async => false);
|
|
||||||
when(
|
|
||||||
() => androidImpl.requestExactAlarmsPermission(),
|
|
||||||
).thenAnswer((_) async => true);
|
|
||||||
when(
|
|
||||||
() => androidImpl.requestFullScreenIntentPermission(),
|
|
||||||
).thenAnswer((_) async => true);
|
|
||||||
when(
|
|
||||||
() => androidImpl.areNotificationsEnabled(),
|
|
||||||
).thenAnswer((_) async => false);
|
|
||||||
|
|
||||||
final service = LocalNotificationService(plugin: plugin);
|
|
||||||
service.bindInAppReminderHandler((payload) async {
|
|
||||||
presentedEventIds.add(payload.eventId);
|
|
||||||
});
|
|
||||||
|
|
||||||
final startAt = DateTime.now().add(const Duration(milliseconds: 80));
|
|
||||||
final staleEvent = ScheduleItemModel(
|
|
||||||
id: 'evt_stale',
|
|
||||||
ownerId: 'u1',
|
|
||||||
title: 'evt_stale',
|
|
||||||
startAt: startAt,
|
|
||||||
endAt: startAt.add(const Duration(minutes: 20)),
|
|
||||||
metadata: ScheduleMetadata(reminderMinutes: 0),
|
|
||||||
);
|
|
||||||
|
|
||||||
await service.rebuildUpcomingReminders([staleEvent]);
|
|
||||||
await service.rebuildUpcomingReminders(const <ScheduleItemModel>[]);
|
|
||||||
await Future<void>.delayed(const Duration(milliseconds: 220));
|
|
||||||
|
|
||||||
expect(presentedEventIds, isNot(contains('evt_stale')));
|
|
||||||
},
|
|
||||||
);
|
|
||||||
}
|
|
||||||
@@ -1,60 +0,0 @@
|
|||||||
import 'package:flutter_test/flutter_test.dart';
|
|
||||||
import 'package:social_app/features/calendar/reminders/ui/reminder_presentation_coordinator.dart';
|
|
||||||
|
|
||||||
void main() {
|
|
||||||
test('blocks foreground presentation when app is not active', () {
|
|
||||||
final coordinator = ReminderPresentationCoordinator();
|
|
||||||
|
|
||||||
final hiddenDecision = coordinator.shouldPresent(
|
|
||||||
eventId: 'event_1',
|
|
||||||
isAppActive: false,
|
|
||||||
);
|
|
||||||
final activeDecision = coordinator.shouldPresent(
|
|
||||||
eventId: 'event_1',
|
|
||||||
isAppActive: true,
|
|
||||||
);
|
|
||||||
|
|
||||||
expect(hiddenDecision, isFalse);
|
|
||||||
expect(activeDecision, isTrue);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('suppresses duplicate foreground presentation inside dedupe window', () {
|
|
||||||
var fakeNow = DateTime(2026, 3, 19, 10, 0, 0);
|
|
||||||
final coordinator = ReminderPresentationCoordinator(
|
|
||||||
dedupeWindow: const Duration(seconds: 30),
|
|
||||||
now: () => fakeNow,
|
|
||||||
);
|
|
||||||
|
|
||||||
final first = coordinator.shouldPresent(
|
|
||||||
eventId: 'event_42',
|
|
||||||
isAppActive: true,
|
|
||||||
);
|
|
||||||
fakeNow = fakeNow.add(const Duration(seconds: 10));
|
|
||||||
final second = coordinator.shouldPresent(
|
|
||||||
eventId: 'event_42',
|
|
||||||
isAppActive: true,
|
|
||||||
);
|
|
||||||
|
|
||||||
expect(first, isTrue);
|
|
||||||
expect(second, isFalse);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('allows same event again after dedupe window expires', () {
|
|
||||||
var fakeNow = DateTime(2026, 3, 19, 10, 0, 0);
|
|
||||||
final coordinator = ReminderPresentationCoordinator(
|
|
||||||
dedupeWindow: const Duration(seconds: 30),
|
|
||||||
now: () => fakeNow,
|
|
||||||
);
|
|
||||||
|
|
||||||
expect(
|
|
||||||
coordinator.shouldPresent(eventId: 'event_42', isAppActive: true),
|
|
||||||
isTrue,
|
|
||||||
);
|
|
||||||
fakeNow = fakeNow.add(const Duration(seconds: 31));
|
|
||||||
|
|
||||||
expect(
|
|
||||||
coordinator.shouldPresent(eventId: 'event_42', isAppActive: true),
|
|
||||||
isTrue,
|
|
||||||
);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user