Scan official MeshCore channel QR codes

The QR scanner only read community codes, so the standard
meshcore://channel/add?name=...&secret=... links used by the official
app and sites like ipnt.uk were rejected as invalid community codes.

The scanner now also accepts channel links. It skips channels already on
the radio, asks before adding, and only uses a free slot after the
channel sync has finished. Also fills in the 59 untranslated strings in
all 17 locales.

Fixes #604
This commit is contained in:
zjs81 committed 2026-10-24 22:26:09 -07:00
1 parent b6c67b113c
commit b1e968e7bf
42 files changed
+3816 -1850

No files matched your search

+5 -3
View File
@@ -1397,7 +1397,9 @@ class _ChannelsScreenState extends State<ChannelsScreen>
}
},
icon: const Icon(Icons.qr_code_scanner),
label: Text(sheetContext.l10n.community_scanQr),
label: Text(
sheetContext.l10n.channels_scanQrCode,
),
),
),
],
@@ -1601,8 +1603,8 @@ class _ChannelsScreenState extends State<ChannelsScreen>
buildOptionCard(
optionIndex: 4,
icon: Icons.qr_code_scanner,
title: sheetContext.l10n.community_scanQr,
subtitle: sheetContext.l10n.community_join,
title: sheetContext.l10n.channels_scanQrCode,
subtitle: sheetContext.l10n.channels_scanQrCodeDesc,
),
if (selectedOption == 4) buildExpandedContent()!,
SectionHeader(
+141 -7
View File
@@ -1,5 +1,6 @@
import 'dart:async';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:uuid/uuid.dart';
@@ -11,15 +12,20 @@ import '../l10n/l10n.dart';
import '../models/community.dart';
import '../storage/community_store.dart';
import '../theme/mesh_theme.dart';
import '../utils/channel_qr.dart';
import '../widgets/adaptive_app_bar_title.dart';
import '../widgets/mesh_ui.dart';
import '../widgets/qr_scanner_widget.dart';
/// Screen for scanning community QR codes to join communities.
/// Screen for scanning community and channel QR codes.
///
/// After successful scan, the user can:
/// After scanning a community QR code, the user can:
/// 1. Join the community (saves to local storage)
/// 2. Optionally add the Community Public Channel to the device
///
/// After scanning an official channel QR code
/// (`meshcore://channel/add?name=…&secret=…`), the user can add that
/// channel to a free slot on the device.
class CommunityQrScannerScreen extends StatefulWidget {
const CommunityQrScannerScreen({super.key});
@@ -54,7 +60,7 @@ class _CommunityQrScannerScreenState extends State<CommunityQrScannerScreen> {
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: AdaptiveAppBarTitle(context.l10n.community_scanQr),
title: AdaptiveAppBarTitle(context.l10n.channels_scanQrCode),
centerTitle: true,
),
body: _isProcessing
@@ -62,12 +68,14 @@ class _CommunityQrScannerScreenState extends State<CommunityQrScannerScreen> {
: QrScannerWidget(
onScanned: (data) => _handleScannedData(context, data),
validator: (data) =>
Community.isValidQrData(data) && !_isRecentlyCancelled(data),
(Community.isValidQrData(data) ||
parseChannelQr(data) != null) &&
!_isRecentlyCancelled(data),
onValidationFailed: (data) {
if (_isRecentlyCancelled(data)) return;
_showInvalidQrError(context);
},
instructions: context.l10n.community_scanInstructions,
instructions: context.l10n.channels_scanQrInstructions,
themed: true,
),
);
@@ -87,6 +95,14 @@ class _CommunityQrScannerScreenState extends State<CommunityQrScannerScreen> {
var leaving = false;
try {
final channel = parseChannelQr(data);
if (channel != null) {
if (context.mounted) {
leaving = await _showJoinChannelDialog(context, channel, data);
}
return;
}
// Parse the community data
final community = Community.fromQrData(const Uuid().v4(), data);
@@ -111,7 +127,7 @@ class _CommunityQrScannerScreenState extends State<CommunityQrScannerScreen> {
if (context.mounted) {
showDismissibleSnackBar(
context,
content: Text(context.l10n.community_invalidQrCode),
content: Text(context.l10n.channels_invalidQrCode),
backgroundColor: MeshPalette.alert,
);
}
@@ -125,7 +141,125 @@ class _CommunityQrScannerScreenState extends State<CommunityQrScannerScreen> {
}
void _showInvalidQrError(BuildContext context) {
showInvalidQrSnackBar(context, context.l10n.community_invalidQrCode);
showInvalidQrSnackBar(context, context.l10n.channels_invalidQrCode);
}
/// Returns true when the channel was added and the screen popped.
Future<bool> _showJoinChannelDialog(
BuildContext context,
ScannedChannel channel,
String data,
) async {
final connector = context.read<MeshCoreConnector>();
final name = truncateUtf8(channel.name, maxRadioNameBytes);
final alreadyOnRadio = connector.channels.any(
(c) => listEquals(c.psk, channel.psk),
);
if (alreadyOnRadio) {
showDismissibleSnackBar(
context,
content: Text(context.l10n.channels_alreadyOnRadio(name)),
);
_markCancelled(data);
return false;
}
final confirmed = await showMeshSheet<bool>(
context,
builder: (sheetContext) {
final sheetScheme = Theme.of(sheetContext).colorScheme;
return Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
BottomSheetHeader(title: context.l10n.channels_joinChannelTitle),
Padding(
padding: const EdgeInsets.fromLTRB(20, 0, 20, 4),
child: Text(
context.l10n.channels_joinChannelConfirm(name),
style: TextStyle(color: sheetScheme.onSurfaceVariant),
),
),
MeshCard(
child: Row(
children: [
AvatarCircle(
name: name,
icon: Icons.lock,
color: MeshPalette.blue,
size: 44,
),
const SizedBox(width: 14),
Expanded(
child: Text(
name,
style: const TextStyle(
fontWeight: FontWeight.w600,
fontSize: 15,
),
),
),
],
),
),
Padding(
padding: const EdgeInsets.fromLTRB(16, 8, 16, 16),
child: Row(
children: [
Expanded(
child: OutlinedButton(
onPressed: () => Navigator.pop(sheetContext, false),
child: Text(context.l10n.common_cancel),
),
),
const SizedBox(width: 12),
Expanded(
child: FilledButton(
onPressed: () => Navigator.pop(sheetContext, true),
child: Text(context.l10n.common_add),
),
),
],
),
),
],
);
},
);
if (confirmed != true || !context.mounted) {
_markCancelled(data);
return false;
}
// Only trust free slots after a complete channel sync; mid-sync, unfetched
// slots look empty and writing one would overwrite an existing channel.
final slotsKnown =
connector.hasLoadedChannels && !connector.isSyncingChannels;
final nextIndex = slotsKnown
? _findNextAvailableChannelIndex(connector)
: null;
if (nextIndex == null) {
showDismissibleSnackBar(
context,
content: Text(
slotsKnown
? context.l10n.channels_noFreeSlots
: context.l10n.channels_waitForSync,
),
);
_markCancelled(data);
return false;
}
await connector.setChannel(nextIndex, name, channel.psk);
if (!context.mounted) return false;
showDismissibleSnackBar(
context,
content: Text(context.l10n.channels_channelAdded(name)),
backgroundColor: MeshPalette.signal,
);
Navigator.pop(context);
return true;
}
/// Returns true when the screen popped itself (OK pressed).