settings_screen.dart 10 KB

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