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; @override Widget build(BuildContext context) { final machineState = ref.watch(bluetoothServiceProvider); final bluetoothService = ref.read(bluetoothServiceProvider.notifier); ref.listen( bluetoothServiceProvider.select((state) => state.autoShutoffMinutes), (_, _) => 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.autoShutoffMinutes} minutes'), trailing: SizedBox( width: 150, child: Slider( value: (_autoShutoffDraft ?? machineState.autoShutoffMinutes.toDouble()) .clamp(0.0, 120.0), min: 0, max: 120, divisions: 120, label: '${(_autoShutoffDraft ?? machineState.autoShutoffMinutes).round()} minutes', onChanged: (v) => setState(() => _autoShutoffDraft = v), onChangeEnd: (v) { setState(() => _autoShutoffDraft = null); bluetoothService.updateAutoShutoff(v.round()); }, ), ), ), const Divider(), ], _SectionHeader(title: 'Daily Schedule'), 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, ), ), ); } }