import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import '../services/bluetooth_service.dart'; class DiagnosticsScreen extends ConsumerWidget { const DiagnosticsScreen({super.key}); @override Widget build(BuildContext context, WidgetRef ref) { final state = ref.watch(bluetoothServiceProvider); final service = ref.read(bluetoothServiceProvider.notifier); return Scaffold( appBar: AppBar( title: const Text('Diagnostics'), actions: [ IconButton( tooltip: 'Copy log', icon: const Icon(Icons.copy), onPressed: state.diagnosticLog.isEmpty ? null : () async { await Clipboard.setData( ClipboardData(text: service.exportDiagnosticLog()), ); if (context.mounted) { ScaffoldMessenger.of(context).showSnackBar( const SnackBar(content: Text('Diagnostic log copied')), ); } }, ), IconButton( tooltip: 'Clear log', icon: const Icon(Icons.delete_outline), onPressed: state.diagnosticLog.isEmpty ? null : service.clearDiagnosticLog, ), ], ), body: Column( crossAxisAlignment: CrossAxisAlignment.stretch, children: [ Padding( padding: const EdgeInsets.all(16), child: Text( '${state.connectionStatus}' '${state.transportName.isEmpty ? '' : ' - ${state.transportName}'}' '\nFirmware: ${state.firmwareVersion.isEmpty ? 'Unknown' : state.firmwareVersion}' '\nTemperature data: ${state.telemetryStale ? 'stale / unavailable' : 'live'}' '\nLast sample: ${state.lastTelemetryAt?.toLocal().toIso8601String() ?? 'none'}' '\nLast 1000 RX/TX/events, held in memory only. Nothing is uploaded.', ), ), if (state.commandError != null) Padding( padding: const EdgeInsets.symmetric(horizontal: 16), child: Text( state.commandError!, style: const TextStyle(color: Colors.redAccent), ), ), const Divider(), Expanded( child: state.diagnosticLog.isEmpty ? const Center(child: Text('No diagnostic entries yet')) : ListView.builder( reverse: true, itemCount: state.diagnosticLog.length, itemBuilder: (context, index) { final entry = state.diagnosticLog[state.diagnosticLog.length - index - 1]; return Padding( padding: const EdgeInsets.symmetric( horizontal: 16, vertical: 4, ), child: SelectableText( entry.text, style: const TextStyle( fontFamily: 'monospace', fontSize: 12, ), ), ); }, ), ), ], ), ); } }