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
@@ -7,14 +7,11 @@ import 'package:syncrow_app/features/devices/bloc/6_scene_switch_bloc/6_scene_st
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/group_devices_model.dart';
import 'package:syncrow_app/features/devices/model/device_info_model.dart';
import 'package:syncrow_app/features/devices/model/six_scene_question_model.dart';
import 'package:syncrow_app/features/devices/model/six_scene_model.dart';
import 'package:syncrow_app/features/devices/model/status_model.dart';
import 'package:syncrow_app/features/devices/model/subspace_model.dart';
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';
@@ -27,29 +24,21 @@ class SixSceneBloc extends Bloc<SixSceneEvent, SixSceneState> {
required this.sixSceneId,
}) : super(const SixSceneState()) {
on<SixSceneInitial>(_fetchDeviceStatus);
on<ToggleNotificationEvent>(_toggleNotification);
on<ChangeNameEvent>(_changeName);
on<SearchFaqEvent>(_onSearchFaq);
on<ChangeSwitchStatusEvent>(changeSwitchStatus);
on<LoadScenes>(_onLoadScenes);
on<SearchScenesEvent>(searchScene);
on<SaveSelectionEvent>(_onSaveSelection);
on<SelectOptionEvent>(_onOptionSelected);
on<AddDeviceToGroup>(_addDeviceToGroup);
on<RemoveDeviceFromGroup>(_removeDeviceFromGroup);
on<SexSceneSwitchInitial>(_fetchFourSceneSwitches);
on<AssignDeviceScene>(assignScene);
on<GetSceneBySwitchName>(getSceneByName);
on<SelectSceneEvent>(_selectScene);
on<SixSceneInitialInfo>(fetchDeviceInfo);
on<SaveNameEvent>(saveName);
on<AssignRoomEvent>(_assignDevice);
on<FetchRoomsEvent>(_fetchRoomsAndDevices);
on<SixSceneInitialQuestion>(_onSixSceneInitial);
on<DeleteDeviceEvent>(deleteDevice);
on<ToggleEnableAlarmEvent>(_toggleLowBattery);
on<ToggleUpdateEvent>(_toggleUpdate);
on<ToggleHelpfulEvent>(_toggleHelpful);
on<UnAssignSceneEvent>(_unAssignScene);
}
final TextEditingController nameController =
@@ -156,6 +145,22 @@ class SixSceneBloc extends Bloc<SixSceneEvent, SixSceneState> {
}
}
void _unAssignScene(
UnAssignSceneEvent event, Emitter<SixSceneState> emit) async {
try {
emit(SixSceneLoadingState());
if (_hasSelectionChanged) {
var response = await DevicesAPI.unAssignScenesDevice(
deviceUuid: event.switchSceneUuid);
CustomSnackBar.displaySnackBar('Save Successfully');
emit(SaveSelectionSuccessState());
}
} catch (e) {
emit(SixSceneFailedState(errorMessage: e.toString()));
return;
}
}
deleteDevice(DeleteDeviceEvent event, Emitter<SixSceneState> emit) async {
try {
emit(SixSceneLoadingState());
@@ -226,9 +231,16 @@ class SixSceneBloc extends Bloc<SixSceneEvent, SixSceneState> {
String selectedSceneId = '';
_selectScene(SelectSceneEvent event, Emitter<SixSceneState> emit) {
_selectScene(SelectSceneEvent event, Emitter<SixSceneState> emit) {
emit(SixSceneLoadingState());
selectedSceneId = event.unitId;
if (event.isSelected == false) {
selectedSceneId = '';
selectedFormApiSceneId = '';
emit(SceneSelectionUpdatedState(selectedSceneId: selectedSceneId));
} else {
selectedSceneId = event.selectedSceneId;
}
emit(SceneSelectionUpdatedState(selectedSceneId: selectedSceneId));
}
@@ -238,7 +250,8 @@ class SixSceneBloc extends Bloc<SixSceneEvent, SixSceneState> {
SexSceneSwitchInitial event, Emitter<SixSceneState> emit) async {
emit(SixSceneLoadingState());
try {
var response = await DevicesAPI.getFourSceneInfo(sixSceneId);
var response = await DevicesAPI.getDeviceSceneInfo(sixSceneId);
Map<String, String> sceneTitles = {
"scene_1": '',
"scene_2": '',
@@ -272,11 +285,16 @@ class SixSceneBloc extends Bloc<SixSceneEvent, SixSceneState> {
void assignScene(AssignDeviceScene event, Emitter<SixSceneState> emit) async {
emit(SixSceneLoadingState());
try {
final response = await DevicesAPI.postFourSceneInfo(
deviceId: sixSceneId,
sceneUuid: event.sceneUuid,
spaceUuid: event.unit!.id,
switchName: event.switchName);
if (event.sceneUuid == '') {
final response = await DevicesAPI.unAssignScenesDevice(
deviceUuid: sixSceneId, switchName: event.switchName);
} else {
final response = await DevicesAPI.postDeviceSceneInfo(
deviceId: sixSceneId,
sceneUuid: event.sceneUuid,
spaceUuid: event.unit!.id,
switchName: event.switchName);
}
emit(SaveSelectionSuccessState());
CustomSnackBar.displaySnackBar('Save Successfully');
@@ -285,27 +303,6 @@ class SixSceneBloc extends Bloc<SixSceneEvent, SixSceneState> {
}
}
void _onSearchFaq(SearchFaqEvent event, Emitter<SixSceneState> emit) {
emit(SixSceneLoadingState());
List<SixSceneQuestionModel> _faqQuestions = faqQuestions.where((question) {
return question.question
.toLowerCase()
.contains(event.query.toLowerCase());
}).toList();
emit(FaqSearchState(filteredFaqQuestions: _faqQuestions));
}
void _toggleNotification(
ToggleNotificationEvent event, Emitter<SixSceneState> emit) async {
emit(LoadingNewSate(device: deviceStatus));
try {
closingReminder = event.isClosingEnabled;
emit(UpdateState(device: deviceStatus));
} catch (e) {
emit(SixSceneFailedState(errorMessage: e.toString()));
}
}
DeviceReport recordGroups =
DeviceReport(startTime: '0', endTime: '0', data: []);
@@ -355,6 +352,7 @@ class SixSceneBloc extends Bloc<SixSceneEvent, SixSceneState> {
} catch (e) {
emit(const SixSceneFailedState(errorMessage: 'Something went wrong'));
}
emit(SuccessState());
}
@@ -368,7 +366,7 @@ class SixSceneBloc extends Bloc<SixSceneEvent, SixSceneState> {
filteredScenes = event.query.isEmpty
? allScenes
: allScenes.where((scene) {
final sceneName = scene.name.toLowerCase() ?? '';
final sceneName = scene.name.toLowerCase();
return sceneName.contains(event.query.toLowerCase());
}).toList();
emit(SearchResultsState());
@@ -393,162 +391,4 @@ class SixSceneBloc extends Bloc<SixSceneEvent, SixSceneState> {
emit(SaveSelectionSuccessState());
}
}
//============================= group settings =================================
//addDevicesIcon
List<GroupDevicesModel> groupDevices = [
GroupDevicesModel(
dec: 'Syncroom', icon: Assets.minusIcon, name: '6 Scene Switch')
];
List<GroupDevicesModel> devices = [
GroupDevicesModel(
dec: 'Syncroom', icon: Assets.addDevicesIcon, name: '6 Scene Switch')
];
// Handler for AddDeviceToGroup
void _addDeviceToGroup(AddDeviceToGroup event, Emitter<SixSceneState> emit) {
devices.remove(event.device);
groupDevices.add(event.device);
for (var device in groupDevices) {
device.icon = event.icon;
}
emit(UpdateStateList(groupDevices: groupDevices, devices: devices));
}
// Handler for RemoveDeviceFromGroup
void _removeDeviceFromGroup(
RemoveDeviceFromGroup event, Emitter<SixSceneState> emit) {
groupDevices.remove(event.device);
devices.add(event.device);
for (var device in groupDevices) {
device.icon = event.icon;
}
emit(UpdateStateList(groupDevices: groupDevices, devices: devices));
}
//======================= update device functions ============================
//////////////////////////////////////////////////////////////////////////////
bool enableUpdate = false;
void _toggleUpdate(
ToggleUpdateEvent event, Emitter<SixSceneState> emit) async {
try {
emit(SixSceneLoadingState());
enableUpdate = event.isUpdateEnabled!;
emit(SaveState());
} catch (e) {
emit(const SixSceneFailedState(errorMessage: 'Something went wrong'));
}
}
//======================= q&a and questions ====================================
////////////////////////////////////////////////////////////////////////////////
bool isHelpful = false;
void _toggleHelpful(
ToggleHelpfulEvent event, Emitter<SixSceneState> emit) async {
try {
emit(SixSceneLoadingState());
isHelpful = event.isHelpful!;
emit(SaveState());
} catch (e) {
emit(const SixSceneFailedState(errorMessage: 'Something went wrong'));
}
}
final List<SixSceneQuestionModel> faqQuestions = [
SixSceneQuestionModel(
id: 1,
question: 'How does an SOS emergency button work?',
answer:
'The SOS emergency button sends an alert to your contacts when pressed.',
),
SixSceneQuestionModel(
id: 2,
question: 'How long will an SOS alarm persist?',
answer:
'The SOS alarm will persist until it is manually turned off or after a set time.',
),
SixSceneQuestionModel(
id: 3,
question: 'What should I do if the SOS button is unresponsive?',
answer: 'Try restarting the device. If it persists, contact support.',
),
SixSceneQuestionModel(
id: 4,
question: 'Can I use the SOS feature without a network connection?',
answer:
'No, a network connection is required to send the alert to your contacts.',
),
SixSceneQuestionModel(
id: 5,
question: 'How often should I check the SOS battery?',
answer:
'Check the SOS battery at least once a month to ensure it is operational.',
),
];
Future<void> _onSixSceneInitial(
SixSceneInitialQuestion event, Emitter<SixSceneState> emit) async {
emit(SixSceneLoadingState());
emit(FaqLoadedState(filteredFaqQuestions: faqQuestions));
}
//=========================== name setting ===================================
////////////////////////////////////////////////////////////////////////////////
void _changeName(ChangeNameEvent event, Emitter<SixSceneState> emit) {
emit(SixSceneLoadingState());
editName = event.value!;
if (editName) {
Future.delayed(const Duration(milliseconds: 500), () {
focusNode.requestFocus();
});
} else {
focusNode.unfocus();
}
emit(NameEditingState(editName: editName));
}
Future<void> saveName(
SaveNameEvent event, Emitter<SixSceneState> emit) async {
if (_validateInputs()) return;
try {
add(const ChangeNameEvent(value: false));
isSaving = true;
emit(SixSceneLoadingState());
var response = await DevicesAPI.putDeviceName(
deviceId: sixSceneId, deviceName: nameController.text);
add(const SixSceneInitialInfo());
CustomSnackBar.displaySnackBar('Save Successfully');
emit(SaveState());
} catch (e) {
emit(SixSceneFailedState(errorMessage: e.toString()));
} finally {
isSaving = false;
}
}
bool _validateInputs() {
final nameError = fullNameValidator(nameController.text);
if (nameError != null) {
CustomSnackBar.displaySnackBar(nameError);
return true;
}
return false;
}
String? fullNameValidator(String? value) {
if (value == null) return 'name is required';
final withoutExtraSpaces = value.replaceAll(RegExp(r"\s+"), ' ').trim();
if (withoutExtraSpaces.length < 2 || withoutExtraSpaces.length > 30) {
return 'name must be between 2 and 30 characters long';
}
if (RegExp(r"/[^ a-zA-Z0-9-\']/").hasMatch(withoutExtraSpaces)) {
return 'Only alphanumeric characters, space, dash and single quote are allowed';
}
return null;
}
}
@@ -24,6 +24,11 @@ class SaveNameEvent extends SixSceneEvent {
const SaveNameEvent();
}
class UnAssignSceneEvent extends SixSceneEvent {
final String switchSceneUuid;
const UnAssignSceneEvent({required this.switchSceneUuid});
}
class SixSceneInitialInfo extends SixSceneEvent {
const SixSceneInitialInfo();
}
@@ -91,12 +96,17 @@ class LoadScenes extends SixSceneEvent {
}
class SelectSceneEvent extends SixSceneEvent {
final String unitId;
final String selectedSceneId;
final bool isSelected;
// final String unitId;
const SelectSceneEvent({
required this.unitId,
// required this.unitId,
required this.selectedSceneId,
required this.isSelected,
});
}
class SearchScenesEvent extends SixSceneEvent {
final String query;
const SearchScenesEvent({
@@ -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;
@@ -1,25 +1,19 @@
import 'dart:async';
import 'package:flutter/material.dart';
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/four_scene_bloc/four_scene_event.dart';
import 'package:syncrow_app/features/devices/bloc/four_scene_bloc/four_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';
import 'package:syncrow_app/features/devices/model/question_model.dart';
import 'package:syncrow_app/features/devices/model/four_scene_switch_model.dart';
import 'package:syncrow_app/features/devices/model/group_devices_model.dart';
import 'package:syncrow_app/features/devices/model/device_info_model.dart';
import 'package:syncrow_app/features/devices/model/status_model.dart';
import 'package:syncrow_app/features/devices/model/subspace_model.dart';
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';
class FourSceneBloc extends Bloc<FourSceneEvent, FourSceneState> {
@@ -29,28 +23,17 @@ class FourSceneBloc extends Bloc<FourSceneEvent, FourSceneState> {
}) : super(const FourSceneState()) {
on<FourSceneInitial>(_fetchDeviceStatus);
on<FourSceneInitialInfo>(fetchDeviceInfo);
on<SaveNameEvent>(saveName);
on<ToggleNotificationEvent>(_toggleNotification);
on<ChangeNameEvent>(_changeName);
on<SearchFaqEvent>(_onSearchFaq);
on<FetchRoomsEvent>(_fetchRoomsAndDevices);
on<ChangeSwitchStatusEvent>(changeSwitchStatus);
on<LoadScenes>(_onLoadScenes);
on<SearchScenesEvent>(searchScene);
on<AssignRoomEvent>(_assignDevice);
on<SelectOptionEvent>(_onOptionSelected);
on<AddDeviceToGroup>(_addDeviceToGroup);
on<RemoveDeviceFromGroup>(_removeDeviceFromGroup);
on<FourSceneInitialQuestion>(_onFourSceneInitial);
on<FetchDeviceScene>(_fetchDeviceScene);
on<FourSceneSwitchInitial>(_fetchFourSceneSwitches);
on<AssignDeviceScene>(assignScene);
on<GetSceneBySwitchName>(getSceneByName);
on<SelectSceneEvent>(_selectScene);
on<DeleteDeviceEvent>(deleteDevice);
on<ToggleEnableAlarmEvent>(_toggleLowBattery);
on<ToggleUpdateEvent>(_toggleUpdate);
on<ToggleHelpfulEvent>(_toggleHelpful);
}
final TextEditingController nameController =
@@ -116,7 +99,15 @@ class FourSceneBloc extends Bloc<FourSceneEvent, FourSceneState> {
_selectScene(SelectSceneEvent event, Emitter<FourSceneState> emit) {
emit(FourSceneLoadingState());
selectedSceneId = event.selectedSceneId;
print('-------${event.selectedSceneId}------');
print('-------${event.isSelected}------');
if (event.isSelected == false) {
selectedSceneId = '';
selectedFormApiSceneId = '';
emit(SceneSelectionUpdatedState(selectedSceneId: selectedSceneId));
} else {
selectedSceneId = event.selectedSceneId;
}
emit(SceneSelectionUpdatedState(selectedSceneId: selectedSceneId));
}
@@ -147,7 +138,7 @@ class FourSceneBloc extends Bloc<FourSceneEvent, FourSceneState> {
FourSceneSwitchInitial event, Emitter<FourSceneState> emit) async {
emit(FourSceneLoadingState());
try {
var response = await DevicesAPI.getFourSceneInfo(fourSceneId);
var response = await DevicesAPI.getDeviceSceneInfo(fourSceneId);
Map<String, String> sceneTitles = {
"scene_1": '',
"scene_2": '',
@@ -178,11 +169,16 @@ class FourSceneBloc extends Bloc<FourSceneEvent, FourSceneState> {
AssignDeviceScene event, Emitter<FourSceneState> emit) async {
emit(FourSceneLoadingState());
try {
final response = await DevicesAPI.postFourSceneInfo(
deviceId: fourSceneId,
sceneUuid: event.sceneUuid,
spaceUuid: event.unit!.id,
switchName: event.switchName);
if (event.sceneUuid == '') {
final response = await DevicesAPI.unAssignScenesDevice(
deviceUuid: fourSceneId, switchName: event.switchName);
} else {
final response = await DevicesAPI.postDeviceSceneInfo(
deviceId: fourSceneId,
sceneUuid: event.sceneUuid,
spaceUuid: event.unit!.id,
switchName: event.switchName);
}
emit(SaveSelectionSuccessState());
CustomSnackBar.displaySnackBar('Save Successfully');
@@ -191,6 +187,7 @@ class FourSceneBloc extends Bloc<FourSceneEvent, FourSceneState> {
}
}
void _fetchDeviceScene(
FetchDeviceScene event, Emitter<FourSceneState> emit) async {
emit(FourSceneLoadingState());
@@ -256,16 +253,6 @@ class FourSceneBloc extends Bloc<FourSceneEvent, FourSceneState> {
}
}
void _onSearchFaq(SearchFaqEvent event, Emitter<FourSceneState> emit) {
emit(FourSceneLoadingState());
List<QuestionModel> _faqQuestions = faqQuestions.where((question) {
return question.question
.toLowerCase()
.contains(event.query.toLowerCase());
}).toList();
emit(FaqSearchState(filteredFaqQuestions: _faqQuestions));
}
//============================ assign Device ==================================
//////////////////////////////////////////////////////////////////////////////
@@ -283,56 +270,6 @@ class FourSceneBloc extends Bloc<FourSceneEvent, FourSceneState> {
DeviceReport recordGroups =
DeviceReport(startTime: '0', endTime: '0', data: []);
//========================= Question and faq ================================
final List<QuestionModel> faqQuestions = [
QuestionModel(
id: 1,
question:
'How does an 4 Scene Switch work? How long will an 4 Scene Switch persist?',
answer:
'Yes. In scenes with high detection requirements, we recommend that you choose phone or message notification in Automation.',
),
QuestionModel(
id: 2,
question: 'Does the 4 Scene Switch support sending notifications?',
answer:
'The SOS alarm will persist until it is manually turned off or after a set time.',
),
QuestionModel(
id: 3,
question:
'Why does the data statistics in the device panel not show the correct data?',
answer: 'Try restarting the device. If it persists, contact support.',
),
QuestionModel(
id: 4,
question:
'How long will the App show offline after a device (low-power devices and normal devices) is powered...',
answer:
'No, a network connection is required to send the alert to your contacts.',
),
];
bool isHelpful = false;
void _toggleHelpful(
ToggleHelpfulEvent event, Emitter<FourSceneState> emit) async {
try {
emit(FourSceneLoadingState());
isHelpful = event.isHelpful!;
emit(SaveState());
} catch (e) {
emit(const FourSceneFailedState(errorMessage: 'Something went wrong'));
}
}
Future<void> _onFourSceneInitial(
FourSceneInitialQuestion event, Emitter<FourSceneState> emit) async {
emit(FourSceneLoadingState());
emit(FaqLoadedState(filteredFaqQuestions: faqQuestions));
}
List<DeviceModel> allDevices = [];
List<SubSpaceModel> roomsList = [];
bool switchStatus = true;
@@ -393,159 +330,4 @@ class FourSceneBloc extends Bloc<FourSceneEvent, FourSceneState> {
}).toList();
emit(SearchResultsState());
}
List<GroupDevicesModel> groupDevices = [
GroupDevicesModel(
dec: 'Syncroom', icon: Assets.minusIcon, name: '6 Scene Switch')
];
List<GroupDevicesModel> devices = [
GroupDevicesModel(
dec: 'Syncroom', icon: Assets.addDevicesIcon, name: '6 Scene Switch')
];
// Handler for AddDeviceToGroup
void _addDeviceToGroup(AddDeviceToGroup event, Emitter<FourSceneState> emit) {
devices.remove(event.device);
groupDevices.add(event.device);
for (var device in groupDevices) {
device.icon = event.icon;
}
emit(UpdateStateList(groupDevices: groupDevices, devices: devices));
}
// Handler for RemoveDeviceFromGroup
void _removeDeviceFromGroup(
RemoveDeviceFromGroup event, Emitter<FourSceneState> emit) {
groupDevices.remove(event.device);
devices.add(event.device);
for (var device in groupDevices) {
device.icon = event.icon;
}
emit(UpdateStateList(groupDevices: groupDevices, devices: devices));
}
//=========================== assign device to room ==========================
void _assignDevice(
AssignRoomEvent event, Emitter<FourSceneState> emit) async {
try {
emit(FourSceneLoadingState());
if (_hasSelectionChanged) {
await HomeManagementAPI.assignDeviceToRoom(event.unit.community.uuid,
event.unit.id, event.roomId, fourSceneId);
final devicesList = await DevicesAPI.getDevicesByRoomId(
communityUuid: event.unit.community.uuid,
spaceUuid: event.unit.id,
roomId: event.roomId);
List<String> allDevicesIds = [];
allDevices.forEach((element) {
allDevicesIds.add(element.uuid!);
});
await HomeCubit.getInstance().fetchUnitsByUserId();
CustomSnackBar.displaySnackBar('Save Successfully');
emit(SaveSelectionSuccessState());
}
} catch (e) {
emit(const FourSceneFailedState(errorMessage: 'Something went wrong'));
return;
}
}
void _fetchRoomsAndDevices(
FetchRoomsEvent event, Emitter<FourSceneState> emit) async {
try {
emit(FourSceneLoadingState());
roomsList = await SpacesAPI.getSubSpaceBySpaceId(
event.unit.community.uuid, event.unit.id);
emit(FetchRoomsState(devicesList: allDevices, roomsList: roomsList));
} catch (e) {
emit(const FourSceneFailedState(errorMessage: 'Something went wrong'));
return;
}
}
//============================ setting name ==================================
//////////////////////////////////////////////////////////////////////////////
void _changeName(ChangeNameEvent event, Emitter<FourSceneState> emit) {
emit(FourSceneLoadingState());
editName = event.value!;
if (editName) {
Future.delayed(const Duration(milliseconds: 500), () {
focusNode.requestFocus();
});
} else {
focusNode.unfocus();
}
emit(NameEditingState(editName: editName));
}
bool _validateInputs() {
final nameError = fullNameValidator(nameController.text);
if (nameError != null) {
CustomSnackBar.displaySnackBar(nameError);
return true;
}
return false;
}
String? fullNameValidator(String? value) {
if (value == null) return 'name is required';
final withoutExtraSpaces = value.replaceAll(RegExp(r"\s+"), ' ').trim();
if (withoutExtraSpaces.length < 2 || withoutExtraSpaces.length > 30) {
return 'name must be between 2 and 30 characters long';
}
if (RegExp(r"/[^ a-zA-Z0-9-\']/").hasMatch(withoutExtraSpaces)) {
return 'Only alphanumeric characters, space, dash and single quote are allowed';
}
return null;
}
Future<void> saveName(
SaveNameEvent event, Emitter<FourSceneState> emit) async {
if (_validateInputs()) return;
try {
add(const ChangeNameEvent(value: false));
isSaving = true;
emit(FourSceneLoadingState());
var response = await DevicesAPI.putDeviceName(
deviceId: fourSceneId, deviceName: nameController.text);
add(const FourSceneInitialInfo());
CustomSnackBar.displaySnackBar('Save Successfully');
emit(SaveState());
} catch (e) {
emit(FourSceneFailedState(errorMessage: e.toString()));
} finally {
isSaving = false;
}
}
//====================== update device ==============================
bool enableUpdate = false;
void _toggleUpdate(
ToggleUpdateEvent event, Emitter<FourSceneState> emit) async {
try {
emit(FourSceneLoadingState());
enableUpdate = event.isUpdateEnabled!;
emit(SaveState());
} catch (e) {
emit(const FourSceneFailedState(errorMessage: 'Something went wrong'));
}
}
deleteDevice(DeleteDeviceEvent event, Emitter<FourSceneState> emit) async {
try {
emit(FourSceneLoadingState());
var response = await DevicesAPI.resetDevise(devicesUuid: fourSceneId);
CustomSnackBar.displaySnackBar('Reset Successfully');
emit(UpdateState(device: deviceStatus));
} catch (e) {
emit(FourSceneFailedState(errorMessage: e.toString()));
return;
}
}
}
@@ -84,10 +84,12 @@ class LoadScenes extends FourSceneEvent {
class SelectSceneEvent extends FourSceneEvent {
final String selectedSceneId;
final bool isSelected;
// final String unitId;
const SelectSceneEvent({
// required this.unitId,
required this.selectedSceneId,
required this.isSelected,
});
}
@@ -143,6 +145,13 @@ class FourSceneSwitchInitial extends FourSceneEvent {
const FourSceneSwitchInitial();
}
class UnAssignSceneEvent extends FourSceneEvent {
final String deviceUuid;
final String switchName;
const UnAssignSceneEvent(
{required this.deviceUuid, required this.switchName});
}
class AssignDeviceScene extends FourSceneEvent {
final String? sceneUuid;
final String? switchName;