garage wizard

This commit is contained in:
mohammad
2024-10-07 10:29:42 +03:00
parent 208fbd63f3
commit 281414ac41
3 changed files with 22 additions and 24 deletions
@@ -418,7 +418,7 @@ class GarageDoorBloc extends Bloc<GarageDoorEvent, GarageDoorSensorState> {
bool oneGangGroup = false;
List<DeviceModel> devicesList = [];
List<GroupGarageModel> groupOneGangList = [];
List<GroupGarageModel> groupList = [];
bool allSwitchesOn = true;
void _fetchWizardStatus(
@@ -426,7 +426,7 @@ class GarageDoorBloc extends Bloc<GarageDoorEvent, GarageDoorSensorState> {
emit(LoadingInitialState());
try {
devicesList = [];
groupOneGangList = [];
groupList = [];
allSwitchesOn = true;
devicesList = await DevicesAPI.getDeviceByGroupName(
HomeCubit.getInstance().selectedSpace?.id ?? '', 'GD');
@@ -439,23 +439,22 @@ class GarageDoorBloc extends Bloc<GarageDoorEvent, GarageDoorSensorState> {
statusModelList.add(StatusModel.fromJson(status));
}
deviceStatus = GarageDoorModel.fromJson(statusModelList);
groupOneGangList.add(GroupGarageModel(
groupList.add(GroupGarageModel(
deviceId: devicesList[i].uuid ?? '',
deviceName: devicesList[i].name ?? '',
firstSwitch: deviceStatus.switch1,
));
}
if (groupOneGangList.isNotEmpty) {
groupOneGangList.firstWhere((element) {
if (groupList.isNotEmpty) {
groupList.firstWhere((element) {
if (!element.firstSwitch) {
allSwitchesOn = false;
}
return true;
});
}
emit(UpdateGroupState(
oneGangList: groupOneGangList, allSwitches: allSwitchesOn));
emit(UpdateGroupState(garageList: groupList, allSwitches: allSwitchesOn));
} catch (e) {
emit(GarageDoorFailedState(errorMessage: e.toString()));
@@ -468,16 +467,16 @@ class GarageDoorBloc extends Bloc<GarageDoorEvent, GarageDoorSensorState> {
emit(LoadingNewSate(doorSensor: deviceStatus));
try {
// Set all switches (firstSwitch and secondSwitch) based on the event value (on/off)
for (int i = 0; i < groupOneGangList.length; i++) {
groupOneGangList[i].firstSwitch = true;
for (int i = 0; i < groupList.length; i++) {
groupList[i].firstSwitch = true;
}
// Emit the state with updated values
emit(UpdateGroupState(oneGangList: groupOneGangList, allSwitches: true));
emit(UpdateGroupState(garageList: groupList, allSwitches: true));
// Get a list of all device IDs
List<String> allDeviceIds =
groupOneGangList.map((device) => device.deviceId).toList();
groupList.map((device) => device.deviceId).toList();
// First call for switch_1
final response = await DevicesAPI.deviceBatchController(
@@ -502,16 +501,16 @@ class GarageDoorBloc extends Bloc<GarageDoorEvent, GarageDoorSensorState> {
emit(LoadingNewSate(doorSensor: deviceStatus));
try {
// Set all switches (firstSwitch and secondSwitch) based on the event value (on/off)
for (int i = 0; i < groupOneGangList.length; i++) {
groupOneGangList[i].firstSwitch = false;
for (int i = 0; i < groupList.length; i++) {
groupList[i].firstSwitch = false;
}
// Emit the state with updated values
emit(UpdateGroupState(oneGangList: groupOneGangList, allSwitches: false));
emit(UpdateGroupState(garageList: groupList, allSwitches: false));
// Get a list of all device IDs
List<String> allDeviceIds =
groupOneGangList.map((device) => device.deviceId).toList();
groupList.map((device) => device.deviceId).toList();
// First call for switch_1
final response = await DevicesAPI.deviceBatchController(
@@ -537,7 +536,7 @@ class GarageDoorBloc extends Bloc<GarageDoorEvent, GarageDoorSensorState> {
emit(LoadingNewSate(doorSensor: deviceStatus));
try {
bool allSwitchesValue = true;
groupOneGangList.forEach((element) {
groupList.forEach((element) {
if (element.deviceId == event.deviceId) {
element.firstSwitch = !event.value;
}
@@ -547,7 +546,7 @@ class GarageDoorBloc extends Bloc<GarageDoorEvent, GarageDoorSensorState> {
});
emit(UpdateGroupState(
oneGangList: groupOneGangList, allSwitches: allSwitchesValue));
garageList: groupList, allSwitches: allSwitchesValue));
final response = await DevicesAPI.deviceBatchController(
code: 'switch_1',
@@ -83,12 +83,12 @@ class UpdateCreateScheduleState extends GarageDoorSensorState {
}
class UpdateGroupState extends GarageDoorSensorState {
final List<GroupGarageModel> oneGangList;
final List<GroupGarageModel> garageList;
final bool allSwitches;
const UpdateGroupState(
{required this.oneGangList, required this.allSwitches});
{required this.garageList, required this.allSwitches});
@override
List<Object> get props => [oneGangList, allSwitches];
List<Object> get props => [garageList, allSwitches];
}
@@ -21,15 +21,14 @@ class GarageWizard extends StatelessWidget {
return DefaultScaffold(
title: 'Garage Door Opener',
child: BlocProvider(
create: (context) =>
GarageDoorBloc( GDId: device?.uuid ?? '')
..add(InitialWizardEvent()),
create: (context) => GarageDoorBloc(GDId: device?.uuid ?? '')
..add(InitialWizardEvent()),
child: BlocBuilder<GarageDoorBloc, GarageDoorSensorState>(
builder: (context, state) {
bool allSwitchesOn = false;
if (state is UpdateGroupState) {
groupGarageModel = state.oneGangList;
groupGarageModel = state.garageList;
allSwitchesOn = state.allSwitches;
}
return state is LoadingInitialState