From 450b773921ea9ba76195fd51df10f594052424ee Mon Sep 17 00:00:00 2001 From: Abdullah Alassaf Date: Thu, 20 Feb 2025 04:13:37 +0300 Subject: [PATCH] set default values in the devices models instead of late, and fixed issues in the AC bloc --- .../devices/bloc/acs_bloc/acs_bloc.dart | 86 +++++++------------ lib/features/devices/model/ac_model.dart | 18 ++-- .../devices/model/ceiling_sensor_model.dart | 6 +- lib/features/devices/model/curtain_model.dart | 6 +- .../devices/model/device_info_model.dart | 2 - .../devices/model/door_sensor_model.dart | 19 ++-- .../devices/model/garage_door_model.dart | 18 ++-- .../devices/model/one_gang_model.dart | 18 ++-- .../devices/model/one_touch_model.dart | 23 ++--- .../devices/model/six_scene_model.dart | 16 ++-- .../devices/model/smart_door_model.dart | 34 ++++---- lib/features/devices/model/sos_model.dart | 4 +- .../devices/view/widgets/ACs/acs_list.dart | 2 +- pubspec.yaml | 2 +- 14 files changed, 106 insertions(+), 148 deletions(-) diff --git a/lib/features/devices/bloc/acs_bloc/acs_bloc.dart b/lib/features/devices/bloc/acs_bloc/acs_bloc.dart index 95de36f..b88cf3b 100644 --- a/lib/features/devices/bloc/acs_bloc/acs_bloc.dart +++ b/lib/features/devices/bloc/acs_bloc/acs_bloc.dart @@ -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/acs_bloc/acs_event.dart'; import 'package:syncrow_app/features/devices/bloc/acs_bloc/acs_state.dart'; -import 'package:syncrow_app/features/devices/bloc/devices_cubit.dart'; import 'package:syncrow_app/features/devices/model/ac_model.dart'; import 'package:syncrow_app/features/devices/model/device_control_model.dart'; import 'package:syncrow_app/features/devices/model/device_model.dart'; @@ -28,7 +27,7 @@ class ACsBloc extends Bloc { bool allAcsPage = false; bool allAcsOn = true; bool allTempSame = true; - int globalTemp = 25; + int globalTemp = 250; Timer? _timer; ACsBloc({required this.acId}) : super(AcsInitialState()) { @@ -69,8 +68,7 @@ class ACsBloc extends Bloc { for (var status in response['status']) { statusModelList.add(StatusModel.fromJson(status)); } - deviceStatus = - AcStatusModel.fromJson(response['productUuid'], statusModelList); + deviceStatus = AcStatusModel.fromJson(response['productUuid'], statusModelList); emit(GetAcStatusState(acStatusModel: deviceStatus)); Future.delayed(const Duration(milliseconds: 500)); // _listenToChanges(); @@ -83,22 +81,18 @@ class ACsBloc extends Bloc { _listenToChanges() { try { - DatabaseReference ref = - FirebaseDatabase.instance.ref('device-status/$acId'); + DatabaseReference ref = FirebaseDatabase.instance.ref('device-status/$acId'); Stream stream = ref.onValue; stream.listen((DatabaseEvent event) { - Map usersMap = - event.snapshot.value as Map; + Map usersMap = event.snapshot.value as Map; List statusList = []; usersMap['status'].forEach((element) { - statusList - .add(StatusModel(code: element['code'], value: element['value'])); + statusList.add(StatusModel(code: element['code'], value: element['value'])); }); - deviceStatus = - AcStatusModel.fromJson(usersMap['productUuid'], statusList); + deviceStatus = AcStatusModel.fromJson(usersMap['productUuid'], statusList); add(AcUpdated()); }); } catch (_) {} @@ -115,14 +109,12 @@ class ACsBloc extends Bloc { HomeCubit.getInstance().selectedSpace?.id ?? '', 'AC'); for (int i = 0; i < devicesList.length; i++) { - var response = - await DevicesAPI.getDeviceStatus(devicesList[i].uuid ?? ''); + var response = await DevicesAPI.getDeviceStatus(devicesList[i].uuid ?? ''); List statusModelList = []; for (var status in response['status']) { statusModelList.add(StatusModel.fromJson(status)); } - deviceStatusList.add( - AcStatusModel.fromJson(response['productUuid'], statusModelList)); + deviceStatusList.add(AcStatusModel.fromJson(devicesList[i].uuid ?? '', statusModelList)); } _setAllAcsTempsAndSwitches(); } @@ -132,7 +124,7 @@ class ACsBloc extends Bloc { if (allAcsPage) { emit(AcsLoadingState()); for (AcStatusModel ac in deviceStatusList) { - if (ac.uuid == event.productId) { + if (ac.uuid == event.deviceId) { ac.acSwitch = acSwitchValue; } } @@ -145,8 +137,7 @@ class ACsBloc extends Bloc { emit(AcModifyingState(acStatusModel: deviceStatus)); } - await _runDeBouncerForOneDevice( - deviceId: event.deviceId, code: 'switch', value: acSwitchValue); + await _runDeBouncerForOneDevice(deviceId: event.deviceId, code: 'switch', value: acSwitchValue); } void _changeAllAcSwitch(ChangeAllSwitch event, Emitter emit) async { @@ -207,8 +198,7 @@ class ACsBloc extends Bloc { deviceStatus.childLock = lockValue; emit(AcModifyingState(acStatusModel: deviceStatus)); - await _runDeBouncerForOneDevice( - deviceId: acId, code: 'child_lock', value: lockValue); + await _runDeBouncerForOneDevice(deviceId: acId, code: 'child_lock', value: lockValue); } void _increaseCoolTo(IncreaseCoolToTemp event, Emitter emit) async { @@ -224,7 +214,7 @@ class ACsBloc extends Bloc { if (allAcsPage) { emit(AcsLoadingState()); for (AcStatusModel ac in deviceStatusList) { - if (ac.uuid == event.productId) { + if (ac.uuid == event.deviceId) { ac.tempSet = value; } } @@ -236,8 +226,7 @@ class ACsBloc extends Bloc { emit(AcModifyingState(acStatusModel: deviceStatus)); } - await _runDeBouncerForOneDevice( - deviceId: event.deviceId, code: 'temp_set', value: value); + await _runDeBouncerForOneDevice(deviceId: event.deviceId, code: 'temp_set', value: value); } void _decreaseCoolTo(DecreaseCoolToTemp event, Emitter emit) async { @@ -253,7 +242,7 @@ class ACsBloc extends Bloc { if (allAcsPage) { emit(AcsLoadingState()); for (AcStatusModel ac in deviceStatusList) { - if (ac.uuid == event.productId) { + if (ac.uuid == event.deviceId) { ac.tempSet = value; } } @@ -265,8 +254,7 @@ class ACsBloc extends Bloc { emit(AcModifyingState(acStatusModel: deviceStatus)); } - await _runDeBouncerForOneDevice( - deviceId: event.deviceId, code: 'temp_set', value: value); + await _runDeBouncerForOneDevice(deviceId: event.deviceId, code: 'temp_set', value: value); } void _changeAcMode(ChangeAcMode event, Emitter emit) async { @@ -274,7 +262,7 @@ class ACsBloc extends Bloc { if (allAcsPage) { emit(AcsLoadingState()); for (AcStatusModel ac in deviceStatusList) { - if (ac.uuid == event.productId) { + if (ac.uuid == event.deviceId) { ac.modeString = getACModeString(tempMode); ac.acMode = AcStatusModel.getACMode(getACModeString(tempMode)); } @@ -288,9 +276,7 @@ class ACsBloc extends Bloc { } await _runDeBouncerForOneDevice( - deviceId: event.deviceId, - code: 'mode', - value: getACModeString(tempMode)); + deviceId: event.deviceId, code: 'mode', value: getACModeString(tempMode)); } void _changeFanSpeed(ChangeFanSpeed event, Emitter emit) async { @@ -301,25 +287,21 @@ class ACsBloc extends Bloc { if (allAcsPage) { emit(AcsLoadingState()); for (AcStatusModel ac in deviceStatusList) { - if (ac.uuid == event.productId) { + if (ac.uuid == event.deviceId) { ac.fanSpeedsString = getNextFanSpeedKey(fanSpeed); - ac.acFanSpeed = - AcStatusModel.getFanSpeed(getNextFanSpeedKey(fanSpeed)); + ac.acFanSpeed = AcStatusModel.getFanSpeed(getNextFanSpeedKey(fanSpeed)); } } _emitAcsStatus(emit); } else { emit(AcChangeLoading(acStatusModel: deviceStatus)); deviceStatus.fanSpeedsString = getNextFanSpeedKey(fanSpeed); - deviceStatus.acFanSpeed = - AcStatusModel.getFanSpeed(getNextFanSpeedKey(fanSpeed)); + deviceStatus.acFanSpeed = AcStatusModel.getFanSpeed(getNextFanSpeedKey(fanSpeed)); emit(AcModifyingState(acStatusModel: deviceStatus)); } await _runDeBouncerForOneDevice( - deviceId: event.deviceId, - code: 'level', - value: getNextFanSpeedKey(fanSpeed)); + deviceId: event.deviceId, code: 'level', value: getNextFanSpeedKey(fanSpeed)); } String getACModeString(TempModes value) { @@ -338,17 +320,15 @@ class ACsBloc extends Bloc { allAcsOn = true; allTempSame = true; if (deviceStatusList.isNotEmpty) { - int temp = deviceStatusList[0].tempSet; - deviceStatusList.firstWhere((element) { + int temp = deviceStatusList.first.tempSet; + for (var element in deviceStatusList) { if (!element.acSwitch) { allAcsOn = false; } if (element.tempSet != temp) { allTempSame = false; } - - return true; - }); + } if (allTempSame) { globalTemp = temp; } @@ -364,8 +344,7 @@ class ACsBloc extends Bloc { for (int i = 0; i < deviceStatusList.length; i++) { try { await DevicesAPI.controlDevice( - DeviceControlModel( - deviceId: devicesList[i].uuid, code: code, value: value), + DeviceControlModel(deviceId: devicesList[i].uuid, code: code, value: value), devicesList[i].uuid ?? ''); } catch (_) { await Future.delayed(const Duration(milliseconds: 500)); @@ -387,10 +366,7 @@ class ACsBloc extends Bloc { _timer = Timer(const Duration(seconds: 1), () async { try { final response = await DevicesAPI.controlDevice( - DeviceControlModel( - deviceId: allAcsPage ? deviceId : acId, - code: code, - value: value), + DeviceControlModel(deviceId: allAcsPage ? deviceId : acId, code: code, value: value), allAcsPage ? deviceId : acId); if (!response['success']) { @@ -407,8 +383,7 @@ class ACsBloc extends Bloc { if (value >= 20 && value <= 30) { return true; } else { - emit(const AcsFailedState( - errorMessage: 'The temperature must be between 20 and 30')); + emit(const AcsFailedState(errorMessage: 'The temperature must be between 20 and 30')); emit(GetAllAcsStatusState( allAcsStatues: deviceStatusList, allAcs: devicesList, @@ -434,9 +409,7 @@ class ACsBloc extends Bloc { try { seconds = event.seconds; final response = await DevicesAPI.controlDevice( - DeviceControlModel( - deviceId: acId, code: 'countdown_time', value: event.duration), - acId); + DeviceControlModel(deviceId: acId, code: 'countdown_time', value: event.duration), acId); if (response['success'] ?? false) { deviceStatus.countdown1 = seconds; @@ -463,8 +436,7 @@ class ACsBloc extends Bloc { for (var status in response['status']) { statusModelList.add(StatusModel.fromJson(status)); } - deviceStatus = - AcStatusModel.fromJson(response['productUuid'], statusModelList); + deviceStatus = AcStatusModel.fromJson(response['productUuid'], statusModelList); deviceStatus.countdown1; var duration; if (deviceStatus.countdown1 == 5) { diff --git a/lib/features/devices/model/ac_model.dart b/lib/features/devices/model/ac_model.dart index 677c414..6962c2a 100644 --- a/lib/features/devices/model/ac_model.dart +++ b/lib/features/devices/model/ac_model.dart @@ -27,24 +27,24 @@ class AcStatusModel { } factory AcStatusModel.fromJson(String id, List jsonList) { - late bool _acSwitch; - late String _mode; - late int _tempSet; - late int _currentTemp; - late String _fanSpeeds; - late int _countdown1; - late bool _childLock; + bool _acSwitch = false; + String _mode = ''; + int _tempSet = 210; + int _currentTemp = 210; + String _fanSpeeds = ''; + int _countdown1 = 0; + bool _childLock = false; for (int i = 0; i < jsonList.length; i++) { if (jsonList[i].code == 'switch') { _acSwitch = jsonList[i].value ?? false; } else if (jsonList[i].code == 'mode') { - _mode = jsonList[i].value ?? TempModes.cold; + _mode = jsonList[i].value ?? ''; } else if (jsonList[i].code == 'temp_set') { _tempSet = jsonList[i].value ?? 210; } else if (jsonList[i].code == 'temp_current') { _currentTemp = jsonList[i].value ?? 210; } else if (jsonList[i].code == 'level') { - _fanSpeeds = jsonList[i].value ?? 210; + _fanSpeeds = jsonList[i].value ?? ''; } else if (jsonList[i].code == 'child_lock') { _childLock = jsonList[i].value ?? false; } else if (jsonList[i].code == 'countdown_time') { diff --git a/lib/features/devices/model/ceiling_sensor_model.dart b/lib/features/devices/model/ceiling_sensor_model.dart index b086038..e66e513 100644 --- a/lib/features/devices/model/ceiling_sensor_model.dart +++ b/lib/features/devices/model/ceiling_sensor_model.dart @@ -23,9 +23,9 @@ class CeilingSensorModel { required this.bodyMovement}); factory CeilingSensorModel.fromJson(List jsonList) { - late String _presenceState; - late int _sensitivity; - late String _checkingResult; + String _presenceState = 'none'; + int _sensitivity = 1; + String _checkingResult = ''; int _presenceRange = 1; int _sportsPara = 1; int _moving_max_dis = 0; diff --git a/lib/features/devices/model/curtain_model.dart b/lib/features/devices/model/curtain_model.dart index a8cf3e5..16f4203 100644 --- a/lib/features/devices/model/curtain_model.dart +++ b/lib/features/devices/model/curtain_model.dart @@ -10,11 +10,11 @@ class CurtainModel { }); factory CurtainModel.fromJson(List jsonList) { - late String _control; - late int _percent; + String _control = ''; + int _percent = 0; for (int i = 0; i < jsonList.length; i++) { if (jsonList[i].code == 'control') { - _control = jsonList[i].value ?? false; + _control = jsonList[i].value ?? ''; } if (jsonList[i].code == 'percent_control') { _percent = jsonList[i].value ?? 0; diff --git a/lib/features/devices/model/device_info_model.dart b/lib/features/devices/model/device_info_model.dart index 9c01d70..b3c1653 100644 --- a/lib/features/devices/model/device_info_model.dart +++ b/lib/features/devices/model/device_info_model.dart @@ -1,5 +1,3 @@ -import 'dart:convert'; - class DeviceInfoModel { final int activeTime; final String category; diff --git a/lib/features/devices/model/door_sensor_model.dart b/lib/features/devices/model/door_sensor_model.dart index 7ef8b1d..fa27287 100644 --- a/lib/features/devices/model/door_sensor_model.dart +++ b/lib/features/devices/model/door_sensor_model.dart @@ -1,27 +1,22 @@ - - - - import 'package:syncrow_app/features/devices/model/status_model.dart'; class DoorSensorModel { bool doorContactState; int batteryPercentage; - DoorSensorModel( - {required this.doorContactState, - required this.batteryPercentage, - }); + DoorSensorModel({ + required this.doorContactState, + required this.batteryPercentage, + }); factory DoorSensorModel.fromJson(List jsonList) { - late bool _doorContactState; - late int _batteryPercentage; - + bool _doorContactState = false; + int _batteryPercentage = 0; for (int i = 0; i < jsonList.length; i++) { if (jsonList[i].code == 'doorcontact_state') { _doorContactState = jsonList[i].value ?? false; - } else if (jsonList[i].code == 'battery_percentage') { + } else if (jsonList[i].code == 'battery_percentage') { _batteryPercentage = jsonList[i].value ?? 0; } } diff --git a/lib/features/devices/model/garage_door_model.dart b/lib/features/devices/model/garage_door_model.dart index 146523a..3bda7ae 100644 --- a/lib/features/devices/model/garage_door_model.dart +++ b/lib/features/devices/model/garage_door_model.dart @@ -24,15 +24,15 @@ class GarageDoorModel { }); factory GarageDoorModel.fromJson(List jsonList) { - late bool _switch1 = false; - late bool _doorContactState = false; - late int _countdown1 = 0; - late int _countdownAlarm = 0; - late String _doorControl1 = "closed"; - late bool _voiceControl1 = false; - late String _doorState1 = "closed"; - late int _batteryPercentage = 0; - late int _tr_timecon = 0; + bool _switch1 = false; + bool _doorContactState = false; + int _countdown1 = 0; + int _countdownAlarm = 0; + String _doorControl1 = "closed"; + bool _voiceControl1 = false; + String _doorState1 = "closed"; + int _batteryPercentage = 0; + int _tr_timecon = 0; for (var status in jsonList) { switch (status.code) { diff --git a/lib/features/devices/model/one_gang_model.dart b/lib/features/devices/model/one_gang_model.dart index 4e73231..a12fcf1 100644 --- a/lib/features/devices/model/one_gang_model.dart +++ b/lib/features/devices/model/one_gang_model.dart @@ -1,29 +1,27 @@ - import 'package:syncrow_app/features/devices/model/status_model.dart'; class OneGangModel { bool firstSwitch; int firstCountDown; - OneGangModel( - {required this.firstSwitch, - required this.firstCountDown, - }); + OneGangModel({ + required this.firstSwitch, + required this.firstCountDown, + }); factory OneGangModel.fromJson(List jsonList) { - late bool _switch; - late int _count; - + bool _switch = false; + int _count = 0; for (int i = 0; i < jsonList.length; i++) { if (jsonList[i].code == 'switch_1') { _switch = jsonList[i].value ?? false; - } else if (jsonList[i].code == 'countdown_1') { + } else if (jsonList[i].code == 'countdown_1') { _count = jsonList[i].value ?? 0; } } return OneGangModel( - firstSwitch: _switch, + firstSwitch: _switch, firstCountDown: _count, ); } diff --git a/lib/features/devices/model/one_touch_model.dart b/lib/features/devices/model/one_touch_model.dart index 34ea764..6ca95b2 100644 --- a/lib/features/devices/model/one_touch_model.dart +++ b/lib/features/devices/model/one_touch_model.dart @@ -1,5 +1,3 @@ - - import 'package:syncrow_app/features/devices/model/status_model.dart'; import 'package:syncrow_app/utils/resource_manager/constants.dart'; @@ -15,15 +13,14 @@ class OneTouchModel { required this.firstCountDown, required this.light_mode, required this.relay, - required this.relay_status_1 - }); + required this.relay_status_1}); factory OneTouchModel.fromJson(List jsonList) { - late bool _switch; - late int _count; - late String _relay; - late String _light_mode; - late String relay_status_1; + bool _switch = false; + int _count = 0; + String _relay = ''; + String _light_mode = ''; + String relay_status_1 = ''; for (int i = 0; i < jsonList.length; i++) { if (jsonList[i].code == 'switch_1') { @@ -41,10 +38,8 @@ class OneTouchModel { return OneTouchModel( firstSwitch: _switch, firstCountDown: _count, - light_mode: lightStatusExtension.fromString(_light_mode) , - relay: StatusExtension.fromString(_relay ) , - relay_status_1: StatusExtension.fromString(relay_status_1 ) - - ); + light_mode: lightStatusExtension.fromString(_light_mode), + relay: StatusExtension.fromString(_relay), + relay_status_1: StatusExtension.fromString(relay_status_1)); } } diff --git a/lib/features/devices/model/six_scene_model.dart b/lib/features/devices/model/six_scene_model.dart index c3c01b5..be697d6 100644 --- a/lib/features/devices/model/six_scene_model.dart +++ b/lib/features/devices/model/six_scene_model.dart @@ -22,14 +22,14 @@ class SixSceneModel { }); factory SixSceneModel.fromJson(List jsonList) { - late dynamic _scene_1; - late dynamic _scene_2; - late dynamic _scene_3; - late dynamic _scene_4; - late dynamic _scene_5; - late dynamic _scene_6; - late dynamic _scene_id_group_id; - late dynamic _switch_backlight; + dynamic _scene_1 = ''; + dynamic _scene_2 = ''; + dynamic _scene_3 = ''; + dynamic _scene_4 = ''; + dynamic _scene_5 = ''; + dynamic _scene_6 = ''; + dynamic _scene_id_group_id = 0; + dynamic _switch_backlight = false; for (int i = 0; i < jsonList.length; i++) { if (jsonList[i].code == 'scene_1') { diff --git a/lib/features/devices/model/smart_door_model.dart b/lib/features/devices/model/smart_door_model.dart index 2403941..a215d0c 100644 --- a/lib/features/devices/model/smart_door_model.dart +++ b/lib/features/devices/model/smart_door_model.dart @@ -39,23 +39,23 @@ class SmartDoorModel { required this.normalOpenSwitch}); factory SmartDoorModel.fromJson(List jsonList) { - late int _unlockFingerprint; - late int _unlockPassword; - late int _unlockTemporary; - late int _unlockCard; - late String _unlockAlarm; - late int _unlockRequest; - late int _residualElectricity; - late bool _reverseLock; - late int _unlockApp; - late bool _hijack; - late bool _doorbell; - late String _unlockOfflinePd; - late String _unlockOfflineClear; - late String _unlockDoubleKit; - late String _remoteNoPdSetkey; - late String _remoteNoDpKey; - late bool _normalOpenSwitch; + int _unlockFingerprint = 0; + int _unlockPassword = 0; + int _unlockTemporary = 0; + int _unlockCard = 0; + String _unlockAlarm = ''; + int _unlockRequest = 0; + int _residualElectricity = 0; + bool _reverseLock = false; + int _unlockApp = 0; + bool _hijack = false; + bool _doorbell = false; + String _unlockOfflinePd = ''; + String _unlockOfflineClear = ''; + String _unlockDoubleKit = ''; + String _remoteNoPdSetkey = ''; + String _remoteNoDpKey = ''; + bool _normalOpenSwitch = false; for (int i = 0; i < jsonList.length; i++) { if (jsonList[i].code == 'unlock_fingerprint') { diff --git a/lib/features/devices/model/sos_model.dart b/lib/features/devices/model/sos_model.dart index 7534b60..9ec0f33 100644 --- a/lib/features/devices/model/sos_model.dart +++ b/lib/features/devices/model/sos_model.dart @@ -10,8 +10,8 @@ class SosModel { }); factory SosModel.fromJson(List jsonList) { - late String _sosContactState; - late int _batteryPercentage; + String _sosContactState = ''; + int _batteryPercentage = 0; for (int i = 0; i < jsonList.length; i++) { if (jsonList[i].code == 'sos') { diff --git a/lib/features/devices/view/widgets/ACs/acs_list.dart b/lib/features/devices/view/widgets/ACs/acs_list.dart index 735bc9c..f1c81f4 100644 --- a/lib/features/devices/view/widgets/ACs/acs_list.dart +++ b/lib/features/devices/view/widgets/ACs/acs_list.dart @@ -31,7 +31,7 @@ class ACsList extends StatelessWidget { List devicesList = []; bool allOn = false; bool allTempSame = false; - int temperature = 20; + int temperature = 250; if (state is GetAllAcsStatusState) { devicesStatuesList = state.allAcsStatues; devicesList = state.allAcs; diff --git a/pubspec.yaml b/pubspec.yaml index 221c843..b60a986 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -5,7 +5,7 @@ description: This is the mobile application project, developed with Flutter for # pub.dev using `flutter pub publish`. This is preferred for private packages. publish_to: "none" # Remove this line if you wish to publish to pub.dev -version: 1.0.18+55 +version: 1.0.27+64 environment: sdk: ">=3.0.6 <4.0.0"