解绑设备完成

This commit is contained in:
mmc
2026-02-27 14:16:29 +08:00
parent 6e6986a49a
commit 5170eeb4e7
3 changed files with 152 additions and 67 deletions

View File

@@ -27,53 +27,50 @@ class DevicesCubit extends Cubit<DevicesState> {
) : super(const DevicesState());
Future<void> unbindDevice(String deviceId, String deviceName) async {
// 1. 加载中状态
emit(state.copyWith(isLoading: true, errorMessage: ''));
try {
// 2. 构建参数
final params = UnbindDeviceParams(deviceId, deviceName);
// 3. 调用 UseCase(核心:执行解绑逻辑)
final result = await _unbindDeviceUseCase.call(params);
// 4. 处理 UseCase 返回的 Either 结果
result.fold(
// 失败:更新错误状态
(failure) => emit(
state.copyWith(
isLoading: false,
errorMessage: failure.message ?? '解绑设备失败',
),
),
// 成功:更新状态(比如从设备列表中移除该设备)
(successCode) {
// 过滤掉已解绑的设备
final updatedDevices = state.devices?.where((device) {
return device.deviceAlias != deviceId &&
device.deviceName != deviceName;
}).toList();
// 核心修复:int 转 bool 条件判断
final isSuccess = successCode == 1; // 显式转为 bool
if (isSuccess) {
final updatedDevices = state.devices?.where((device) {
return device.deviceName != deviceId;
}).toList();
// 更新状态:清空加载、更新设备列表、清空选中设备(如果解绑的是当前选中设备)
emit(
state.copyWith(
isLoading: false,
devices: updatedDevices,
selectedDevice: state.selectedDevice?.deviceAlias == deviceId
? null
: state.selectedDevice,
errorMessage: '',
),
);
emit(
state.copyWith(
isLoading: false,
devices: updatedDevices,
selectedDevice: state.selectedDevice?.deviceName == deviceId
? null
: state.selectedDevice,
errorMessage: '',
),
);
} else {
emit(
state.copyWith(
isLoading: false,
errorMessage: '解绑失败:状态码 $successCode',
),
);
}
},
);
} catch (e) {
// 捕获其他异常
emit(
state.copyWith(
isLoading: false,
errorMessage: '解绑设备异常:${e.toString()}',
),
state.copyWith(isLoading: false, errorMessage: '解绑异常:${e.toString()}'),
);
}
}