我的界面 修改昵称完成
This commit is contained in:
48
lib/features/my/presentation/bloc/my_cubit.dart
Normal file
48
lib/features/my/presentation/bloc/my_cubit.dart
Normal file
@@ -0,0 +1,48 @@
|
||||
import 'dart:collection';
|
||||
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:maibu_satabot_v2/core/di/injection.dart';
|
||||
import 'package:maibu_satabot_v2/core/storage/user_storage.dart';
|
||||
|
||||
import 'package:maibu_satabot_v2/features/devices/domain/usecases/unbind_device_usecase.dart';
|
||||
import 'package:maibu_satabot_v2/features/my/presentation/bloc/my_state.dart';
|
||||
import 'package:maibu_satabot_v2/features/my/repository/my_repository.dart';
|
||||
import 'package:maibu_satabot_v2/features/my/usecases/updatename_usecase.dart';
|
||||
|
||||
class MyCubit extends Cubit<MyState> {
|
||||
// 1. 修正变量命名:小驼峰规范(_updateNameUsecase)
|
||||
final UpdateNameUsecase _updateNameUsecase;
|
||||
final MyRepository repository;
|
||||
|
||||
// 2. 构造函数参数顺序与命名对齐
|
||||
MyCubit(this.repository, this._updateNameUsecase) : super(const MyState());
|
||||
|
||||
Future<void> updateName(String nickName) async {
|
||||
emit(state.copyWith(isLoading: true, errorMessage: ''));
|
||||
|
||||
try {
|
||||
final params = UpdateNameParams(nickName);
|
||||
final result = await _updateNameUsecase(params);
|
||||
print("$result 修改名称结构");
|
||||
|
||||
// 4. 恢复 fold 逻辑,处理 UseCase 返回结果(关键:更新 UI 状态)
|
||||
result.fold((failure) => emit(state.copyWith(isLoading: false, errorMessage: failure.message ?? '修改昵称失败')), (successCode) {
|
||||
print("$successCode 修改名称结构");
|
||||
final isSuccess = successCode == 200;
|
||||
if (isSuccess) {
|
||||
emit(state.copyWith(isLoading: false, errorMessage: '', nickName: nickName));
|
||||
} else {
|
||||
emit(state.copyWith(isLoading: false, errorMessage: '修改昵称失败:状态码 $successCode'));
|
||||
}
|
||||
});
|
||||
} catch (e) {
|
||||
emit(state.copyWith(isLoading: false, errorMessage: '修改昵称异常:${e.toString()}'));
|
||||
}
|
||||
}
|
||||
|
||||
// 保留解绑设备方法(如果需要)
|
||||
Future<void> unbindDevice(String deviceId, String deviceName) async {
|
||||
emit(state.copyWith(isLoading: true, errorMessage: ''));
|
||||
// 解绑逻辑(如需保留,需补充 UnbindDeviceUsecase 依赖注入)
|
||||
}
|
||||
}
|
||||
22
lib/features/my/presentation/bloc/my_state.dart
Normal file
22
lib/features/my/presentation/bloc/my_state.dart
Normal file
@@ -0,0 +1,22 @@
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:maibu_satabot_v2/features/devices/domain/entities/device_entity.dart';
|
||||
|
||||
class MyState extends Equatable {
|
||||
final bool isLoading; // 是否正在加载
|
||||
final String? errorMessage; // 错误信息
|
||||
final String? nickName;
|
||||
|
||||
const MyState({this.isLoading = false, this.errorMessage, this.nickName});
|
||||
|
||||
// 使用 copyWith 方便局部更新状态
|
||||
MyState copyWith({bool? isLoading, String? errorMessage, String? nickName}) {
|
||||
return MyState(
|
||||
isLoading: isLoading ?? this.isLoading,
|
||||
errorMessage: errorMessage, // 错误信息通常每次更新都要重新赋值或清空
|
||||
nickName: nickName,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
List<Object?> get props => [isLoading, errorMessage, nickName];
|
||||
}
|
||||
Reference in New Issue
Block a user