feat: 重构会话管理与提醒通知系统

This commit is contained in:
qzl
2026-03-31 18:26:36 +08:00
parent a8c262e9c7
commit 9a231dae9e
31 changed files with 650 additions and 223 deletions
+103
View File
@@ -0,0 +1,103 @@
---
## 6. 日历提醒未使用自定义提示音
**文件**: `apps/lib/core/notification/services/reminder_scheduler_service.dart`
**问题描述**:
已添加 `apps/assets/reminder_sound/1.WAV` 自定义提示音文件,但代码中未使用,仍使用系统默认铃声:
```dart
// 当前代码
const androidDetails = AndroidNotificationDetails(
_channelId,
_channelName,
// ...
playSound: true, // 使用系统默认
// 没有 customSound 配置
);
```
**根因**:
添加了资源文件但未在通知配置中引用。
**建议修复**:
1.`pubspec.yaml` 添加 assets 路径:
```yaml
flutter:
assets:
- assets/reminder_sound/
```
2. 在通知配置中引用自定义铃声:
```dart
// Android
final androidDetails = AndroidNotificationDetails(
_channelId,
_channelName,
playSound: true,
sound: RawResourceAndroidNotificationSound('reminder_sound/1'),
// ...
);
// iOS
const iosDetails = DarwinNotificationDetails(
presentSound: true,
sound: 'reminder_sound_1.wav',
// ...
);
```
---
## 7. 通知调度缺少变化检测(重复重建)
**文件**: `apps/lib/core/notification/services/reminder_scheduler_service.dart`
**问题描述**:
每次获取日历事件都会调用 `upsertEventReminders`,内部执行 `cancelEventReminders` + 重建,即使事件没变化:
```dart
Future<void> upsertEventReminders(event) async {
await cancelEventReminders(event.eventId); // 总是先取消
for (alarm in buildAlarmsForEvent(event)) {
await _scheduleAlarm(alarm); // 总是重建
}
}
```
**影响**:
- 通知 ID 变化导致系统重新调度,浪费资源
- 用户频繁访问日历时产生不必要的操作
**建议修复**:
```dart
Future<void> upsertEventReminders(event) async {
final existing = await _getScheduledAlarm(event.eventId);
if (existing != null && _isSameAlarm(existing, event)) {
return; // 跳过,没变化
}
await cancelEventReminders(event.eventId);
for (alarm in buildAlarmsForEvent(event)) {
await _scheduleAlarm(alarm);
}
}
```
## 相关文件
- `apps/lib/app/app.dart`
- `apps/lib/data/cache/cache_scope.dart`
- `apps/lib/data/cache/cache_store.dart`
- `apps/lib/data/cache/cached_repository.dart`
- `apps/lib/core/inbox/inbox_sync_store.dart`
- `apps/lib/features/chat/presentation/bloc/chat_bloc.dart`
- `apps/lib/app/services/app_prewarm_orchestrator.dart`
- `apps/lib/core/notification/services/reminder_scheduler_service.dart`
- `apps/lib/core/notification/services/reminder_permission_service.dart`
- `apps/lib/core/notification/services/reminder_reconcile_service.dart`
- `apps/assets/reminder_sound/`
-106
View File
@@ -1,106 +0,0 @@
# 路由守卫逻辑分散
## 问题描述
当前路由守卫逻辑分散在两处,可能导致判断不一致:
1. `app_router.dart``redirect()` - 核心守卫逻辑
2. `LinksyApp``BlocListener` - 预留了位置但未使用
## 当前代码
```dart
// app_router.dart
GoRouter createAppRouter(AuthBloc authBloc) {
return GoRouter(
refreshListenable: GoRouterRefreshStream(authBloc.stream),
redirect: (context, state) {
final authState = authBloc.state;
final isAuthenticated = authState is AuthAuthenticated;
// ... 守卫判断逻辑
},
);
}
// app.dart (LinksyApp)
BlocListener<AuthBloc, AuthState>(
listener: (context, state) {
// Handle auth state changes if needed ← 预留但未使用
},
)
```
## 问题
| 问题 | 说明 |
|------|------|
| 逻辑分散 | 守卫在 `redirect()`,但 BlocListener 预留了位置 |
| 隐患 | 将来可能有人在两处都加逻辑,导致不一致 |
| 职责不清 | 到底是 redirect 管跳转,还是 BlocListener 管跳转 |
## 建议方案
**方案1:路由守卫集中在 redirect()(当前方案,保持但清理)**
```dart
// app_router.dart
GoRouter createAppRouter() {
return GoRouter(
refreshListenable: GoRouterRefreshStream(sl<AuthBloc>().stream),
redirect: (context, state) {
// 唯一的守卫逻辑
},
);
}
// LinksyApp - 只做副作用,不做路由跳转
BlocListener<AuthBloc, AuthState>(
listener: (context, state) {
// 埋点、Toast 等副作用
},
)
```
**方案2:路由守卫集中在 BlocListener**
```dart
// app_router.dart - 不再有 redirect
GoRouter createAppRouter() {
return GoRouter(
routes: [...],
);
}
// LinksyApp - 唯一的路由守卫入口
BlocListener<AuthBloc, AuthState>(
listener: (context, state) {
final router = GoRouter.of(context);
if (state is AuthUnauthenticated) {
if (!isPublicRoute(router.matchedLocation)) {
router.go(AppRoutes.authLogin);
}
} else if (state is AuthAuthenticated) {
if (router.matchedLocation == AppRoutes.authLogin) {
router.go(AppRoutes.homeMain);
}
}
},
)
```
## 收益
| 收益 | 说明 |
|------|------|
| 单一职责 | 路由跳转只在一处判断 |
| 可维护 | 将来不会有人误在另一处加逻辑 |
| 清晰 | 开发者知道去哪改守卫逻辑 |
## 涉及文件
- `apps/lib/app/app.dart`
- `apps/lib/app/router/app_router.dart`
## 状态
- [ ] 待修复