集成 获取用户设备的作业列表/删除作业列表/添加作业区域

This commit is contained in:
2026-02-26 16:20:02 +08:00
parent de093bc867
commit 0fa3d2351b
9 changed files with 274 additions and 14 deletions

View File

@@ -3,15 +3,25 @@ import 'package:maibu_satabot_v2/features/devices/domain/entities/device_entity.
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 '../../domain/usecases/delete_work_record_usecase.dart';
import '../../domain/usecases/get_device_location_usecase.dart';
import '../../domain/usecases/get_work_record_usecase.dart';
import 'devices_state.dart';
class DevicesCubit extends Cubit<DevicesState> {
final GetUserDeviceUseCase _getUserDeviceUseCase;
final GetDeviceLocationUseCase _getDeviceLocationUseCase;
final DeviceRepository repository;
final GetWorkRecordUseCase _getWorkRecordUseCase;
final DeleteWorkRecordUseCase _deleteWorkRecordUseCase;
DevicesCubit(this.repository, this._getUserDeviceUseCase, this._getDeviceLocationUseCase)
DevicesCubit(this.repository,
this._getUserDeviceUseCase,
this._getDeviceLocationUseCase,
this._getWorkRecordUseCase,
this._deleteWorkRecordUseCase,
)
: super(const DevicesState());
// 获取所有设备列表
@@ -27,7 +37,7 @@ class DevicesCubit extends Cubit<DevicesState> {
(failure) => emit(
state.copyWith(
isLoading: false,
errorMessage: failure.message, // 假设你的 Failure 类有 message 字段
errorMessage: failure.message, //
),
),
(deviceList) {
@@ -64,7 +74,7 @@ class DevicesCubit extends Cubit<DevicesState> {
emit(state.copyWith(devices: newList, selectedDevice: newSelected));
}
// 切换设备
/// 切换设备
Future<void> switchDevice(DeviceEntity device) async {
// 保持现有列表,只改 loading
emit(state.copyWith(isLoading: true));
@@ -76,7 +86,7 @@ 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);
@@ -89,6 +99,29 @@ class DevicesCubit extends Cubit<DevicesState> {
)),
);
}
///获取记录
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

@@ -8,7 +8,7 @@ class DevicesState extends Equatable {
final String? errorMessage; // 错误信息
final double? deviceLatitude; // 新增字段
final double? deviceLongitude; // 新增字段
final List<Map<String, dynamic>>? workRecords;
const DevicesState({
this.devices = const [],
this.selectedDevice,
@@ -16,6 +16,7 @@ class DevicesState extends Equatable {
this.errorMessage,
this.deviceLatitude,
this.deviceLongitude,
this.workRecords
});
// 使用 copyWith 方便局部更新状态
@@ -26,6 +27,7 @@ class DevicesState extends Equatable {
String? errorMessage,
double? deviceLatitude,
double? deviceLongitude,
List<Map<String, dynamic>>? workRecords,
}) {
return DevicesState(
devices: devices ?? this.devices,
@@ -42,5 +44,6 @@ class DevicesState extends Equatable {
errorMessage,
deviceLatitude,
deviceLongitude,
workRecords,
];
}