修改设备别名接口+UI
This commit is contained in:
@@ -3,6 +3,7 @@ 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 'package:maibu_satabot_v2/features/devices/domain/usecases/unbind_device_usecase.dart';
|
||||
import 'package:maibu_satabot_v2/features/devices/domain/usecases/update_devicename_usecase.dart';
|
||||
|
||||
import '../../domain/usecases/delete_work_record_usecase.dart';
|
||||
import '../../domain/usecases/get_device_location_usecase.dart';
|
||||
@@ -16,6 +17,7 @@ class DevicesCubit extends Cubit<DevicesState> {
|
||||
final GetWorkRecordUseCase _getWorkRecordUseCase;
|
||||
final DeleteWorkRecordUseCase _deleteWorkRecordUseCase;
|
||||
final UnbindDeviceUseCase _unbindDeviceUseCase;
|
||||
final UpdateDevicenameUsecase _updateDevicename;
|
||||
|
||||
DevicesCubit(
|
||||
this.repository,
|
||||
@@ -24,10 +26,17 @@ class DevicesCubit extends Cubit<DevicesState> {
|
||||
this._getWorkRecordUseCase,
|
||||
this._deleteWorkRecordUseCase,
|
||||
this._unbindDeviceUseCase,
|
||||
this._updateDevicename,
|
||||
) : super(const DevicesState());
|
||||
|
||||
Future<void> unbindDevice(String deviceId, String deviceName) async {
|
||||
emit(state.copyWith(isLoading: true, errorMessage: ''));
|
||||
emit(
|
||||
state.copyWith(
|
||||
isLoading: true,
|
||||
errorMessage: '',
|
||||
operationType: DeviceOperationType.unbind,
|
||||
),
|
||||
);
|
||||
|
||||
try {
|
||||
final params = UnbindDeviceParams(deviceId, deviceName);
|
||||
@@ -38,6 +47,7 @@ class DevicesCubit extends Cubit<DevicesState> {
|
||||
state.copyWith(
|
||||
isLoading: false,
|
||||
errorMessage: failure.message ?? '解绑设备失败',
|
||||
operationType: DeviceOperationType.none, // 操作结束重置
|
||||
),
|
||||
),
|
||||
(successCode) {
|
||||
@@ -56,6 +66,7 @@ class DevicesCubit extends Cubit<DevicesState> {
|
||||
? null
|
||||
: state.selectedDevice,
|
||||
errorMessage: '',
|
||||
operationType: DeviceOperationType.none,
|
||||
),
|
||||
);
|
||||
} else {
|
||||
@@ -63,6 +74,7 @@ class DevicesCubit extends Cubit<DevicesState> {
|
||||
state.copyWith(
|
||||
isLoading: false,
|
||||
errorMessage: '解绑失败:状态码 $successCode',
|
||||
operationType: DeviceOperationType.none,
|
||||
),
|
||||
);
|
||||
}
|
||||
@@ -70,7 +82,68 @@ class DevicesCubit extends Cubit<DevicesState> {
|
||||
);
|
||||
} catch (e) {
|
||||
emit(
|
||||
state.copyWith(isLoading: false, errorMessage: '解绑异常:${e.toString()}'),
|
||||
state.copyWith(isLoading: false, errorMessage: '解绑异常:${e.toString()}', operationType: DeviceOperationType.none),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> updateDeviceName(String deviceId, String deviceName) async {
|
||||
emit(
|
||||
state.copyWith(
|
||||
isLoading: true,
|
||||
errorMessage: '',
|
||||
operationType: DeviceOperationType.updateName,
|
||||
),
|
||||
);
|
||||
|
||||
try {
|
||||
final params = UpdateDevicenameParams(deviceId, deviceName);
|
||||
final result = await _updateDevicename.call(params);
|
||||
|
||||
result.fold(
|
||||
(failure) => emit(
|
||||
state.copyWith(
|
||||
isLoading: false,
|
||||
errorMessage: failure.message ?? '更新设备名称失败',
|
||||
operationType: DeviceOperationType.none,
|
||||
),
|
||||
),
|
||||
(successCode) {
|
||||
// 核心修复:int 转 bool 条件判断
|
||||
print('更新设备名称结果代码: $successCode'); // 调试输出结果代码
|
||||
final isSuccess = successCode == 1; // 显式转为 bool
|
||||
if (isSuccess) {
|
||||
//final updatedDevices = state.devices?.map((device) {
|
||||
// return device.deviceName == deviceId ? device.copyWith(deviceName: deviceName) : device;
|
||||
//}).toList();
|
||||
|
||||
emit(
|
||||
state.copyWith(
|
||||
isLoading: false,
|
||||
//devices: updatedDevices,
|
||||
//selectedDevice: state.selectedDevice?.deviceName == deviceId ? state.selectedDevice?.copyWith(deviceName: deviceName) : state.selectedDevice,
|
||||
errorMessage: '',
|
||||
operationType: DeviceOperationType.none,
|
||||
),
|
||||
);
|
||||
} else {
|
||||
emit(
|
||||
state.copyWith(
|
||||
isLoading: false,
|
||||
errorMessage: '更新设备名称失败:状态码 $successCode',
|
||||
operationType: DeviceOperationType.none,
|
||||
),
|
||||
);
|
||||
}
|
||||
},
|
||||
);
|
||||
} catch (e) {
|
||||
emit(
|
||||
state.copyWith(
|
||||
isLoading: false,
|
||||
errorMessage: '更新设备名称异常:${e.toString()}',
|
||||
operationType: DeviceOperationType.none,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,11 @@
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:maibu_satabot_v2/features/devices/domain/entities/device_entity.dart';
|
||||
enum DeviceOperationType {
|
||||
none, // 无操作
|
||||
unbind, // 解绑设备
|
||||
updateName, // 修改设备名称
|
||||
}
|
||||
|
||||
|
||||
class DevicesState extends Equatable {
|
||||
final List<DeviceEntity> devices; // 名下所有设备列表
|
||||
@@ -9,6 +15,7 @@ class DevicesState extends Equatable {
|
||||
final double? deviceLatitude; // 新增字段
|
||||
final double? deviceLongitude; // 新增字段
|
||||
final List<Map<String, dynamic>>? workRecords;
|
||||
final DeviceOperationType operationType; // 标记当前操作类型
|
||||
const DevicesState({
|
||||
this.devices = const [],
|
||||
this.selectedDevice,
|
||||
@@ -16,7 +23,8 @@ class DevicesState extends Equatable {
|
||||
this.errorMessage,
|
||||
this.deviceLatitude,
|
||||
this.deviceLongitude,
|
||||
this.workRecords
|
||||
this.workRecords,
|
||||
this.operationType = DeviceOperationType.none, // 默认无操作
|
||||
});
|
||||
|
||||
// 使用 copyWith 方便局部更新状态
|
||||
@@ -28,6 +36,7 @@ class DevicesState extends Equatable {
|
||||
double? deviceLatitude,
|
||||
double? deviceLongitude,
|
||||
List<Map<String, dynamic>>? workRecords,
|
||||
DeviceOperationType? operationType,
|
||||
}) {
|
||||
return DevicesState(
|
||||
devices: devices ?? this.devices,
|
||||
@@ -37,6 +46,7 @@ class DevicesState extends Equatable {
|
||||
deviceLatitude: deviceLatitude ?? this.deviceLatitude,
|
||||
deviceLongitude: deviceLongitude ?? this.deviceLongitude,
|
||||
workRecords: workRecords ?? this.workRecords,
|
||||
operationType: operationType ?? this.operationType, // 更新操作类型
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user