路径规划作业列表 其余界面优化

This commit is contained in:
mmc
2026-02-27 13:17:09 +08:00
parent e690ec7993
commit 6e6986a49a
14 changed files with 625 additions and 360 deletions

View File

@@ -2,6 +2,7 @@ import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:maibu_satabot_v2/features/devices/domain/entities/device_entity.dart';
import 'package:maibu_satabot_v2/features/devices/domain/repositories/device_repository.dart';
import 'package:maibu_satabot_v2/features/devices/domain/usecases/get_user_device_usecase.dart';
import 'package:maibu_satabot_v2/features/devices/domain/usecases/unbind_device_usecase.dart';
import '../../domain/usecases/delete_work_record_usecase.dart';
import '../../domain/usecases/get_device_location_usecase.dart';
@@ -14,15 +15,68 @@ class DevicesCubit extends Cubit<DevicesState> {
final DeviceRepository repository;
final GetWorkRecordUseCase _getWorkRecordUseCase;
final DeleteWorkRecordUseCase _deleteWorkRecordUseCase;
final UnbindDeviceUseCase _unbindDeviceUseCase;
DevicesCubit(
this.repository,
this._getUserDeviceUseCase,
this._getDeviceLocationUseCase,
this._getWorkRecordUseCase,
this._deleteWorkRecordUseCase,
this._unbindDeviceUseCase,
) : super(const DevicesState());
DevicesCubit(this.repository,
this._getUserDeviceUseCase,
this._getDeviceLocationUseCase,
this._getWorkRecordUseCase,
this._deleteWorkRecordUseCase,
)
: 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();
// 更新状态:清空加载、更新设备列表、清空选中设备(如果解绑的是当前选中设备)
emit(
state.copyWith(
isLoading: false,
devices: updatedDevices,
selectedDevice: state.selectedDevice?.deviceAlias == deviceId
? null
: state.selectedDevice,
errorMessage: '',
),
);
},
);
} catch (e) {
// 捕获其他异常
emit(
state.copyWith(
isLoading: false,
errorMessage: '解绑设备异常:${e.toString()}',
),
);
}
}
// 获取所有设备列表
Future<void> fetchAllDevices(String username) async {
@@ -41,6 +95,7 @@ class DevicesCubit extends Cubit<DevicesState> {
),
),
(deviceList) {
print("设备列表:$deviceList"); // 调试输出
emit(
state.copyWith(
devices: deviceList,
@@ -74,6 +129,7 @@ class DevicesCubit extends Cubit<DevicesState> {
emit(state.copyWith(devices: newList, selectedDevice: newSelected));
}
/// 切换设备
Future<void> switchDevice(DeviceEntity device) async {
// 保持现有列表,只改 loading
@@ -86,42 +142,50 @@ class DevicesCubit extends Cubit<DevicesState> {
selectDevice(device);
}, (r) => emit(state.copyWith(isLoading: false, selectedDevice: device)));
}
/// 获取设备位置
Future<void> getDeviceLocation(DeviceEntity device) async {
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,
)),
);
}
///获取记录
Future<void> loadWorkRecords(String userId) async {
emit(state.copyWith(isLoading: true));
final result = await _getWorkRecordUseCase(userId);
result.fold(
(failure) => emit(state.copyWith(isLoading: false, errorMessage: failure.message)),
(records) => emit(state.copyWith(isLoading: false, workRecords: records)),
);
}
/// 删除工作记录
Future<void> deleteWorkRecord(String workName) async {
emit(state.copyWith(isLoading: true));
final result = await _deleteWorkRecordUseCase(workName);
result.fold(
(failure) => emit(state.copyWith(isLoading: false, errorMessage: failure.message)),
(_) => emit(
(failure) =>
emit(state.copyWith(isLoading: false, errorMessage: failure.message)),
(location) => emit(
state.copyWith(
isLoading: false,
workRecords: state.workRecords?.where((record) => record != workName).toList(),
deviceLatitude: location.latitude,
deviceLongitude: location.longitude,
),
),
);
}
///获取记录
Future<void> loadWorkRecords(String userId) async {
emit(state.copyWith(isLoading: true));
final result = await _getWorkRecordUseCase(userId);
result.fold(
(failure) =>
emit(state.copyWith(isLoading: false, errorMessage: failure.message)),
(records) => emit(state.copyWith(isLoading: false, workRecords: records)),
);
}
/// 删除工作记录
Future<void> deleteWorkRecord(String workName) async {
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(),
),
),
);
}
}

View File

@@ -36,6 +36,7 @@ class DevicesState extends Equatable {
errorMessage: errorMessage, // 错误信息通常每次更新都要重新赋值或清空
deviceLatitude: deviceLatitude ?? this.deviceLatitude,
deviceLongitude: deviceLongitude ?? this.deviceLongitude,
workRecords: workRecords ?? this.workRecords,
);
}