settings_screen.dart 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter_riverpod/flutter_riverpod.dart';
  3. import '../services/bluetooth_service.dart';
  4. import 'pid_settings_screen.dart';
  5. import 'statistics_screen.dart';
  6. import 'pressure_settings_screen.dart';
  7. import 'timer_settings_screen.dart';
  8. import 'hardware_settings_screen.dart';
  9. class SettingsScreen extends ConsumerWidget {
  10. const SettingsScreen({super.key});
  11. @override
  12. Widget build(BuildContext context, WidgetRef ref) {
  13. final machineState = ref.watch(bluetoothServiceProvider);
  14. final bluetoothService = ref.read(bluetoothServiceProvider.notifier);
  15. return Scaffold(
  16. appBar: AppBar(title: const Text('Machine Settings')),
  17. body: ListView(
  18. padding: const EdgeInsets.all(16.0),
  19. children: [
  20. _SectionHeader(title: 'Extraction'),
  21. ListTile(
  22. title: const Text('Brew Temperature'),
  23. subtitle: Text(
  24. '${machineState.brewTemperature.toStringAsFixed(1)}°C',
  25. ),
  26. trailing: SizedBox(
  27. width: 200,
  28. child: _DraftSlider(
  29. value: machineState.brewTemperature,
  30. min: 50,
  31. max: 125,
  32. divisions: 750,
  33. format: (val) => '${val.toStringAsFixed(1)}°C',
  34. onSubmitted: (val) =>
  35. bluetoothService.updateBrewTemperature(val),
  36. ),
  37. ),
  38. ),
  39. ListTile(
  40. title: const Text('PID Tuning'),
  41. subtitle: const Text('Advanced temperature control parameters'),
  42. trailing: const Icon(Icons.chevron_right),
  43. onTap: () {
  44. Navigator.of(context).push(
  45. MaterialPageRoute(
  46. builder: (context) => const PIDSettingsScreen(),
  47. ),
  48. );
  49. },
  50. ),
  51. SwitchListTile(
  52. title: const Text('Continuous Mode'),
  53. subtitle: const Text(
  54. 'Keep boiler hot without waiting for a shot trigger',
  55. ),
  56. value: machineState.continuousMode,
  57. onChanged: (val) => bluetoothService.updateContinuousMode(val),
  58. ),
  59. const Divider(),
  60. _SectionHeader(title: 'Steam'),
  61. ListTile(
  62. title: const Text('Steam Temperature'),
  63. subtitle: Text(
  64. '${machineState.steamTemperature.toStringAsFixed(1)}°C',
  65. ),
  66. trailing: SizedBox(
  67. width: 200,
  68. child: _DraftSlider(
  69. value: machineState.steamTemperature,
  70. min: 110,
  71. max: 150,
  72. divisions: 80,
  73. format: (val) => '${val.toStringAsFixed(1)}°C',
  74. onSubmitted: (val) =>
  75. bluetoothService.updateSteamTemperature(val),
  76. ),
  77. ),
  78. ),
  79. _SectionHeader(title: 'Pressure'),
  80. ListTile(
  81. title: const Text('Pressure Profiling'),
  82. subtitle: const Text('Start pressure, end pressure and period'),
  83. trailing: const Icon(Icons.chevron_right),
  84. onTap: () {
  85. Navigator.of(context).push(
  86. MaterialPageRoute(
  87. builder: (context) => const PressureSettingsScreen(),
  88. ),
  89. );
  90. },
  91. ),
  92. const Divider(),
  93. _SectionHeader(title: 'Pre-infusion'),
  94. SwitchListTile(
  95. title: const Text('Enable Pre-infusion'),
  96. value: machineState.preinfusionEnabled,
  97. onChanged: (val) =>
  98. bluetoothService.updatePreinfusionSettings(enabled: val),
  99. ),
  100. ListTile(
  101. title: const Text('Pump Time'),
  102. subtitle: Text(
  103. '${machineState.preinfusionTime.toStringAsFixed(1)}s',
  104. ),
  105. enabled: machineState.preinfusionEnabled,
  106. trailing: SizedBox(
  107. width: 200,
  108. child: _DraftSlider(
  109. value: machineState.preinfusionTime,
  110. min: 0,
  111. max: 10,
  112. divisions: machineState.isLegacyV4Firmware ? 10 : 100,
  113. format: (val) =>
  114. '${val.toStringAsFixed(machineState.isLegacyV4Firmware ? 0 : 1)}s',
  115. onSubmitted: machineState.preinfusionEnabled
  116. ? (val) =>
  117. bluetoothService.updatePreinfusionSettings(time: val)
  118. : null,
  119. ),
  120. ),
  121. ),
  122. ListTile(
  123. title: const Text('Pause Time'),
  124. subtitle: Text(
  125. '${machineState.preinfusionPauseTime.toStringAsFixed(1)}s',
  126. ),
  127. enabled: machineState.preinfusionEnabled,
  128. trailing: SizedBox(
  129. width: 200,
  130. child: _DraftSlider(
  131. value: machineState.preinfusionPauseTime,
  132. min: 0,
  133. max: 10,
  134. divisions: machineState.isLegacyV4Firmware ? 10 : 100,
  135. format: (val) =>
  136. '${val.toStringAsFixed(machineState.isLegacyV4Firmware ? 0 : 1)}s',
  137. onSubmitted: machineState.preinfusionEnabled
  138. ? (val) =>
  139. bluetoothService.updatePreinfusionSettings(pause: val)
  140. : null,
  141. ),
  142. ),
  143. ),
  144. SwitchListTile(
  145. title: const Text('Close Valve While Paused'),
  146. subtitle: const Text(
  147. 'Prevents pressure venting during pre-infusion',
  148. ),
  149. value: machineState.valveClosedWhilePaused,
  150. onChanged: machineState.preinfusionEnabled
  151. ? (val) => bluetoothService.updatePreinfusionSettings(
  152. closeValve: val,
  153. )
  154. : null,
  155. ),
  156. if (!machineState.isLegacyV4Firmware)
  157. ListTile(
  158. title: const Text('Max Shot Time'),
  159. subtitle: Text(
  160. machineState.maxShotTime == 0
  161. ? 'Disabled'
  162. : '${machineState.maxShotTime} s',
  163. ),
  164. trailing: SizedBox(
  165. width: 200,
  166. child: _DraftSlider(
  167. value: machineState.maxShotTime.toDouble(),
  168. min: 0,
  169. max: 120,
  170. divisions: 120,
  171. format: (val) => val == 0 ? 'Off' : '${val.round()} s',
  172. onSubmitted: (val) =>
  173. bluetoothService.updateMaxShotTime(val.round()),
  174. ),
  175. ),
  176. ),
  177. const Divider(),
  178. _SectionHeader(title: 'Automation'),
  179. ListTile(
  180. title: const Text('Timers & Scheduling'),
  181. subtitle: const Text('Wakeup, shutdown, and auto-off'),
  182. trailing: const Icon(Icons.chevron_right),
  183. onTap: () {
  184. Navigator.of(context).push(
  185. MaterialPageRoute(
  186. builder: (context) => const TimerSettingsScreen(),
  187. ),
  188. );
  189. },
  190. ),
  191. const Divider(),
  192. _SectionHeader(title: 'Machine Information'),
  193. ListTile(
  194. title: const Text('Hardware Configuration'),
  195. subtitle: const Text('Output mapping and machine quirks'),
  196. trailing: const Icon(Icons.chevron_right),
  197. onTap: () {
  198. Navigator.of(context).push(
  199. MaterialPageRoute(
  200. builder: (context) => const HardwareSettingsScreen(),
  201. ),
  202. );
  203. },
  204. ),
  205. ListTile(
  206. title: const Text('Usage Statistics'),
  207. subtitle: const Text('Shot counts and grinder usage'),
  208. trailing: const Icon(Icons.chevron_right),
  209. onTap: () {
  210. Navigator.of(context).push(
  211. MaterialPageRoute(
  212. builder: (context) => const StatisticsScreen(),
  213. ),
  214. );
  215. },
  216. ),
  217. ListTile(
  218. title: const Text('Connection Status'),
  219. subtitle: Text(machineState.connectionStatus),
  220. ),
  221. ],
  222. ),
  223. );
  224. }
  225. }
  226. class _DraftSlider extends StatefulWidget {
  227. final double value;
  228. final double min;
  229. final double max;
  230. final int divisions;
  231. final String Function(double) format;
  232. final ValueChanged<double>? onSubmitted;
  233. const _DraftSlider({
  234. required this.value,
  235. required this.min,
  236. required this.max,
  237. required this.divisions,
  238. required this.format,
  239. required this.onSubmitted,
  240. });
  241. @override
  242. State<_DraftSlider> createState() => _DraftSliderState();
  243. }
  244. class _DraftSliderState extends State<_DraftSlider> {
  245. double? _draft;
  246. @override
  247. void didUpdateWidget(covariant _DraftSlider oldWidget) {
  248. super.didUpdateWidget(oldWidget);
  249. if (oldWidget.value != widget.value ||
  250. oldWidget.divisions != widget.divisions ||
  251. widget.onSubmitted == null) {
  252. _draft = null;
  253. }
  254. }
  255. @override
  256. Widget build(BuildContext context) {
  257. final value = (_draft ?? widget.value).clamp(widget.min, widget.max);
  258. return Slider(
  259. value: value,
  260. min: widget.min,
  261. max: widget.max,
  262. divisions: widget.divisions,
  263. label: widget.format(value),
  264. onChanged: widget.onSubmitted == null
  265. ? null
  266. : (value) => setState(() => _draft = value),
  267. onChangeEnd: widget.onSubmitted == null
  268. ? null
  269. : (value) {
  270. setState(() => _draft = null);
  271. final step = (widget.max - widget.min) / widget.divisions;
  272. final rounded =
  273. widget.min + ((value - widget.min) / step).round() * step;
  274. widget.onSubmitted!(double.parse(rounded.toStringAsFixed(3)));
  275. },
  276. );
  277. }
  278. }
  279. class _SectionHeader extends StatelessWidget {
  280. final String title;
  281. const _SectionHeader({required this.title});
  282. @override
  283. Widget build(BuildContext context) {
  284. return Padding(
  285. padding: const EdgeInsets.symmetric(vertical: 8.0),
  286. child: Text(
  287. title.toUpperCase(),
  288. style: TextStyle(
  289. color: Theme.of(context).colorScheme.primary,
  290. fontWeight: FontWeight.bold,
  291. fontSize: 12,
  292. ),
  293. ),
  294. );
  295. }
  296. }