Fixed the three gang performance issue

This commit is contained in:
Abdullah Alassaf
2024-07-14 02:01:36 +03:00
parent ce2f419828
commit a67fd44f32
3 changed files with 206 additions and 128 deletions

View File

@ -95,6 +95,22 @@ class ThreeGangBloc extends Bloc<ThreeGangEvent, ThreeGangState> {
void _changeFirstSwitch(ChangeFirstSwitchStatusEvent event, Emitter<ThreeGangState> emit) async { void _changeFirstSwitch(ChangeFirstSwitchStatusEvent event, Emitter<ThreeGangState> emit) async {
emit(LoadingNewSate(threeGangModel: deviceStatus)); emit(LoadingNewSate(threeGangModel: deviceStatus));
try { 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( final response = await DevicesAPI.controlDevice(
DeviceControlModel( DeviceControlModel(
deviceId: threeGangGroup ? event.deviceId : threeGangId, deviceId: threeGangGroup ? event.deviceId : threeGangId,
@ -102,15 +118,11 @@ class ThreeGangBloc extends Bloc<ThreeGangEvent, ThreeGangState> {
value: !event.value), value: !event.value),
threeGangGroup ? event.deviceId : threeGangId); threeGangGroup ? event.deviceId : threeGangId);
if (response['success'] ?? false) { if (!response['success']) {
deviceStatus.firstSwitch = !event.value; add(InitialEvent(groupScreen: threeGangGroup));
} }
} catch (_) {} } catch (_) {
if (threeGangGroup) { add(InitialEvent(groupScreen: threeGangGroup));
await Future.delayed(const Duration(seconds: 1));
add(const InitialEvent(groupScreen: true));
} else {
emit(UpdateState(threeGangModel: deviceStatus));
} }
} }
@ -118,6 +130,22 @@ class ThreeGangBloc extends Bloc<ThreeGangEvent, ThreeGangState> {
ChangeSecondSwitchStatusEvent event, Emitter<ThreeGangState> emit) async { ChangeSecondSwitchStatusEvent event, Emitter<ThreeGangState> emit) async {
emit(LoadingNewSate(threeGangModel: deviceStatus)); emit(LoadingNewSate(threeGangModel: deviceStatus));
try { 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( final response = await DevicesAPI.controlDevice(
DeviceControlModel( DeviceControlModel(
deviceId: threeGangGroup ? event.deviceId : threeGangId, deviceId: threeGangGroup ? event.deviceId : threeGangId,
@ -125,21 +153,33 @@ class ThreeGangBloc extends Bloc<ThreeGangEvent, ThreeGangState> {
value: !event.value), value: !event.value),
threeGangGroup ? event.deviceId : threeGangId); threeGangGroup ? event.deviceId : threeGangId);
if (response['success'] ?? false) { if (!response['success']) {
deviceStatus.secondSwitch = !event.value; add(InitialEvent(groupScreen: threeGangGroup));
} }
} catch (_) {} } catch (_) {
if (threeGangGroup) { add(InitialEvent(groupScreen: threeGangGroup));
await Future.delayed(const Duration(seconds: 1));
add(const InitialEvent(groupScreen: true));
} else {
emit(UpdateState(threeGangModel: deviceStatus));
} }
} }
void _changeThirdSwitch(ChangeThirdSwitchStatusEvent event, Emitter<ThreeGangState> emit) async { void _changeThirdSwitch(ChangeThirdSwitchStatusEvent event, Emitter<ThreeGangState> emit) async {
emit(LoadingNewSate(threeGangModel: deviceStatus)); emit(LoadingNewSate(threeGangModel: deviceStatus));
try { 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( final response = await DevicesAPI.controlDevice(
DeviceControlModel( DeviceControlModel(
deviceId: threeGangGroup ? event.deviceId : threeGangId, deviceId: threeGangGroup ? event.deviceId : threeGangId,
@ -147,15 +187,11 @@ class ThreeGangBloc extends Bloc<ThreeGangEvent, ThreeGangState> {
value: !event.value), value: !event.value),
threeGangGroup ? event.deviceId : threeGangId); threeGangGroup ? event.deviceId : threeGangId);
if (response['success'] ?? false) { if (!response['success']) {
deviceStatus.thirdSwitch = !event.value; add(InitialEvent(groupScreen: threeGangGroup));
} }
} catch (_) {} } catch (_) {
if (threeGangGroup) { add(InitialEvent(groupScreen: threeGangGroup));
await Future.delayed(const Duration(seconds: 1));
add(const InitialEvent(groupScreen: true));
} else {
emit(UpdateState(threeGangModel: deviceStatus));
} }
} }
@ -163,52 +199,82 @@ class ThreeGangBloc extends Bloc<ThreeGangEvent, ThreeGangState> {
emit(LoadingNewSate(threeGangModel: deviceStatus)); emit(LoadingNewSate(threeGangModel: deviceStatus));
try { try {
deviceStatus.firstSwitch = false;
deviceStatus.secondSwitch = false;
deviceStatus.thirdSwitch = false;
emit(UpdateState(threeGangModel: deviceStatus));
final response = await Future.wait([ final response = await Future.wait([
DevicesAPI.controlDevice( DevicesAPI.controlDevice(
DeviceControlModel(deviceId: threeGangId, code: 'switch_1', value: false), threeGangId), DeviceControlModel(
deviceId: threeGangId, code: 'switch_1', value: deviceStatus.firstSwitch),
threeGangId),
DevicesAPI.controlDevice( DevicesAPI.controlDevice(
DeviceControlModel(deviceId: threeGangId, code: 'switch_2', value: false), threeGangId), DeviceControlModel(
deviceId: threeGangId, code: 'switch_2', value: deviceStatus.secondSwitch),
threeGangId),
DevicesAPI.controlDevice( 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)) { if (response.every((element) => !element['success'])) {
deviceStatus.firstSwitch = false; await Future.delayed(const Duration(milliseconds: 500));
deviceStatus.secondSwitch = false; add(const InitialEvent(groupScreen: false));
deviceStatus.thirdSwitch = false;
} }
} catch (_) {} } catch (_) {
emit(UpdateState(threeGangModel: deviceStatus)); await Future.delayed(const Duration(milliseconds: 500));
add(const InitialEvent(groupScreen: false));
}
} }
void _allOn(AllOnEvent event, Emitter<ThreeGangState> emit) async { void _allOn(AllOnEvent event, Emitter<ThreeGangState> emit) async {
emit(LoadingNewSate(threeGangModel: deviceStatus)); emit(LoadingNewSate(threeGangModel: deviceStatus));
try { try {
deviceStatus.firstSwitch = true;
deviceStatus.secondSwitch = true;
deviceStatus.thirdSwitch = true;
emit(UpdateState(threeGangModel: deviceStatus));
final response = await Future.wait([ final response = await Future.wait([
DevicesAPI.controlDevice( DevicesAPI.controlDevice(
DeviceControlModel(deviceId: threeGangId, code: 'switch_1', value: true), threeGangId), DeviceControlModel(
deviceId: threeGangId, code: 'switch_1', value: deviceStatus.firstSwitch),
threeGangId),
DevicesAPI.controlDevice( DevicesAPI.controlDevice(
DeviceControlModel(deviceId: threeGangId, code: 'switch_2', value: true), threeGangId), DeviceControlModel(
deviceId: threeGangId, code: 'switch_2', value: deviceStatus.secondSwitch),
threeGangId),
DevicesAPI.controlDevice( 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)) { if (response.every((element) => !element['success'])) {
deviceStatus.firstSwitch = true; await Future.delayed(const Duration(milliseconds: 500));
deviceStatus.secondSwitch = true; add(const InitialEvent(groupScreen: false));
deviceStatus.thirdSwitch = true;
} }
} catch (_) {} } catch (_) {
emit(UpdateState(threeGangModel: deviceStatus)); await Future.delayed(const Duration(milliseconds: 500));
add(const InitialEvent(groupScreen: false));
}
} }
void _groupAllOn(GroupAllOnEvent event, Emitter<ThreeGangState> emit) async { void _groupAllOn(GroupAllOnEvent event, Emitter<ThreeGangState> emit) async {
emit(LoadingNewSate(threeGangModel: deviceStatus)); emit(LoadingNewSate(threeGangModel: deviceStatus));
try { try {
for (int i = 0; i < groupThreeGangList.length; i++) { 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( DevicesAPI.controlDevice(
DeviceControlModel( DeviceControlModel(
deviceId: groupThreeGangList[i].deviceId, code: 'switch_1', value: true), deviceId: groupThreeGangList[i].deviceId, code: 'switch_1', value: true),
@ -222,18 +288,31 @@ class ThreeGangBloc extends Bloc<ThreeGangEvent, ThreeGangState> {
deviceId: groupThreeGangList[i].deviceId, code: 'switch_3', value: true), deviceId: groupThreeGangList[i].deviceId, code: 'switch_3', value: true),
groupThreeGangList[i].deviceId), groupThreeGangList[i].deviceId),
]); ]);
if (response.every((element) => !element['success'])) {
await Future.delayed(const Duration(milliseconds: 500));
add(const InitialEvent(groupScreen: true));
break;
}
} }
} catch (_) {} } catch (_) {
await Future.delayed(const Duration(seconds: 1)); await Future.delayed(const Duration(milliseconds: 500));
add(const InitialEvent(groupScreen: true)); add(const InitialEvent(groupScreen: true));
}
} }
void _groupAllOff(GroupAllOffEvent event, Emitter<ThreeGangState> emit) async { void _groupAllOff(GroupAllOffEvent event, Emitter<ThreeGangState> emit) async {
emit(LoadingNewSate(threeGangModel: deviceStatus)); emit(LoadingNewSate(threeGangModel: deviceStatus));
try { try {
for (int i = 0; i < groupThreeGangList.length; i++) { 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( DevicesAPI.controlDevice(
DeviceControlModel( DeviceControlModel(
deviceId: groupThreeGangList[i].deviceId, code: 'switch_1', value: false), deviceId: groupThreeGangList[i].deviceId, code: 'switch_1', value: false),
@ -247,10 +326,17 @@ class ThreeGangBloc extends Bloc<ThreeGangEvent, ThreeGangState> {
deviceId: groupThreeGangList[i].deviceId, code: 'switch_3', value: false), deviceId: groupThreeGangList[i].deviceId, code: 'switch_3', value: false),
groupThreeGangList[i].deviceId), groupThreeGangList[i].deviceId),
]); ]);
if (response.every((element) => !element['success'])) {
await Future.delayed(const Duration(milliseconds: 500));
add(const InitialEvent(groupScreen: true));
break;
}
} }
} catch (_) {} } catch (_) {
await Future.delayed(const Duration(seconds: 1)); await Future.delayed(const Duration(milliseconds: 500));
add(const InitialEvent(groupScreen: true)); add(const InitialEvent(groupScreen: true));
}
} }
void _changeSliding(ChangeSlidingSegment event, Emitter<ThreeGangState> emit) async { void _changeSliding(ChangeSlidingSegment event, Emitter<ThreeGangState> emit) async {

View File

@ -1,9 +1,9 @@
class GroupThreeGangModel { class GroupThreeGangModel {
final String deviceId; final String deviceId;
final String deviceName; final String deviceName;
final bool firstSwitch; bool firstSwitch;
final bool secondSwitch; bool secondSwitch;
final bool thirdSwitch; bool thirdSwitch;
GroupThreeGangModel({ GroupThreeGangModel({
required this.deviceId, required this.deviceId,

View File

@ -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_event.dart';
import 'package:syncrow_app/features/devices/bloc/three_gang_bloc/three_gang_state.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/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/devices_default_switch.dart';
import 'package:syncrow_app/features/shared_widgets/text_widgets/body_small.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) { Widget build(BuildContext context) {
return BlocBuilder<ThreeGangBloc, ThreeGangState>( return BlocBuilder<ThreeGangBloc, ThreeGangState>(
builder: (context, state) { builder: (context, state) {
return state is LoadingNewSate return SingleChildScrollView(
? const Center( child: Column(
child: DefaultContainer(width: 50, height: 50, child: CircularProgressIndicator()), crossAxisAlignment: CrossAxisAlignment.stretch,
) children: [
: SingleChildScrollView( const SizedBox(height: 10),
child: Column( const BodySmall(text: 'All Lights'),
crossAxisAlignment: CrossAxisAlignment.stretch, const SizedBox(height: 5),
children: [ DevicesDefaultSwitch(
const SizedBox(height: 10), switchValue: allSwitches,
const BodySmall(text: 'All Lights'), action: () {
const SizedBox(height: 5), BlocProvider.of<ThreeGangBloc>(context).add(GroupAllOnEvent());
DevicesDefaultSwitch( },
switchValue: allSwitches, secondAction: () {
action: () { BlocProvider.of<ThreeGangBloc>(context).add(GroupAllOffEvent());
BlocProvider.of<ThreeGangBloc>(context).add(GroupAllOnEvent()); },
}, ),
secondAction: () { ListView.builder(
BlocProvider.of<ThreeGangBloc>(context).add(GroupAllOffEvent()); shrinkWrap: true,
}, physics: const NeverScrollableScrollPhysics(),
), padding: const EdgeInsets.all(0),
ListView.builder( itemCount: threeGangList.length,
shrinkWrap: true, itemBuilder: (context, index) {
physics: const NeverScrollableScrollPhysics(), return Column(
padding: const EdgeInsets.all(0), crossAxisAlignment: CrossAxisAlignment.start,
itemCount: threeGangList.length, children: [
itemBuilder: (context, index) { const SizedBox(height: 10),
return Column( BodySmall(text: '${threeGangList[index].deviceName} beside light'),
crossAxisAlignment: CrossAxisAlignment.start, const SizedBox(height: 5),
children: [ DevicesDefaultSwitch(
const SizedBox(height: 10), switchValue: threeGangList[index].firstSwitch,
BodySmall(text: '${threeGangList[index].deviceName} beside light'), action: () {
const SizedBox(height: 5), BlocProvider.of<ThreeGangBloc>(context).add(ChangeFirstSwitchStatusEvent(
DevicesDefaultSwitch( value: threeGangList[index].firstSwitch,
switchValue: threeGangList[index].firstSwitch, deviceId: threeGangList[index].deviceId));
action: () { },
BlocProvider.of<ThreeGangBloc>(context).add( ),
ChangeFirstSwitchStatusEvent( const SizedBox(height: 10),
value: threeGangList[index].firstSwitch, BodySmall(text: '${threeGangList[index].deviceName} ceiling light'),
deviceId: threeGangList[index].deviceId)); const SizedBox(height: 5),
}, DevicesDefaultSwitch(
), switchValue: threeGangList[index].secondSwitch,
const SizedBox(height: 10), action: () {
BodySmall(text: '${threeGangList[index].deviceName} ceiling light'), BlocProvider.of<ThreeGangBloc>(context).add(ChangeSecondSwitchStatusEvent(
const SizedBox(height: 5), value: threeGangList[index].secondSwitch,
DevicesDefaultSwitch( deviceId: threeGangList[index].deviceId));
switchValue: threeGangList[index].secondSwitch, },
action: () { ),
BlocProvider.of<ThreeGangBloc>(context).add( const SizedBox(height: 10),
ChangeSecondSwitchStatusEvent( BodySmall(text: '${threeGangList[index].deviceName} spotlight'),
value: threeGangList[index].secondSwitch, const SizedBox(height: 5),
deviceId: threeGangList[index].deviceId)); DevicesDefaultSwitch(
}, switchValue: threeGangList[index].thirdSwitch,
), action: () {
const SizedBox(height: 10), BlocProvider.of<ThreeGangBloc>(context).add(ChangeThirdSwitchStatusEvent(
BodySmall(text: '${threeGangList[index].deviceName} spotlight'), value: threeGangList[index].thirdSwitch,
const SizedBox(height: 5), deviceId: threeGangList[index].deviceId));
DevicesDefaultSwitch( },
switchValue: threeGangList[index].thirdSwitch, ),
action: () { ],
BlocProvider.of<ThreeGangBloc>(context).add( );
ChangeThirdSwitchStatusEvent( },
value: threeGangList[index].thirdSwitch, ),
deviceId: threeGangList[index].deviceId)); ],
}, ),
), );
],
);
},
),
],
),
);
}, },
); );
} }