timer_settings_screen.dart 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. static String _autoShutoffLabel(int minutes) =>
  13. minutes == 0 ? 'Off' : '$minutes minutes';
  14. @override
  15. Widget build(BuildContext context) {
  16. final machineState = ref.watch(bluetoothServiceProvider);
  17. final bluetoothService = ref.read(bluetoothServiceProvider.notifier);
  18. ref.listen(
  19. bluetoothServiceProvider.select(
  20. (state) => (state.autoShutoffMinutes, state.autoShutoffEnabled),
  21. ),
  22. (_, _) => setState(() => _autoShutoffDraft = null),
  23. );
  24. return Scaffold(
  25. appBar: AppBar(title: const Text('Timers & Automation')),
  26. body: ListView(
  27. padding: const EdgeInsets.all(16),
  28. children: [
  29. if (!machineState.isLegacyV4Firmware) ...[
  30. _SectionHeader(title: 'Auto-Shutoff'),
  31. ListTile(
  32. title: const Text('Inactivity Timer'),
  33. subtitle: Text(
  34. machineState.autoShutoffEnabled
  35. ? '${machineState.autoShutoffMinutes} minutes'
  36. : 'Off',
  37. ),
  38. trailing: SizedBox(
  39. width: 150,
  40. child: Slider(
  41. value:
  42. (_autoShutoffDraft ??
  43. (machineState.autoShutoffEnabled
  44. ? machineState.autoShutoffMinutes.toDouble()
  45. : 0.0))
  46. .clamp(0.0, 120.0),
  47. min: 0,
  48. max: 120,
  49. divisions: 120,
  50. label: _autoShutoffLabel(
  51. (_autoShutoffDraft ??
  52. (machineState.autoShutoffEnabled
  53. ? machineState.autoShutoffMinutes
  54. : 0))
  55. .round(),
  56. ),
  57. onChanged: (v) => setState(() => _autoShutoffDraft = v),
  58. onChangeEnd: (v) {
  59. setState(() => _autoShutoffDraft = null);
  60. bluetoothService.updateAutoShutoff(v.round());
  61. },
  62. ),
  63. ),
  64. ),
  65. const Divider(),
  66. ],
  67. _SectionHeader(title: 'Daily Schedule'),
  68. const Padding(
  69. padding: EdgeInsets.only(bottom: 8),
  70. child: Text(
  71. 'The controller clock restarts when the machine loses power. '
  72. 'Wake-up and shutdown times only run after this app has '
  73. 'connected once since then.',
  74. ),
  75. ),
  76. SwitchListTile(
  77. title: const Text('Enable Wakeup Timer'),
  78. value: machineState.wakeupEnabled,
  79. onChanged: (v) => bluetoothService.updateSchedule(wEnabled: v),
  80. ),
  81. ListTile(
  82. title: const Text('Wakeup Time'),
  83. subtitle: Text(machineState.wakeupTime),
  84. enabled: machineState.wakeupEnabled,
  85. onTap: () async {
  86. final time = await showTimePicker(
  87. context: context,
  88. initialTime: TimeOfDay(
  89. hour: int.parse(machineState.wakeupTime.split(':')[0]),
  90. minute: int.parse(machineState.wakeupTime.split(':')[1]),
  91. ),
  92. );
  93. if (time != null) {
  94. final formattedTime =
  95. '${time.hour.toString().padLeft(2, '0')}:${time.minute.toString().padLeft(2, '0')}';
  96. bluetoothService.updateSchedule(wTime: formattedTime);
  97. }
  98. },
  99. ),
  100. const SizedBox(height: 16),
  101. SwitchListTile(
  102. title: const Text('Enable Shutdown Timer'),
  103. value: machineState.shutdownEnabled,
  104. onChanged: (v) => bluetoothService.updateSchedule(sEnabled: v),
  105. ),
  106. ListTile(
  107. title: const Text('Shutdown Time'),
  108. subtitle: Text(machineState.shutdownTime),
  109. enabled: machineState.shutdownEnabled,
  110. onTap: () async {
  111. final time = await showTimePicker(
  112. context: context,
  113. initialTime: TimeOfDay(
  114. hour: int.parse(machineState.shutdownTime.split(':')[0]),
  115. minute: int.parse(machineState.shutdownTime.split(':')[1]),
  116. ),
  117. );
  118. if (time != null) {
  119. final formattedTime =
  120. '${time.hour.toString().padLeft(2, '0')}:${time.minute.toString().padLeft(2, '0')}';
  121. bluetoothService.updateSchedule(sTime: formattedTime);
  122. }
  123. },
  124. ),
  125. ],
  126. ),
  127. );
  128. }
  129. }
  130. class _SectionHeader extends StatelessWidget {
  131. final String title;
  132. const _SectionHeader({required this.title});
  133. @override
  134. Widget build(BuildContext context) {
  135. return Padding(
  136. padding: const EdgeInsets.symmetric(vertical: 8.0),
  137. child: Text(
  138. title.toUpperCase(),
  139. style: TextStyle(
  140. color: Theme.of(context).colorScheme.primary,
  141. fontWeight: FontWeight.bold,
  142. fontSize: 12,
  143. ),
  144. ),
  145. );
  146. }
  147. }