From f38ac58442deb9172cdc702ee2dde1983749a0cb Mon Sep 17 00:00:00 2001 From: mohammad Date: Wed, 25 Jun 2025 14:45:10 +0300 Subject: [PATCH 1/2] Add bloc closure handling and improve device status updates in AcBloc --- .../device_managment/ac/bloc/ac_bloc.dart | 49 +++++++++++++------ 1 file changed, 33 insertions(+), 16 deletions(-) diff --git a/lib/pages/device_managment/ac/bloc/ac_bloc.dart b/lib/pages/device_managment/ac/bloc/ac_bloc.dart index af5a7b0a..eaababe1 100644 --- a/lib/pages/device_managment/ac/bloc/ac_bloc.dart +++ b/lib/pages/device_managment/ac/bloc/ac_bloc.dart @@ -16,6 +16,7 @@ class AcBloc extends Bloc { final ControlDeviceService controlDeviceService; final BatchControlDevicesService batchControlDevicesService; Timer? _countdownTimer; + bool _isBlocClosed = false; AcBloc({ required this.deviceId, @@ -45,7 +46,8 @@ class AcBloc extends Bloc { ) async { emit(AcsLoadingState()); try { - final status = await DevicesManagementApi().getDeviceStatus(event.deviceId); + final status = + await DevicesManagementApi().getDeviceStatus(event.deviceId); deviceStatus = AcStatusModel.fromJson(event.deviceId, status.status); if (deviceStatus.countdown1 != 0) { final totalMinutes = deviceStatus.countdown1 * 6; @@ -68,12 +70,13 @@ class AcBloc extends Bloc { } } - void _listenToChanges(deviceId) { + StreamSubscription? _deviceStatusSubscription; + + void _listenToChanges(String deviceId) { try { final ref = FirebaseDatabase.instance.ref('device-status/$deviceId'); - final stream = ref.onValue; - - stream.listen((DatabaseEvent event) async { + _deviceStatusSubscription = + ref.onValue.listen((DatabaseEvent event) async { if (event.snapshot.value == null) return; Map usersMap = @@ -82,11 +85,15 @@ class AcBloc extends Bloc { List statusList = []; usersMap['status'].forEach((element) { - statusList.add(Status(code: element['code'], value: element['value'])); + statusList + .add(Status(code: element['code'], value: element['value'])); }); - deviceStatus = AcStatusModel.fromJson(usersMap['productUuid'], statusList); - if (!isClosed) { + deviceStatus = + AcStatusModel.fromJson(usersMap['productUuid'], statusList); + print('Device status updated: ${deviceStatus.acSwitch}'); + + if (!_isBlocClosed) { add(AcStatusUpdated(deviceStatus)); } }); @@ -106,15 +113,14 @@ class AcBloc extends Bloc { Emitter emit, ) async { emit(AcsLoadingState()); - _updateDeviceFunctionFromCode(event.code, event.value); - emit(ACStatusLoaded(status: deviceStatus)); try { final success = await controlDeviceService.controlDevice( deviceUuid: event.deviceId, status: Status(code: event.code, value: event.value), ); - + _updateDeviceFunctionFromCode(event.code, event.value); + emit(ACStatusLoaded(status: deviceStatus)); if (!success) { emit(const AcsFailedState(error: 'Failed to control device')); } @@ -129,8 +135,10 @@ class AcBloc extends Bloc { ) async { emit(AcsLoadingState()); try { - final status = await DevicesManagementApi().getBatchStatus(event.devicesIds); - deviceStatus = AcStatusModel.fromJson(event.devicesIds.first, status.status); + final status = + await DevicesManagementApi().getBatchStatus(event.devicesIds); + deviceStatus = + AcStatusModel.fromJson(event.devicesIds.first, status.status); emit(ACStatusLoaded(status: deviceStatus)); } catch (e) { emit(AcsFailedState(error: e.toString())); @@ -293,13 +301,17 @@ class AcBloc extends Bloc { totalSeconds--; scheduledHours = totalSeconds ~/ 3600; scheduledMinutes = (totalSeconds % 3600) ~/ 60; - add(UpdateTimerEvent()); + if (!_isBlocClosed) { + add(UpdateTimerEvent()); + } } else { _countdownTimer?.cancel(); timerActive = false; scheduledHours = 0; scheduledMinutes = 0; - add(TimerCompletedEvent()); + if (!_isBlocClosed) { + add(TimerCompletedEvent()); + } } }); } @@ -326,7 +338,9 @@ class AcBloc extends Bloc { _startCountdownTimer( emit, ); - add(UpdateTimerEvent()); + if (!_isBlocClosed) { + add(UpdateTimerEvent()); + } } } @@ -370,6 +384,9 @@ class AcBloc extends Bloc { @override Future close() { add(OnClose()); + _countdownTimer?.cancel(); + _deviceStatusSubscription?.cancel(); + _isBlocClosed = true; return super.close(); } } From 3c9494963d60a12767f559d84629fc6eee356079 Mon Sep 17 00:00:00 2001 From: mohammad Date: Wed, 25 Jun 2025 15:58:58 +0300 Subject: [PATCH 2/2] Add generated configuration files for Flutter integration across platforms --- lib/pages/device_managment/ac/bloc/ac_bloc.dart | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/lib/pages/device_managment/ac/bloc/ac_bloc.dart b/lib/pages/device_managment/ac/bloc/ac_bloc.dart index eaababe1..38d11a46 100644 --- a/lib/pages/device_managment/ac/bloc/ac_bloc.dart +++ b/lib/pages/device_managment/ac/bloc/ac_bloc.dart @@ -16,7 +16,6 @@ class AcBloc extends Bloc { final ControlDeviceService controlDeviceService; final BatchControlDevicesService batchControlDevicesService; Timer? _countdownTimer; - bool _isBlocClosed = false; AcBloc({ required this.deviceId, @@ -93,7 +92,7 @@ class AcBloc extends Bloc { AcStatusModel.fromJson(usersMap['productUuid'], statusList); print('Device status updated: ${deviceStatus.acSwitch}'); - if (!_isBlocClosed) { + if (!isClosed) { add(AcStatusUpdated(deviceStatus)); } }); @@ -301,7 +300,7 @@ class AcBloc extends Bloc { totalSeconds--; scheduledHours = totalSeconds ~/ 3600; scheduledMinutes = (totalSeconds % 3600) ~/ 60; - if (!_isBlocClosed) { + if (!isClosed) { add(UpdateTimerEvent()); } } else { @@ -309,7 +308,7 @@ class AcBloc extends Bloc { timerActive = false; scheduledHours = 0; scheduledMinutes = 0; - if (!_isBlocClosed) { + if (!isClosed) { add(TimerCompletedEvent()); } } @@ -338,7 +337,7 @@ class AcBloc extends Bloc { _startCountdownTimer( emit, ); - if (!_isBlocClosed) { + if (!isClosed) { add(UpdateTimerEvent()); } } @@ -386,7 +385,6 @@ class AcBloc extends Bloc { add(OnClose()); _countdownTimer?.cancel(); _deviceStatusSubscription?.cancel(); - _isBlocClosed = true; return super.close(); } }