mirror of
https://github.com/SyncrowIOT/web.git
synced 2025-07-10 15:17:31 +00:00
push garage door delay
This commit is contained in:
@ -19,7 +19,7 @@ class GarageDoorBloc extends Bloc<GarageDoorEvent, GarageDoorState> {
|
|||||||
|
|
||||||
GarageDoorBloc({required this.deviceId}) : super(GarageDoorInitialState()) {
|
GarageDoorBloc({required this.deviceId}) : super(GarageDoorInitialState()) {
|
||||||
on<GarageDoorInitialEvent>(_fetchGarageDoorStatus);
|
on<GarageDoorInitialEvent>(_fetchGarageDoorStatus);
|
||||||
on<ToggleGarageDoorEvent>(_toggleGarageDoor);
|
on<GarageDoorControlEvent>(_garageDoorControlEvent);
|
||||||
on<AddGarageDoorScheduleEvent>(_addSchedule);
|
on<AddGarageDoorScheduleEvent>(_addSchedule);
|
||||||
on<UpdateGarageDoorScheduleEvent>(_updateSchedule);
|
on<UpdateGarageDoorScheduleEvent>(_updateSchedule);
|
||||||
on<DeleteGarageDoorScheduleEvent>(_deleteSchedule);
|
on<DeleteGarageDoorScheduleEvent>(_deleteSchedule);
|
||||||
@ -45,23 +45,6 @@ class GarageDoorBloc extends Bloc<GarageDoorEvent, GarageDoorState> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void _toggleGarageDoor(ToggleGarageDoorEvent event, Emitter<GarageDoorState> emit) async {
|
|
||||||
final oldValue = deviceStatus.switch1;
|
|
||||||
_updateLocalValue('switch_1', event.isOpen);
|
|
||||||
emit(GarageDoorLoadedState(status: deviceStatus));
|
|
||||||
final success = await _runDeBouncer(
|
|
||||||
deviceId: event.deviceId,
|
|
||||||
code: 'switch_1',
|
|
||||||
value: event.isOpen,
|
|
||||||
oldValue: oldValue,
|
|
||||||
emit: emit,
|
|
||||||
isBatch: false,
|
|
||||||
);
|
|
||||||
if (!success) {
|
|
||||||
_revertValue('switch_1', oldValue, emit);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<void> _addSchedule(AddGarageDoorScheduleEvent event, Emitter<GarageDoorState> emit) async {
|
Future<void> _addSchedule(AddGarageDoorScheduleEvent event, Emitter<GarageDoorState> emit) async {
|
||||||
try {
|
try {
|
||||||
ScheduleEntry newSchedule = ScheduleEntry(
|
ScheduleEntry newSchedule = ScheduleEntry(
|
||||||
@ -146,26 +129,6 @@ class GarageDoorBloc extends Bloc<GarageDoorEvent, GarageDoorState> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void _increaseDelay(IncreaseGarageDoorDelayEvent event, Emitter<GarageDoorState> emit) async {
|
|
||||||
try {
|
|
||||||
deviceStatus = deviceStatus.copyWith(delay: deviceStatus.delay + Duration(minutes: 10));
|
|
||||||
emit(GarageDoorLoadedState(status: deviceStatus));
|
|
||||||
} catch (e) {
|
|
||||||
emit(GarageDoorErrorState(message: e.toString()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void _decreaseDelay(DecreaseGarageDoorDelayEvent event, Emitter<GarageDoorState> emit) async {
|
|
||||||
try {
|
|
||||||
if (deviceStatus.delay.inMinutes > 10) {
|
|
||||||
deviceStatus = deviceStatus.copyWith(delay: deviceStatus.delay - Duration(minutes: 10));
|
|
||||||
}
|
|
||||||
emit(GarageDoorLoadedState(status: deviceStatus));
|
|
||||||
} catch (e) {
|
|
||||||
emit(GarageDoorErrorState(message: e.toString()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<void> _updateSelectedTime(UpdateSelectedTimeEvent event, Emitter<GarageDoorState> emit) async {
|
Future<void> _updateSelectedTime(UpdateSelectedTimeEvent event, Emitter<GarageDoorState> emit) async {
|
||||||
final currentState = state;
|
final currentState = state;
|
||||||
if (currentState is GarageDoorLoadedState) {
|
if (currentState is GarageDoorLoadedState) {
|
||||||
@ -244,6 +207,49 @@ class GarageDoorBloc extends Bloc<GarageDoorEvent, GarageDoorState> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void _increaseDelay(IncreaseGarageDoorDelayEvent event, Emitter<GarageDoorState> emit) async {
|
||||||
|
// if (deviceStatus.countdown1 != 0) {
|
||||||
|
try {
|
||||||
|
deviceStatus = deviceStatus.copyWith(delay: deviceStatus.delay + Duration(minutes: 10));
|
||||||
|
emit(GarageDoorLoadedState(status: deviceStatus));
|
||||||
|
add(GarageDoorControlEvent(deviceId: event.deviceId, value: deviceStatus.delay.inSeconds, code: 'countdown_1'));
|
||||||
|
} catch (e) {
|
||||||
|
emit(GarageDoorErrorState(message: e.toString()));
|
||||||
|
}
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
|
||||||
|
void _decreaseDelay(DecreaseGarageDoorDelayEvent event, Emitter<GarageDoorState> emit) async {
|
||||||
|
// if (deviceStatus.countdown1 != 0) {
|
||||||
|
try {
|
||||||
|
if (deviceStatus.delay.inMinutes > 10) {
|
||||||
|
deviceStatus = deviceStatus.copyWith(delay: deviceStatus.delay - Duration(minutes: 10));
|
||||||
|
}
|
||||||
|
emit(GarageDoorLoadedState(status: deviceStatus));
|
||||||
|
add(GarageDoorControlEvent(deviceId: event.deviceId, value: deviceStatus.delay.inSeconds, code: 'countdown_1'));
|
||||||
|
} catch (e) {
|
||||||
|
emit(GarageDoorErrorState(message: e.toString()));
|
||||||
|
}
|
||||||
|
//}
|
||||||
|
}
|
||||||
|
|
||||||
|
void _garageDoorControlEvent(GarageDoorControlEvent event, Emitter<GarageDoorState> emit) async {
|
||||||
|
final oldValue = event.code == 'countdown_1' ? deviceStatus.countdown1 : deviceStatus.switch1;
|
||||||
|
_updateLocalValue(event.code, event.value);
|
||||||
|
emit(GarageDoorLoadedState(status: deviceStatus));
|
||||||
|
final success = await _runDeBouncer(
|
||||||
|
deviceId: event.deviceId,
|
||||||
|
code: event.code,
|
||||||
|
value: event.value,
|
||||||
|
oldValue: oldValue,
|
||||||
|
emit: emit,
|
||||||
|
isBatch: false,
|
||||||
|
);
|
||||||
|
if (!success) {
|
||||||
|
_revertValue(event.code, oldValue, emit);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void _revertValue(String code, dynamic oldValue, Emitter<GarageDoorState> emit) {
|
void _revertValue(String code, dynamic oldValue, Emitter<GarageDoorState> emit) {
|
||||||
switch (code) {
|
switch (code) {
|
||||||
case 'switch_1':
|
case 'switch_1':
|
||||||
@ -251,6 +257,11 @@ class GarageDoorBloc extends Bloc<GarageDoorEvent, GarageDoorState> {
|
|||||||
deviceStatus = deviceStatus.copyWith(switch1: oldValue);
|
deviceStatus = deviceStatus.copyWith(switch1: oldValue);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
case 'countdown_1':
|
||||||
|
if (oldValue is int) {
|
||||||
|
deviceStatus = deviceStatus.copyWith(countdown1: oldValue, delay: Duration(seconds: oldValue));
|
||||||
|
}
|
||||||
|
break;
|
||||||
// Add other cases if needed
|
// Add other cases if needed
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
@ -268,6 +279,11 @@ class GarageDoorBloc extends Bloc<GarageDoorEvent, GarageDoorState> {
|
|||||||
deviceStatus = deviceStatus.copyWith(switch1: value);
|
deviceStatus = deviceStatus.copyWith(switch1: value);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
case 'countdown_1':
|
||||||
|
if (value is int) {
|
||||||
|
deviceStatus = deviceStatus.copyWith(countdown1: value, delay: Duration(seconds: value));
|
||||||
|
}
|
||||||
|
break;
|
||||||
// Add other cases if needed
|
// Add other cases if needed
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
|
@ -19,15 +19,15 @@ class GarageDoorInitialEvent extends GarageDoorEvent {
|
|||||||
List<Object?> get props => [deviceId];
|
List<Object?> get props => [deviceId];
|
||||||
}
|
}
|
||||||
|
|
||||||
class ToggleGarageDoorEvent extends GarageDoorEvent {
|
class GarageDoorControlEvent extends GarageDoorEvent {
|
||||||
final String deviceId;
|
final String deviceId;
|
||||||
final bool isOpen;
|
final dynamic value;
|
||||||
final String code;
|
final String code;
|
||||||
|
|
||||||
const ToggleGarageDoorEvent({required this.deviceId, required this.isOpen, required this.code});
|
const GarageDoorControlEvent({required this.deviceId, required this.value, required this.code});
|
||||||
|
|
||||||
@override
|
@override
|
||||||
List<Object?> get props => [deviceId, isOpen];
|
List<Object?> get props => [deviceId, value];
|
||||||
}
|
}
|
||||||
|
|
||||||
class AddGarageDoorScheduleEvent extends GarageDoorEvent {
|
class AddGarageDoorScheduleEvent extends GarageDoorEvent {
|
||||||
|
@ -64,7 +64,7 @@ class GarageDoorControlView extends StatelessWidget with HelperResponsiveLayout
|
|||||||
icon: status.switch1 ? Assets.openedDoor : Assets.closedDoor,
|
icon: status.switch1 ? Assets.openedDoor : Assets.closedDoor,
|
||||||
onTap: () {
|
onTap: () {
|
||||||
context.read<GarageDoorBloc>().add(
|
context.read<GarageDoorBloc>().add(
|
||||||
ToggleGarageDoorEvent(deviceId: status.uuid, isOpen: !status.switch1, code: 'switch_1'),
|
GarageDoorControlEvent(deviceId: status.uuid, value: !status.switch1, code: 'switch_1'),
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
status: status.switch1,
|
status: status.switch1,
|
||||||
@ -94,20 +94,23 @@ class GarageDoorControlView extends StatelessWidget with HelperResponsiveLayout
|
|||||||
label: '',
|
label: '',
|
||||||
labelWidget: Row(
|
labelWidget: Row(
|
||||||
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
|
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
|
||||||
crossAxisAlignment: CrossAxisAlignment.end,
|
crossAxisAlignment: CrossAxisAlignment.center,
|
||||||
children: [
|
children: [
|
||||||
IconButton(
|
IconButton(
|
||||||
onPressed: () {
|
onPressed: () {
|
||||||
context.read<GarageDoorBloc>().add(DecreaseGarageDoorDelayEvent(deviceId: deviceId));
|
// if (status.countdown1 != 0) {
|
||||||
|
context.read<GarageDoorBloc>().add(DecreaseGarageDoorDelayEvent(deviceId: status.uuid));
|
||||||
|
// }
|
||||||
},
|
},
|
||||||
icon: const Icon(
|
icon: const Icon(
|
||||||
Icons.remove,
|
Icons.remove,
|
||||||
size: 28,
|
size: 28,
|
||||||
color: ColorsManager.greyColor,
|
color: ColorsManager.greyColor,
|
||||||
),
|
),
|
||||||
|
padding: EdgeInsets.zero,
|
||||||
),
|
),
|
||||||
Text(
|
Text(
|
||||||
'${status.delay.inHours.toString().padLeft(2, '0')}',
|
status.delay.inHours.toString().padLeft(2, '0'),
|
||||||
style: context.textTheme.titleLarge!.copyWith(
|
style: context.textTheme.titleLarge!.copyWith(
|
||||||
color: ColorsManager.dialogBlueTitle,
|
color: ColorsManager.dialogBlueTitle,
|
||||||
fontWeight: FontWeight.bold,
|
fontWeight: FontWeight.bold,
|
||||||
@ -118,7 +121,7 @@ class GarageDoorControlView extends StatelessWidget with HelperResponsiveLayout
|
|||||||
style: context.textTheme.bodySmall!.copyWith(color: ColorsManager.blackColor),
|
style: context.textTheme.bodySmall!.copyWith(color: ColorsManager.blackColor),
|
||||||
),
|
),
|
||||||
Text(
|
Text(
|
||||||
'${(status.delay.inMinutes % 60).toString().padLeft(2, '0')}',
|
(status.delay.inMinutes % 60).toString().padLeft(2, '0'),
|
||||||
style: context.textTheme.titleLarge!.copyWith(
|
style: context.textTheme.titleLarge!.copyWith(
|
||||||
color: ColorsManager.dialogBlueTitle,
|
color: ColorsManager.dialogBlueTitle,
|
||||||
fontWeight: FontWeight.bold,
|
fontWeight: FontWeight.bold,
|
||||||
@ -130,21 +133,29 @@ class GarageDoorControlView extends StatelessWidget with HelperResponsiveLayout
|
|||||||
),
|
),
|
||||||
IconButton(
|
IconButton(
|
||||||
onPressed: () {
|
onPressed: () {
|
||||||
context.read<GarageDoorBloc>().add(IncreaseGarageDoorDelayEvent(deviceId: deviceId));
|
// if (status.countdown1 != 0) {
|
||||||
|
context.read<GarageDoorBloc>().add(IncreaseGarageDoorDelayEvent(deviceId: status.uuid));
|
||||||
|
// }
|
||||||
},
|
},
|
||||||
icon: const Icon(
|
icon: const Icon(
|
||||||
Icons.add,
|
Icons.add,
|
||||||
size: 28,
|
size: 28,
|
||||||
color: ColorsManager.greyColor,
|
color: ColorsManager.greyColor,
|
||||||
),
|
),
|
||||||
|
padding: EdgeInsets.zero,
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
value: false,
|
value: status.countdown1 != 0 ? true : false,
|
||||||
code: 'switch_1',
|
code: 'countdown_1',
|
||||||
deviceId: status.uuid,
|
deviceId: status.uuid,
|
||||||
icon: Assets.doorDelay,
|
icon: Assets.doorDelay,
|
||||||
onChange: (value) {},
|
onChange: (value) {
|
||||||
|
context.read<GarageDoorBloc>().add(
|
||||||
|
GarageDoorControlEvent(
|
||||||
|
deviceId: status.uuid, value: value ? status.delay.inSeconds : 0, code: 'countdown_1'),
|
||||||
|
);
|
||||||
|
},
|
||||||
),
|
),
|
||||||
IconNameStatusContainer(
|
IconNameStatusContainer(
|
||||||
isFullIcon: false,
|
isFullIcon: false,
|
||||||
|
Reference in New Issue
Block a user