garage Alarm

This commit is contained in:
mohammad
2024-10-08 13:06:32 +03:00
parent e5af727144
commit f2172bd6e8
6 changed files with 289 additions and 33 deletions
@@ -18,7 +18,8 @@ class PreferencesPage extends StatelessWidget {
return DefaultScaffold(
title: 'Preferences',
child: BlocProvider(
create: (context) => GarageDoorBloc(GDId: GDId)..add(const GarageDoorInitial()),
create: (context) =>
GarageDoorBloc(GDId: GDId)..add(const GarageDoorInitial()),
child: BlocBuilder<GarageDoorBloc, GarageDoorSensorState>(
builder: (context, state) {
final garageDoorBloc = BlocProvider.of<GarageDoorBloc>(context);
@@ -26,7 +27,9 @@ class PreferencesPage extends StatelessWidget {
return state is GarageDoorLoadingState
? const Center(
child: DefaultContainer(
width: 50, height: 50, child: CircularProgressIndicator()),
width: 50,
height: 50,
child: CircularProgressIndicator()),
)
: Column(
children: [
@@ -49,14 +52,42 @@ class PreferencesPage extends StatelessWidget {
height: 50,
child: ListTile(
contentPadding: EdgeInsets.zero,
leading: const BodyMedium(
text: 'Alarm when door is open',
fontWeight: FontWeight.normal,
leading: InkWell(
onTap: () {
showDialog(
context: context,
builder: (context) {
return TimeoutDialog(
duration: Duration(
seconds: garageDoorBloc
.deviceStatus
.countdownAlarm),
title: 'Timeout Alarm',
cancelTab: () {
Navigator.of(context).pop();
},
confirmTab: (selectedSecond) {
garageDoorBloc.add(
SetTimeOutValue(
deviceCode:
'countdown_alarm',
duration:
selectedSecond));
Navigator.of(context).pop();
},
);
});
},
child: const BodyMedium(
text: 'Alarm when door is open',
fontWeight: FontWeight.normal,
),
),
trailing: Container(
width: 100,
child: Row(
mainAxisAlignment: MainAxisAlignment.end,
mainAxisAlignment:
MainAxisAlignment.end,
children: [
Container(
height: 30,
@@ -64,18 +95,25 @@ class PreferencesPage extends StatelessWidget {
color: ColorsManager.graysColor,
),
Transform.scale(
scale: .8,
child: CupertinoSwitch(
value: garageDoorBloc.lowBattery,
onChanged: (value) {
// context
// .read<GarageDoorBloc>()
// .add(
// ToggleLowBatteryEvent(
// value));
},
applyTheme: true,
)),
scale: .8,
child: CupertinoSwitch(
value: garageDoorBloc.deviceStatus
.doorState1 ==
'unclosed_time',
onChanged: (value) {
context
.read<GarageDoorBloc>()
.add(
ToggleAlarmEvent(
value
? 'unclosed_time'
: 'close_time_alarm',
),
);
},
applyTheme: true,
),
),
],
),
)),
@@ -103,12 +141,15 @@ class PreferencesPage extends StatelessWidget {
builder: (context) {
return SecondDialog(
label2: 'Close',
initialSelectedLabel: garageDoorBloc.secondSelected.toString(),
initialSelectedLabel: garageDoorBloc
.secondSelected
.toString(),
cancelTab: () {
Navigator.of(context).pop();
},
confirmTab: (v) {
garageDoorBloc.add(SelectSecondsEvent(seconds: v));
garageDoorBloc
.add(SelectSecondsEvent(seconds: v));
Navigator.of(context).pop();
},
title: 'Control',
@@ -129,8 +170,10 @@ class PreferencesPage extends StatelessWidget {
height: 90,
width: 120,
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment:
CrossAxisAlignment.center,
mainAxisAlignment:
MainAxisAlignment.spaceBetween,
children: [
BodyMedium(
fontColor: ColorsManager.textGray,
@@ -156,6 +199,7 @@ class PreferencesPage extends StatelessWidget {
));
}
}
//Timeout
class SecondDialog extends StatefulWidget {
final String label1;
@@ -183,15 +227,12 @@ class SecondDialog extends StatefulWidget {
}
class _SecondDialogState extends State<SecondDialog> {
// late String _selectedOption;
late int selectedSecond;
@override
void initState() {
super.initState();
// Parse the initialSelectedLabel as an integer. Default to 10 if invalid or not provided.
selectedSecond = int.tryParse(widget.initialSelectedLabel ?? '10') ?? 10;
// _selectedOption = widget.initialSelectedLabel ?? '';
}
@override
@@ -229,7 +270,6 @@ class _SecondDialogState extends State<SecondDialog> {
child: CupertinoPicker(
itemExtent: 40.0,
scrollController: FixedExtentScrollController(
// Set the initial position based on selectedSecond
initialItem: selectedSecond - 10,
),
onSelectedItemChanged: (int index) {
@@ -242,7 +282,9 @@ class _SecondDialogState extends State<SecondDialog> {
child: BodyLarge(
text: (index + 10).toString().padLeft(2, '0'),
style: const TextStyle(
fontWeight: FontWeight.w400, fontSize: 30, color: Colors.blue),
fontWeight: FontWeight.w400,
fontSize: 30,
color: Colors.blue),
),
);
}),
@@ -326,3 +368,153 @@ class _SecondDialogState extends State<SecondDialog> {
);
}
}
class TimeoutDialog extends StatefulWidget {
final String title;
final Function(String)? onTapLabel1;
final Function()? cancelTab;
final Function(Duration timeSelected)? confirmTab;
final Duration? duration;
TimeoutDialog({
required this.title,
this.onTapLabel1,
required this.cancelTab,
required this.confirmTab,
this.duration,
});
@override
_TimeoutDialogState createState() => _TimeoutDialogState();
}
class _TimeoutDialogState extends State<TimeoutDialog> {
late int selectedSecond;
Duration duration = Duration.zero;
int countNum = 0;
@override
void initState() {
super.initState();
duration = widget.duration!;
}
@override
Widget build(BuildContext context) {
return AlertDialog(
contentPadding: EdgeInsets.zero,
content: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
const SizedBox(
height: 10,
),
BodyLarge(
text: widget.title,
fontWeight: FontWeight.w700,
fontColor: ColorsManager.primaryColor,
fontSize: 16,
),
const Padding(
padding: EdgeInsets.only(left: 15, right: 15),
child: Divider(
color: ColorsManager.textGray,
),
),
Padding(
padding: const EdgeInsets.only(left: 15, right: 15, bottom: 10),
child: Column(
children: [
Center(
child: Container(
height: 150,
width: MediaQuery.of(context).size.width * 1,
child: CupertinoTimerPicker(
initialTimerDuration: duration,
mode: CupertinoTimerPickerMode.hm,
onTimerDurationChanged: (Duration newDuration) {
duration = newDuration;
},
),
),
),
],
),
),
Row(
children: [
Expanded(
child: Container(
decoration: const BoxDecoration(
border: Border(
right: BorderSide(
color: ColorsManager.textGray,
width: 0.5,
),
top: BorderSide(
color: ColorsManager.textGray,
width: 1.0,
),
)),
child: SizedBox(
child: InkWell(
onTap: widget.cancelTab,
child: const Padding(
padding: EdgeInsets.all(15),
child: Center(
child: Text(
'Cancel',
style: TextStyle(
color: ColorsManager.textGray,
fontSize: 14,
fontWeight: FontWeight.w400),
),
),
),
),
),
),
),
Expanded(
child: Container(
decoration: const BoxDecoration(
border: Border(
left: BorderSide(
color: ColorsManager.textGray,
width: 0.5,
),
top: BorderSide(
color: ColorsManager.textGray,
width: 1.0,
),
)),
child: InkWell(
onTap: () {
widget.confirmTab?.call(duration);
},
child: const Padding(
padding: EdgeInsets.all(15),
child: Center(
child: Text(
'Confirm',
style: TextStyle(
color: ColorsManager.primaryColor,
fontSize: 14,
fontWeight: FontWeight.w400),
),
),
)),
))
],
)
],
),
);
}
String _formatDuration(int seconds) {
final hours = (seconds ~/ 3600).toString().padLeft(2, '0');
final minutes = ((seconds % 3600) ~/ 60).toString().padLeft(2, '0');
final secs = (seconds % 60).toString().padLeft(2, '0');
return '$hours:$minutes:$secs';
}
}