From a67fd44f32bb7ab79b2ad221dcd49c142b616e04 Mon Sep 17 00:00:00 2001 From: Abdullah Alassaf Date: Sun, 14 Jul 2024 02:01:36 +0300 Subject: [PATCH] Fixed the three gang performance issue --- .../bloc/three_gang_bloc/three_gang_bloc.dart | 190 +++++++++++++----- .../devices/model/group_three_gang_model.dart | 6 +- .../widgets/three_gang/three_gang_list.dart | 138 ++++++------- 3 files changed, 206 insertions(+), 128 deletions(-) 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 b6a566c..39a9444 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 @@ -95,6 +95,22 @@ class ThreeGangBloc extends Bloc { void _changeFirstSwitch(ChangeFirstSwitchStatusEvent event, Emitter emit) async { emit(LoadingNewSate(threeGangModel: deviceStatus)); try { + if (threeGangGroup) { + bool allSwitchesValue = true; + groupThreeGangList.forEach((element) { + if (element.deviceId == event.deviceId) { + element.firstSwitch = !event.value; + } + if (!element.firstSwitch || !element.secondSwitch || !element.thirdSwitch) { + allSwitchesValue = false; + } + }); + emit(UpdateGroupState(threeGangList: groupThreeGangList, allSwitches: allSwitchesValue)); + } else { + deviceStatus.firstSwitch = !event.value; + emit(UpdateState(threeGangModel: deviceStatus)); + } + final response = await DevicesAPI.controlDevice( DeviceControlModel( deviceId: threeGangGroup ? event.deviceId : threeGangId, @@ -102,15 +118,11 @@ class ThreeGangBloc extends Bloc { value: !event.value), threeGangGroup ? event.deviceId : threeGangId); - if (response['success'] ?? false) { - deviceStatus.firstSwitch = !event.value; + if (!response['success']) { + add(InitialEvent(groupScreen: threeGangGroup)); } - } catch (_) {} - if (threeGangGroup) { - await Future.delayed(const Duration(seconds: 1)); - add(const InitialEvent(groupScreen: true)); - } else { - emit(UpdateState(threeGangModel: deviceStatus)); + } catch (_) { + add(InitialEvent(groupScreen: threeGangGroup)); } } @@ -118,6 +130,22 @@ class ThreeGangBloc extends Bloc { ChangeSecondSwitchStatusEvent event, Emitter emit) async { emit(LoadingNewSate(threeGangModel: deviceStatus)); try { + if (threeGangGroup) { + bool allSwitchesValue = true; + groupThreeGangList.forEach((element) { + if (element.deviceId == event.deviceId) { + element.secondSwitch = !event.value; + } + if (!element.firstSwitch || !element.secondSwitch || !element.thirdSwitch) { + allSwitchesValue = false; + } + }); + emit(UpdateGroupState(threeGangList: groupThreeGangList, allSwitches: allSwitchesValue)); + } else { + deviceStatus.secondSwitch = !event.value; + emit(UpdateState(threeGangModel: deviceStatus)); + } + final response = await DevicesAPI.controlDevice( DeviceControlModel( deviceId: threeGangGroup ? event.deviceId : threeGangId, @@ -125,21 +153,33 @@ class ThreeGangBloc extends Bloc { value: !event.value), threeGangGroup ? event.deviceId : threeGangId); - if (response['success'] ?? false) { - deviceStatus.secondSwitch = !event.value; + if (!response['success']) { + add(InitialEvent(groupScreen: threeGangGroup)); } - } catch (_) {} - if (threeGangGroup) { - await Future.delayed(const Duration(seconds: 1)); - add(const InitialEvent(groupScreen: true)); - } else { - emit(UpdateState(threeGangModel: deviceStatus)); + } catch (_) { + add(InitialEvent(groupScreen: threeGangGroup)); } } void _changeThirdSwitch(ChangeThirdSwitchStatusEvent event, Emitter emit) async { emit(LoadingNewSate(threeGangModel: deviceStatus)); try { + if (threeGangGroup) { + bool allSwitchesValue = true; + groupThreeGangList.forEach((element) { + if (element.deviceId == event.deviceId) { + element.thirdSwitch = !event.value; + } + if (!element.firstSwitch || !element.secondSwitch || !element.thirdSwitch) { + allSwitchesValue = false; + } + }); + emit(UpdateGroupState(threeGangList: groupThreeGangList, allSwitches: allSwitchesValue)); + } else { + deviceStatus.thirdSwitch = !event.value; + emit(UpdateState(threeGangModel: deviceStatus)); + } + final response = await DevicesAPI.controlDevice( DeviceControlModel( deviceId: threeGangGroup ? event.deviceId : threeGangId, @@ -147,15 +187,11 @@ class ThreeGangBloc extends Bloc { value: !event.value), threeGangGroup ? event.deviceId : threeGangId); - if (response['success'] ?? false) { - deviceStatus.thirdSwitch = !event.value; + if (!response['success']) { + add(InitialEvent(groupScreen: threeGangGroup)); } - } catch (_) {} - if (threeGangGroup) { - await Future.delayed(const Duration(seconds: 1)); - add(const InitialEvent(groupScreen: true)); - } else { - emit(UpdateState(threeGangModel: deviceStatus)); + } catch (_) { + add(InitialEvent(groupScreen: threeGangGroup)); } } @@ -163,52 +199,82 @@ class ThreeGangBloc extends Bloc { emit(LoadingNewSate(threeGangModel: deviceStatus)); try { + deviceStatus.firstSwitch = false; + deviceStatus.secondSwitch = false; + deviceStatus.thirdSwitch = false; + emit(UpdateState(threeGangModel: deviceStatus)); + final response = await Future.wait([ DevicesAPI.controlDevice( - DeviceControlModel(deviceId: threeGangId, code: 'switch_1', value: false), threeGangId), + DeviceControlModel( + deviceId: threeGangId, code: 'switch_1', value: deviceStatus.firstSwitch), + threeGangId), DevicesAPI.controlDevice( - DeviceControlModel(deviceId: threeGangId, code: 'switch_2', value: false), threeGangId), + DeviceControlModel( + deviceId: threeGangId, code: 'switch_2', value: deviceStatus.secondSwitch), + threeGangId), DevicesAPI.controlDevice( - DeviceControlModel(deviceId: threeGangId, code: 'switch_3', value: false), threeGangId), + DeviceControlModel( + deviceId: threeGangId, code: 'switch_3', value: deviceStatus.thirdSwitch), + threeGangId), ]); - if (response.every((element) => element['success'] ?? false)) { - deviceStatus.firstSwitch = false; - deviceStatus.secondSwitch = false; - deviceStatus.thirdSwitch = false; + if (response.every((element) => !element['success'])) { + await Future.delayed(const Duration(milliseconds: 500)); + add(const InitialEvent(groupScreen: false)); } - } catch (_) {} - emit(UpdateState(threeGangModel: deviceStatus)); + } catch (_) { + await Future.delayed(const Duration(milliseconds: 500)); + add(const InitialEvent(groupScreen: false)); + } } void _allOn(AllOnEvent event, Emitter emit) async { emit(LoadingNewSate(threeGangModel: deviceStatus)); try { + deviceStatus.firstSwitch = true; + deviceStatus.secondSwitch = true; + deviceStatus.thirdSwitch = true; + emit(UpdateState(threeGangModel: deviceStatus)); + final response = await Future.wait([ DevicesAPI.controlDevice( - DeviceControlModel(deviceId: threeGangId, code: 'switch_1', value: true), threeGangId), + DeviceControlModel( + deviceId: threeGangId, code: 'switch_1', value: deviceStatus.firstSwitch), + threeGangId), DevicesAPI.controlDevice( - DeviceControlModel(deviceId: threeGangId, code: 'switch_2', value: true), threeGangId), + DeviceControlModel( + deviceId: threeGangId, code: 'switch_2', value: deviceStatus.secondSwitch), + threeGangId), DevicesAPI.controlDevice( - DeviceControlModel(deviceId: threeGangId, code: 'switch_3', value: true), threeGangId), + DeviceControlModel( + deviceId: threeGangId, code: 'switch_3', value: deviceStatus.thirdSwitch), + threeGangId), ]); - if (response.every((element) => element['success'] ?? false)) { - deviceStatus.firstSwitch = true; - deviceStatus.secondSwitch = true; - deviceStatus.thirdSwitch = true; + if (response.every((element) => !element['success'])) { + await Future.delayed(const Duration(milliseconds: 500)); + add(const InitialEvent(groupScreen: false)); } - } catch (_) {} - emit(UpdateState(threeGangModel: deviceStatus)); + } catch (_) { + await Future.delayed(const Duration(milliseconds: 500)); + add(const InitialEvent(groupScreen: false)); + } } void _groupAllOn(GroupAllOnEvent event, Emitter emit) async { emit(LoadingNewSate(threeGangModel: deviceStatus)); - try { for (int i = 0; i < groupThreeGangList.length; i++) { - await Future.wait([ + groupThreeGangList[i].firstSwitch = true; + groupThreeGangList[i].secondSwitch = true; + groupThreeGangList[i].thirdSwitch = true; + } + emit(UpdateGroupState(threeGangList: groupThreeGangList, allSwitches: true)); + + for (int i = 0; i < groupThreeGangList.length; i++) { + final response = await Future.wait([ DevicesAPI.controlDevice( DeviceControlModel( deviceId: groupThreeGangList[i].deviceId, code: 'switch_1', value: true), @@ -222,18 +288,31 @@ class ThreeGangBloc extends Bloc { deviceId: groupThreeGangList[i].deviceId, code: 'switch_3', value: true), groupThreeGangList[i].deviceId), ]); + + if (response.every((element) => !element['success'])) { + await Future.delayed(const Duration(milliseconds: 500)); + add(const InitialEvent(groupScreen: true)); + break; + } } - } catch (_) {} - await Future.delayed(const Duration(seconds: 1)); - add(const InitialEvent(groupScreen: true)); + } catch (_) { + await Future.delayed(const Duration(milliseconds: 500)); + add(const InitialEvent(groupScreen: true)); + } } void _groupAllOff(GroupAllOffEvent event, Emitter emit) async { emit(LoadingNewSate(threeGangModel: deviceStatus)); - try { for (int i = 0; i < groupThreeGangList.length; i++) { - await Future.wait([ + groupThreeGangList[i].firstSwitch = false; + groupThreeGangList[i].secondSwitch = false; + groupThreeGangList[i].thirdSwitch = false; + } + emit(UpdateGroupState(threeGangList: groupThreeGangList, allSwitches: false)); + + for (int i = 0; i < groupThreeGangList.length; i++) { + final response = await Future.wait([ DevicesAPI.controlDevice( DeviceControlModel( deviceId: groupThreeGangList[i].deviceId, code: 'switch_1', value: false), @@ -247,10 +326,17 @@ class ThreeGangBloc extends Bloc { deviceId: groupThreeGangList[i].deviceId, code: 'switch_3', value: false), groupThreeGangList[i].deviceId), ]); + + if (response.every((element) => !element['success'])) { + await Future.delayed(const Duration(milliseconds: 500)); + add(const InitialEvent(groupScreen: true)); + break; + } } - } catch (_) {} - await Future.delayed(const Duration(seconds: 1)); - add(const InitialEvent(groupScreen: true)); + } catch (_) { + await Future.delayed(const Duration(milliseconds: 500)); + add(const InitialEvent(groupScreen: true)); + } } void _changeSliding(ChangeSlidingSegment event, Emitter emit) async { diff --git a/lib/features/devices/model/group_three_gang_model.dart b/lib/features/devices/model/group_three_gang_model.dart index 76ca7e9..8c77e0f 100644 --- a/lib/features/devices/model/group_three_gang_model.dart +++ b/lib/features/devices/model/group_three_gang_model.dart @@ -1,9 +1,9 @@ class GroupThreeGangModel { final String deviceId; final String deviceName; - final bool firstSwitch; - final bool secondSwitch; - final bool thirdSwitch; + bool firstSwitch; + bool secondSwitch; + bool thirdSwitch; GroupThreeGangModel({ required this.deviceId, diff --git a/lib/features/devices/view/widgets/three_gang/three_gang_list.dart b/lib/features/devices/view/widgets/three_gang/three_gang_list.dart index 7bbf700..0d9b143 100644 --- a/lib/features/devices/view/widgets/three_gang/three_gang_list.dart +++ b/lib/features/devices/view/widgets/three_gang/three_gang_list.dart @@ -4,7 +4,6 @@ import 'package:syncrow_app/features/devices/bloc/three_gang_bloc/three_gang_blo import 'package:syncrow_app/features/devices/bloc/three_gang_bloc/three_gang_event.dart'; import 'package:syncrow_app/features/devices/bloc/three_gang_bloc/three_gang_state.dart'; import 'package:syncrow_app/features/devices/model/group_three_gang_model.dart'; -import 'package:syncrow_app/features/shared_widgets/default_container.dart'; import 'package:syncrow_app/features/shared_widgets/devices_default_switch.dart'; import 'package:syncrow_app/features/shared_widgets/text_widgets/body_small.dart'; @@ -18,78 +17,71 @@ class ThreeGangList extends StatelessWidget { Widget build(BuildContext context) { return BlocBuilder( builder: (context, state) { - return state is LoadingNewSate - ? const Center( - child: DefaultContainer(width: 50, height: 50, child: CircularProgressIndicator()), - ) - : SingleChildScrollView( - child: Column( - crossAxisAlignment: CrossAxisAlignment.stretch, - children: [ - const SizedBox(height: 10), - const BodySmall(text: 'All Lights'), - const SizedBox(height: 5), - DevicesDefaultSwitch( - switchValue: allSwitches, - action: () { - BlocProvider.of(context).add(GroupAllOnEvent()); - }, - secondAction: () { - BlocProvider.of(context).add(GroupAllOffEvent()); - }, - ), - ListView.builder( - shrinkWrap: true, - physics: const NeverScrollableScrollPhysics(), - padding: const EdgeInsets.all(0), - itemCount: threeGangList.length, - itemBuilder: (context, index) { - return Column( - crossAxisAlignment: CrossAxisAlignment.start, - children: [ - const SizedBox(height: 10), - BodySmall(text: '${threeGangList[index].deviceName} beside light'), - const SizedBox(height: 5), - DevicesDefaultSwitch( - switchValue: threeGangList[index].firstSwitch, - action: () { - BlocProvider.of(context).add( - ChangeFirstSwitchStatusEvent( - value: threeGangList[index].firstSwitch, - deviceId: threeGangList[index].deviceId)); - }, - ), - const SizedBox(height: 10), - BodySmall(text: '${threeGangList[index].deviceName} ceiling light'), - const SizedBox(height: 5), - DevicesDefaultSwitch( - switchValue: threeGangList[index].secondSwitch, - action: () { - BlocProvider.of(context).add( - ChangeSecondSwitchStatusEvent( - value: threeGangList[index].secondSwitch, - deviceId: threeGangList[index].deviceId)); - }, - ), - const SizedBox(height: 10), - BodySmall(text: '${threeGangList[index].deviceName} spotlight'), - const SizedBox(height: 5), - DevicesDefaultSwitch( - switchValue: threeGangList[index].thirdSwitch, - action: () { - BlocProvider.of(context).add( - ChangeThirdSwitchStatusEvent( - value: threeGangList[index].thirdSwitch, - deviceId: threeGangList[index].deviceId)); - }, - ), - ], - ); - }, - ), - ], - ), - ); + return SingleChildScrollView( + child: Column( + crossAxisAlignment: CrossAxisAlignment.stretch, + children: [ + const SizedBox(height: 10), + const BodySmall(text: 'All Lights'), + const SizedBox(height: 5), + DevicesDefaultSwitch( + switchValue: allSwitches, + action: () { + BlocProvider.of(context).add(GroupAllOnEvent()); + }, + secondAction: () { + BlocProvider.of(context).add(GroupAllOffEvent()); + }, + ), + ListView.builder( + shrinkWrap: true, + physics: const NeverScrollableScrollPhysics(), + padding: const EdgeInsets.all(0), + itemCount: threeGangList.length, + itemBuilder: (context, index) { + return Column( + crossAxisAlignment: CrossAxisAlignment.start, + children: [ + const SizedBox(height: 10), + BodySmall(text: '${threeGangList[index].deviceName} beside light'), + const SizedBox(height: 5), + DevicesDefaultSwitch( + switchValue: threeGangList[index].firstSwitch, + action: () { + BlocProvider.of(context).add(ChangeFirstSwitchStatusEvent( + value: threeGangList[index].firstSwitch, + deviceId: threeGangList[index].deviceId)); + }, + ), + const SizedBox(height: 10), + BodySmall(text: '${threeGangList[index].deviceName} ceiling light'), + const SizedBox(height: 5), + DevicesDefaultSwitch( + switchValue: threeGangList[index].secondSwitch, + action: () { + BlocProvider.of(context).add(ChangeSecondSwitchStatusEvent( + value: threeGangList[index].secondSwitch, + deviceId: threeGangList[index].deviceId)); + }, + ), + const SizedBox(height: 10), + BodySmall(text: '${threeGangList[index].deviceName} spotlight'), + const SizedBox(height: 5), + DevicesDefaultSwitch( + switchValue: threeGangList[index].thirdSwitch, + action: () { + BlocProvider.of(context).add(ChangeThirdSwitchStatusEvent( + value: threeGangList[index].thirdSwitch, + deviceId: threeGangList[index].deviceId)); + }, + ), + ], + ); + }, + ), + ], + ), + ); }, ); }