| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309 |
- import 'package:flutter/material.dart';
- import 'package:flutter_riverpod/flutter_riverpod.dart';
- import '../services/bluetooth_service.dart';
- import '../services/me_coffee_protocol.dart';
- import 'pid_settings_screen.dart';
- import 'statistics_screen.dart';
- import 'pressure_settings_screen.dart';
- import 'timer_settings_screen.dart';
- import 'hardware_settings_screen.dart';
- class SettingsScreen extends ConsumerWidget {
- const SettingsScreen({super.key});
- @override
- Widget build(BuildContext context, WidgetRef ref) {
- final machineState = ref.watch(bluetoothServiceProvider);
- final bluetoothService = ref.read(bluetoothServiceProvider.notifier);
- return Scaffold(
- appBar: AppBar(title: const Text('Machine Settings')),
- body: ListView(
- padding: const EdgeInsets.all(16.0),
- children: [
- _SectionHeader(title: 'Extraction'),
- ListTile(
- title: const Text('Brew Temperature'),
- subtitle: Text(
- '${machineState.brewTemperature.toStringAsFixed(1)}°C',
- ),
- trailing: SizedBox(
- width: 200,
- child: _DraftSlider(
- value: machineState.brewTemperature,
- min: 50,
- max: 125,
- divisions: 750,
- format: (val) => '${val.toStringAsFixed(1)}°C',
- onSubmitted: (val) =>
- bluetoothService.updateBrewTemperature(val),
- ),
- ),
- ),
- ListTile(
- title: const Text('PID Tuning'),
- subtitle: const Text('Advanced temperature control parameters'),
- trailing: const Icon(Icons.chevron_right),
- onTap: () {
- Navigator.of(context).push(
- MaterialPageRoute(
- builder: (context) => const PIDSettingsScreen(),
- ),
- );
- },
- ),
- SwitchListTile(
- title: const Text('Continuous Mode'),
- subtitle: const Text(
- 'Keep boiler hot without waiting for a shot trigger',
- ),
- value: machineState.continuousMode,
- onChanged: (val) => bluetoothService.updateContinuousMode(val),
- ),
- const Divider(),
- _SectionHeader(title: 'Steam'),
- ListTile(
- title: const Text('Steam Temperature'),
- subtitle: Text(
- '${machineState.steamTemperature.toStringAsFixed(1)}°C',
- ),
- trailing: SizedBox(
- width: 200,
- child: _DraftSlider(
- value: machineState.steamTemperature,
- min: 110,
- max: 150,
- divisions: 80,
- format: (val) => '${val.toStringAsFixed(1)}°C',
- onSubmitted: (val) =>
- bluetoothService.updateSteamTemperature(val),
- ),
- ),
- ),
- _SectionHeader(title: 'Pressure'),
- ListTile(
- title: const Text('Pressure Profiling'),
- subtitle: const Text('Start pressure, end pressure and period'),
- trailing: const Icon(Icons.chevron_right),
- onTap: () {
- Navigator.of(context).push(
- MaterialPageRoute(
- builder: (context) => const PressureSettingsScreen(),
- ),
- );
- },
- ),
- const Divider(),
- _SectionHeader(title: 'Pre-infusion'),
- SwitchListTile(
- title: const Text('Enable Pre-infusion'),
- value: machineState.preinfusionEnabled,
- onChanged: (val) =>
- bluetoothService.updatePreinfusionSettings(enabled: val),
- ),
- ListTile(
- title: const Text('Pump Time'),
- subtitle: Text(
- '${machineState.preinfusionTime.toStringAsFixed(1)}s',
- ),
- enabled: machineState.preinfusionEnabled,
- trailing: SizedBox(
- width: 200,
- child: _DraftSlider(
- value: machineState.preinfusionTime,
- min: 0,
- max: 10,
- divisions: machineState.isLegacyV4Firmware ? 10 : 100,
- format: (val) =>
- '${val.toStringAsFixed(machineState.isLegacyV4Firmware ? 0 : 1)}s',
- onSubmitted: machineState.preinfusionEnabled
- ? (val) =>
- bluetoothService.updatePreinfusionSettings(time: val)
- : null,
- ),
- ),
- ),
- ListTile(
- title: const Text('Pause Time'),
- subtitle: Text(
- '${machineState.preinfusionPauseTime.toStringAsFixed(1)}s',
- ),
- enabled: machineState.preinfusionEnabled,
- trailing: SizedBox(
- width: 200,
- child: _DraftSlider(
- value: machineState.preinfusionPauseTime,
- min: 0,
- max: 10,
- divisions: machineState.isLegacyV4Firmware ? 10 : 100,
- format: (val) =>
- '${val.toStringAsFixed(machineState.isLegacyV4Firmware ? 0 : 1)}s',
- onSubmitted: machineState.preinfusionEnabled
- ? (val) =>
- bluetoothService.updatePreinfusionSettings(pause: val)
- : null,
- ),
- ),
- ),
- SwitchListTile(
- title: const Text('Close Valve While Paused'),
- subtitle: const Text(
- 'Prevents pressure venting during pre-infusion',
- ),
- value: machineState.valveClosedWhilePaused,
- onChanged: machineState.preinfusionEnabled
- ? (val) => bluetoothService.updatePreinfusionSettings(
- closeValve: val,
- )
- : null,
- ),
- if (!machineState.isLegacyV4Firmware)
- ListTile(
- title: const Text('Max Shot Time'),
- subtitle: Text(
- machineState.maxShotTime == 0
- ? 'Disabled'
- : '${machineState.maxShotTime} s',
- ),
- trailing: SizedBox(
- width: 200,
- child: _DraftSlider(
- value: machineState.maxShotTime.toDouble(),
- min: 0,
- max: MeCoffeeProtocol.maxShotSeconds.toDouble(),
- divisions: MeCoffeeProtocol.maxShotSeconds,
- format: (val) => val == 0 ? 'Off' : '${val.round()} s',
- onSubmitted: (val) =>
- bluetoothService.updateMaxShotTime(val.round()),
- ),
- ),
- ),
- const Divider(),
- _SectionHeader(title: 'Automation'),
- ListTile(
- title: const Text('Timers & Scheduling'),
- subtitle: const Text('Wakeup, shutdown, and auto-off'),
- trailing: const Icon(Icons.chevron_right),
- onTap: () {
- Navigator.of(context).push(
- MaterialPageRoute(
- builder: (context) => const TimerSettingsScreen(),
- ),
- );
- },
- ),
- const Divider(),
- _SectionHeader(title: 'Machine Information'),
- ListTile(
- title: const Text('Hardware Configuration'),
- subtitle: const Text('Output mapping and machine quirks'),
- trailing: const Icon(Icons.chevron_right),
- onTap: () {
- Navigator.of(context).push(
- MaterialPageRoute(
- builder: (context) => const HardwareSettingsScreen(),
- ),
- );
- },
- ),
- ListTile(
- title: const Text('Usage Statistics'),
- subtitle: const Text('Shot counts and grinder usage'),
- trailing: const Icon(Icons.chevron_right),
- onTap: () {
- Navigator.of(context).push(
- MaterialPageRoute(
- builder: (context) => const StatisticsScreen(),
- ),
- );
- },
- ),
- ListTile(
- title: const Text('Connection Status'),
- subtitle: Text(machineState.connectionStatus),
- ),
- ],
- ),
- );
- }
- }
- class _DraftSlider extends StatefulWidget {
- final double value;
- final double min;
- final double max;
- final int divisions;
- final String Function(double) format;
- final ValueChanged<double>? onSubmitted;
- const _DraftSlider({
- required this.value,
- required this.min,
- required this.max,
- required this.divisions,
- required this.format,
- required this.onSubmitted,
- });
- @override
- State<_DraftSlider> createState() => _DraftSliderState();
- }
- class _DraftSliderState extends State<_DraftSlider> {
- double? _draft;
- @override
- void didUpdateWidget(covariant _DraftSlider oldWidget) {
- super.didUpdateWidget(oldWidget);
- if (oldWidget.value != widget.value ||
- oldWidget.divisions != widget.divisions ||
- widget.onSubmitted == null) {
- _draft = null;
- }
- }
- @override
- Widget build(BuildContext context) {
- final value = (_draft ?? widget.value).clamp(widget.min, widget.max);
- return Slider(
- value: value,
- min: widget.min,
- max: widget.max,
- divisions: widget.divisions,
- label: widget.format(value),
- onChanged: widget.onSubmitted == null
- ? null
- : (value) => setState(() => _draft = value),
- onChangeEnd: widget.onSubmitted == null
- ? null
- : (value) {
- setState(() => _draft = null);
- final step = (widget.max - widget.min) / widget.divisions;
- final rounded =
- widget.min + ((value - widget.min) / step).round() * step;
- widget.onSubmitted!(double.parse(rounded.toStringAsFixed(3)));
- },
- );
- }
- }
- class _SectionHeader extends StatelessWidget {
- final String title;
- const _SectionHeader({required this.title});
- @override
- Widget build(BuildContext context) {
- return Padding(
- padding: const EdgeInsets.symmetric(vertical: 8.0),
- child: Text(
- title.toUpperCase(),
- style: TextStyle(
- color: Theme.of(context).colorScheme.primary,
- fontWeight: FontWeight.bold,
- fontSize: 12,
- ),
- ),
- );
- }
- }
|