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(
+4
View File
@@ -36,6 +36,7 @@ import '../../features/contacts/data/apis/users_api.dart';
import '../../features/todo/data/apis/todo_api.dart';
import '../../features/todo/data/repositories/todo_repository.dart';
import '../services/app_prewarm_orchestrator.dart';
import '../services/session_scope_manager.dart';
import '../services/auth_session_controller.dart';
import '../../core/notification/services/reminder_scheduler_service.dart';
import '../../core/notification/services/reminder_permission_service.dart';
@@ -83,6 +84,9 @@ Future<void> configureDependencies() async {
sl.registerSingleton<MemoryCacheStore>(memoryCacheStore);
sl.registerSingleton<PersistentCacheStore>(persistentCacheStore);
sl.registerSingleton<HybridCacheStore>(hybridCacheStore);
sl.registerSingleton<SessionScopeManager>(
SessionScopeManager(cacheStore: hybridCacheStore),
);
sl.registerSingleton<CacheInvalidator>(
CacheInvalidator(store: hybridCacheStore),
);
+4 -3
View File
@@ -91,10 +91,11 @@ String? resolveAuthRedirect({
return null;
}
Widget buildHomeRouteScreen() {
Widget buildHomeRouteScreen(AuthState authState) {
final userId = authState is AuthAuthenticated ? authState.user.id : null;
return BlocProvider<ChatBloc>.value(
value: sl<ChatBloc>(),
child: const HomeScreen(),
child: HomeScreen(initialUserId: userId),
);
}
@@ -149,7 +150,7 @@ GoRouter createAppRouter(AuthBloc authBloc) {
),
GoRoute(
path: AppRoutes.homeMain,
builder: (context, state) => buildHomeRouteScreen(),
builder: (context, state) => buildHomeRouteScreen(authBloc.state),
),
GoRoute(
path: AppRoutes.messageInviteList,
@@ -0,0 +1,42 @@
import '../../data/cache/cache_scope.dart';
import '../../data/cache/cache_store.dart';
class SessionScopeManager {
SessionScopeManager({required HybridCacheStore cacheStore})
: _cacheStore = cacheStore;
final HybridCacheStore _cacheStore;
String? _activeUserId;
Future<void> activate(String userId) async {
final normalizedUserId = userId.trim();
if (normalizedUserId.isEmpty) {
throw StateError('User id cannot be empty when activating cache scope');
}
final previousUserId = _activeUserId;
if (previousUserId == normalizedUserId) {
return;
}
_activeUserId = normalizedUserId;
CacheScope.configureProvider(() => 'user:$normalizedUserId');
if (previousUserId != null && previousUserId != normalizedUserId) {
await _clearUserCache(previousUserId);
}
}
Future<void> clearActiveUserScope() async {
final userId = _activeUserId;
_activeUserId = null;
CacheScope.resetProvider();
if (userId != null) {
await _clearUserCache(userId);
}
}
Future<void> _clearUserCache(String userId) {
return _cacheStore.clearByPrefix('cache:user:$userId:');
}
}