refactor(apps): 主题系统迁移至 ColorScheme + 扩展架构并支持 Dark Mode
This commit is contained in:
+100
-14
@@ -1,37 +1,72 @@
|
||||
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 '../core/network/i_api_client.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/chat/presentation/bloc/chat_bloc.dart';
|
||||
import '../features/notification/domain/models/reminder_action.dart';
|
||||
import '../features/notification/domain/services/reminder_action_executor.dart';
|
||||
import 'router/app_router.dart';
|
||||
import '../core/theme/app_theme.dart';
|
||||
|
||||
class LinksyApp extends StatelessWidget {
|
||||
final AuthBloc authBloc;
|
||||
class LinksyApp extends StatefulWidget {
|
||||
const LinksyApp({super.key});
|
||||
|
||||
const LinksyApp({super.key, required this.authBloc});
|
||||
@override
|
||||
State<LinksyApp> createState() => _LinksyAppState();
|
||||
}
|
||||
|
||||
class _LinksyAppState extends State<LinksyApp> {
|
||||
late final AuthBloc _authBloc;
|
||||
late final GoRouter _router;
|
||||
String? _reminderBootstrapUserId;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_authBloc = sl<AuthBloc>();
|
||||
_authBloc.add(AuthStarted());
|
||||
_router = createAppRouter(_authBloc);
|
||||
unawaited(_bindNotificationResponseHandler());
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_router.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MultiBlocProvider(
|
||||
providers: [
|
||||
BlocProvider<AuthBloc>.value(value: authBloc),
|
||||
BlocProvider<ChatBloc>(
|
||||
create: (_) => ChatBloc(apiClient: sl<IApiClient>()),
|
||||
),
|
||||
],
|
||||
return BlocProvider<AuthBloc>.value(
|
||||
value: _authBloc,
|
||||
child: BlocListener<AuthBloc, AuthState>(
|
||||
listener: (context, state) {
|
||||
// Handle auth state changes if needed
|
||||
if (state is AuthAuthenticated &&
|
||||
state.user.id != _reminderBootstrapUserId) {
|
||||
_reminderBootstrapUserId = state.user.id;
|
||||
unawaited(_rebuildUpcomingReminders());
|
||||
}
|
||||
if (state is AuthUnauthenticated) {
|
||||
_reminderBootstrapUserId = null;
|
||||
}
|
||||
},
|
||||
child: MaterialApp.router(
|
||||
onGenerateTitle: (context) => AppLocalizations.of(context).appTitle,
|
||||
debugShowCheckedModeBanner: false,
|
||||
theme: AppTheme.light,
|
||||
darkTheme: AppTheme.dark,
|
||||
themeMode: ThemeMode.system,
|
||||
locale: const Locale('zh'),
|
||||
supportedLocales: AppLocalizations.supportedLocales,
|
||||
localizationsDelegates: AppLocalizations.localizationsDelegates,
|
||||
@@ -39,9 +74,60 @@ class LinksyApp extends StatelessWidget {
|
||||
L10n.setLocale(Localizations.localeOf(context));
|
||||
return child ?? const SizedBox.shrink();
|
||||
},
|
||||
routerConfig: createAppRouter(authBloc),
|
||||
routerConfig: _router,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
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,
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,35 +2,43 @@ import 'package:dio/dio.dart';
|
||||
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
||||
import 'package:get_it/get_it.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import '../../core/cache/cache_invalidator.dart';
|
||||
import '../../core/cache/hybrid_cache_store.dart';
|
||||
import '../../core/cache/memory_cache_store.dart';
|
||||
import '../../core/cache/persistent_cache_store.dart';
|
||||
import '../../data/cache/cache_invalidator.dart';
|
||||
import '../../data/cache/hybrid_cache_store.dart';
|
||||
import '../../data/cache/memory_cache_store.dart';
|
||||
import '../../data/cache/persistent_cache_store.dart';
|
||||
import '../../data/repositories/calendar_event_repository.dart';
|
||||
import '../../data/repositories/calendar_repository.dart';
|
||||
import '../../data/repositories/friend_repository.dart';
|
||||
import '../../data/repositories/inbox_repository.dart';
|
||||
import '../../data/repositories/user_repository.dart';
|
||||
import '../../core/auth/session_controller.dart';
|
||||
import '../../core/network/api_client.dart';
|
||||
import '../../core/network/i_api_client.dart';
|
||||
import '../../core/storage/app_preferences.dart';
|
||||
import '../../core/storage/token_storage.dart';
|
||||
import '../../core/config/env.dart';
|
||||
import '../../features/notification/data/services/local_notification_service.dart';
|
||||
import '../../data/services/local_notification_service.dart';
|
||||
import '../../features/auth/data/auth_api.dart';
|
||||
import '../../features/auth/data/auth_repository.dart';
|
||||
import '../../features/auth/data/auth_repository_impl.dart';
|
||||
import '../../features/auth/presentation/bloc/auth_bloc.dart';
|
||||
import '../../features/auth/presentation/bloc/auth_event.dart';
|
||||
import '../../features/chat/presentation/bloc/chat_bloc.dart';
|
||||
import '../../features/calendar/data/calendar_api.dart';
|
||||
import '../../features/calendar/data/services/calendar_repository.dart';
|
||||
import '../../features/calendar/data/services/calendar_service.dart';
|
||||
import '../../data/services/calendar_service.dart';
|
||||
import '../../features/notification/domain/services/reminder_action_executor.dart';
|
||||
import '../../features/calendar/presentation/calendar_state_manager.dart';
|
||||
import '../../shared/state/calendar_state_manager.dart';
|
||||
import '../../features/contacts/data/friends_api.dart';
|
||||
import '../../features/messages/data/inbox_api.dart';
|
||||
import '../../features/settings/data/settings_api.dart';
|
||||
import '../../features/settings/data/services/automation_jobs_api.dart';
|
||||
import '../../features/settings/data/services/settings_user_cache.dart';
|
||||
import '../../features/settings/data/services/user_profile_cache_repository.dart';
|
||||
import '../../features/settings/data/services/user_profile_service.dart';
|
||||
import '../../features/settings/data/services/memory_service.dart';
|
||||
import '../../features/contacts/data/users/users_api.dart';
|
||||
import '../../features/todo/data/todo_api.dart';
|
||||
import '../../features/todo/data/todo_repository.dart';
|
||||
import '../services/auth_session_controller.dart';
|
||||
|
||||
final sl = GetIt.instance;
|
||||
|
||||
@@ -63,6 +71,7 @@ Future<void> configureDependencies() async {
|
||||
|
||||
final sharedPreferences = await SharedPreferences.getInstance();
|
||||
sl.registerSingleton<SharedPreferences>(sharedPreferences);
|
||||
sl.registerSingleton<AppPreferences>(AppPreferences(sharedPreferences));
|
||||
|
||||
final memoryCacheStore = MemoryCacheStore();
|
||||
final persistentCacheStore = PersistentCacheStore();
|
||||
@@ -79,23 +88,31 @@ Future<void> configureDependencies() async {
|
||||
|
||||
final usersApi = UsersApi(apiClient);
|
||||
sl.registerSingleton<UsersApi>(usersApi);
|
||||
sl.registerSingleton<UserRepository>(UserRepositoryImpl(apiClient));
|
||||
final userProfileService = UserProfileService(apiClient);
|
||||
sl.registerSingleton<UserProfileService>(userProfileService);
|
||||
|
||||
final userProfileCacheRepository = UserProfileCacheRepository(
|
||||
store: hybridCacheStore,
|
||||
remoteLoader: usersApi.getMe,
|
||||
remoteLoader: userProfileService.getMe,
|
||||
);
|
||||
sl.registerSingleton<UserProfileCacheRepository>(userProfileCacheRepository);
|
||||
|
||||
final calendarApi = CalendarApi(apiClient);
|
||||
sl.registerSingleton<CalendarApi>(calendarApi);
|
||||
sl.registerSingleton<CalendarEventRepository>(
|
||||
CalendarEventRepositoryImpl(apiClient),
|
||||
);
|
||||
|
||||
final calendarService = CalendarService(apiClient: apiClient);
|
||||
final calendarService = CalendarService(
|
||||
apiClient: apiClient,
|
||||
invalidator: sl<CacheInvalidator>(),
|
||||
);
|
||||
sl.registerSingleton<CalendarService>(calendarService);
|
||||
|
||||
final calendarRepository = CalendarRepository(
|
||||
store: hybridCacheStore,
|
||||
loadDayFromRemote: calendarService.getEventsForDay,
|
||||
loadMonthFromRemote: calendarService.getEventsForRange,
|
||||
apiClient: apiClient,
|
||||
);
|
||||
sl.registerSingleton<CalendarRepository>(calendarRepository);
|
||||
|
||||
@@ -109,6 +126,7 @@ Future<void> configureDependencies() async {
|
||||
|
||||
final friendsApi = FriendsApi(apiClient);
|
||||
sl.registerSingleton<FriendsApi>(friendsApi);
|
||||
sl.registerSingleton<FriendRepository>(FriendRepositoryImpl(apiClient));
|
||||
|
||||
final settingsApi = SettingsApi(apiClient);
|
||||
sl.registerSingleton<SettingsApi>(settingsApi);
|
||||
@@ -119,12 +137,9 @@ Future<void> configureDependencies() async {
|
||||
final memoryService = MemoryService(apiClient);
|
||||
sl.registerSingleton<MemoryService>(memoryService);
|
||||
|
||||
sl.registerSingleton<SettingsUserCache>(
|
||||
SettingsUserCache(userProfileCacheRepository),
|
||||
);
|
||||
|
||||
final inboxApi = InboxApi(apiClient);
|
||||
sl.registerSingleton<InboxApi>(inboxApi);
|
||||
sl.registerSingleton<InboxRepository>(InboxRepositoryImpl(apiClient));
|
||||
|
||||
final todoApi = TodoApi(apiClient);
|
||||
sl.registerSingleton<TodoApi>(todoApi);
|
||||
@@ -141,8 +156,8 @@ Future<void> configureDependencies() async {
|
||||
tokenStorage: tokenStorage,
|
||||
onLogout: () async {
|
||||
apiClient.resetInterceptor();
|
||||
if (sl.isRegistered<SettingsUserCache>()) {
|
||||
sl<SettingsUserCache>().invalidate();
|
||||
if (sl.isRegistered<UserProfileCacheRepository>()) {
|
||||
await sl<UserProfileCacheRepository>().invalidate();
|
||||
}
|
||||
},
|
||||
);
|
||||
@@ -150,6 +165,8 @@ Future<void> configureDependencies() async {
|
||||
|
||||
final authBloc = AuthBloc(authRepository);
|
||||
sl.registerSingleton<AuthBloc>(authBloc);
|
||||
sl.registerSingleton<SessionController>(AuthSessionController(authBloc));
|
||||
sl.registerSingleton<ChatBloc>(ChatBloc(apiClient: apiClient));
|
||||
|
||||
apiClient.setRefreshCallback((token) async {
|
||||
try {
|
||||
@@ -161,8 +178,8 @@ Future<void> configureDependencies() async {
|
||||
});
|
||||
|
||||
apiClient.setAuthFailureCallback(() async {
|
||||
if (sl.isRegistered<SettingsUserCache>()) {
|
||||
sl<SettingsUserCache>().invalidate();
|
||||
if (sl.isRegistered<UserProfileCacheRepository>()) {
|
||||
await sl<UserProfileCacheRepository>().invalidate();
|
||||
}
|
||||
authBloc.add(
|
||||
const AuthSessionInvalidated(
|
||||
|
||||
@@ -1,7 +1,11 @@
|
||||
import 'package:flutter/widgets.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
import 'app_route_observer.dart';
|
||||
import '../di/injection.dart';
|
||||
import '../../../features/auth/presentation/bloc/auth_bloc.dart';
|
||||
import '../../../features/auth/presentation/bloc/auth_state.dart';
|
||||
import '../../../features/chat/presentation/bloc/chat_bloc.dart';
|
||||
import 'app_routes.dart';
|
||||
import 'go_router_refresh_stream.dart';
|
||||
import '../../../features/auth/presentation/screens/auth_boot_screen.dart';
|
||||
@@ -32,7 +36,6 @@ import '../../../features/settings/presentation/screens/work_memory_detail_scree
|
||||
import '../../../features/settings/presentation/screens/edit_profile_screen.dart';
|
||||
|
||||
final _homeSecondLevelRoutes = [
|
||||
AppRoutes.shellHomeBranch,
|
||||
AppRoutes.shellCalendarBranch,
|
||||
AppRoutes.calendarMonth,
|
||||
AppRoutes.shellTodoBranch,
|
||||
@@ -54,37 +57,53 @@ final _protectedRoutes = [
|
||||
AppRoutes.messageInviteList,
|
||||
];
|
||||
|
||||
String? resolveAuthRedirect({
|
||||
required AuthState authState,
|
||||
required String matchedLocation,
|
||||
}) {
|
||||
final isAuthenticated = authState is AuthAuthenticated;
|
||||
final isAuthChecking = authState is AuthInitial || authState is AuthLoading;
|
||||
final isBootRoute = matchedLocation == AppRoutes.authBoot;
|
||||
final isAuthRoute =
|
||||
matchedLocation == AppRoutes.authLogin ||
|
||||
matchedLocation.startsWith('/login');
|
||||
final isHomeRoute = matchedLocation == AppRoutes.homeMain;
|
||||
final isProtected =
|
||||
isHomeRoute ||
|
||||
_protectedRoutes.any((route) => matchedLocation.startsWith(route));
|
||||
|
||||
if (isAuthChecking && !isBootRoute) {
|
||||
return AppRoutes.authBoot;
|
||||
}
|
||||
if (!isAuthChecking && isBootRoute) {
|
||||
return isAuthenticated ? AppRoutes.homeMain : AppRoutes.authLogin;
|
||||
}
|
||||
if (!isAuthenticated && isProtected) {
|
||||
return AppRoutes.authLogin;
|
||||
}
|
||||
if (isAuthenticated && isAuthRoute) {
|
||||
return AppRoutes.homeMain;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
Widget buildHomeRouteScreen() {
|
||||
return BlocProvider<ChatBloc>.value(
|
||||
value: sl<ChatBloc>(),
|
||||
child: const HomeScreen(),
|
||||
);
|
||||
}
|
||||
|
||||
GoRouter createAppRouter(AuthBloc authBloc) {
|
||||
return GoRouter(
|
||||
initialLocation: AppRoutes.authBoot,
|
||||
observers: [appRouteObserver],
|
||||
refreshListenable: GoRouterRefreshStream(authBloc.stream),
|
||||
redirect: (context, state) {
|
||||
final authState = authBloc.state;
|
||||
final isAuthenticated = authState is AuthAuthenticated;
|
||||
final isAuthChecking =
|
||||
authState is AuthInitial || authState is AuthLoading;
|
||||
final isBootRoute = state.matchedLocation == AppRoutes.authBoot;
|
||||
final isAuthRoute =
|
||||
state.matchedLocation == AppRoutes.authLogin ||
|
||||
state.matchedLocation.startsWith('/login');
|
||||
final isProtected = _protectedRoutes.any(
|
||||
(route) => state.matchedLocation.startsWith(route),
|
||||
return resolveAuthRedirect(
|
||||
authState: authBloc.state,
|
||||
matchedLocation: state.matchedLocation,
|
||||
);
|
||||
|
||||
if (isAuthChecking && !isBootRoute) {
|
||||
return AppRoutes.authBoot;
|
||||
}
|
||||
if (!isAuthChecking && isBootRoute) {
|
||||
return isAuthenticated ? AppRoutes.homeMain : AppRoutes.authLogin;
|
||||
}
|
||||
if (!isAuthenticated && isProtected) {
|
||||
return AppRoutes.authLogin;
|
||||
}
|
||||
if (isAuthenticated && isAuthRoute) {
|
||||
return AppRoutes.homeMain;
|
||||
}
|
||||
return null;
|
||||
},
|
||||
routes: [
|
||||
GoRoute(
|
||||
@@ -118,7 +137,7 @@ GoRouter createAppRouter(AuthBloc authBloc) {
|
||||
),
|
||||
GoRoute(
|
||||
path: AppRoutes.homeMain,
|
||||
builder: (context, state) => const HomeScreen(),
|
||||
builder: (context, state) => buildHomeRouteScreen(),
|
||||
),
|
||||
GoRoute(
|
||||
path: AppRoutes.messageInviteList,
|
||||
|
||||
@@ -2,10 +2,9 @@ class AppRoutes {
|
||||
AppRoutes._();
|
||||
|
||||
static const authBoot = '/boot';
|
||||
static const authLogin = '/';
|
||||
static const authLogin = '/login';
|
||||
|
||||
static const homeMain = '/home';
|
||||
static const shellHomeBranch = homeMain;
|
||||
static const homeMain = '/';
|
||||
static const shellCalendarBranch = calendarDayWeek;
|
||||
static const shellTodoBranch = todoList;
|
||||
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
import 'package:flutter/widgets.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
|
||||
import 'app_routes.dart';
|
||||
|
||||
enum HomeReturnAction { pop, goHome, goHomeForDock }
|
||||
|
||||
HomeReturnAction resolveHomeReturnAction({
|
||||
required bool canPop,
|
||||
required bool isAuthEntry,
|
||||
bool forceGoHome = false,
|
||||
}) {
|
||||
if (forceGoHome) {
|
||||
return HomeReturnAction.goHome;
|
||||
}
|
||||
if (isAuthEntry) {
|
||||
return HomeReturnAction.goHome;
|
||||
}
|
||||
if (canPop) {
|
||||
return HomeReturnAction.goHomeForDock;
|
||||
}
|
||||
return HomeReturnAction.goHome;
|
||||
}
|
||||
|
||||
void returnToHomePreserveState(
|
||||
BuildContext context, {
|
||||
bool isAuthEntry = false,
|
||||
bool forceGoHome = false,
|
||||
}) {
|
||||
final action = resolveHomeReturnAction(
|
||||
canPop: context.canPop(),
|
||||
isAuthEntry: isAuthEntry,
|
||||
forceGoHome: forceGoHome,
|
||||
);
|
||||
switch (action) {
|
||||
case HomeReturnAction.pop:
|
||||
context.pop();
|
||||
return;
|
||||
case HomeReturnAction.goHome:
|
||||
context.go(AppRoutes.homeMain);
|
||||
return;
|
||||
case HomeReturnAction.goHomeForDock:
|
||||
if (context.canPop()) {
|
||||
context.pop();
|
||||
return;
|
||||
}
|
||||
context.go(AppRoutes.homeMain);
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:social_app/core/auth/session_controller.dart';
|
||||
import 'package:social_app/features/auth/presentation/bloc/auth_bloc.dart';
|
||||
import 'package:social_app/features/auth/presentation/bloc/auth_event.dart';
|
||||
import 'package:social_app/features/auth/presentation/bloc/auth_state.dart';
|
||||
|
||||
class AuthSessionController implements SessionController {
|
||||
final AuthBloc _authBloc;
|
||||
|
||||
AuthSessionController(this._authBloc);
|
||||
|
||||
@override
|
||||
Future<void> logoutAndWaitUnauthenticated() async {
|
||||
_authBloc.add(AuthLoggedOut());
|
||||
await _authBloc.stream
|
||||
.firstWhere((state) => state is AuthUnauthenticated)
|
||||
.timeout(const Duration(seconds: 5));
|
||||
}
|
||||
}
|
||||
@@ -1,38 +0,0 @@
|
||||
import '../../features/auth/presentation/bloc/auth_state.dart';
|
||||
import '../../features/calendar/data/services/calendar_service.dart';
|
||||
import '../../features/notification/data/services/local_notification_service.dart';
|
||||
|
||||
class AuthSessionBootstrapper {
|
||||
AuthSessionBootstrapper({
|
||||
required CalendarService calendarService,
|
||||
required LocalNotificationService notificationService,
|
||||
}) : _calendarService = calendarService,
|
||||
_notificationService = notificationService;
|
||||
|
||||
final CalendarService _calendarService;
|
||||
final LocalNotificationService _notificationService;
|
||||
|
||||
String? _syncedUserId;
|
||||
|
||||
Future<void> syncForAuthState(AuthState state) async {
|
||||
if (state is! AuthAuthenticated) {
|
||||
_syncedUserId = null;
|
||||
return;
|
||||
}
|
||||
|
||||
if (_syncedUserId == state.user.id) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
final now = DateTime.now();
|
||||
final start = now.subtract(const Duration(days: 90));
|
||||
final end = now.add(const Duration(days: 90));
|
||||
final events = await _calendarService.getEventsForRange(start, end);
|
||||
await _notificationService.rebuildUpcomingReminders(events);
|
||||
_syncedUserId = state.user.id;
|
||||
} catch (_) {
|
||||
// ignore reminder bootstrap failures
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user