timer_settings_screen.dart 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter_riverpod/flutter_riverpod.dart';
  3. import '../services/bluetooth_service.dart';
  4. class TimerSettingsScreen extends ConsumerStatefulWidget {
  5. const TimerSettingsScreen({super.key});
  6. @override
  7. ConsumerState<TimerSettingsScreen> createState() =>
  8. _TimerSettingsScreenState();
  9. }
  10. class _TimerSettingsScreenState extends ConsumerState<TimerSettingsScreen> {
  11. double? _autoShutoffDraft;
  12. @override
  13. Widget build(BuildContext context) {
  14. final machineState = ref.watch(bluetoothServiceProvider);
  15. final bluetoothService = ref.read(bluetoothServiceProvider.notifier);
  16. ref.listen(
  17. bluetoothServiceProvider.select((state) => state.autoShutoffMinutes),
  18. (_, _) => setState(() => _autoShutoffDraft = null),
  19. );
  20. return Scaffold(
  21. appBar: AppBar(title: const Text('Timers & Automation')),
  22. body: ListView(
  23. padding: const EdgeInsets.all(16),
  24. children: [
  25. if (!machineState.isLegacyV4Firmware) ...[
  26. _SectionHeader(title: 'Auto-Shutoff'),
  27. ListTile(
  28. title: const Text('Inactivity Timer'),
  29. subtitle: Text('${machineState.autoShutoffMinutes} minutes'),
  30. trailing: SizedBox(
  31. width: 150,
  32. child: Slider(
  33. value:
  34. (_autoShutoffDraft ??
  35. machineState.autoShutoffMinutes.toDouble())
  36. .clamp(0.0, 120.0),
  37. min: 0,
  38. max: 120,
  39. divisions: 120,
  40. label:
  41. '${(_autoShutoffDraft ?? machineState.autoShutoffMinutes).round()} minutes',
  42. onChanged: (v) => setState(() => _autoShutoffDraft = v),
  43. onChangeEnd: (v) {
  44. setState(() => _autoShutoffDraft = null);
  45. bluetoothService.updateAutoShutoff(v.round());
  46. },
  47. ),
  48. ),
  49. ),
  50. const Divider(),
  51. ],
  52. _SectionHeader(title: 'Daily Schedule'),
  53. SwitchListTile(
  54. title: const Text('Enable Wakeup Timer'),
  55. value: machineState.wakeupEnabled,
  56. onChanged: (v) => bluetoothService.updateSchedule(wEnabled: v),
  57. ),
  58. ListTile(
  59. title: const Text('Wakeup Time'),
  60. subtitle: Text(machineState.wakeupTime),
  61. enabled: machineState.wakeupEnabled,
  62. onTap: () async {
  63. final time = await showTimePicker(
  64. context: context,
  65. initialTime: TimeOfDay(
  66. hour: int.parse(machineState.wakeupTime.split(':')[0]),
  67. minute: int.parse(machineState.wakeupTime.split(':')[1]),
  68. ),
  69. );
  70. if (time != null) {
  71. final formattedTime =
  72. '${time.hour.toString().padLeft(2, '0')}:${time.minute.toString().padLeft(2, '0')}';
  73. bluetoothService.updateSchedule(wTime: formattedTime);
  74. }
  75. },
  76. ),
  77. const SizedBox(height: 16),
  78. SwitchListTile(
  79. title: const Text('Enable Shutdown Timer'),
  80. value: machineState.shutdownEnabled,
  81. onChanged: (v) => bluetoothService.updateSchedule(sEnabled: v),
  82. ),
  83. ListTile(
  84. title: const Text('Shutdown Time'),
  85. subtitle: Text(machineState.shutdownTime),
  86. enabled: machineState.shutdownEnabled,
  87. onTap: () async {
  88. final time = await showTimePicker(
  89. context: context,
  90. initialTime: TimeOfDay(
  91. hour: int.parse(machineState.shutdownTime.split(':')[0]),
  92. minute: int.parse(machineState.shutdownTime.split(':')[1]),
  93. ),
  94. );
  95. if (time != null) {
  96. final formattedTime =
  97. '${time.hour.toString().padLeft(2, '0')}:${time.minute.toString().padLeft(2, '0')}';
  98. bluetoothService.updateSchedule(sTime: formattedTime);
  99. }
  100. },
  101. ),
  102. ],
  103. ),
  104. );
  105. }
  106. }
  107. class _SectionHeader extends StatelessWidget {
  108. final String title;
  109. const _SectionHeader({required this.title});
  110. @override
  111. Widget build(BuildContext context) {
  112. return Padding(
  113. padding: const EdgeInsets.symmetric(vertical: 8.0),
  114. child: Text(
  115. title.toUpperCase(),
  116. style: TextStyle(
  117. color: Theme.of(context).colorScheme.primary,
  118. fontWeight: FontWeight.bold,
  119. fontSize: 12,
  120. ),
  121. ),
  122. );
  123. }
  124. }