import 'package:flutter/material.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'services/bluetooth_service.dart'; import 'widgets/temperature_chart.dart'; import 'screens/settings_screen.dart'; import 'screens/connection_screen.dart'; import 'screens/diagnostics_screen.dart'; void main() { runApp(const ProviderScope(child: MyApp())); } class MyApp extends StatelessWidget { const MyApp({super.key}); @override Widget build(BuildContext context) { return MaterialApp( title: 'meBarista', theme: ThemeData( brightness: Brightness.dark, colorScheme: ColorScheme.fromSeed( seedColor: Colors.brown, brightness: Brightness.dark, ), useMaterial3: true, ), home: const MyHomePage(title: 'meBarista Dashboard'), ); } } class MyHomePage extends ConsumerWidget { const MyHomePage({super.key, required this.title}); final String title; @override Widget build(BuildContext context, WidgetRef ref) { final machineState = ref.watch(bluetoothServiceProvider); final bluetoothService = ref.read(bluetoothServiceProvider.notifier); void connect() { if (bluetoothService.supportsClassic) { Navigator.of( context, ).push(MaterialPageRoute(builder: (_) => const ConnectionScreen())); } else { bluetoothService.startScan(); } } ref.listen(bluetoothServiceProvider, (previous, next) { if (next.commandError != null && next.commandError != previous?.commandError) { ScaffoldMessenger.of( context, ).showSnackBar(SnackBar(content: Text(next.commandError!))); } }); return Scaffold( appBar: AppBar( title: Text(title), actions: [ IconButton( tooltip: 'Diagnostics', icon: const Icon(Icons.receipt_long), onPressed: () { Navigator.of(context).push( MaterialPageRoute(builder: (_) => const DiagnosticsScreen()), ); }, ), IconButton( icon: const Icon(Icons.settings), onPressed: () { Navigator.of(context).push( MaterialPageRoute(builder: (context) => const SettingsScreen()), ); }, ), IconButton( onPressed: machineState.firmwareUpdating ? null : connect, icon: Icon( machineState.connectionStatus == 'Connected' ? Icons.bluetooth_connected : machineState.connectionStatus.startsWith('Error') ? Icons.bluetooth_disabled : Icons.bluetooth_searching, color: machineState.connectionStatus == 'Connected' ? Colors.blue : machineState.connectionStatus.startsWith('Error') ? Colors.redAccent : null, ), ), ], ), body: SafeArea( top: false, child: SingleChildScrollView( padding: const EdgeInsets.all(16.0), child: Column( crossAxisAlignment: CrossAxisAlignment.stretch, children: [ if (machineState.connectionStatus == 'Connected' && machineState.telemetryStale) Card( color: Colors.amber.withValues(alpha: 0.15), child: Padding( padding: const EdgeInsets.all(12), child: Text( machineState.lastTelemetryAt == null ? 'Waiting for temperature telemetry. Readings are unavailable.' : 'Telemetry stale: no temperature sample for 3 seconds. ' 'Displayed readings are the last received values.', style: const TextStyle(color: Colors.amber), ), ), ), if (machineState.firmwareUpdating) Card( child: Padding( padding: const EdgeInsets.all(16), child: Column( children: [ Text(machineState.firmwareUpdateStatus), const SizedBox(height: 8), LinearProgressIndicator( value: machineState.firmwareProgress, ), const Text( 'Keep controller power and Bluetooth connected.', ), ], ), ), ), // Temperature Display Card Card( child: Padding( padding: const EdgeInsets.all(20.0), child: Column( children: [ Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ const Text( 'Boiler Temperature', style: TextStyle(fontSize: 18), ), Icon( Icons.coffee_maker, size: 32, color: machineState.connectionStatus == 'Connected' && !machineState.telemetryStale ? (machineState.isHeating ? Colors.redAccent : Colors.blueAccent) : Colors.grey, ), ], ), const SizedBox(height: 10), Text( '${machineState.temperature.toStringAsFixed(1)}°C', style: const TextStyle( fontSize: 64, fontWeight: FontWeight.bold, color: Colors.orangeAccent, ), ), // Second sensor reading (shown only when present) if (machineState.sensor2Temperature > 0) Padding( padding: const EdgeInsets.only(top: 4.0), child: Row( mainAxisAlignment: MainAxisAlignment.center, children: [ const Icon( Icons.thermostat, size: 14, color: Colors.greenAccent, ), const SizedBox(width: 4), Text( 'Sensor 2: ${machineState.sensor2Temperature.toStringAsFixed(1)}°C', style: const TextStyle( fontSize: 13, color: Colors.greenAccent, ), ), ], ), ), const SizedBox(height: 10), // PID Power Indicator Row( children: [ const Icon( Icons.bolt, size: 16, color: Colors.yellowAccent, ), Expanded( child: Padding( padding: const EdgeInsets.symmetric( horizontal: 8.0, ), child: LinearProgressIndicator( value: machineState.telemetryStale ? 0 : machineState.isSteaming ? null : machineState.heatingPower, backgroundColor: Colors.grey.withValues( alpha: 0.2, ), color: Colors.redAccent, minHeight: 8, borderRadius: BorderRadius.circular(4), ), ), ), Text( machineState.telemetryStale ? 'Unavailable' : machineState.isSteaming ? 'Steam' : '${(machineState.heatingPower * 100).toStringAsFixed(0)}%', style: const TextStyle( fontSize: 12, color: Colors.grey, ), ), ], ), const SizedBox(height: 16), // Target Control Row( mainAxisAlignment: MainAxisAlignment.center, children: [ IconButton( onPressed: () => bluetoothService.decrementSetpoint(), icon: const Icon( Icons.remove_circle_outline, color: Colors.grey, ), ), Column( children: [ const Text( 'BREW TARGET', style: TextStyle( fontSize: 10, color: Colors.grey, ), ), Text( '${machineState.brewTemperature.toStringAsFixed(1)}°C', style: const TextStyle( fontSize: 18, fontWeight: FontWeight.bold, ), ), ], ), IconButton( onPressed: () => bluetoothService.incrementSetpoint(), icon: const Icon( Icons.add_circle_outline, color: Colors.grey, ), ), ], ), ], ), ), ), const SizedBox(height: 16), // Shot Timer (Visible when brewing or after a shot) if (machineState.isBrewing || machineState.shotTimer > 0) Card( color: machineState.isBrewing && !machineState.telemetryStale ? Colors.green.withValues(alpha: 0.2) : null, child: Padding( padding: const EdgeInsets.all(16.0), child: Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ const Text( 'Shot Timer', style: TextStyle( fontSize: 18, fontWeight: FontWeight.bold, ), ), Text( '${machineState.shotTimer}s', style: TextStyle( fontSize: 32, fontWeight: FontWeight.bold, color: machineState.isBrewing && !machineState.telemetryStale ? Colors.greenAccent : Colors.grey, ), ), ], ), ), ), const SizedBox(height: 16), // Status Indicators Row( children: [ Expanded( child: _StatusCard( label: 'Heating', isActive: machineState.isHeating && !machineState.telemetryStale, activeColor: Colors.redAccent, icon: Icons.hot_tub, ), ), const SizedBox(width: 16), Expanded( child: _StatusCard( label: 'Brewing', isActive: machineState.isBrewing && !machineState.telemetryStale, activeColor: Colors.greenAccent, icon: Icons.coffee, ), ), ], ), const SizedBox(height: 16), // Temperature Chart Card( child: Padding( padding: const EdgeInsets.all(16.0), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Row( children: [ const Text( 'Temperature Stability', style: TextStyle(fontSize: 16), ), const Spacer(), // Legend const _LegendDot( color: Colors.redAccent, label: 'Boiler', ), if (machineState .sensor2TemperatureHistory .isNotEmpty) ...[ const SizedBox(width: 12), const _LegendDot( color: Colors.greenAccent, label: 'Sensor 2', ), ], const SizedBox(width: 12), _LegendDot( color: Colors.blue.withValues(alpha: 0.5), label: 'Target', ), ], ), const SizedBox(height: 20), SizedBox( height: 320, child: TemperatureChart( history: machineState.temperatureHistory, sensor2History: machineState.sensor2TemperatureHistory, targetTemperature: machineState.targetTemperature, samples: machineState.temperatureSamples, ), ), ], ), ), ), const SizedBox(height: 20), if (machineState.connectionStatus != 'Connected') ElevatedButton.icon( onPressed: machineState.connectionStatus == 'Scanning...' ? null : machineState.firmwareUpdating ? null : connect, icon: Icon( machineState.connectionStatus.startsWith('Error') ? Icons.bluetooth_disabled : Icons.search, ), style: machineState.connectionStatus.startsWith('Error') ? ElevatedButton.styleFrom( backgroundColor: Colors.redAccent.withValues( alpha: 0.2, ), ) : null, label: Text(switch (machineState.connectionStatus) { 'Scanning...' => 'Scanning…', 'Reconnecting...' => 'Reconnecting in 30s… (tap to retry now)', 'Not found' => 'Not found — tap to retry', String s when s.startsWith('Error') => '${machineState.connectionStatus} — tap to retry', _ => 'Connect to meCoffee', }), ) else OutlinedButton( onPressed: machineState.firmwareUpdating ? null : () => bluetoothService.disconnect(), child: const Text('Disconnect'), ), ], ), ), ), ); } } class _StatusCard extends StatelessWidget { final String label; final bool isActive; final Color activeColor; final IconData icon; const _StatusCard({ required this.label, required this.isActive, required this.activeColor, required this.icon, }); @override Widget build(BuildContext context) { return Card( color: isActive ? activeColor.withValues(alpha: 0.2) : null, child: Padding( padding: const EdgeInsets.symmetric(vertical: 16.0), child: Column( children: [ Icon(icon, color: isActive ? activeColor : Colors.grey), const SizedBox(height: 8), Text( label, style: TextStyle( color: isActive ? activeColor : Colors.grey, fontWeight: isActive ? FontWeight.bold : FontWeight.normal, ), ), ], ), ), ); } } class _LegendDot extends StatelessWidget { final Color color; final String label; const _LegendDot({required this.color, required this.label}); @override Widget build(BuildContext context) { return Row( mainAxisSize: MainAxisSize.min, children: [ Container( width: 10, height: 10, decoration: BoxDecoration(color: color, shape: BoxShape.circle), ), const SizedBox(width: 4), Text(label, style: const TextStyle(fontSize: 11, color: Colors.grey)), ], ); } }