feat: 增强 HomeScreen 录音交互与 ChatBloc 状态管理

- 新增录音启动延迟处理,解决权限未就绪时的竞态问题
- 实现历史分页滚动位置保持,提升加载体验
- 添加文本输入框点击键盘显示与焦点管理
- 优化 ChatBloc provider 到 MultiBlocProvider 支持
- 修复 ApiException 429 错误详情解析(支持 JSON 字符串 body)
- 改进 LocalNotificationService 精确闹钟权限请求
- 优化 UiSchemaRenderer GridView children 生成
- 支持导航 action 的 replace 参数
- 移除 Agent router 速率限制逻辑(_allow_run_request, _allow_transcribe_request)
- 补充相关单元测试与集成测试
This commit is contained in:
qzl
2026-03-18 17:03:22 +08:00
parent b34697660d
commit 8539f05a66
13 changed files with 578 additions and 143 deletions
@@ -1,12 +1,23 @@
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:flutter/foundation.dart';
import 'package:timezone/data/latest.dart' as tz_data;
import 'package:timezone/timezone.dart' as tz;
import '../../features/calendar/data/models/schedule_item_model.dart';
class NotificationScheduleException implements Exception {
final String message;
NotificationScheduleException(this.message);
@override
String toString() => message;
}
class LocalNotificationService {
final FlutterLocalNotificationsPlugin _plugin;
bool _initialized = false;
bool _exactAlarmPermissionRequested = false;
LocalNotificationService({FlutterLocalNotificationsPlugin? plugin})
: _plugin = plugin ?? FlutterLocalNotificationsPlugin();
@@ -27,17 +38,17 @@ class LocalNotificationService {
await _plugin.initialize(settings);
await _plugin
final androidImpl = _plugin
.resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin
>()
?.requestNotificationsPermission();
>();
await androidImpl?.requestNotificationsPermission();
await _plugin
final iosImpl = _plugin
.resolvePlatformSpecificImplementation<
IOSFlutterLocalNotificationsPlugin
>()
?.requestPermissions(alert: true, badge: true, sound: true);
>();
await iosImpl?.requestPermissions(alert: true, badge: true, sound: true);
_initialized = true;
}
@@ -59,6 +70,41 @@ class LocalNotificationService {
final notificationId = _notificationIdForEvent(event.id);
final scheduledAt = tz.TZDateTime.from(fireAt, tz.local);
final androidImpl = _plugin
.resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin
>();
var androidScheduleMode = AndroidScheduleMode.exactAllowWhileIdle;
if (defaultTargetPlatform == TargetPlatform.android &&
androidImpl != null) {
var notificationsEnabled =
await androidImpl.areNotificationsEnabled() ?? false;
if (!notificationsEnabled) {
await androidImpl.requestNotificationsPermission();
notificationsEnabled =
await androidImpl.areNotificationsEnabled() ?? false;
}
if (!notificationsEnabled) {
throw NotificationScheduleException('系统通知权限未开启,无法创建提醒');
}
try {
var canScheduleExact =
await androidImpl.canScheduleExactNotifications() ?? false;
if (!canScheduleExact && !_exactAlarmPermissionRequested) {
_exactAlarmPermissionRequested = true;
await androidImpl.requestExactAlarmsPermission();
canScheduleExact =
await androidImpl.canScheduleExactNotifications() ?? false;
}
if (!canScheduleExact) {
androidScheduleMode = AndroidScheduleMode.inexactAllowWhileIdle;
}
} catch (_) {
androidScheduleMode = AndroidScheduleMode.inexactAllowWhileIdle;
}
}
final details = NotificationDetails(
android: AndroidNotificationDetails(
@@ -83,11 +129,20 @@ class LocalNotificationService {
_buildReminderBody(event, reminderMinutes),
scheduledAt,
details,
androidScheduleMode: AndroidScheduleMode.exactAllowWhileIdle,
androidScheduleMode: androidScheduleMode,
uiLocalNotificationDateInterpretation:
UILocalNotificationDateInterpretation.absoluteTime,
);
} catch (_) {
final pending = await _plugin.pendingNotificationRequests();
final scheduled = pending.any((item) => item.id == notificationId);
if (!scheduled) {
throw NotificationScheduleException('提醒未被系统接受,请检查系统通知和电池优化设置');
}
} catch (error) {
if (error is NotificationScheduleException) {
rethrow;
}
await _plugin.zonedSchedule(
notificationId,
event.title,
@@ -98,6 +153,12 @@ class LocalNotificationService {
uiLocalNotificationDateInterpretation:
UILocalNotificationDateInterpretation.absoluteTime,
);
final pending = await _plugin.pendingNotificationRequests();
final scheduled = pending.any((item) => item.id == notificationId);
if (!scheduled) {
throw NotificationScheduleException('提醒创建失败,请检查系统设置后重试');
}
}
}