主要变更: 1. 新增桌面端托盘图标支持深色/浅色主题切换 2. 重构应用锁、动画配置、小组件导航服务职责 3. 修复Riverpod初始化断言、防重复点击、工作台模式残留选中态问题 4. 优化诗词服务、阅读进度、搜索结果空状态体验 5. 完善macOS打包配置与错误静默处理逻辑 6. 新增快速卡片多语言适配与动画退出队列管理
117 lines
3.9 KiB
Dart
117 lines
3.9 KiB
Dart
/// ============================================================
|
||
/// 闲言APP — 快捷操作处理器
|
||
/// 创建时间: 2026-06-22
|
||
/// 更新时间: 2026-06-22
|
||
/// 作用: 处理快捷方式回调逻辑(搜索快捷方式、路由导航)
|
||
/// 上次更新: 从 app.dart 中提取快捷操作处理职责
|
||
/// ============================================================
|
||
|
||
import 'package:flutter/widgets.dart';
|
||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||
|
||
import '../../core/router/app_router.dart' show appRouter, rootNavigatorKey;
|
||
import '../../core/router/app_routes.dart';
|
||
import '../../core/router/ohos_nav_bridge.dart';
|
||
import '../../core/utils/logger.dart';
|
||
import '../../core/utils/platform/platform_utils.dart' as pu;
|
||
import '../../features/profile/presentation/spotlight_search/spotlight_search_overlay.dart';
|
||
import '../../core/services/device/quick_actions_service.dart';
|
||
|
||
/// 快捷操作处理器
|
||
///
|
||
/// 负责处理快捷方式回调逻辑:
|
||
/// - 搜索快捷方式:切换到 profile Tab + 弹出搜索浮层
|
||
/// - 普通快捷方式:路由导航到目标页面
|
||
///
|
||
/// 修复白屏问题:
|
||
/// - 旧逻辑:push('/profile?action=search') → shell 外创建新页面 → 白屏
|
||
/// - 新逻辑:go('/profile') 切换 Tab + 直接弹出 SpotlightSearchOverlay
|
||
class QuickActionsHandler {
|
||
QuickActionsHandler(this._ref);
|
||
|
||
final WidgetRef _ref;
|
||
|
||
/// 初始化快捷操作回调
|
||
void initialize() {
|
||
QuickActionsService.init(
|
||
onActionCallback: _handleAction,
|
||
);
|
||
}
|
||
|
||
/// 处理快捷操作回调
|
||
void _handleAction(String route) {
|
||
try {
|
||
final context = rootNavigatorKey.currentContext;
|
||
if (context == null) {
|
||
Log.w('🚀 [QuickActions] context不可用,延迟导航');
|
||
return;
|
||
}
|
||
|
||
// 搜索快捷方式:特殊标记 'action:search'
|
||
if (route == 'action:search') {
|
||
_handleSearchShortcut();
|
||
return;
|
||
}
|
||
|
||
final cleanRoute = route.split('?').first;
|
||
|
||
if (pu.isOhos) {
|
||
OhosNavBridge.push(context, cleanRoute);
|
||
} else {
|
||
// 安卓端:确保路由系统已就绪再导航,防止冷启动时GoRouter未初始化导致闪退
|
||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||
try {
|
||
appRouter.push(cleanRoute);
|
||
} catch (e) {
|
||
Log.e('🚀 [QuickActions] Android端快捷方式导航失败', e);
|
||
}
|
||
});
|
||
}
|
||
} catch (e) {
|
||
Log.e('🚀 [QuickActions] 快捷方式回调异常', e);
|
||
}
|
||
}
|
||
|
||
/// 处理搜索快捷方式
|
||
///
|
||
/// 兼容冷启动和热启动场景:
|
||
/// - 冷启动:ProfilePage 首次构建,通过 pendingSearch 机制弹出
|
||
/// - 热启动:ProfilePage 已构建,直接在 root context 弹出搜索浮层
|
||
void _handleSearchShortcut() {
|
||
final context = rootNavigatorKey.currentContext;
|
||
if (context == null) {
|
||
Log.w('🚀 [QuickActions] 搜索快捷方式: context不可用');
|
||
return;
|
||
}
|
||
|
||
if (pu.isOhos) {
|
||
// 鸿蒙端:切换到 profile 页面后弹出搜索
|
||
OhosNavBridge.push(context, AppRoutes.profile);
|
||
Future.delayed(const Duration(milliseconds: 600), () {
|
||
final ctx = rootNavigatorKey.currentContext;
|
||
if (ctx != null && ctx.mounted) {
|
||
SpotlightSearchOverlay.show(ctx, _ref);
|
||
}
|
||
});
|
||
return;
|
||
}
|
||
|
||
// 安卓/iOS端:
|
||
// 1. 切换到 profile Tab(go 而非 push,避免在 shell 外创建新页面)
|
||
// 2. 延迟弹出搜索浮层,等待 Tab 切换动画完成
|
||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||
try {
|
||
appRouter.go(AppRoutes.profile);
|
||
Future.delayed(const Duration(milliseconds: 500), () {
|
||
final ctx = rootNavigatorKey.currentContext;
|
||
if (ctx != null && ctx.mounted) {
|
||
SpotlightSearchOverlay.show(ctx, _ref);
|
||
}
|
||
});
|
||
} catch (e) {
|
||
Log.e('🚀 [QuickActions] 搜索快捷方式处理失败', e);
|
||
}
|
||
});
|
||
}
|
||
}
|