import 'package:flutter/material.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import '../services/bluetooth_service.dart'; class TimerSettingsScreen extends ConsumerStatefulWidget { const TimerSettingsScreen({super.key}); @override ConsumerState createState() => _TimerSettingsScreenState(); } class _TimerSettingsScreenState extends ConsumerState { double? _autoShutoffDraft; static String _autoShutoffLabel(int minutes) => minutes == 0 ? 'Off' : '$minutes minutes'; @override Widget build(BuildContext context) { final machineState = ref.watch(bluetoothServiceProvider); final bluetoothService = ref.read(bluetoothServiceProvider.notifier); ref.listen( bluetoothServiceProvider.select( (state) => (state.autoShutoffMinutes, state.autoShutoffEnabled), ), (_, _) => setState(() => _autoShutoffDraft = null), ); return Scaffold( appBar: AppBar(title: const Text('Timers & Automation')), body: ListView( padding: const EdgeInsets.all(16), children: [ if (!machineState.isLegacyV4Firmware) ...[ _SectionHeader(title: 'Auto-Shutoff'), ListTile( title: const Text('Inactivity Timer'), subtitle: Text( machineState.autoShutoffEnabled ? '${machineState.autoShutoffMinutes} minutes' : 'Off', ), trailing: SizedBox( width: 150, child: Slider( value: (_autoShutoffDraft ?? (machineState.autoShutoffEnabled ? machineState.autoShutoffMinutes.toDouble() : 0.0)) .clamp(0.0, 120.0), min: 0, max: 120, divisions: 120, label: _autoShutoffLabel( (_autoShutoffDraft ?? (machineState.autoShutoffEnabled ? machineState.autoShutoffMinutes : 0)) .round(), ), onChanged: (v) => setState(() => _autoShutoffDraft = v), onChangeEnd: (v) { setState(() => _autoShutoffDraft = null); bluetoothService.updateAutoShutoff(v.round()); }, ), ), ), const Divider(), ], _SectionHeader(title: 'Daily Schedule'), const Padding( padding: EdgeInsets.only(bottom: 8), child: Text( 'The controller clock restarts when the machine loses power. ' 'Wake-up and shutdown times only run after this app has ' 'connected once since then.', ), ), SwitchListTile( title: const Text('Enable Wakeup Timer'), value: machineState.wakeupEnabled, onChanged: (v) => bluetoothService.updateSchedule(wEnabled: v), ), ListTile( title: const Text('Wakeup Time'), subtitle: Text(machineState.wakeupTime), enabled: machineState.wakeupEnabled, onTap: () async { final time = await showTimePicker( context: context, initialTime: TimeOfDay( hour: int.parse(machineState.wakeupTime.split(':')[0]), minute: int.parse(machineState.wakeupTime.split(':')[1]), ), ); if (time != null) { final formattedTime = '${time.hour.toString().padLeft(2, '0')}:${time.minute.toString().padLeft(2, '0')}'; bluetoothService.updateSchedule(wTime: formattedTime); } }, ), const SizedBox(height: 16), SwitchListTile( title: const Text('Enable Shutdown Timer'), value: machineState.shutdownEnabled, onChanged: (v) => bluetoothService.updateSchedule(sEnabled: v), ), ListTile( title: const Text('Shutdown Time'), subtitle: Text(machineState.shutdownTime), enabled: machineState.shutdownEnabled, onTap: () async { final time = await showTimePicker( context: context, initialTime: TimeOfDay( hour: int.parse(machineState.shutdownTime.split(':')[0]), minute: int.parse(machineState.shutdownTime.split(':')[1]), ), ); if (time != null) { final formattedTime = '${time.hour.toString().padLeft(2, '0')}:${time.minute.toString().padLeft(2, '0')}'; bluetoothService.updateSchedule(sTime: formattedTime); } }, ), ], ), ); } } 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, ), ), ); } }