第二次提交

This commit is contained in:
cc
2026-01-18 20:21:14 +08:00
parent 4346601481
commit 289e706337
129 changed files with 4615 additions and 135 deletions

View File

@@ -0,0 +1,78 @@
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 'devices_state.dart';
class DevicesCubit extends Cubit<DevicesState> {
final GetUserDeviceUseCase _getUserDeviceUseCase;
final DeviceRepository repository;
DevicesCubit(this.repository, this._getUserDeviceUseCase)
: super(const DevicesState());
// 获取所有设备列表
Future<void> fetchAllDevices(String username) async {
emit(state.copyWith(isLoading: true));
try {
// 模拟网络请求获取列表
var resultEither = await _getUserDeviceUseCase.call(
GetUserDeviceParams(username),
);
resultEither.fold(
(failure) => emit(
state.copyWith(
isLoading: false,
errorMessage: failure.message, // 假设你的 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()));
}
}
// 切换当前选中的设备
void selectDevice(DeviceEntity device) {
emit(state.copyWith(selectedDevice: device));
}
// 更新单个设备的状态(例如从 Tcp 收到实时电量更新)
void updateDeviceStatus(DeviceEntity updatedDevice) {
final newList = state.devices.map((d) {
return d.deviceName == updatedDevice.deviceName ? updatedDevice : d;
}).toList();
// 如果更新的是当前选中的设备,也要同步更新 selectedDevice
final newSelected =
state.selectedDevice?.deviceName == updatedDevice.deviceName
? updatedDevice
: state.selectedDevice;
emit(state.copyWith(devices: newList, selectedDevice: newSelected));
}
Future<void> switchDevice(DeviceEntity device) async {
// 保持现有列表,只改 loading
emit(state.copyWith(isLoading: true));
final result = await repository.switchDevice("app", device.deviceName);
result.fold((l) {
emit(state.copyWith(isLoading: false, errorMessage: l.message));
selectDevice(device);
}, (r) => emit(state.copyWith(isLoading: false, selectedDevice: device)));
}
}

View File

@@ -0,0 +1,34 @@
import 'package:equatable/equatable.dart';
import 'package:maibu_satabot_v2/features/devices/domain/entities/device_entity.dart';
class DevicesState extends Equatable {
final List<DeviceEntity> devices; // 名下所有设备列表
final DeviceEntity? selectedDevice; // 当前主界面选中的/操作的设备
final bool isLoading; // 是否正在加载
final String? errorMessage; // 错误信息
const DevicesState({
this.devices = const [],
this.selectedDevice,
this.isLoading = false,
this.errorMessage,
});
// 使用 copyWith 方便局部更新状态
DevicesState copyWith({
List<DeviceEntity>? devices,
DeviceEntity? selectedDevice,
bool? isLoading,
String? errorMessage,
}) {
return DevicesState(
devices: devices ?? this.devices,
selectedDevice: selectedDevice ?? this.selectedDevice,
isLoading: isLoading ?? this.isLoading,
errorMessage: errorMessage, // 错误信息通常每次更新都要重新赋值或清空
);
}
@override
List<Object?> get props => [devices, selectedDevice, isLoading, errorMessage];
}