Refactor USB screen, add debug logging, fix UI issues

- Rewrite UsbScreen to mirror ScannerScreen patterns (status bar,
  tap-to-connect port list, bottom FABs, SnackBar errors)
- Extract MeshCoreUsbManager from MeshCoreConnector for cleaner
  USB transport ownership
- Add debug logging throughout USB connection flow (connector,
  manager, web/native services)
- Print debug logs to console in debug mode even when app debug
  log setting is disabled
- Localize remaining hardcoded strings (Web Serial Device fallback
  label, USB status bar keys, companion firmware timeout hint)
- Fix Swedish misspelling in translations (stöderliga → stödda)
- Guard Linux notification init against missing D-Bus session bus
- Fix SNRIndicator hit-test error by adding minimum size constraints
- Update USB flow tests for new UI patterns
This commit is contained in:
zjs81
2026-03-07 12:38:28 -07:00
parent 8238b6197f
commit fef73b7b62
42 changed files with 981 additions and 553 deletions
+6 -1
View File
@@ -52,7 +52,12 @@ class AppDebugLogService extends ChangeNotifier {
String tag = 'App',
AppDebugLogLevel level = AppDebugLogLevel.info,
}) {
if (!_enabled) return;
if (!_enabled && !kDebugMode) return;
if (!_enabled) {
// In debug mode, still print to console but don't store entries.
debugPrint('[$tag] $message');
return;
}
_entries.add(
AppDebugLogEntry(
+21
View File
@@ -1,9 +1,11 @@
import 'dart:io' show Platform, File;
import 'dart:ui';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:flutter/foundation.dart';
import '../l10n/app_localizations.dart';
import '../utils/platform_info.dart';
class NotificationService {
static final NotificationService _instance = NotificationService._internal();
@@ -75,6 +77,15 @@ class NotificationService {
linux: linuxSettings,
);
// On Linux, the notifications plugin opens a D-Bus session bus
// connection whose async subscription can throw an unhandled
// SocketException when the bus socket is missing (e.g. running as
// root or inside a container without a session bus).
if (PlatformInfo.isLinux && !_isDbusSessionAvailable()) {
debugPrint('Skipping notification init: D-Bus session bus unavailable');
return;
}
try {
await _notifications.initialize(
settings: initSettings,
@@ -86,6 +97,16 @@ class NotificationService {
}
}
static bool _isDbusSessionAvailable() {
final addr = Platform.environment['DBUS_SESSION_BUS_ADDRESS'];
if (addr != null && addr.isNotEmpty) return true;
// Fallback: check the default socket for the current user.
final uid = Platform.environment['UID'] ??
Platform.environment['EUID'];
final path = '/run/user/${uid ?? '1000'}/bus';
return File(path).existsSync();
}
Future<bool> _ensureInitialized() async {
if (!_isInitialized) {
await initialize();
@@ -325,6 +325,10 @@ class UsbSerialService {
// Native implementations do not use a synthetic chooser row.
}
void setFallbackDeviceName(String label) {
// Native implementations use OS-provided device names.
}
void updateConnectedLabel(String label) {
final trimmed = label.trim();
if (trimmed.isEmpty) {
+26
View File
@@ -32,6 +32,7 @@ class UsbSerialService {
String? _connectedPortName;
String? _connectedPortKey;
String _requestPortLabel = 'Choose USB Device';
String _fallbackDeviceName = 'Web Serial Device';
AppDebugLogService? _debugLogService;
UsbSerialStatus get status => _status;
@@ -77,11 +78,19 @@ class UsbSerialService {
try {
final requestedPortName = normalizeUsbPortName(portName);
_debugLogService?.info(
'Web connect: requested=$requestedPortName baud=$baudRate',
tag: 'USB Serial',
);
final selectedPortKey = requestedPortName.startsWith('web:port:')
? requestedPortName
: null;
_port = _authorizedPortsByKey[requestedPortName];
final authorizedPorts = await _getAuthorizedPorts();
_debugLogService?.info(
'Web connect: ${authorizedPorts.length} authorized port(s), cached=${_port != null}',
tag: 'USB Serial',
);
_port ??= _selectPort(authorizedPorts, requestedPortName);
_port ??= await _requestPort();
@@ -89,6 +98,10 @@ class UsbSerialService {
throw StateError('No USB serial device selected');
}
_debugLogService?.info(
'Web connect: opening port at $baudRate baud…',
tag: 'USB Serial',
);
await _openPort(_port!, baudRate);
_connectedPortKey = _cachePort(_port!, preferredKey: selectedPortKey);
_connectedPortName = _displayLabelForPort(
@@ -105,6 +118,10 @@ class UsbSerialService {
tag: 'USB Serial',
);
} catch (error) {
_debugLogService?.error(
'Web connect failed: $error',
tag: 'USB Serial',
);
await _cleanupFailedConnect();
_status = UsbSerialStatus.disconnected;
_connectedPortName = null;
@@ -194,6 +211,14 @@ class UsbSerialService {
_requestPortLabel = trimmed;
}
void setFallbackDeviceName(String label) {
final trimmed = label.trim();
if (trimmed.isEmpty) {
return;
}
_fallbackDeviceName = trimmed;
}
void setDebugLogService(AppDebugLogService? service) {
_debugLogService = service;
}
@@ -403,6 +428,7 @@ class UsbSerialService {
vendorId: hasVendor ? vendorId : null,
productId: hasProduct ? productId : null,
requestPortLabel: _requestPortLabel,
fallbackDeviceName: _fallbackDeviceName,
knownUsbNames: _knownUsbNames,
);
}