空洞逻辑

This commit is contained in:
mmc
2026-03-02 13:25:21 +08:00
parent 991c4fe174
commit fe9b763783
4 changed files with 300 additions and 168 deletions

View File

@@ -37,17 +37,11 @@ class DevicesCubit extends Cubit<DevicesState> {
this._updateDevicename,
this._selectWorkRecordUseCase,
this._saveWorkRecordUseCase,
this._generatePathUseCase
this._generatePathUseCase,
) : super(const DevicesState());
Future<void> unbindDevice(String deviceId, String deviceName) async {
emit(
state.copyWith(
isLoading: true,
errorMessage: '',
operationType: DeviceOperationType.unbind,
),
);
emit(state.copyWith(isLoading: true, errorMessage: '', operationType: DeviceOperationType.unbind));
try {
final params = UnbindDeviceParams(deviceId, deviceName);
@@ -73,93 +67,54 @@ class DevicesCubit extends Cubit<DevicesState> {
state.copyWith(
isLoading: false,
devices: updatedDevices,
selectedDevice: state.selectedDevice?.deviceName == deviceId
? null
: state.selectedDevice,
selectedDevice: state.selectedDevice?.deviceName == deviceId ? null : state.selectedDevice,
errorMessage: '',
operationType: DeviceOperationType.none,
),
);
} else {
emit(
state.copyWith(
isLoading: false,
errorMessage: '解绑失败:状态码 $successCode',
operationType: DeviceOperationType.none,
),
);
emit(state.copyWith(isLoading: false, errorMessage: '解绑失败:状态码 $successCode', operationType: DeviceOperationType.none));
}
},
);
} catch (e) {
emit(
state.copyWith(
isLoading: false,
errorMessage: '解绑异常:${e.toString()}',
operationType: DeviceOperationType.none,
),
);
emit(state.copyWith(isLoading: false, errorMessage: '解绑异常:${e.toString()}', operationType: DeviceOperationType.none));
}
}
Future<void> updateDeviceName(String deviceId, String deviceName) async {
emit(
state.copyWith(
isLoading: true,
errorMessage: '',
operationType: DeviceOperationType.updateName,
),
);
emit(state.copyWith(isLoading: true, errorMessage: '', operationType: DeviceOperationType.updateName));
try {
final params = UpdateDevicenameParams(deviceId, deviceName);
final result = await _updateDevicename.call(params);
result.fold(
(failure) => emit(
state.copyWith(
isLoading: false,
errorMessage: failure.message ?? '更新设备名称失败',
operationType: DeviceOperationType.none,
),
),
(successCode) {
// 核心修复:int 转 bool 条件判断
print('更新设备名称结果代码: $successCode'); // 调试输出结果代码
final isSuccess = successCode == 1; // 显式转为 bool
if (isSuccess) {
//final updatedDevices = state.devices?.map((device) {
// return device.deviceName == deviceId ? device.copyWith(deviceName: deviceName) : device;
//}).toList();
result.fold((failure) => emit(state.copyWith(isLoading: false, errorMessage: failure.message ?? '更新设备名称失败', operationType: DeviceOperationType.none)), (
successCode,
) {
// 核心修复:int 转 bool 条件判断
print('更新设备名称结果代码: $successCode'); // 调试输出结果代码
final isSuccess = successCode == 1; // 显式转为 bool
if (isSuccess) {
//final updatedDevices = state.devices?.map((device) {
// return device.deviceName == deviceId ? device.copyWith(deviceName: deviceName) : device;
//}).toList();
emit(
state.copyWith(
isLoading: false,
//devices: updatedDevices,
//selectedDevice: state.selectedDevice?.deviceName == deviceId ? state.selectedDevice?.copyWith(deviceName: deviceName) : state.selectedDevice,
errorMessage: '',
operationType: DeviceOperationType.none,
),
);
} else {
emit(
state.copyWith(
isLoading: false,
errorMessage: '更新设备名称失败:状态码 $successCode',
operationType: DeviceOperationType.none,
),
);
}
},
);
emit(
state.copyWith(
isLoading: false,
//devices: updatedDevices,
//selectedDevice: state.selectedDevice?.deviceName == deviceId ? state.selectedDevice?.copyWith(deviceName: deviceName) : state.selectedDevice,
errorMessage: '',
operationType: DeviceOperationType.none,
),
);
} else {
emit(state.copyWith(isLoading: false, errorMessage: '更新设备名称失败:状态码 $successCode', operationType: DeviceOperationType.none));
}
});
} catch (e) {
emit(
state.copyWith(
isLoading: false,
errorMessage: '更新设备名称异常:${e.toString()}',
operationType: DeviceOperationType.none,
),
);
emit(state.copyWith(isLoading: false, errorMessage: '更新设备名称异常:${e.toString()}', operationType: DeviceOperationType.none));
}
}
@@ -168,9 +123,7 @@ class DevicesCubit extends Cubit<DevicesState> {
emit(state.copyWith(isLoading: true));
try {
// 网络请求获取列表
var resultEither = await _getUserDeviceUseCase.call(
GetUserDeviceParams(username),
);
var resultEither = await _getUserDeviceUseCase.call(GetUserDeviceParams(username));
resultEither.fold(
(failure) => emit(
@@ -180,13 +133,7 @@ class DevicesCubit extends Cubit<DevicesState> {
),
),
(deviceList) {
emit(
state.copyWith(
devices: deviceList,
selectedDevice: deviceList.isNotEmpty ? deviceList.first : null,
isLoading: false,
),
);
emit(state.copyWith(devices: deviceList, selectedDevice: deviceList.isNotEmpty ? deviceList.first : null, isLoading: false));
},
);
} catch (e) {
@@ -206,10 +153,7 @@ class DevicesCubit extends Cubit<DevicesState> {
}).toList();
// 如果更新的是当前选中的设备,也要同步更新 selectedDevice
final newSelected =
state.selectedDevice?.deviceName == updatedDevice.deviceName
? updatedDevice
: state.selectedDevice;
final newSelected = state.selectedDevice?.deviceName == updatedDevice.deviceName ? updatedDevice : state.selectedDevice;
emit(state.copyWith(devices: newList, selectedDevice: newSelected));
}
@@ -232,15 +176,8 @@ class DevicesCubit extends Cubit<DevicesState> {
emit(state.copyWith(isLoading: true));
final result = await _getDeviceLocationUseCase.call(device.deviceName);
result.fold(
(failure) =>
emit(state.copyWith(isLoading: false, errorMessage: failure.message)),
(location) => emit(
state.copyWith(
isLoading: false,
deviceLatitude: location.latitude,
deviceLongitude: location.longitude,
),
),
(failure) => emit(state.copyWith(isLoading: false, errorMessage: failure.message)),
(location) => emit(state.copyWith(isLoading: false, deviceLatitude: location.latitude, deviceLongitude: location.longitude)),
);
}
@@ -249,8 +186,7 @@ class DevicesCubit extends Cubit<DevicesState> {
emit(state.copyWith(isLoading: true));
final result = await _getWorkRecordUseCase(userId);
result.fold(
(failure) =>
emit(state.copyWith(isLoading: false, errorMessage: failure.message)),
(failure) => emit(state.copyWith(isLoading: false, errorMessage: failure.message)),
(records) => emit(state.copyWith(isLoading: false, workRecords: records)),
);
}
@@ -260,16 +196,8 @@ class DevicesCubit extends Cubit<DevicesState> {
emit(state.copyWith(isLoading: true));
final result = await _deleteWorkRecordUseCase(workName);
result.fold(
(failure) =>
emit(state.copyWith(isLoading: false, errorMessage: failure.message)),
(_) => emit(
state.copyWith(
isLoading: false,
workRecords: state.workRecords
?.where((record) => record != workName)
.toList(),
),
),
(failure) => emit(state.copyWith(isLoading: false, errorMessage: failure.message)),
(_) => emit(state.copyWith(isLoading: false, workRecords: state.workRecords?.where((record) => record != workName).toList())),
);
}
@@ -279,28 +207,21 @@ class DevicesCubit extends Cubit<DevicesState> {
try {
final result = await _selectWorkRecordUseCase.call(workName);
result.fold(
(failure) => emit(
state.copyWith(isLoading: false, errorMessage: failure.message),
),
(failure) => emit(state.copyWith(isLoading: false, errorMessage: failure.message)),
(data) => emit(state.copyWith(isLoading: false, pathData: data)),
);
} catch (e) {
emit(state.copyWith(isLoading: false, errorMessage: e.toString()));
}
}
/// 保存路径数据
Future<void> saveWorkRecord(String workName, String userId, String jsonData) async {
emit(state.copyWith(isLoading: true));
final result = await _saveWorkRecordUseCase.call(
workName: workName,
userId: userId,
jsonData: jsonData,
);
result.fold(
(failure) => emit(state.copyWith(isLoading: false, errorMessage: failure.message)),
(data) => emit(state.copyWith(isLoading: false)),
);
final result = await _saveWorkRecordUseCase.call(workName: workName, userId: userId, jsonData: jsonData);
result.fold((failure) => emit(state.copyWith(isLoading: false, errorMessage: failure.message)), (data) => emit(state.copyWith(isLoading: false)));
}
/// generatePath
Future<void> generatePath({
required ReferencePoint reference,
@@ -309,22 +230,13 @@ class DevicesCubit extends Cubit<DevicesState> {
required List<HoleBoundary> holes,
required int workType,
}) async {
holes.asMap().forEach((index, hole) {
final pointsStr = hole.position.map((p) => "(${p.lat.toStringAsFixed(6)}, ${p.lon.toStringAsFixed(6)})").join(', ');
debugPrint('第${index + 1}组holes:$pointsStr');
});
emit(state.copyWith(isLoading: true));
final result = await _generatePathUseCase.execute(
reference: reference,
heading: heading,
outer: outer,
holes: holes,
workType: workType,
);
result.fold(
(failure) => emit(state.copyWith(errorMessage: failure.message)),
(pathData) => emit(state.copyWith(generatedPath: pathData)),
);
final result = await _generatePathUseCase.execute(reference: reference, heading: heading, outer: outer, holes: holes, workType: workType);
result.fold((failure) => emit(state.copyWith(errorMessage: failure.message)), (pathData) => emit(state.copyWith(generatedPath: pathData)));
}
}