diagnostics_screen.dart 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter/services.dart';
  3. import 'package:flutter_riverpod/flutter_riverpod.dart';
  4. import '../services/bluetooth_service.dart';
  5. class DiagnosticsScreen extends ConsumerWidget {
  6. const DiagnosticsScreen({super.key});
  7. @override
  8. Widget build(BuildContext context, WidgetRef ref) {
  9. final state = ref.watch(bluetoothServiceProvider);
  10. final service = ref.read(bluetoothServiceProvider.notifier);
  11. return Scaffold(
  12. appBar: AppBar(
  13. title: const Text('Diagnostics'),
  14. actions: [
  15. IconButton(
  16. tooltip: 'Copy log',
  17. icon: const Icon(Icons.copy),
  18. onPressed: state.diagnosticLog.isEmpty
  19. ? null
  20. : () async {
  21. await Clipboard.setData(
  22. ClipboardData(text: service.exportDiagnosticLog()),
  23. );
  24. if (context.mounted) {
  25. ScaffoldMessenger.of(context).showSnackBar(
  26. const SnackBar(content: Text('Diagnostic log copied')),
  27. );
  28. }
  29. },
  30. ),
  31. IconButton(
  32. tooltip: 'Clear log',
  33. icon: const Icon(Icons.delete_outline),
  34. onPressed: state.diagnosticLog.isEmpty
  35. ? null
  36. : service.clearDiagnosticLog,
  37. ),
  38. ],
  39. ),
  40. body: Column(
  41. crossAxisAlignment: CrossAxisAlignment.stretch,
  42. children: [
  43. Padding(
  44. padding: const EdgeInsets.all(16),
  45. child: Text(
  46. '${state.connectionStatus}'
  47. '${state.transportName.isEmpty ? '' : ' - ${state.transportName}'}'
  48. '\nFirmware: ${state.firmwareVersion.isEmpty ? 'Unknown' : state.firmwareVersion}'
  49. '\nTemperature data: ${state.telemetryStale ? 'stale / unavailable' : 'live'}'
  50. '\nLast sample: ${state.lastTelemetryAt?.toLocal().toIso8601String() ?? 'none'}'
  51. '\nLast 1000 RX/TX/events, held in memory only. Nothing is uploaded.',
  52. ),
  53. ),
  54. if (state.commandError != null)
  55. Padding(
  56. padding: const EdgeInsets.symmetric(horizontal: 16),
  57. child: Text(
  58. state.commandError!,
  59. style: const TextStyle(color: Colors.redAccent),
  60. ),
  61. ),
  62. const Divider(),
  63. Expanded(
  64. child: state.diagnosticLog.isEmpty
  65. ? const Center(child: Text('No diagnostic entries yet'))
  66. : ListView.builder(
  67. reverse: true,
  68. itemCount: state.diagnosticLog.length,
  69. itemBuilder: (context, index) {
  70. final entry =
  71. state.diagnosticLog[state.diagnosticLog.length -
  72. index -
  73. 1];
  74. return Padding(
  75. padding: const EdgeInsets.symmetric(
  76. horizontal: 16,
  77. vertical: 4,
  78. ),
  79. child: SelectableText(
  80. entry.text,
  81. style: const TextStyle(
  82. fontFamily: 'monospace',
  83. fontSize: 12,
  84. ),
  85. ),
  86. );
  87. },
  88. ),
  89. ),
  90. ],
  91. ),
  92. );
  93. }
  94. }