业务更新User所需(企业成员、聊天室群组成员)
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
import 'package:im_app/app/di/user_provider.dart';
|
||||
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
||||
import 'package:im_app/data/local/drift/app_database.dart';
|
||||
import 'package:im_app/domain/entities/user.dart';
|
||||
import 'package:im_app/domain/repositories/user_repository.dart';
|
||||
|
||||
@@ -16,10 +15,16 @@ part 'user_notifier.g.dart';
|
||||
/// // 即时读取,无需 await
|
||||
/// final user = ref.read(userNotifierProvider(123).notifier).current;
|
||||
///
|
||||
/// // 部分更新
|
||||
/// ref.read(userNotifierProvider(123).notifier).updateFields(
|
||||
/// UsersCompanion(nickname: Value('New Name')),
|
||||
/// // 插入或替换
|
||||
/// 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);
|
||||
/// ```
|
||||
@riverpod
|
||||
class UserNotifier extends _$UserNotifier {
|
||||
@@ -31,13 +36,11 @@ class UserNotifier extends _$UserNotifier {
|
||||
Future<User?> build(int uid) async {
|
||||
ref.onDispose(() => _cached = null);
|
||||
|
||||
// Stream starts automatically — uid is the family arg
|
||||
_repo.watchUser(uid).listen((user) {
|
||||
_cached = user;
|
||||
state = AsyncData(user);
|
||||
});
|
||||
|
||||
// Return initial DB value
|
||||
return _repo.getUser(uid);
|
||||
}
|
||||
|
||||
@@ -47,16 +50,31 @@ class UserNotifier extends _$UserNotifier {
|
||||
|
||||
// ── 写入 ─────────────────────────────────────────────────────────────────
|
||||
|
||||
Future<void> saveUser(User user) async {
|
||||
await _repo.saveUser(user);
|
||||
}
|
||||
|
||||
Future<void> updateFields(UsersCompanion companion) async {
|
||||
await _repo.updateFields(uid, companion); // uid from build arg
|
||||
/// 插入或替换单个用户
|
||||
Future<void> insertOrReplaceUser(User user) async {
|
||||
await _repo.insertOrReplaceUser(user);
|
||||
}
|
||||
|
||||
/// 更新单个用户所有字段,按 uid 匹配
|
||||
///
|
||||
/// 示例:
|
||||
/// ```dart
|
||||
/// await notifier.updateUser(user.copyWith(nickname: 'New Name'));
|
||||
/// ```
|
||||
Future<void> updateUser(User user) async {
|
||||
await _repo.saveUser(user);
|
||||
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);
|
||||
}
|
||||
|
||||
// ── 删除 ─────────────────────────────────────────────────────────────────
|
||||
|
||||
Reference in New Issue
Block a user