feat(chat): 发收消息全量实现 (#25~#28)
- 移除 @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>
This commit is contained in:
@@ -3,9 +3,6 @@ import 'package:im_app/app/di/db_provider.dart';
|
||||
import 'package:im_app/data/repositories/api_retry_repository_impl.dart';
|
||||
import 'package:im_app/domain/entities/api_retry.dart';
|
||||
import 'package:im_app/domain/repositories/api_retry_repository.dart';
|
||||
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
||||
|
||||
part 'api_retry_provider.g.dart';
|
||||
|
||||
// ── Repository ────────────────────────────────────────────────────────────────
|
||||
|
||||
@@ -16,13 +13,11 @@ final apiRetryRepositoryProvider = Provider<ApiRetryRepository>((ref) {
|
||||
// ── Streams ───────────────────────────────────────────────────────────────────
|
||||
|
||||
/// 监听所有重试任务
|
||||
@riverpod
|
||||
Stream<List<ApiRetry>> allApiRetries(Ref ref) {
|
||||
final allApiRetriesProvider = StreamProvider<List<ApiRetry>>((ref) {
|
||||
return ref.watch(apiRetryRepositoryProvider).watchAll();
|
||||
}
|
||||
});
|
||||
|
||||
/// 监听未同步的重试任务
|
||||
@riverpod
|
||||
Stream<List<ApiRetry>> pendingApiRetries(Ref ref) {
|
||||
final pendingApiRetriesProvider = StreamProvider<List<ApiRetry>>((ref) {
|
||||
return ref.watch(apiRetryRepositoryProvider).watchPending();
|
||||
}
|
||||
});
|
||||
|
||||
@@ -3,9 +3,6 @@ import 'package:im_app/app/di/db_provider.dart';
|
||||
import 'package:im_app/data/repositories/sound_repository_impl.dart';
|
||||
import 'package:im_app/domain/entities/sound.dart';
|
||||
import 'package:im_app/domain/repositories/sound_repository.dart';
|
||||
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
||||
|
||||
part 'sound_provider.g.dart';
|
||||
|
||||
// ── Repository ────────────────────────────────────────────────────────────────
|
||||
|
||||
@@ -16,19 +13,16 @@ final soundRepositoryProvider = Provider<SoundRepository>((ref) {
|
||||
// ── Streams ───────────────────────────────────────────────────────────────────
|
||||
|
||||
/// 监听所有音效
|
||||
@riverpod
|
||||
Stream<List<Sound>> allSounds(Ref ref) {
|
||||
final allSoundsProvider = StreamProvider<List<Sound>>((ref) {
|
||||
return ref.watch(soundRepositoryProvider).watchAll();
|
||||
}
|
||||
});
|
||||
|
||||
/// 监听指定类型音效
|
||||
@riverpod
|
||||
Stream<List<Sound>> soundsByType(Ref ref, int typ) {
|
||||
final soundsByTypeProvider = StreamProvider.family<List<Sound>, int>((ref, typ) {
|
||||
return ref.watch(soundRepositoryProvider).watchByType(typ);
|
||||
}
|
||||
});
|
||||
|
||||
/// 监听指定音效
|
||||
@riverpod
|
||||
Stream<Sound?> soundById(Ref ref, int id) {
|
||||
final soundByIdProvider = StreamProvider.family<Sound?, int>((ref, id) {
|
||||
return ref.watch(soundRepositoryProvider).watchById(id);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -3,9 +3,6 @@ import 'package:im_app/app/di/db_provider.dart';
|
||||
import 'package:im_app/data/repositories/tag_repository_impl.dart';
|
||||
import 'package:im_app/domain/entities/tag.dart';
|
||||
import 'package:im_app/domain/repositories/tag_repository.dart';
|
||||
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
||||
|
||||
part 'tag_provider.g.dart';
|
||||
|
||||
// ── Repository ────────────────────────────────────────────────────────────────
|
||||
|
||||
@@ -16,25 +13,21 @@ final tagRepositoryProvider = Provider<TagRepository>((ref) {
|
||||
// ── Streams ───────────────────────────────────────────────────────────────────
|
||||
|
||||
/// 监听所有标签
|
||||
@riverpod
|
||||
Stream<List<Tag>> allTags(Ref ref) {
|
||||
final allTagsProvider = StreamProvider<List<Tag>>((ref) {
|
||||
return ref.watch(tagRepositoryProvider).watchAll();
|
||||
}
|
||||
});
|
||||
|
||||
/// 监听指定 uid 的标签
|
||||
@riverpod
|
||||
Stream<List<Tag>> tagsByUid(Ref ref, int uid) {
|
||||
final tagsByUidProvider = StreamProvider.family<List<Tag>, int>((ref, uid) {
|
||||
return ref.watch(tagRepositoryProvider).watchByUid(uid);
|
||||
}
|
||||
});
|
||||
|
||||
/// 监听指定类型的标签
|
||||
@riverpod
|
||||
Stream<List<Tag>> tagsByType(Ref ref, int type) {
|
||||
final tagsByTypeProvider = StreamProvider.family<List<Tag>, int>((ref, type) {
|
||||
return ref.watch(tagRepositoryProvider).watchByType(type);
|
||||
}
|
||||
});
|
||||
|
||||
/// 监听指定标签
|
||||
@riverpod
|
||||
Stream<Tag?> tagById(Ref ref, int id) {
|
||||
final tagByIdProvider = StreamProvider.family<Tag?, int>((ref, id) {
|
||||
return ref.watch(tagRepositoryProvider).watchById(id);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -2,20 +2,16 @@ import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:im_app/features/chat/usecases/delete_users_use_case.dart';
|
||||
import 'package:im_app/features/chat/usecases/insert_users_use_case.dart';
|
||||
import 'package:im_app/features/chat/usecases/update_users_use_case.dart';
|
||||
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
||||
import 'package:im_app/app/di/db_provider.dart';
|
||||
import 'package:im_app/data/repositories/user_repository_impl.dart';
|
||||
import 'package:im_app/domain/entities/user.dart';
|
||||
import 'package:im_app/domain/repositories/user_repository.dart';
|
||||
|
||||
part 'user_provider.g.dart';
|
||||
|
||||
// ── Repository ────────────────────────────────────────────────────────────────
|
||||
|
||||
@riverpod
|
||||
UserRepository userRepository(Ref ref) {
|
||||
final userRepositoryProvider = Provider<UserRepository>((ref) {
|
||||
return UserRepositoryImpl(ref.watch(storageSdkProvider));
|
||||
}
|
||||
});
|
||||
|
||||
// ── Use Cases ─────────────────────────────────────────────────────────────────
|
||||
|
||||
@@ -42,12 +38,10 @@ final deleteUsersUseCaseProvider = Provider<DeleteUsersUseCase>((ref) {
|
||||
|
||||
// ── Streams ───────────────────────────────────────────────────────────────────
|
||||
|
||||
@riverpod
|
||||
Stream<List<User>> users(Ref ref, Set<int> uids) {
|
||||
final usersProvider = StreamProvider.family<List<User>, Set<int>>((ref, uids) {
|
||||
return ref.watch(userRepositoryProvider).watchUsers(uids.toList());
|
||||
}
|
||||
});
|
||||
|
||||
@riverpod
|
||||
Stream<List<User>> allUsers(Ref ref) {
|
||||
final allUsersProvider = StreamProvider<List<User>>((ref) {
|
||||
return ref.watch(userRepositoryProvider).watchAllUsers();
|
||||
}
|
||||
});
|
||||
|
||||
@@ -1,10 +1,8 @@
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:im_app/app/di/user_provider.dart';
|
||||
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
||||
import 'package:im_app/domain/entities/user.dart';
|
||||
import 'package:im_app/domain/repositories/user_repository.dart';
|
||||
|
||||
part 'user_notifier.g.dart';
|
||||
|
||||
/// 单个用户状态管理 (family — 每个 uid 独立 notifier)
|
||||
///
|
||||
/// ## 用法
|
||||
@@ -26,22 +24,21 @@ part 'user_notifier.g.dart';
|
||||
/// // 批量更新
|
||||
/// ref.read(userNotifierProvider(123).notifier).updateUsers(updatedList);
|
||||
/// ```
|
||||
@riverpod
|
||||
class UserNotifier extends _$UserNotifier {
|
||||
class UserNotifier extends FamilyAsyncNotifier<User?, int> {
|
||||
User? _cached;
|
||||
|
||||
UserRepository get _repo => ref.watch(userRepositoryProvider);
|
||||
|
||||
@override
|
||||
Future<User?> build(int uid) async {
|
||||
Future<User?> build(int arg) async {
|
||||
ref.onDispose(() => _cached = null);
|
||||
|
||||
_repo.watchUser(uid).listen((user) {
|
||||
_repo.watchUser(arg).listen((user) {
|
||||
_cached = user;
|
||||
state = AsyncData(user);
|
||||
});
|
||||
|
||||
return _repo.getUser(uid);
|
||||
return _repo.getUser(arg);
|
||||
}
|
||||
|
||||
// ── 即时访问,无需 await ──────────────────────────────────────────────────
|
||||
@@ -56,23 +53,11 @@ class UserNotifier extends _$UserNotifier {
|
||||
}
|
||||
|
||||
/// 更新单个用户所有字段,按 uid 匹配
|
||||
///
|
||||
/// 示例:
|
||||
/// ```dart
|
||||
/// await notifier.updateUser(user.copyWith(nickname: 'New Name'));
|
||||
/// ```
|
||||
Future<void> updateUser(User user) async {
|
||||
await _repo.updateUser(user);
|
||||
}
|
||||
|
||||
/// 批量更新用户,每条按 uid 匹配更新所有字段
|
||||
///
|
||||
/// 示例:
|
||||
/// ```dart
|
||||
/// await notifier.updateUsers(
|
||||
/// users.map((u) => u.copyWith(nickname: 'new')).toList(),
|
||||
/// );
|
||||
/// ```
|
||||
Future<void> updateUsers(List<User> users) async {
|
||||
await _repo.updateUsersBatch(users);
|
||||
}
|
||||
@@ -80,8 +65,11 @@ class UserNotifier extends _$UserNotifier {
|
||||
// ── 删除 ─────────────────────────────────────────────────────────────────
|
||||
|
||||
Future<void> deleteUser() async {
|
||||
await _repo.deleteUser(uid);
|
||||
await _repo.deleteUser(arg);
|
||||
_cached = null;
|
||||
state = const AsyncData(null);
|
||||
}
|
||||
}
|
||||
|
||||
final userNotifierProvider =
|
||||
AsyncNotifierProvider.family<UserNotifier, User?, int>(UserNotifier.new);
|
||||
|
||||
Reference in New Issue
Block a user