fixes issues

This commit is contained in:
mohammad
2024-11-27 14:23:00 +03:00
parent 80d92de625
commit 51b1438d0e
15 changed files with 405 additions and 810 deletions
@@ -4,7 +4,6 @@ import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:syncrow_app/features/app_layout/bloc/home_cubit.dart';
import 'package:syncrow_app/features/devices/bloc/device_settings_bloc/device_scene_event.dart';
import 'package:syncrow_app/features/devices/bloc/device_settings_bloc/device_scene_state.dart';
import 'package:syncrow_app/features/devices/model/device_control_model.dart';
import 'package:syncrow_app/features/devices/model/device_model.dart';
import 'package:syncrow_app/features/devices/model/device_report_model.dart';
import 'package:syncrow_app/features/devices/model/four_scene_model.dart';
@@ -18,7 +17,6 @@ import 'package:syncrow_app/features/scene/model/scenes_model.dart';
import 'package:syncrow_app/generated/assets.dart';
import 'package:syncrow_app/services/api/devices_api.dart';
import 'package:syncrow_app/services/api/home_management_api.dart';
import 'package:syncrow_app/services/api/scene_api.dart';
import 'package:syncrow_app/services/api/spaces_api.dart';
import 'package:syncrow_app/utils/helpers/snack_bar.dart';
@@ -34,23 +32,20 @@ class DeviceSettingBloc extends Bloc<DeviceSettingEvent, DeviceSettingState> {
on<ChangeNameEvent>(_changeName);
on<SearchFaqEvent>(_onSearchFaq);
on<FetchRoomsEvent>(_fetchRoomsAndDevices);
on<ChangeSwitchStatusEvent>(changeSwitchStatus);
on<LoadSettings>(_onLoadSettings);
on<SearchSettingsEvent>(searchSetting);
on<AssignRoomEvent>(_assignDevice);
on<SelectOptionEvent>(_onOptionSelected);
on<AddDeviceToGroup>(_addDeviceToGroup);
on<RemoveDeviceFromGroup>(_removeDeviceFromGroup);
on<DeviceSettingInitialQuestion>(_onDeviceSettingInitial);
on<FetchDeviceSetting>(_fetchDeviceSetting);
on<DeviceSettingSwitchInitial>(_fetchDeviceSettingSwitches);
on<AssignDeviceSetting>(assignSetting);
on<GetSettingBySwitchName>(getSettingByName);
on<SelectSettingEvent>(_selectSetting);
on<DeleteDeviceEvent>(deleteDevice);
on<ToggleEnableAlarmEvent>(_toggleLowBattery);
on<ToggleUpdateEvent>(_toggleUpdate);
on<ToggleHelpfulEvent>(_toggleHelpful);
on<SelectOptionEvent>(_onOptionSelected);
}
final TextEditingController nameController =
@@ -133,64 +128,6 @@ class DeviceSettingBloc extends Bloc<DeviceSettingEvent, DeviceSettingState> {
}
}
void _onOptionSelected(
SelectOptionEvent event, Emitter<DeviceSettingState> emit) {
emit(DeviceSettingLoadingState());
_selectedOption = event.selectedOption;
_hasSelectionChanged = true;
emit(OptionSelectedState(
selectedOption: _selectedOption,
hasSelectionChanged: _hasSelectionChanged));
}
void _fetchDeviceSettingSwitches(DeviceSettingSwitchInitial event,
Emitter<DeviceSettingState> emit) async {
emit(DeviceSettingLoadingState());
try {
var response = await DevicesAPI.getFourSceneInfo(deviceId);
Map<String, String> sceneTitles = {
"scene_1": '',
"scene_2": '',
"scene_3": '',
"scene_4": '',
};
for (var item in response) {
if (item["switchName"] != null) {
sceneTitles[item["switchName"]] = item["scene"]["name"] ?? '';
}
}
FourSceneModelState deviceStatus = FourSceneModelState(
scene_1: sceneTitles["scene_1"] ?? '',
scene_2: sceneTitles["scene_2"] ?? '',
scene_3: sceneTitles["scene_3"] ?? '',
scene_4: sceneTitles["scene_4"] ?? '',
scene_id_group_id: '',
switch_backlight: '',
);
emit(UpdateState(device: deviceStatus));
} catch (e) {
emit(DeviceSettingFailedState(errorMessage: e.toString()));
return;
}
}
void assignSetting(
AssignDeviceSetting event, Emitter<DeviceSettingState> emit) async {
emit(DeviceSettingLoadingState());
try {
final response = await DevicesAPI.postFourSceneInfo(
deviceId: deviceId,
sceneUuid: event.sceneUuid,
spaceUuid: event.unit!.id,
switchName: event.switchName);
emit(SaveSelectionSuccessState());
CustomSnackBar.displaySnackBar('Save Successfully');
} catch (e) {
emit(DeviceSettingFailedState(errorMessage: e.toString()));
}
}
void _fetchDeviceSetting(
FetchDeviceSetting event, Emitter<DeviceSettingState> emit) async {
emit(DeviceSettingLoadingState());
@@ -315,7 +252,7 @@ class DeviceSettingBloc extends Bloc<DeviceSettingEvent, DeviceSettingState> {
),
];
bool isHelpful = false;
bool? isHelpful;
void _toggleHelpful(
ToggleHelpfulEvent event, Emitter<DeviceSettingState> emit) async {
@@ -338,30 +275,6 @@ class DeviceSettingBloc extends Bloc<DeviceSettingEvent, DeviceSettingState> {
List<DeviceModel> allDevices = [];
List<SubSpaceModel> roomsList = [];
bool switchStatus = true;
Future<void> changeSwitchStatus(
ChangeSwitchStatusEvent event, Emitter<DeviceSettingState> emit) async {
try {
emit(DeviceSettingLoadingState());
switchStatus = deviceStatus.switch_backlight;
switchStatus = !switchStatus;
final response = await DevicesAPI.controlDevice(
DeviceControlModel(
deviceId: deviceId,
code: 'switch_backlight',
value: switchStatus),
deviceId);
deviceStatus.switch_backlight = switchStatus;
Future.delayed(const Duration(milliseconds: 200), () {
add(const DeviceSettingSwitchInitial());
});
Future.delayed(const Duration(milliseconds: 200), () {
emit(ChangeSwitchState(isEnable: switchStatus));
emit(UpdateState(device: deviceStatus));
});
} catch (_) {
add(const DeviceSettingInitial());
}
}
Future<void> _onLoadSettings(
LoadSettings event, Emitter<DeviceSettingState> emit) async {
@@ -519,6 +432,7 @@ class DeviceSettingBloc extends Bloc<DeviceSettingEvent, DeviceSettingState> {
var response = await DevicesAPI.putDeviceName(
deviceId: deviceId, deviceName: nameController.text);
add(const DeviceSettingInitialInfo());
await HomeCubit.getInstance().fetchUnitsByUserId();
CustomSnackBar.displaySnackBar('Save Successfully');
emit(SaveState());
} catch (e) {
@@ -556,4 +470,14 @@ class DeviceSettingBloc extends Bloc<DeviceSettingEvent, DeviceSettingState> {
return;
}
}
void _onOptionSelected(
SelectOptionEvent event, Emitter<DeviceSettingState> emit) {
emit(DeviceSettingLoadingState());
_selectedOption = event.selectedOption;
_hasSelectionChanged = true;
emit(OptionSelectedState(
selectedOption: _selectedOption,
hasSelectionChanged: _hasSelectionChanged));
}
}
@@ -59,7 +59,7 @@ class DeviceSettingInitialQuestion extends DeviceSettingEvent {
const DeviceSettingInitialQuestion();
}
class ChangeSwitchStatusEvent extends DeviceSettingEvent {}
// class ChangeSwitchStatusEvent extends DeviceSettingEvent {}
class FetchRoomsEvent extends DeviceSettingEvent {
final SpaceModel unit;
@@ -139,9 +139,9 @@ class AssignRoomEvent extends DeviceSettingEvent {
class FetchDeviceSetting extends DeviceSettingEvent {}
class DeviceSettingSwitchInitial extends DeviceSettingEvent {
const DeviceSettingSwitchInitial();
}
// class DeviceSettingSwitchInitial extends DeviceSettingEvent {
// const DeviceSettingSwitchInitial();
// }
class AssignDeviceSetting extends DeviceSettingEvent {
final String? sceneUuid;