feat: 重构会话管理与提醒通知系统

This commit is contained in:
qzl
2026-03-31 18:26:36 +08:00
parent a8c262e9c7
commit 9a231dae9e
31 changed files with 650 additions and 223 deletions
+35 -21
View File
@@ -13,6 +13,7 @@ import '../features/auth/presentation/bloc/auth_state.dart';
import '../features/chat/presentation/bloc/chat_bloc.dart';
import '../data/cache/cache_scope.dart';
import 'services/app_prewarm_orchestrator.dart';
import 'services/session_scope_manager.dart';
import 'router/app_router.dart';
import '../core/theme/app_theme.dart';
import '../core/inbox/inbox_sync_store.dart';
@@ -33,21 +34,16 @@ class _LinksyAppState extends State<LinksyApp> {
late final GoRouter _router;
StreamSubscription<ReminderNotificationTap>? _reminderTapSubscription;
String? _pendingReminderRoute;
int _cacheScopeVersion = 0;
Future<void> _authTransitionQueue = Future<void>.value();
Future<void> _onAuthenticated(String userId) async {
_cacheScopeVersion += 1;
final scopeKey = 'user:$userId:v$_cacheScopeVersion';
CacheScope.configureProvider(() => scopeKey);
await sl<SessionScopeManager>().activate(userId);
await sl<InboxSyncStore>().resetForUser(userId);
await sl<ChatBloc>().switchUser(userId);
await sl<AppPrewarmOrchestrator>().ensureStartedFor(userId);
}
Future<void> _onUnauthenticated() async {
_cacheScopeVersion += 1;
final scopeKey = 'anonymous:v$_cacheScopeVersion';
CacheScope.configureProvider(() => scopeKey);
await sl<SessionScopeManager>().clearActiveUserScope();
await sl<InboxSyncStore>().resetForUser(null);
await sl<ChatBloc>().switchUser(null);
sl<AppPrewarmOrchestrator>().reset();
@@ -57,8 +53,7 @@ class _LinksyAppState extends State<LinksyApp> {
void initState() {
super.initState();
_authBloc = sl<AuthBloc>();
const initialScopeKey = 'anonymous:v0';
CacheScope.configureProvider(() => initialScopeKey);
CacheScope.resetProvider();
_authBloc.add(AuthStarted());
_router = createAppRouter(_authBloc);
SchedulerBinding.instance.addPostFrameCallback((_) {
@@ -75,11 +70,18 @@ class _LinksyAppState extends State<LinksyApp> {
void _onReminderTap(ReminderNotificationTap tap) {
final route = AppRoutes.calendarReminderAlarm(tap.eventId);
if (_authBloc.state is AuthAuthenticated) {
_router.go(route);
return;
}
_pendingReminderRoute = route;
_enqueueAuthTransition(() async {
if (_authBloc.state is! AuthAuthenticated) {
return;
}
final pendingRoute = _pendingReminderRoute;
if (pendingRoute == null) {
return;
}
_pendingReminderRoute = null;
_router.go(pendingRoute);
});
}
@override
@@ -89,6 +91,23 @@ class _LinksyAppState extends State<LinksyApp> {
super.dispose();
}
void _enqueueAuthTransition(Future<void> Function() transition) {
_authTransitionQueue = _authTransitionQueue
.catchError((Object error, StackTrace stackTrace) {
FlutterError.reportError(
FlutterErrorDetails(exception: error, stack: stackTrace),
);
Zone.current.handleUncaughtError(error, stackTrace);
})
.then((_) => transition())
.catchError((Object error, StackTrace stackTrace) {
FlutterError.reportError(
FlutterErrorDetails(exception: error, stack: stackTrace),
);
Zone.current.handleUncaughtError(error, stackTrace);
});
}
@override
Widget build(BuildContext context) {
return BlocProvider<AuthBloc>.value(
@@ -96,15 +115,10 @@ class _LinksyAppState extends State<LinksyApp> {
child: BlocListener<AuthBloc, AuthState>(
listener: (context, state) {
if (state is AuthAuthenticated) {
unawaited(_onAuthenticated(state.user.id));
final pendingRoute = _pendingReminderRoute;
if (pendingRoute != null) {
_pendingReminderRoute = null;
_router.go(pendingRoute);
}
_enqueueAuthTransition(() => _onAuthenticated(state.user.id));
}
if (state is AuthUnauthenticated) {
unawaited(_onUnauthenticated());
_enqueueAuthTransition(_onUnauthenticated);
}
},
child: MaterialApp.router(