From d3068b8e14b6f7e43bec77d8e1caa841a3cda473 Mon Sep 17 00:00:00 2001 From: mohammad Date: Wed, 26 Feb 2025 10:58:32 +0300 Subject: [PATCH] changes in threeGang --- lib/features/devices/bloc/devices_cubit.dart | 22 ++++++++++++++----- .../bloc/three_gang_bloc/three_gang_bloc.dart | 5 ++--- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/lib/features/devices/bloc/devices_cubit.dart b/lib/features/devices/bloc/devices_cubit.dart index 57c4306..fffe7bc 100644 --- a/lib/features/devices/bloc/devices_cubit.dart +++ b/lib/features/devices/bloc/devices_cubit.dart @@ -518,26 +518,35 @@ class DevicesCubit extends Cubit { } final device = allDevices[deviceIndex]; final switches = ['switch_1', 'switch_2', 'switch_3']; + final anySwitchOff = device.status.any( + (s) => (s.code?.startsWith('switch_') ?? false) && (s.value == false), + ); + final targetState = anySwitchOff ? true : false; + for (final switchCode in switches) { final statusIndex = device.status.indexWhere((s) => s.code == switchCode); if (statusIndex != -1) { final currentValue = device.status[statusIndex].value ?? false; - final toggledValue = !currentValue; + + if (!targetState && !currentValue) { + continue; + } final controlRequest = DeviceControlModel( - code: switchCode, value: toggledValue, deviceId: deviceUuid); + code: switchCode, value: targetState, deviceId: deviceUuid); final response = await DevicesAPI.controlDevice(controlRequest, deviceUuid); if (response['success'] != true) { throw Exception('Failed to toggle $switchCode'); } - device.status[statusIndex].value = toggledValue; + + device.status[statusIndex].value = targetState; } } - final anySwitchOff = device.status.any( - (s) => (s.code?.startsWith('switch_') ?? false) && (s.value == false), + device.toggleStatus = device.status.every( + (s) => (s.code?.startsWith('switch_') ?? false) && (s.value == true), ); - device.toggleStatus = !anySwitchOff; + allDevices[deviceIndex] = device; emit(DeviceControlSuccess(code: control.code)); } catch (failure) { @@ -545,6 +554,7 @@ class DevicesCubit extends Cubit { } } + Future towGangToggle( DeviceControlModel control, String deviceUuid) async { emit(SwitchControlLoading(code: control.code)); diff --git a/lib/features/devices/bloc/three_gang_bloc/three_gang_bloc.dart b/lib/features/devices/bloc/three_gang_bloc/three_gang_bloc.dart index a9dfd97..ceeffd0 100644 --- a/lib/features/devices/bloc/three_gang_bloc/three_gang_bloc.dart +++ b/lib/features/devices/bloc/three_gang_bloc/three_gang_bloc.dart @@ -115,7 +115,6 @@ class ThreeGangBloc extends Bloc { } } - StreamSubscription? _streamSubscription; void _listenToChanges() { @@ -127,7 +126,7 @@ class ThreeGangBloc extends Bloc { _streamSubscription = stream.listen((DatabaseEvent event) async { if (_timer != null) { - await Future.delayed(const Duration(seconds: 2)); + await Future.delayed(const Duration(seconds: 1)); } Map usersMap = event.snapshot.value as Map; @@ -147,7 +146,7 @@ class ThreeGangBloc extends Bloc { @override Future close() async { _streamSubscription?.cancel(); - _streamSubscription = null; + _streamSubscription = null; return super.close(); }