- 移除 @riverpod/@freezed 注解依赖,全部改为手写 Provider(无需 build_runner) · LoginState 改为纯 Dart,LoginViewModel/ThemeViewModel/ChatViewModel 改为 Notifier · UserNotifier 改为 FamilyAsyncNotifier<User?,int>,mini_app_provider 改为手写 Provider · 15 个 StreamProvider/StreamProvider.family 从 @riverpod 迁移至手写 - 发送消息(#25) · SendMessageRequest/SendMessageResponse DTO · SendMessageUseCase:乐观写入 DB → HTTP POST → 更新 Chat 摘要 - 接收消息 WS(#26) · WsMessageService:监听 mode2 WS 帧 → HTTP 补拉 → DB 写入 → Chat 更新 · FetchHistoryRequest/FetchHistoryResponse DTO(GET /app/api/chat/history) · FetchHistoryUseCase:拉取 → insertOrReplaceAll - DI 装配(chat_service_providers.dart) · wsMessageServiceProvider、sendMessageUseCaseProvider、fetchHistoryUseCaseProvider - 聊天列表页(#27) · ChatListViewModel(Notifier<void>)+ chat_page.dart 真实会话列表 UI · ListTile:头像首字母、最新消息摘要、未读角标、时间格式化 - 聊天详情页(#28) · ChatDetailViewModel(FamilyNotifier<ChatDetailState,int>)+ chat_detail_page.dart · 消息气泡(自己/他人分左右)、底部输入框、发送状态与错误提示 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
76 lines
2.5 KiB
Dart
76 lines
2.5 KiB
Dart
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
import 'package:im_app/app/di/user_provider.dart';
|
|
import 'package:im_app/domain/entities/user.dart';
|
|
import 'package:im_app/domain/repositories/user_repository.dart';
|
|
|
|
/// 单个用户状态管理 (family — 每个 uid 独立 notifier)
|
|
///
|
|
/// ## 用法
|
|
/// ```dart
|
|
/// // 监听 — 自动重建
|
|
/// final userAsync = ref.watch(userNotifierProvider(123));
|
|
///
|
|
/// // 即时读取,无需 await
|
|
/// final user = ref.read(userNotifierProvider(123).notifier).current;
|
|
///
|
|
/// // 插入或替换
|
|
/// ref.read(userNotifierProvider(123).notifier).insertOrReplaceUser(user);
|
|
///
|
|
/// // 单个更新
|
|
/// ref.read(userNotifierProvider(123).notifier).updateUser(
|
|
/// user.copyWith(nickname: 'New Name'),
|
|
/// );
|
|
///
|
|
/// // 批量更新
|
|
/// ref.read(userNotifierProvider(123).notifier).updateUsers(updatedList);
|
|
/// ```
|
|
class UserNotifier extends FamilyAsyncNotifier<User?, int> {
|
|
User? _cached;
|
|
|
|
UserRepository get _repo => ref.watch(userRepositoryProvider);
|
|
|
|
@override
|
|
Future<User?> build(int arg) async {
|
|
ref.onDispose(() => _cached = null);
|
|
|
|
_repo.watchUser(arg).listen((user) {
|
|
_cached = user;
|
|
state = AsyncData(user);
|
|
});
|
|
|
|
return _repo.getUser(arg);
|
|
}
|
|
|
|
// ── 即时访问,无需 await ──────────────────────────────────────────────────
|
|
|
|
User? get current => _cached;
|
|
|
|
// ── 写入 ─────────────────────────────────────────────────────────────────
|
|
|
|
/// 插入或替换单个用户
|
|
Future<void> insertOrReplaceUser(User user) async {
|
|
await _repo.insertOrReplaceUser(user);
|
|
}
|
|
|
|
/// 更新单个用户所有字段,按 uid 匹配
|
|
Future<void> updateUser(User user) async {
|
|
await _repo.updateUser(user);
|
|
}
|
|
|
|
/// 批量更新用户,每条按 uid 匹配更新所有字段
|
|
Future<void> updateUsers(List<User> users) async {
|
|
await _repo.updateUsersBatch(users);
|
|
}
|
|
|
|
// ── 删除 ─────────────────────────────────────────────────────────────────
|
|
|
|
Future<void> deleteUser() async {
|
|
await _repo.deleteUser(arg);
|
|
_cached = null;
|
|
state = const AsyncData(null);
|
|
}
|
|
}
|
|
|
|
final userNotifierProvider =
|
|
AsyncNotifierProvider.family<UserNotifier, User?, int>(UserNotifier.new);
|