refactor(apps): 重构数据层目录结构并新增启动预热编排器
This commit is contained in:
+6
-65
@@ -1,21 +1,15 @@
|
||||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
import 'di/injection.dart';
|
||||
import '../data/models/reminder_payload.dart';
|
||||
import '../data/services/calendar_service.dart';
|
||||
import '../data/services/local_notification_service.dart';
|
||||
import '../data/services/reminder_notification_callbacks.dart';
|
||||
import '../core/l10n/l10n.dart';
|
||||
import '../l10n/app_localizations.dart';
|
||||
import '../features/auth/presentation/bloc/auth_bloc.dart';
|
||||
import '../features/auth/presentation/bloc/auth_event.dart';
|
||||
import '../features/auth/presentation/bloc/auth_state.dart';
|
||||
import '../features/notification/domain/models/reminder_action.dart';
|
||||
import '../features/notification/domain/services/reminder_action_executor.dart';
|
||||
import 'services/app_prewarm_orchestrator.dart';
|
||||
import 'router/app_router.dart';
|
||||
import '../core/theme/app_theme.dart';
|
||||
|
||||
@@ -29,7 +23,6 @@ class LinksyApp extends StatefulWidget {
|
||||
class _LinksyAppState extends State<LinksyApp> {
|
||||
late final AuthBloc _authBloc;
|
||||
late final GoRouter _router;
|
||||
String? _reminderBootstrapUserId;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
@@ -37,7 +30,6 @@ class _LinksyAppState extends State<LinksyApp> {
|
||||
_authBloc = sl<AuthBloc>();
|
||||
_authBloc.add(AuthStarted());
|
||||
_router = createAppRouter(_authBloc);
|
||||
unawaited(_bindNotificationResponseHandler());
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -52,13 +44,13 @@ class _LinksyAppState extends State<LinksyApp> {
|
||||
value: _authBloc,
|
||||
child: BlocListener<AuthBloc, AuthState>(
|
||||
listener: (context, state) {
|
||||
if (state is AuthAuthenticated &&
|
||||
state.user.id != _reminderBootstrapUserId) {
|
||||
_reminderBootstrapUserId = state.user.id;
|
||||
unawaited(_rebuildUpcomingReminders());
|
||||
if (state is AuthAuthenticated) {
|
||||
unawaited(
|
||||
sl<AppPrewarmOrchestrator>().ensureStartedFor(state.user.id),
|
||||
);
|
||||
}
|
||||
if (state is AuthUnauthenticated) {
|
||||
_reminderBootstrapUserId = null;
|
||||
sl<AppPrewarmOrchestrator>().reset();
|
||||
}
|
||||
},
|
||||
child: MaterialApp.router(
|
||||
@@ -79,55 +71,4 @@ class _LinksyAppState extends State<LinksyApp> {
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _rebuildUpcomingReminders() async {
|
||||
final now = DateTime.now();
|
||||
final start = now.subtract(const Duration(days: 90));
|
||||
final end = now.add(const Duration(days: 90));
|
||||
try {
|
||||
final events = await sl<CalendarService>().getEventsForRange(start, end);
|
||||
await sl<LocalNotificationService>().rebuildUpcomingReminders(events);
|
||||
} catch (error) {
|
||||
debugPrint('reminder bootstrap skipped: $error');
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _bindNotificationResponseHandler() async {
|
||||
await ReminderNotificationCallbacks.bindResponseHandler((response) async {
|
||||
final payloadRaw = response.payload;
|
||||
if (payloadRaw == null || payloadRaw.isEmpty) {
|
||||
return;
|
||||
}
|
||||
|
||||
ReminderPayload payload;
|
||||
try {
|
||||
payload = ReminderPayload.fromJson(
|
||||
Map<String, dynamic>.from(jsonDecode(payloadRaw) as Map),
|
||||
);
|
||||
} catch (_) {
|
||||
return;
|
||||
}
|
||||
|
||||
final actionId = response.actionId;
|
||||
ReminderAction? action;
|
||||
if (actionId != null) {
|
||||
try {
|
||||
action = ReminderAction.fromValue(actionId);
|
||||
} catch (_) {
|
||||
action = null;
|
||||
}
|
||||
}
|
||||
if (action == null) {
|
||||
ReminderNotificationCallbacks.onNotificationPayloadReceived?.call(
|
||||
payload,
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
await sl<ReminderActionExecutor>().handleAction(
|
||||
action: action,
|
||||
payload: payload,
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user