Refine USB transport flow

- replace Android USB dependency with app-owned USB host implementation\n- restore BLE-first scanner flow with USB secondary action\n- tighten Web Serial key handling and disconnect logging\n\nTODO (follow-up):\n- review non-English localization copy for tone and consistency\n- trim remaining unused/awkward localization strings introduced during USB UI changes
This commit is contained in:
just_stuff_tm
2026-03-02 22:48:19 -05:00
committed by just-stuff-tm
parent 74da9e82b5
commit 44c0670dae
45 changed files with 16316 additions and 15541 deletions
-232
View File
@@ -1,232 +0,0 @@
import 'dart:math' as math;
import 'package:flutter/material.dart';
import '../l10n/l10n.dart';
import '../utils/platform_info.dart';
import 'scanner_screen.dart';
import 'usb_screen.dart';
/// Entry point that lets the user choose between USB or Bluetooth.
class ConnectionChoiceScreen extends StatelessWidget {
const ConnectionChoiceScreen({super.key});
@override
Widget build(BuildContext context) {
final l10n = context.l10n;
final theme = Theme.of(context);
final usbSupported = PlatformInfo.supportsUsbSerial;
return Scaffold(
appBar: AppBar(
title: FittedBox(
fit: BoxFit.scaleDown,
child: Text(l10n.appTitle, textAlign: TextAlign.center),
),
centerTitle: true,
automaticallyImplyLeading: false,
),
body: SafeArea(
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 32),
child: LayoutBuilder(
builder: (context, constraints) {
final availableHeight = constraints.maxHeight.isFinite
? constraints.maxHeight
: 600.0;
final gap = math.max(
8.0,
math.min(20.0, availableHeight * 0.035),
);
return Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Flexible(
flex: 3,
child: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Flexible(
child: FittedBox(
fit: BoxFit.scaleDown,
child: Text(
l10n.connectionChoiceTitle,
textAlign: TextAlign.center,
style: theme.textTheme.headlineSmall?.copyWith(
fontWeight: FontWeight.w600,
),
),
),
),
SizedBox(height: math.max(4.0, gap * 0.5)),
Flexible(
child: FittedBox(
fit: BoxFit.scaleDown,
child: Text(
l10n.connectionChoiceSubtitle,
textAlign: TextAlign.center,
style: theme.textTheme.bodyMedium?.copyWith(
color: theme.colorScheme.onSurfaceVariant,
),
),
),
),
],
),
),
),
SizedBox(height: gap),
Expanded(
flex: 4,
child: _ConnectionMethodButton(
icon: Icons.usb,
label: l10n.connectionChoiceUsbLabel,
color: theme.colorScheme.primaryContainer,
iconColor: theme.colorScheme.onPrimaryContainer,
onPressed: usbSupported
? () {
debugPrint(
'ConnectionChoiceScreen: USB selected, opening UsbScreen',
);
Navigator.of(context).push(
MaterialPageRoute(
builder: (_) => const UsbScreen(),
),
);
}
: null,
),
),
SizedBox(height: gap),
Expanded(
flex: 4,
child: _ConnectionMethodButton(
icon: Icons.bluetooth,
label: l10n.connectionChoiceBluetoothLabel,
color: theme.colorScheme.surfaceContainerHighest,
iconColor: theme.colorScheme.onSurfaceVariant,
onPressed: () {
debugPrint(
'ConnectionChoiceScreen: Bluetooth selected, opening ScannerScreen',
);
Navigator.of(context).push(
MaterialPageRoute(
builder: (_) => const ScannerScreen(),
),
);
},
),
),
],
);
},
),
),
),
);
}
}
class _ConnectionMethodButton extends StatelessWidget {
const _ConnectionMethodButton({
required this.icon,
required this.label,
required this.onPressed,
required this.color,
required this.iconColor,
});
final IconData icon;
final String label;
final VoidCallback? onPressed;
final Color color;
final Color iconColor;
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
return ElevatedButton(
style: ElevatedButton.styleFrom(
backgroundColor: color,
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 8),
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(18)),
minimumSize: const Size.fromHeight(0),
),
onPressed: onPressed,
child: LayoutBuilder(
builder: (context, constraints) {
final availableHeight = constraints.maxHeight.isFinite
? constraints.maxHeight
: 200.0;
final availableWidth = constraints.maxWidth.isFinite
? constraints.maxWidth
: 320.0;
final isCompact = availableHeight < 72.0 || availableWidth < 180.0;
final useTightVertical = !isCompact && availableHeight < 120.0;
final baseGap = isCompact
? 8.0
: (useTightVertical
? math.max(4.0, math.min(8.0, availableHeight * 0.06))
: 12.0);
final labelStyle =
(isCompact
? theme.textTheme.titleMedium
: (useTightVertical
? theme.textTheme.titleMedium
: theme.textTheme.titleLarge))
?.copyWith(fontWeight: FontWeight.w600);
final verticalIconSize = useTightVertical
? math.max(32.0, math.min(48.0, availableHeight * 0.42))
: 60.0;
final content = isCompact
? Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(icon, size: 24.0, color: iconColor),
SizedBox(width: baseGap),
Flexible(
child: Text(
label,
textAlign: TextAlign.center,
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: labelStyle,
),
),
],
)
: Column(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(icon, size: verticalIconSize, color: iconColor),
SizedBox(height: baseGap),
Text(
label,
textAlign: TextAlign.center,
maxLines: 1,
overflow: TextOverflow.visible,
style: labelStyle,
),
],
);
return Center(
child: FittedBox(
fit: BoxFit.scaleDown,
child: ConstrainedBox(
constraints: BoxConstraints(
maxWidth: math.max(0, availableWidth - 12),
maxHeight: math.max(0, availableHeight - 12),
),
child: content,
),
),
);
},
),
);
}
}
+56 -28
View File
@@ -9,6 +9,7 @@ import '../l10n/l10n.dart';
import '../widgets/adaptive_app_bar_title.dart';
import '../widgets/device_tile.dart';
import 'contacts_screen.dart';
import 'usb_screen.dart';
/// Screen for scanning and connecting to MeshCore devices
class ScannerScreen extends StatefulWidget {
@@ -114,40 +115,67 @@ class _ScannerScreenState extends State<ScannerScreen> {
},
),
),
floatingActionButton: Consumer<MeshCoreConnector>(
bottomNavigationBar: Consumer<MeshCoreConnector>(
builder: (context, connector, child) {
final isScanning =
connector.state == MeshCoreConnectionState.scanning;
final isBluetoothOff = _bluetoothState == BluetoothAdapterState.off;
final usbSupported = PlatformInfo.supportsUsbSerial;
return FloatingActionButton.extended(
onPressed: isBluetoothOff
? null
: () {
if (isScanning) {
connector.stopScan();
} else {
unawaited(
connector.startScan().catchError((e) {
debugPrint("Scanner screen startScan error: $e");
}),
return SafeArea(
top: false,
minimum: const EdgeInsets.fromLTRB(16, 8, 16, 16),
child: Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
if (usbSupported)
FloatingActionButton.extended(
onPressed: () {
debugPrint(
'ScannerScreen: USB selected, opening UsbScreen',
);
}
},
icon: isScanning
? const SizedBox(
width: 20,
height: 20,
child: CircularProgressIndicator(
strokeWidth: 2,
color: Colors.white,
),
)
: const Icon(Icons.bluetooth_searching),
label: Text(
isScanning
? context.l10n.scanner_stop
: context.l10n.scanner_scan,
Navigator.of(context).push(
MaterialPageRoute(builder: (_) => const UsbScreen()),
);
},
heroTag: 'scanner_usb_action',
icon: const Icon(Icons.usb),
label: Text(context.l10n.connectionChoiceUsbLabel),
),
if (usbSupported) const SizedBox(width: 12),
FloatingActionButton.extended(
heroTag: 'scanner_ble_action',
onPressed: isBluetoothOff
? null
: () {
if (isScanning) {
connector.stopScan();
} else {
unawaited(
connector.startScan().catchError((e) {
debugPrint(
"Scanner screen startScan error: $e",
);
}),
);
}
},
icon: isScanning
? const SizedBox(
width: 20,
height: 20,
child: CircularProgressIndicator(
strokeWidth: 2,
),
)
: const Icon(Icons.bluetooth_searching),
label: Text(
isScanning
? context.l10n.scanner_stop
: context.l10n.scanner_scan,
),
),
],
),
);
},
+38 -16
View File
@@ -5,10 +5,12 @@ import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../connector/meshcore_connector.dart';
import '../connector/meshcore_connector_usb.dart';
import '../l10n/l10n.dart';
import '../utils/platform_info.dart';
import '../utils/usb_port_labels.dart';
import 'contacts_screen.dart';
import 'scanner_screen.dart';
class UsbScreen extends StatefulWidget {
const UsbScreen({super.key});
@@ -28,6 +30,7 @@ class _UsbScreenState extends State<UsbScreen> {
String? _errorText;
Timer? _hotPlugTimer;
late final MeshCoreConnector _connector;
late final MeshCoreConnectorUsb _usbConnector;
late final VoidCallback _connectionListener;
/// Whether the current platform supports dynamic hot-plug polling.
@@ -40,12 +43,13 @@ class _UsbScreenState extends State<UsbScreen> {
void initState() {
super.initState();
_connector = context.read<MeshCoreConnector>();
_usbConnector = MeshCoreConnectorUsb(_connector);
_connectionListener = () {
if (!mounted) return;
final activeUsbPortDisplayLabel = _connector.activeUsbPortDisplayLabel;
final activeUsbPortDisplayLabel = _usbConnector.activeUsbPortDisplayLabel;
final shouldUpdateDisplayLabel =
activeUsbPortDisplayLabel != _connectedPortDisplayLabel;
if (_connector.state == MeshCoreConnectionState.disconnected) {
if (_usbConnector.state == MeshCoreConnectionState.disconnected) {
_navigatedToContacts = false;
setState(() {
_isConnecting = false;
@@ -56,8 +60,8 @@ class _UsbScreenState extends State<UsbScreen> {
_connectedPortDisplayLabel = activeUsbPortDisplayLabel;
});
}
if (_connector.state == MeshCoreConnectionState.connected &&
_connector.isUsbTransportConnected &&
if (_usbConnector.state == MeshCoreConnectionState.connected &&
_usbConnector.isUsbTransportConnected &&
!_navigatedToContacts) {
_navigatedToContacts = true;
Navigator.of(context).pushReplacement(
@@ -65,14 +69,14 @@ class _UsbScreenState extends State<UsbScreen> {
);
}
};
_connector.addListener(_connectionListener);
_usbConnector.addListener(_connectionListener);
_startHotPlugTimer();
}
@override
void didChangeDependencies() {
super.didChangeDependencies();
_connector.setUsbRequestPortLabel(context.l10n.usbScreenStatus);
_usbConnector.setRequestPortLabel(context.l10n.usbScreenStatus);
if (!_didScheduleInitialLoad) {
_didScheduleInitialLoad = true;
unawaited(_loadPorts());
@@ -83,12 +87,12 @@ class _UsbScreenState extends State<UsbScreen> {
void dispose() {
_hotPlugTimer?.cancel();
_hotPlugTimer = null;
_connector.removeListener(_connectionListener);
_usbConnector.removeListener(_connectionListener);
if (!_navigatedToContacts &&
_connector.activeTransport == MeshCoreTransportType.usb &&
_connector.state != MeshCoreConnectionState.disconnected) {
_usbConnector.activeTransport == MeshCoreTransportType.usb &&
_usbConnector.state != MeshCoreConnectionState.disconnected) {
WidgetsBinding.instance.addPostFrameCallback((_) {
unawaited(_connector.disconnect(manual: true));
unawaited(_usbConnector.disconnect(manual: true));
});
}
super.dispose();
@@ -113,6 +117,23 @@ class _UsbScreenState extends State<UsbScreen> {
style: theme.textTheme.titleLarge,
),
centerTitle: true,
actions: [
if (PlatformInfo.isWeb ||
PlatformInfo.isAndroid ||
PlatformInfo.isIOS)
TextButton.icon(
onPressed: () {
debugPrint(
'UsbScreen: Bluetooth selected, opening ScannerScreen',
);
Navigator.of(context).pushReplacement(
MaterialPageRoute(builder: (_) => const ScannerScreen()),
);
},
icon: const Icon(Icons.bluetooth),
label: Text(l10n.connectionChoiceBluetoothLabel),
),
],
),
body: SafeArea(
child: LayoutBuilder(
@@ -376,7 +397,8 @@ class _UsbScreenState extends State<UsbScreen> {
final isSelected = port == _selectedPort;
final displayName = _friendlyPortName(port);
final rawName = normalizeUsbPortName(port);
final showRawName = rawName != displayName;
final showRawName =
rawName != displayName && !rawName.startsWith('web:');
return Material(
color: isSelected
? theme.colorScheme.primaryContainer
@@ -433,7 +455,7 @@ class _UsbScreenState extends State<UsbScreen> {
Future<void> _loadPorts() async {
if (!mounted) return;
_connector.setUsbRequestPortLabel(context.l10n.usbScreenStatus);
_usbConnector.setRequestPortLabel(context.l10n.usbScreenStatus);
setState(() {
_isLoadingPorts = true;
@@ -441,7 +463,7 @@ class _UsbScreenState extends State<UsbScreen> {
});
try {
final ports = await _connector.listUsbPorts();
final ports = await _usbConnector.listPorts();
if (!mounted) return;
setState(() {
_ports
@@ -470,8 +492,8 @@ class _UsbScreenState extends State<UsbScreen> {
if (selectedPort == null || selectedPort.isEmpty) {
return;
}
_connector.setUsbRequestPortLabel(context.l10n.usbScreenStatus);
if (_connector.state != MeshCoreConnectionState.disconnected) {
_usbConnector.setRequestPortLabel(context.l10n.usbScreenStatus);
if (_usbConnector.state != MeshCoreConnectionState.disconnected) {
setState(() {
_isConnecting = false;
_errorText = null;
@@ -486,7 +508,7 @@ class _UsbScreenState extends State<UsbScreen> {
});
try {
await _connector.connectUsb(portName: rawPortName);
await _usbConnector.connect(portName: rawPortName);
} catch (error, stackTrace) {
debugPrint(
'UsbScreen: connect failed for $rawPortName: $error\n$stackTrace',