2026-03-27 19:07:39 +08:00
|
|
|
import 'dart:async';
|
|
|
|
|
import 'dart:convert';
|
|
|
|
|
|
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';
|
2026-03-27 19:07:39 +08:00
|
|
|
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';
|
2026-03-27 14:05:03 +08:00
|
|
|
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-27 19:07:39 +08:00
|
|
|
import '../features/notification/domain/models/reminder_action.dart';
|
|
|
|
|
import '../features/notification/domain/services/reminder_action_executor.dart';
|
2026-03-27 14:05:03 +08:00
|
|
|
import 'router/app_router.dart';
|
|
|
|
|
import '../core/theme/app_theme.dart';
|
|
|
|
|
|
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;
|
|
|
|
|
String? _reminderBootstrapUserId;
|
2026-03-27 14:05:03 +08:00
|
|
|
|
2026-03-27 19:07:39 +08:00
|
|
|
@override
|
|
|
|
|
void initState() {
|
|
|
|
|
super.initState();
|
|
|
|
|
_authBloc = sl<AuthBloc>();
|
|
|
|
|
_authBloc.add(AuthStarted());
|
|
|
|
|
_router = createAppRouter(_authBloc);
|
|
|
|
|
unawaited(_bindNotificationResponseHandler());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
void dispose() {
|
|
|
|
|
_router.dispose();
|
|
|
|
|
super.dispose();
|
|
|
|
|
}
|
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-27 19:07:39 +08:00
|
|
|
if (state is AuthAuthenticated &&
|
|
|
|
|
state.user.id != _reminderBootstrapUserId) {
|
|
|
|
|
_reminderBootstrapUserId = state.user.id;
|
|
|
|
|
unawaited(_rebuildUpcomingReminders());
|
|
|
|
|
}
|
|
|
|
|
if (state is AuthUnauthenticated) {
|
|
|
|
|
_reminderBootstrapUserId = null;
|
|
|
|
|
}
|
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
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
2026-03-27 19:07:39 +08:00
|
|
|
|
|
|
|
|
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,
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
}
|
2026-03-27 14:05:03 +08:00
|
|
|
}
|