2026-03-27 19:07:39 +08:00
|
|
|
import 'dart:async';
|
2026-03-30 18:36:57 +08:00
|
|
|
import 'package:flutter/scheduler.dart';
|
2026-03-27 19:07:39 +08:00
|
|
|
|
2026-03-27 14:05:03 +08:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
2026-03-27 19:07:39 +08:00
|
|
|
import 'package:go_router/go_router.dart';
|
2026-03-27 14:05:03 +08:00
|
|
|
import 'di/injection.dart';
|
|
|
|
|
import '../core/l10n/l10n.dart';
|
|
|
|
|
import '../l10n/app_localizations.dart';
|
|
|
|
|
import '../features/auth/presentation/bloc/auth_bloc.dart';
|
2026-03-27 19:07:39 +08:00
|
|
|
import '../features/auth/presentation/bloc/auth_event.dart';
|
2026-03-27 14:05:03 +08:00
|
|
|
import '../features/auth/presentation/bloc/auth_state.dart';
|
2026-03-30 09:06:38 +08:00
|
|
|
import '../features/chat/presentation/bloc/chat_bloc.dart';
|
|
|
|
|
import '../data/cache/cache_scope.dart';
|
2026-03-29 20:26:30 +08:00
|
|
|
import 'services/app_prewarm_orchestrator.dart';
|
2026-03-31 18:26:36 +08:00
|
|
|
import 'services/session_scope_manager.dart';
|
2026-03-27 14:05:03 +08:00
|
|
|
import 'router/app_router.dart';
|
|
|
|
|
import '../core/theme/app_theme.dart';
|
2026-03-30 18:36:57 +08:00
|
|
|
import '../core/inbox/inbox_sync_store.dart';
|
|
|
|
|
import '../core/notification/services/reminder_permission_service.dart';
|
|
|
|
|
import '../core/notification/services/reminder_notification_router.dart';
|
|
|
|
|
import '../core/notification/models/reminder_alarm.dart';
|
|
|
|
|
import 'router/app_routes.dart';
|
2026-03-27 14:05:03 +08:00
|
|
|
|
2026-03-27 19:07:39 +08:00
|
|
|
class LinksyApp extends StatefulWidget {
|
|
|
|
|
const LinksyApp({super.key});
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
State<LinksyApp> createState() => _LinksyAppState();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class _LinksyAppState extends State<LinksyApp> {
|
|
|
|
|
late final AuthBloc _authBloc;
|
|
|
|
|
late final GoRouter _router;
|
2026-03-30 18:36:57 +08:00
|
|
|
StreamSubscription<ReminderNotificationTap>? _reminderTapSubscription;
|
|
|
|
|
String? _pendingReminderRoute;
|
2026-03-31 18:26:36 +08:00
|
|
|
Future<void> _authTransitionQueue = Future<void>.value();
|
2026-03-30 09:06:38 +08:00
|
|
|
|
|
|
|
|
Future<void> _onAuthenticated(String userId) async {
|
2026-03-31 18:26:36 +08:00
|
|
|
await sl<SessionScopeManager>().activate(userId);
|
2026-03-30 18:36:57 +08:00
|
|
|
await sl<InboxSyncStore>().resetForUser(userId);
|
2026-03-30 09:06:38 +08:00
|
|
|
await sl<AppPrewarmOrchestrator>().ensureStartedFor(userId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<void> _onUnauthenticated() async {
|
2026-03-31 18:26:36 +08:00
|
|
|
await sl<SessionScopeManager>().clearActiveUserScope();
|
2026-03-30 18:36:57 +08:00
|
|
|
await sl<InboxSyncStore>().resetForUser(null);
|
2026-03-30 09:06:38 +08:00
|
|
|
await sl<ChatBloc>().switchUser(null);
|
|
|
|
|
sl<AppPrewarmOrchestrator>().reset();
|
|
|
|
|
}
|
2026-03-27 14:05:03 +08:00
|
|
|
|
2026-03-27 19:07:39 +08:00
|
|
|
@override
|
|
|
|
|
void initState() {
|
|
|
|
|
super.initState();
|
|
|
|
|
_authBloc = sl<AuthBloc>();
|
2026-03-31 18:26:36 +08:00
|
|
|
CacheScope.resetProvider();
|
2026-03-27 19:07:39 +08:00
|
|
|
_authBloc.add(AuthStarted());
|
|
|
|
|
_router = createAppRouter(_authBloc);
|
2026-03-30 18:36:57 +08:00
|
|
|
SchedulerBinding.instance.addPostFrameCallback((_) {
|
|
|
|
|
unawaited(_bootstrapReminderNotification());
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<void> _bootstrapReminderNotification() async {
|
|
|
|
|
await sl<ReminderPermissionService>().initializeAtBoot();
|
|
|
|
|
final router = sl<ReminderNotificationRouter>();
|
|
|
|
|
_reminderTapSubscription ??= router.taps.listen(_onReminderTap);
|
2026-04-01 00:42:34 +08:00
|
|
|
await router.start();
|
2026-03-30 18:36:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void _onReminderTap(ReminderNotificationTap tap) {
|
|
|
|
|
final route = AppRoutes.calendarReminderAlarm(tap.eventId);
|
|
|
|
|
_pendingReminderRoute = route;
|
2026-03-31 18:26:36 +08:00
|
|
|
_enqueueAuthTransition(() async {
|
|
|
|
|
if (_authBloc.state is! AuthAuthenticated) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
final pendingRoute = _pendingReminderRoute;
|
|
|
|
|
if (pendingRoute == null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
_pendingReminderRoute = null;
|
|
|
|
|
_router.go(pendingRoute);
|
|
|
|
|
});
|
2026-03-27 19:07:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
void dispose() {
|
2026-03-30 18:36:57 +08:00
|
|
|
_reminderTapSubscription?.cancel();
|
2026-03-27 19:07:39 +08:00
|
|
|
_router.dispose();
|
|
|
|
|
super.dispose();
|
|
|
|
|
}
|
2026-03-27 14:05:03 +08:00
|
|
|
|
2026-03-31 18:26:36 +08:00
|
|
|
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);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-27 14:05:03 +08:00
|
|
|
@override
|
|
|
|
|
Widget build(BuildContext context) {
|
2026-03-27 19:07:39 +08:00
|
|
|
return BlocProvider<AuthBloc>.value(
|
|
|
|
|
value: _authBloc,
|
2026-03-27 14:05:03 +08:00
|
|
|
child: BlocListener<AuthBloc, AuthState>(
|
|
|
|
|
listener: (context, state) {
|
2026-03-29 20:26:30 +08:00
|
|
|
if (state is AuthAuthenticated) {
|
2026-03-31 18:26:36 +08:00
|
|
|
_enqueueAuthTransition(() => _onAuthenticated(state.user.id));
|
2026-03-27 19:07:39 +08:00
|
|
|
}
|
|
|
|
|
if (state is AuthUnauthenticated) {
|
2026-03-31 18:26:36 +08:00
|
|
|
_enqueueAuthTransition(_onUnauthenticated);
|
2026-03-27 19:07:39 +08:00
|
|
|
}
|
2026-03-27 14:05:03 +08:00
|
|
|
},
|
|
|
|
|
child: MaterialApp.router(
|
|
|
|
|
onGenerateTitle: (context) => AppLocalizations.of(context).appTitle,
|
|
|
|
|
debugShowCheckedModeBanner: false,
|
|
|
|
|
theme: AppTheme.light,
|
2026-03-27 19:07:39 +08:00
|
|
|
darkTheme: AppTheme.dark,
|
|
|
|
|
themeMode: ThemeMode.system,
|
2026-03-27 14:05:03 +08:00
|
|
|
locale: const Locale('zh'),
|
|
|
|
|
supportedLocales: AppLocalizations.supportedLocales,
|
|
|
|
|
localizationsDelegates: AppLocalizations.localizationsDelegates,
|
|
|
|
|
builder: (context, child) {
|
|
|
|
|
L10n.setLocale(Localizations.localeOf(context));
|
|
|
|
|
return child ?? const SizedBox.shrink();
|
|
|
|
|
},
|
2026-03-27 19:07:39 +08:00
|
|
|
routerConfig: _router,
|
2026-03-27 14:05:03 +08:00
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|