绑定设备的位置的更新和获取接口

This commit is contained in:
2026-02-26 08:45:37 +08:00
parent 74632770f5
commit 08d91aa8b1
10 changed files with 118 additions and 5 deletions

View File

@@ -3,21 +3,22 @@ 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/get_device_location_usecase.dart';
import 'devices_state.dart';
class DevicesCubit extends Cubit<DevicesState> {
final GetUserDeviceUseCase _getUserDeviceUseCase;
final GetDeviceLocationUseCase _getDeviceLocationUseCase;
final DeviceRepository repository;
DevicesCubit(this.repository, this._getUserDeviceUseCase)
DevicesCubit(this.repository, this._getUserDeviceUseCase, this._getDeviceLocationUseCase)
: super(const DevicesState());
// 获取所有设备列表
Future<void> fetchAllDevices(String username) async {
emit(state.copyWith(isLoading: true));
try {
// 模拟网络请求获取列表
// 网络请求获取列表
var resultEither = await _getUserDeviceUseCase.call(
GetUserDeviceParams(username),
);
@@ -63,7 +64,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));
@@ -75,4 +76,19 @@ 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,
)),
);
}
}

View File

@@ -6,12 +6,16 @@ class DevicesState extends Equatable {
final DeviceEntity? selectedDevice; // 当前主界面选中的/操作的设备
final bool isLoading; // 是否正在加载
final String? errorMessage; // 错误信息
final double? deviceLatitude; // 新增字段
final double? deviceLongitude; // 新增字段
const DevicesState({
this.devices = const [],
this.selectedDevice,
this.isLoading = false,
this.errorMessage,
this.deviceLatitude,
this.deviceLongitude,
});
// 使用 copyWith 方便局部更新状态
@@ -20,15 +24,23 @@ class DevicesState extends Equatable {
DeviceEntity? selectedDevice,
bool? isLoading,
String? errorMessage,
double? deviceLatitude,
double? deviceLongitude,
}) {
return DevicesState(
devices: devices ?? this.devices,
selectedDevice: selectedDevice ?? this.selectedDevice,
isLoading: isLoading ?? this.isLoading,
errorMessage: errorMessage, // 错误信息通常每次更新都要重新赋值或清空
deviceLatitude: deviceLatitude ?? this.deviceLatitude,
deviceLongitude: deviceLongitude ?? this.deviceLongitude,
);
}
@override
List<Object?> get props => [devices, selectedDevice, isLoading, errorMessage];
List<Object?> get props => [devices, selectedDevice, isLoading,
errorMessage,
deviceLatitude,
deviceLongitude,
];
}