修复切换设备每次 都选中在第一个
This commit is contained in:
@@ -45,7 +45,8 @@ class DevicesCubit extends Cubit<DevicesState> {
|
||||
this._selectWorkRecordUseCase,
|
||||
this._saveWorkRecordUseCase,
|
||||
this._generatePathUseCase,
|
||||
this._routePlanningUseCase, this._bindDeviceUseCase
|
||||
this._routePlanningUseCase,
|
||||
this._bindDeviceUseCase,
|
||||
) : super(const DevicesState());
|
||||
|
||||
Future<void> unbindDevice(String deviceId, String deviceName) async {
|
||||
@@ -126,24 +127,42 @@ class DevicesCubit extends Cubit<DevicesState> {
|
||||
}
|
||||
}
|
||||
|
||||
// 获取所有设备列表
|
||||
// 获取所有设备列表
|
||||
Future<void> fetchAllDevices(String username) async {
|
||||
emit(state.copyWith(isLoading: true));
|
||||
|
||||
try {
|
||||
// 网络请求获取列表
|
||||
// 🔥 关键步骤1:记录刷新前的选中设备标识(用 deviceName 作为唯一标识)
|
||||
final String? oldSelectedDeviceName = state.selectedDevice?.deviceName;
|
||||
|
||||
// 网络请求获取新列表
|
||||
var resultEither = await _getUserDeviceUseCase.call(GetUserDeviceParams(username));
|
||||
|
||||
resultEither.fold(
|
||||
(failure) => emit(
|
||||
resultEither.fold((failure) => emit(state.copyWith(isLoading: false, errorMessage: failure.message)), (deviceList) {
|
||||
// 🔥 关键步骤2:匹配新列表中对应的旧选中设备
|
||||
DeviceEntity? newSelectedDevice;
|
||||
if (oldSelectedDeviceName != null && deviceList.isNotEmpty) {
|
||||
// 在新列表中查找和旧选中设备名称一致的设备
|
||||
newSelectedDevice = deviceList.firstWhere(
|
||||
(device) => device.deviceName == oldSelectedDeviceName,
|
||||
// 如果找不到(如设备已解绑),返回 null
|
||||
orElse: () => deviceList.first, // 兜底:选中第一个
|
||||
);
|
||||
} else {
|
||||
// 无旧选中设备,默认选中第一个
|
||||
newSelectedDevice = deviceList.isNotEmpty ? deviceList.first : null;
|
||||
}
|
||||
|
||||
// 🔥 关键步骤3:更新状态,使用匹配后的选中设备
|
||||
emit(
|
||||
state.copyWith(
|
||||
devices: deviceList,
|
||||
selectedDevice: newSelectedDevice, // 保留旧选中设备
|
||||
isLoading: false,
|
||||
errorMessage: failure.message, //
|
||||
),
|
||||
),
|
||||
(deviceList) {
|
||||
emit(state.copyWith(devices: deviceList, selectedDevice: deviceList.isNotEmpty ? deviceList.first : null, isLoading: false));
|
||||
},
|
||||
);
|
||||
);
|
||||
});
|
||||
} catch (e) {
|
||||
emit(state.copyWith(isLoading: false, errorMessage: e.toString()));
|
||||
}
|
||||
@@ -178,6 +197,7 @@ class DevicesCubit extends Cubit<DevicesState> {
|
||||
selectDevice(device);
|
||||
}, (r) => emit(state.copyWith(isLoading: false, selectedDevice: device)));
|
||||
}
|
||||
|
||||
/// 绑定设备
|
||||
Future<void> bindDevice(String deviceId, String deviceAlias) async {
|
||||
emit(state.copyWith(isLoading: true, errorMessage: ''));
|
||||
@@ -186,34 +206,23 @@ class DevicesCubit extends Cubit<DevicesState> {
|
||||
final result = await _bindDeviceUseCase.call(params);
|
||||
result.fold(
|
||||
// 失败处理
|
||||
(failure) => emit(state.copyWith(
|
||||
isLoading: false,
|
||||
errorMessage: failure.message ?? '绑定设备失败',
|
||||
)),
|
||||
(failure) => emit(state.copyWith(isLoading: false, errorMessage: failure.message ?? '绑定设备失败')),
|
||||
// 成功处理(返回 int 状态码)
|
||||
(successCode) {
|
||||
(successCode) {
|
||||
if (successCode == 1) {
|
||||
emit(state.copyWith(isLoading: false, errorMessage: ''));
|
||||
// 绑定成功后刷新设备列表
|
||||
//fetchAllDevices(state.?.username ?? '');
|
||||
//fetchAllDevices(state.?.username ?? '');
|
||||
} else {
|
||||
emit(state.copyWith(
|
||||
isLoading: false,
|
||||
errorMessage: '绑定失败:状态码 $successCode',
|
||||
));
|
||||
emit(state.copyWith(isLoading: false, errorMessage: '绑定失败:状态码 $successCode'));
|
||||
}
|
||||
},
|
||||
);
|
||||
} catch (e) {
|
||||
emit(state.copyWith(
|
||||
isLoading: false,
|
||||
errorMessage: '绑定异常:${e.toString()}',
|
||||
));
|
||||
emit(state.copyWith(isLoading: false, errorMessage: '绑定异常:${e.toString()}'));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// 获取设备位置
|
||||
Future<void> getDeviceLocation(DeviceEntity device) async {
|
||||
emit(state.copyWith(isLoading: true));
|
||||
@@ -282,60 +291,35 @@ class DevicesCubit extends Cubit<DevicesState> {
|
||||
|
||||
result.fold((failure) => emit(state.copyWith(errorMessage: failure.message)), (pathData) => emit(state.copyWith(generatedPath: pathData)));
|
||||
}
|
||||
// 开始路径规划
|
||||
|
||||
// 开始路径规划
|
||||
Future<void> startRoutePlanning(Queue<DeviceAddPathPointModel> locationQueue) async {
|
||||
emit(state.copyWith(isLoading: true));
|
||||
final result = await _routePlanningUseCase.startRoutePlanning(locationQueue);
|
||||
result.fold(
|
||||
(failure) => emit(state.copyWith(
|
||||
isLoading: false,
|
||||
errorMessage: failure.message ?? '路径规划启动失败',
|
||||
)),
|
||||
(_) => emit(state.copyWith(
|
||||
isLoading: false,
|
||||
errorMessage: '',
|
||||
)),
|
||||
(failure) => emit(state.copyWith(isLoading: false, errorMessage: failure.message ?? '路径规划启动失败')),
|
||||
(_) => emit(state.copyWith(isLoading: false, errorMessage: '')),
|
||||
);
|
||||
}
|
||||
|
||||
// 暂停
|
||||
// 暂停
|
||||
Future<void> pauseRoutePlanning() async {
|
||||
emit(state.copyWith(isLoading: true));
|
||||
final result = await _routePlanningUseCase.pauseRPWork();
|
||||
result.fold(
|
||||
(failure) => emit(state.copyWith(
|
||||
isLoading: false,
|
||||
errorMessage: failure.message ?? '暂停失败',
|
||||
)),
|
||||
(_) => emit(state.copyWith(isLoading: false)),
|
||||
);
|
||||
result.fold((failure) => emit(state.copyWith(isLoading: false, errorMessage: failure.message ?? '暂停失败')), (_) => emit(state.copyWith(isLoading: false)));
|
||||
}
|
||||
|
||||
// 恢复
|
||||
// 恢复
|
||||
Future<void> resumeRoutePlanning() async {
|
||||
emit(state.copyWith(isLoading: true));
|
||||
final result = await _routePlanningUseCase.resumeRPWork();
|
||||
result.fold(
|
||||
(failure) => emit(state.copyWith(
|
||||
isLoading: false,
|
||||
errorMessage: failure.message ?? '恢复失败',
|
||||
)),
|
||||
(_) => emit(state.copyWith(isLoading: false)),
|
||||
);
|
||||
result.fold((failure) => emit(state.copyWith(isLoading: false, errorMessage: failure.message ?? '恢复失败')), (_) => emit(state.copyWith(isLoading: false)));
|
||||
}
|
||||
|
||||
// 停止
|
||||
// 停止
|
||||
Future<void> stopRoutePlanning() async {
|
||||
emit(state.copyWith(isLoading: true));
|
||||
final result = await _routePlanningUseCase.stopRoutePlanning();
|
||||
result.fold(
|
||||
(failure) => emit(state.copyWith(
|
||||
isLoading: false,
|
||||
errorMessage: failure.message ?? '停止失败',
|
||||
)),
|
||||
(_) => emit(state.copyWith(isLoading: false)),
|
||||
);
|
||||
result.fold((failure) => emit(state.copyWith(isLoading: false, errorMessage: failure.message ?? '停止失败')), (_) => emit(state.copyWith(isLoading: false)));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user