Files
feature-next-arch/lib/features/my/presentation/bloc/my_cubit.dart
2026-08-07 08:49:29 +08:00

56 lines
2.2 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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 依赖注入)
}
/// 🔥 退出登录时清空所有状态(昵称等个人信息)
void clearAll() {
if (!isClosed) {
emit(const MyState());
}
}
}