diff --git a/lib/helpers/channel_slot_ui.dart b/lib/helpers/channel_slot_ui.dart new file mode 100644 index 00000000..c8a7f5c0 --- /dev/null +++ b/lib/helpers/channel_slot_ui.dart @@ -0,0 +1,59 @@ +import 'package:flutter/material.dart'; + +import '../connector/meshcore_connector.dart'; +import '../l10n/l10n.dart'; +import 'channel_slot_decision.dart'; +import 'snack_bar_builder.dart'; + +/// A channel slot that is safe to write right now, or null after telling the +/// user why not (#1021). +/// +/// Every surface that adds a channel goes through this one function: the +/// channels screen, the community QR scanner, and anything else that writes a +/// slot. That is deliberate. The bug this fixes existed because the same +/// question ("which slot is free?") was answered three different ways in three +/// files, and only one of them was wrong. One answer cannot drift. +/// +/// Call it at the moment of the write and never earlier. The connector empties +/// its channel list for the duration of a channel sync, and `setChannel` itself +/// starts one, so a slot chosen before a dialog or a confirmation can point at +/// an occupied slot by the time it is written. +/// +/// The owner's ruling is that the unsafe write is blocked but the control stays +/// live: a greyed-out button prevents the damage and tells the user nothing. So +/// a refusal returns null *and* says what is wrong. +int? allocateChannelSlotOrExplain( + BuildContext context, + MeshCoreConnector connector, +) { + final decision = decideChannelSlot( + channels: connector.channels, + maxChannels: connector.maxChannels, + // isSyncingChannels, not hasLoadedChannels: the latter stays false after a + // FAILED sync while the cached list is restored, so gating on it would make + // adding a channel impossible on a flaky link. + isSyncing: connector.isSyncingChannels, + ); + switch (decision.refusal) { + case ChannelSlotRefusal.syncing: + showChannelBlockedMessage(context, context.l10n.channels_slotBusySyncing); + return null; + case ChannelSlotRefusal.full: + showChannelBlockedMessage( + context, + context.l10n.channels_slotsAllFull(connector.maxChannels), + ); + return null; + case null: + return decision.slot; + } +} + +/// A refusal the user has to be able to read and act on, so it persists until +/// dismissed. +/// +/// `showDismissibleSnackBar` defaults to `persist: false` and a 4 second toast, +/// which SAFELANE 6 rules out for an error. +void showChannelBlockedMessage(BuildContext context, String message) { + showDismissibleSnackBar(context, content: Text(message), persist: true); +} diff --git a/lib/l10n/app_en.arb b/lib/l10n/app_en.arb index ba4d811d..a1126c8e 100644 --- a/lib/l10n/app_en.arb +++ b/lib/l10n/app_en.arb @@ -2522,7 +2522,6 @@ "channels_scanQrDesc": "Add a community or channel from a QR code", "channels_scanQrInstructions": "Point at a MeshCore QR code", "channels_invalidQr": "Invalid QR code", - "channels_noFreeSlot": "No free channel slot", "channels_qrAddConfirm": "Add channel \"{name}\"?", "@channels_qrAddConfirm": { "placeholders": { @@ -3413,6 +3412,10 @@ "@importConfig_reasonNoFreeSlot": { "description": "Skip reason: every channel slot is occupied (#576)." }, + "importConfig_reasonChannelsSyncing": "channels were still being read from the radio; import again in a moment", + "@importConfig_reasonChannelsSyncing": { + "description": "Skip reason: the channel list was mid-sync, so no slot could be trusted to be free (#1021)." + }, "importConfig_reasonAbsentFromFile": "not present in this file", "@importConfig_reasonAbsentFromFile": { "description": "Failure reason: the file did not contain the section (#576)." diff --git a/lib/l10n/app_localizations.dart b/lib/l10n/app_localizations.dart index bc8831c0..7c326819 100644 --- a/lib/l10n/app_localizations.dart +++ b/lib/l10n/app_localizations.dart @@ -7502,12 +7502,6 @@ abstract class AppLocalizations { /// **'Invalid QR code'** String get channels_invalidQr; - /// No description provided for @channels_noFreeSlot. - /// - /// In en, this message translates to: - /// **'No free channel slot'** - String get channels_noFreeSlot; - /// No description provided for @channels_qrAddConfirm. /// /// In en, this message translates to: @@ -9502,6 +9496,12 @@ abstract class AppLocalizations { /// **'no free channel slot on the device'** String get importConfig_reasonNoFreeSlot; + /// Skip reason: the channel list was mid-sync, so no slot could be trusted to be free (#1021). + /// + /// In en, this message translates to: + /// **'channels were still being read from the radio; import again in a moment'** + String get importConfig_reasonChannelsSyncing; + /// Failure reason: the file did not contain the section (#576). /// /// In en, this message translates to: diff --git a/lib/l10n/app_localizations_bg.dart b/lib/l10n/app_localizations_bg.dart index ae852baa..150fc508 100644 --- a/lib/l10n/app_localizations_bg.dart +++ b/lib/l10n/app_localizations_bg.dart @@ -4416,9 +4416,6 @@ class AppLocalizationsBg extends AppLocalizations { @override String get channels_invalidQr => 'Invalid QR code'; - @override - String get channels_noFreeSlot => 'No free channel slot'; - @override String channels_qrAddConfirm(String name) { return 'Add channel \"$name\"?'; @@ -5645,6 +5642,10 @@ class AppLocalizationsBg extends AppLocalizations { String get importConfig_reasonNoFreeSlot => 'no free channel slot on the device'; + @override + String get importConfig_reasonChannelsSyncing => + 'channels were still being read from the radio; import again in a moment'; + @override String get importConfig_reasonAbsentFromFile => 'not present in this file'; diff --git a/lib/l10n/app_localizations_de.dart b/lib/l10n/app_localizations_de.dart index 4bb3ddc0..3e135b4a 100644 --- a/lib/l10n/app_localizations_de.dart +++ b/lib/l10n/app_localizations_de.dart @@ -4422,9 +4422,6 @@ class AppLocalizationsDe extends AppLocalizations { @override String get channels_invalidQr => 'Invalid QR code'; - @override - String get channels_noFreeSlot => 'No free channel slot'; - @override String channels_qrAddConfirm(String name) { return 'Add channel \"$name\"?'; @@ -5662,6 +5659,10 @@ class AppLocalizationsDe extends AppLocalizations { String get importConfig_reasonNoFreeSlot => 'no free channel slot on the device'; + @override + String get importConfig_reasonChannelsSyncing => + 'channels were still being read from the radio; import again in a moment'; + @override String get importConfig_reasonAbsentFromFile => 'not present in this file'; diff --git a/lib/l10n/app_localizations_en.dart b/lib/l10n/app_localizations_en.dart index b48c700d..7e9f11fa 100644 --- a/lib/l10n/app_localizations_en.dart +++ b/lib/l10n/app_localizations_en.dart @@ -4353,9 +4353,6 @@ class AppLocalizationsEn extends AppLocalizations { @override String get channels_invalidQr => 'Invalid QR code'; - @override - String get channels_noFreeSlot => 'No free channel slot'; - @override String channels_qrAddConfirm(String name) { return 'Add channel \"$name\"?'; @@ -5568,6 +5565,10 @@ class AppLocalizationsEn extends AppLocalizations { String get importConfig_reasonNoFreeSlot => 'no free channel slot on the device'; + @override + String get importConfig_reasonChannelsSyncing => + 'channels were still being read from the radio; import again in a moment'; + @override String get importConfig_reasonAbsentFromFile => 'not present in this file'; diff --git a/lib/l10n/app_localizations_es.dart b/lib/l10n/app_localizations_es.dart index 4239bc39..a5235000 100644 --- a/lib/l10n/app_localizations_es.dart +++ b/lib/l10n/app_localizations_es.dart @@ -4411,9 +4411,6 @@ class AppLocalizationsEs extends AppLocalizations { @override String get channels_invalidQr => 'Invalid QR code'; - @override - String get channels_noFreeSlot => 'No free channel slot'; - @override String channels_qrAddConfirm(String name) { return 'Add channel \"$name\"?'; @@ -5650,6 +5647,10 @@ class AppLocalizationsEs extends AppLocalizations { String get importConfig_reasonNoFreeSlot => 'no free channel slot on the device'; + @override + String get importConfig_reasonChannelsSyncing => + 'channels were still being read from the radio; import again in a moment'; + @override String get importConfig_reasonAbsentFromFile => 'not present in this file'; diff --git a/lib/l10n/app_localizations_fr.dart b/lib/l10n/app_localizations_fr.dart index 90b876c1..b97536a3 100644 --- a/lib/l10n/app_localizations_fr.dart +++ b/lib/l10n/app_localizations_fr.dart @@ -4433,9 +4433,6 @@ class AppLocalizationsFr extends AppLocalizations { @override String get channels_invalidQr => 'Invalid QR code'; - @override - String get channels_noFreeSlot => 'No free channel slot'; - @override String channels_qrAddConfirm(String name) { return 'Add channel \"$name\"?'; @@ -5678,6 +5675,10 @@ class AppLocalizationsFr extends AppLocalizations { String get importConfig_reasonNoFreeSlot => 'no free channel slot on the device'; + @override + String get importConfig_reasonChannelsSyncing => + 'channels were still being read from the radio; import again in a moment'; + @override String get importConfig_reasonAbsentFromFile => 'not present in this file'; diff --git a/lib/l10n/app_localizations_hu.dart b/lib/l10n/app_localizations_hu.dart index 6a20fec6..feb311cb 100644 --- a/lib/l10n/app_localizations_hu.dart +++ b/lib/l10n/app_localizations_hu.dart @@ -4428,9 +4428,6 @@ class AppLocalizationsHu extends AppLocalizations { @override String get channels_invalidQr => 'Invalid QR code'; - @override - String get channels_noFreeSlot => 'No free channel slot'; - @override String channels_qrAddConfirm(String name) { return 'Add channel \"$name\"?'; @@ -5667,6 +5664,10 @@ class AppLocalizationsHu extends AppLocalizations { String get importConfig_reasonNoFreeSlot => 'no free channel slot on the device'; + @override + String get importConfig_reasonChannelsSyncing => + 'channels were still being read from the radio; import again in a moment'; + @override String get importConfig_reasonAbsentFromFile => 'not present in this file'; diff --git a/lib/l10n/app_localizations_it.dart b/lib/l10n/app_localizations_it.dart index 4f5f5c6a..63dfc9b3 100644 --- a/lib/l10n/app_localizations_it.dart +++ b/lib/l10n/app_localizations_it.dart @@ -4416,9 +4416,6 @@ class AppLocalizationsIt extends AppLocalizations { @override String get channels_invalidQr => 'Invalid QR code'; - @override - String get channels_noFreeSlot => 'No free channel slot'; - @override String channels_qrAddConfirm(String name) { return 'Add channel \"$name\"?'; @@ -5655,6 +5652,10 @@ class AppLocalizationsIt extends AppLocalizations { String get importConfig_reasonNoFreeSlot => 'no free channel slot on the device'; + @override + String get importConfig_reasonChannelsSyncing => + 'channels were still being read from the radio; import again in a moment'; + @override String get importConfig_reasonAbsentFromFile => 'not present in this file'; diff --git a/lib/l10n/app_localizations_ja.dart b/lib/l10n/app_localizations_ja.dart index 1772fac0..108d7516 100644 --- a/lib/l10n/app_localizations_ja.dart +++ b/lib/l10n/app_localizations_ja.dart @@ -4230,9 +4230,6 @@ class AppLocalizationsJa extends AppLocalizations { @override String get channels_invalidQr => 'Invalid QR code'; - @override - String get channels_noFreeSlot => 'No free channel slot'; - @override String channels_qrAddConfirm(String name) { return 'Add channel \"$name\"?'; @@ -5421,6 +5418,10 @@ class AppLocalizationsJa extends AppLocalizations { String get importConfig_reasonNoFreeSlot => 'no free channel slot on the device'; + @override + String get importConfig_reasonChannelsSyncing => + 'channels were still being read from the radio; import again in a moment'; + @override String get importConfig_reasonAbsentFromFile => 'not present in this file'; diff --git a/lib/l10n/app_localizations_ko.dart b/lib/l10n/app_localizations_ko.dart index d128b434..954a697b 100644 --- a/lib/l10n/app_localizations_ko.dart +++ b/lib/l10n/app_localizations_ko.dart @@ -4232,9 +4232,6 @@ class AppLocalizationsKo extends AppLocalizations { @override String get channels_invalidQr => 'Invalid QR code'; - @override - String get channels_noFreeSlot => 'No free channel slot'; - @override String channels_qrAddConfirm(String name) { return 'Add channel \"$name\"?'; @@ -5422,6 +5419,10 @@ class AppLocalizationsKo extends AppLocalizations { String get importConfig_reasonNoFreeSlot => 'no free channel slot on the device'; + @override + String get importConfig_reasonChannelsSyncing => + 'channels were still being read from the radio; import again in a moment'; + @override String get importConfig_reasonAbsentFromFile => 'not present in this file'; diff --git a/lib/l10n/app_localizations_nl.dart b/lib/l10n/app_localizations_nl.dart index 4525860c..cddd566b 100644 --- a/lib/l10n/app_localizations_nl.dart +++ b/lib/l10n/app_localizations_nl.dart @@ -4397,9 +4397,6 @@ class AppLocalizationsNl extends AppLocalizations { @override String get channels_invalidQr => 'Invalid QR code'; - @override - String get channels_noFreeSlot => 'No free channel slot'; - @override String channels_qrAddConfirm(String name) { return 'Add channel \"$name\"?'; @@ -5630,6 +5627,10 @@ class AppLocalizationsNl extends AppLocalizations { String get importConfig_reasonNoFreeSlot => 'no free channel slot on the device'; + @override + String get importConfig_reasonChannelsSyncing => + 'channels were still being read from the radio; import again in a moment'; + @override String get importConfig_reasonAbsentFromFile => 'not present in this file'; diff --git a/lib/l10n/app_localizations_pl.dart b/lib/l10n/app_localizations_pl.dart index 5a059857..75756c0d 100644 --- a/lib/l10n/app_localizations_pl.dart +++ b/lib/l10n/app_localizations_pl.dart @@ -4429,9 +4429,6 @@ class AppLocalizationsPl extends AppLocalizations { @override String get channels_invalidQr => 'Invalid QR code'; - @override - String get channels_noFreeSlot => 'No free channel slot'; - @override String channels_qrAddConfirm(String name) { return 'Add channel \"$name\"?'; @@ -5667,6 +5664,10 @@ class AppLocalizationsPl extends AppLocalizations { String get importConfig_reasonNoFreeSlot => 'no free channel slot on the device'; + @override + String get importConfig_reasonChannelsSyncing => + 'channels were still being read from the radio; import again in a moment'; + @override String get importConfig_reasonAbsentFromFile => 'not present in this file'; diff --git a/lib/l10n/app_localizations_pt.dart b/lib/l10n/app_localizations_pt.dart index 65735667..a2372d1f 100644 --- a/lib/l10n/app_localizations_pt.dart +++ b/lib/l10n/app_localizations_pt.dart @@ -4410,9 +4410,6 @@ class AppLocalizationsPt extends AppLocalizations { @override String get channels_invalidQr => 'Invalid QR code'; - @override - String get channels_noFreeSlot => 'No free channel slot'; - @override String channels_qrAddConfirm(String name) { return 'Add channel \"$name\"?'; @@ -5643,6 +5640,10 @@ class AppLocalizationsPt extends AppLocalizations { String get importConfig_reasonNoFreeSlot => 'no free channel slot on the device'; + @override + String get importConfig_reasonChannelsSyncing => + 'channels were still being read from the radio; import again in a moment'; + @override String get importConfig_reasonAbsentFromFile => 'not present in this file'; diff --git a/lib/l10n/app_localizations_ru.dart b/lib/l10n/app_localizations_ru.dart index 1301da16..e7f17869 100644 --- a/lib/l10n/app_localizations_ru.dart +++ b/lib/l10n/app_localizations_ru.dart @@ -4418,9 +4418,6 @@ class AppLocalizationsRu extends AppLocalizations { @override String get channels_invalidQr => 'Invalid QR code'; - @override - String get channels_noFreeSlot => 'No free channel slot'; - @override String channels_qrAddConfirm(String name) { return 'Add channel \"$name\"?'; @@ -5661,6 +5658,10 @@ class AppLocalizationsRu extends AppLocalizations { String get importConfig_reasonNoFreeSlot => 'no free channel slot on the device'; + @override + String get importConfig_reasonChannelsSyncing => + 'channels were still being read from the radio; import again in a moment'; + @override String get importConfig_reasonAbsentFromFile => 'not present in this file'; diff --git a/lib/l10n/app_localizations_sk.dart b/lib/l10n/app_localizations_sk.dart index cd33d4ce..4f10fd9f 100644 --- a/lib/l10n/app_localizations_sk.dart +++ b/lib/l10n/app_localizations_sk.dart @@ -4397,9 +4397,6 @@ class AppLocalizationsSk extends AppLocalizations { @override String get channels_invalidQr => 'Invalid QR code'; - @override - String get channels_noFreeSlot => 'No free channel slot'; - @override String channels_qrAddConfirm(String name) { return 'Add channel \"$name\"?'; @@ -5627,6 +5624,10 @@ class AppLocalizationsSk extends AppLocalizations { String get importConfig_reasonNoFreeSlot => 'no free channel slot on the device'; + @override + String get importConfig_reasonChannelsSyncing => + 'channels were still being read from the radio; import again in a moment'; + @override String get importConfig_reasonAbsentFromFile => 'not present in this file'; diff --git a/lib/l10n/app_localizations_sl.dart b/lib/l10n/app_localizations_sl.dart index 94af0124..b58fd784 100644 --- a/lib/l10n/app_localizations_sl.dart +++ b/lib/l10n/app_localizations_sl.dart @@ -4392,9 +4392,6 @@ class AppLocalizationsSl extends AppLocalizations { @override String get channels_invalidQr => 'Invalid QR code'; - @override - String get channels_noFreeSlot => 'No free channel slot'; - @override String channels_qrAddConfirm(String name) { return 'Add channel \"$name\"?'; @@ -5625,6 +5622,10 @@ class AppLocalizationsSl extends AppLocalizations { String get importConfig_reasonNoFreeSlot => 'no free channel slot on the device'; + @override + String get importConfig_reasonChannelsSyncing => + 'channels were still being read from the radio; import again in a moment'; + @override String get importConfig_reasonAbsentFromFile => 'not present in this file'; diff --git a/lib/l10n/app_localizations_sv.dart b/lib/l10n/app_localizations_sv.dart index f58a1b15..fd18a6f2 100644 --- a/lib/l10n/app_localizations_sv.dart +++ b/lib/l10n/app_localizations_sv.dart @@ -4372,9 +4372,6 @@ class AppLocalizationsSv extends AppLocalizations { @override String get channels_invalidQr => 'Invalid QR code'; - @override - String get channels_noFreeSlot => 'No free channel slot'; - @override String channels_qrAddConfirm(String name) { return 'Add channel \"$name\"?'; @@ -5600,6 +5597,10 @@ class AppLocalizationsSv extends AppLocalizations { String get importConfig_reasonNoFreeSlot => 'no free channel slot on the device'; + @override + String get importConfig_reasonChannelsSyncing => + 'channels were still being read from the radio; import again in a moment'; + @override String get importConfig_reasonAbsentFromFile => 'not present in this file'; diff --git a/lib/l10n/app_localizations_uk.dart b/lib/l10n/app_localizations_uk.dart index 92fdebc8..42aebaf0 100644 --- a/lib/l10n/app_localizations_uk.dart +++ b/lib/l10n/app_localizations_uk.dart @@ -4414,9 +4414,6 @@ class AppLocalizationsUk extends AppLocalizations { @override String get channels_invalidQr => 'Invalid QR code'; - @override - String get channels_noFreeSlot => 'No free channel slot'; - @override String channels_qrAddConfirm(String name) { return 'Add channel \"$name\"?'; @@ -5662,6 +5659,10 @@ class AppLocalizationsUk extends AppLocalizations { String get importConfig_reasonNoFreeSlot => 'no free channel slot on the device'; + @override + String get importConfig_reasonChannelsSyncing => + 'channels were still being read from the radio; import again in a moment'; + @override String get importConfig_reasonAbsentFromFile => 'not present in this file'; diff --git a/lib/l10n/app_localizations_zh.dart b/lib/l10n/app_localizations_zh.dart index 5f4748ca..eae23af1 100644 --- a/lib/l10n/app_localizations_zh.dart +++ b/lib/l10n/app_localizations_zh.dart @@ -4135,9 +4135,6 @@ class AppLocalizationsZh extends AppLocalizations { @override String get channels_invalidQr => 'Invalid QR code'; - @override - String get channels_noFreeSlot => 'No free channel slot'; - @override String channels_qrAddConfirm(String name) { return 'Add channel \"$name\"?'; @@ -5295,6 +5292,10 @@ class AppLocalizationsZh extends AppLocalizations { String get importConfig_reasonNoFreeSlot => 'no free channel slot on the device'; + @override + String get importConfig_reasonChannelsSyncing => + 'channels were still being read from the radio; import again in a moment'; + @override String get importConfig_reasonAbsentFromFile => 'not present in this file'; diff --git a/lib/screens/channels_screen.dart b/lib/screens/channels_screen.dart index 74a4fb25..052e186f 100644 --- a/lib/screens/channels_screen.dart +++ b/lib/screens/channels_screen.dart @@ -30,7 +30,7 @@ import '../widgets/channel_notify_mode.dart'; import '../widgets/radio_channel_notify.dart'; import '../widgets/radio_tone.dart'; import '../widgets/battery_optimization_banner.dart'; -import '../helpers/channel_slot_decision.dart'; +import '../helpers/channel_slot_ui.dart'; import '../helpers/snack_bar_builder.dart'; import 'channel_chat_screen.dart'; import 'community_qr_scanner_screen.dart'; @@ -1878,7 +1878,7 @@ class _ChannelsScreenState extends State // (`Channel.isPublicChannel`), not by its index, so it has no claim on // slot 0 and can live anywhere. if (connector.channels.any((c) => c.isPublicChannel)) { - _showChannelBlockedMessage( + showChannelBlockedMessage( context, context.l10n.channels_publicAlreadyAdded, ); @@ -1894,54 +1894,14 @@ class _ChannelsScreenState extends State ); } - /// A slot that is safe to write right now, or null after explaining why not. - /// - /// Call this at the moment of the write and never earlier. The connector - /// empties `channels` for the duration of a channel sync, so a slot chosen - /// when a dialog opened can look free when it is not; that is what lost - /// Public in #1021. - /// - /// The decision itself lives in [decideChannelSlot] so it can be tested - /// without a widget and shared with the QR and config-import paths. This - /// wrapper only turns a refusal into something the user can read. - /// - /// The owner's ruling is that the unsafe write is blocked but the control - /// stays live: a greyed-out button prevents the damage and tells the user - /// nothing. So both refusals return null *and* say what is wrong. - int? _allocateChannelSlot(BuildContext context, MeshCoreConnector connector) { - final decision = decideChannelSlot( - channels: connector.channels, - maxChannels: connector.maxChannels, - // isSyncingChannels, not hasLoadedChannels: the latter stays false after - // a FAILED sync while the cached list is restored, so gating on it would - // make adding a channel impossible on a flaky link. - isSyncing: connector.isSyncingChannels, - ); - switch (decision.refusal) { - case ChannelSlotRefusal.syncing: - _showChannelBlockedMessage( - context, - context.l10n.channels_slotBusySyncing, - ); - return null; - case ChannelSlotRefusal.full: - _showChannelBlockedMessage( - context, - context.l10n.channels_slotsAllFull(connector.maxChannels), - ); - return null; - case null: - return decision.slot; - } - } - - /// A refusal the user has to be able to read and act on, so it persists. - /// - /// `showDismissibleSnackBar` defaults to `persist: false` and a 4 second - /// toast, which SAFELANE 6 rules out for an error. - void _showChannelBlockedMessage(BuildContext context, String message) { - showDismissibleSnackBar(context, content: Text(message), persist: true); - } + /// Thin alias for the shared allocator, kept so the five call sites in this + /// file read the same as before. The logic lives in + /// `helpers/channel_slot_ui.dart` so this screen, the QR scanner and config + /// import all answer "which slot is free?" the same way (#1021). + int? _allocateChannelSlot( + BuildContext context, + MeshCoreConnector connector, + ) => allocateChannelSlotOrExplain(context, connector); void _showManageCommunitiesDialog(BuildContext context) { showModalBottomSheet( diff --git a/lib/screens/community_qr_scanner_screen.dart b/lib/screens/community_qr_scanner_screen.dart index 289edce7..f29711c4 100644 --- a/lib/screens/community_qr_scanner_screen.dart +++ b/lib/screens/community_qr_scanner_screen.dart @@ -9,6 +9,7 @@ import '../models/community.dart'; import '../storage/community_store.dart'; import '../widgets/adaptive_app_bar_title.dart'; import '../widgets/qr_scanner_widget.dart'; +import '../helpers/channel_slot_ui.dart'; import '../helpers/snack_bar_builder.dart'; /// Screen for scanning community QR codes to join communities. @@ -224,24 +225,37 @@ class _CommunityQrScannerScreenState extends State { _communityStore.setPublicKeyHex = connector.selfPublicKeyHex; await _communityStore.addCommunity(community); - // Optionally add the community public channel to the device + // Optionally add the community public channel to the device. The + // community is already saved above, so a refused slot only skips the + // channel; it never loses the community. + // + // This used to skip the channel silently when the radio was full and then + // show a green "joined" toast, which told the user everything had worked + // when the channel had not been added (#1021). A refusal now explains + // itself, and only a real success shows the green confirmation. + var channelRefused = false; if (addPublicChannel && context.mounted) { final connector = context.read(); - final nextIndex = _findNextAvailableChannelIndex(connector); - - if (nextIndex != null) { + final slot = allocateChannelSlotOrExplain(context, connector); + if (slot == null) { + channelRefused = true; + } else { final psk = community.deriveCommunityPublicPsk(); final channelName = '${community.name} Public'; - connector.setChannel(nextIndex, channelName, psk); + connector.setChannel(slot, channelName, psk); } } if (context.mounted) { - showDismissibleSnackBar( - context, - content: Text(context.l10n.community_joined(community.name)), - backgroundColor: Colors.green, - ); + // The refusal message is already showing and persists; a green toast + // on top of it would contradict it. + if (!channelRefused) { + showDismissibleSnackBar( + context, + content: Text(context.l10n.community_joined(community.name)), + backgroundColor: Colors.green, + ); + } // Return to previous screen Navigator.pop(context, community); @@ -271,17 +285,11 @@ class _CommunityQrScannerScreenState extends State { } return; } - final nextIndex = _findNextAvailableChannelIndex(connector); - if (nextIndex == null) { - if (context.mounted) { - showDismissibleSnackBar( - context, - content: Text(context.l10n.channels_noFreeSlot), - backgroundColor: Colors.red, - ); - } - return; - } + // No slot is chosen here. It used to be, and that is the same bug the + // channels screen had: the index was picked before the confirmation + // dialog below and written after it, so a channel sync during the + // confirmation (setChannel itself starts one) could leave it pointing at + // an occupied slot. The slot is now chosen after the user confirms. if (!context.mounted) return; final confirmed = await showDialog( context: context, @@ -301,7 +309,11 @@ class _CommunityQrScannerScreenState extends State { ), ); if (confirmed == true && context.mounted) { - connector.setChannel(nextIndex, channel.name, channel.psk); + final slot = allocateChannelSlotOrExplain(context, connector); + // Refused: the persistent message explains why. Stay on this screen so + // the user can retry, rather than popping away from the explanation. + if (slot == null) return; + connector.setChannel(slot, channel.name, channel.psk); showDismissibleSnackBar( context, content: Text(context.l10n.channels_qrAdded(channel.name)), @@ -312,12 +324,4 @@ class _CommunityQrScannerScreenState extends State { Navigator.pop(context); } } - - int? _findNextAvailableChannelIndex(MeshCoreConnector connector) { - final usedIndices = connector.channels.map((c) => c.index).toSet(); - for (int i = 0; i < connector.maxChannels; i++) { - if (!usedIndices.contains(i)) return i; - } - return null; - } } diff --git a/lib/screens/import_config_screen.dart b/lib/screens/import_config_screen.dart index 5f44f8ad..27f86520 100644 --- a/lib/screens/import_config_screen.dart +++ b/lib/screens/import_config_screen.dart @@ -323,6 +323,8 @@ String _reasonText(BuildContext context, StockConfigImportIssue reason) { return l10n.importConfig_reasonAlreadyPresent; case StockConfigImportIssue.noFreeSlot: return l10n.importConfig_reasonNoFreeSlot; + case StockConfigImportIssue.channelsSyncing: + return l10n.importConfig_reasonChannelsSyncing; case StockConfigImportIssue.notWritable: return l10n.importConfig_reasonNotWritable; } diff --git a/lib/services/stock_config_import_service.dart b/lib/services/stock_config_import_service.dart index 9f2db4a2..7c068042 100644 --- a/lib/services/stock_config_import_service.dart +++ b/lib/services/stock_config_import_service.dart @@ -47,6 +47,10 @@ enum StockConfigImportIssue { /// Every channel slot on the device is occupied. noFreeSlot, + /// The channel list was being re-read from the radio, so no slot could be + /// trusted to be free. Importing again once it finishes will work (#1021). + channelsSyncing, + /// We have no supported way to write this value to the device. notWritable, } @@ -274,6 +278,7 @@ class StockConfigImportService { existing: _connector.channels, incoming: incoming, maxChannels: _connector.maxChannels, + isSyncing: _connector.isSyncingChannels, ); for (final assignment in plan.assignments) { await _connector.setChannel( @@ -357,11 +362,25 @@ class ChannelImportPlan { /// New channels go into the lowest free slot. Stock's file has no index field, /// so incoming order is the only ordering information there is, and slots /// already occupied are left alone. +/// +/// While [isSyncing] is true nothing is planned. The connector empties its +/// channel list for the duration of a sync, so [existing] would look empty and +/// every incoming channel would be written over slots that are in fact +/// occupied, Public included (#1021). Each channel is reported as skipped +/// instead, so the user sees why and can import again. [isSyncing] is required +/// so a caller cannot forget it and get the unsafe behavior by default. ChannelImportPlan planChannelImport({ required List existing, required List incoming, required int maxChannels, + required bool isSyncing, }) { + if (isSyncing) { + return ChannelImportPlan(const [], [ + for (final channel in incoming) + SkippedChannel(channel.name, StockConfigImportIssue.channelsSyncing), + ]); + } final live = existing.where((c) => !c.isEmpty).toList(); final takenSlots = live.map((c) => c.index).toSet(); final psks = live.map((c) => c.pskHex).toSet(); diff --git a/test/services/stock_config_import_service_test.dart b/test/services/stock_config_import_service_test.dart index a8bcfcc1..fbafbe76 100644 --- a/test/services/stock_config_import_service_test.dart +++ b/test/services/stock_config_import_service_test.dart @@ -28,6 +28,7 @@ void main() { existing: [live(0, 'Public', 0x11)], incoming: [incoming('Weather', 0x22)], maxChannels: 40, + isSyncing: false, ); expect(plan.assignments, hasLength(1)); @@ -41,6 +42,7 @@ void main() { existing: [live(0, 'Public', 0x11)], incoming: [incoming('Renamed', 0x11)], maxChannels: 40, + isSyncing: false, ); expect(plan.assignments, isEmpty); @@ -55,6 +57,7 @@ void main() { existing: [live(0, 'Public', 0x11)], incoming: [incoming('Public', 0x99)], maxChannels: 40, + isSyncing: false, ); expect(plan.assignments, isEmpty); @@ -72,6 +75,7 @@ void main() { ], incoming: [incoming('Emergency', 0x33)], maxChannels: 40, + isSyncing: false, ); expect(plan.assignments.single.slot, 1); @@ -86,6 +90,7 @@ void main() { incoming('Third', 0x44), ], maxChannels: 40, + isSyncing: false, ); expect(plan.assignments.map((a) => a.slot), [1, 2, 3]); @@ -101,6 +106,7 @@ void main() { existing: [live(0, 'A', 0x11), live(1, 'B', 0x22)], incoming: [incoming('C', 0x33), incoming('D', 0x44)], maxChannels: 2, + isSyncing: false, ); expect(plan.assignments, isEmpty); @@ -116,6 +122,7 @@ void main() { existing: const [], incoming: [incoming('Dupe', 0x55), incoming('Dupe', 0x55)], maxChannels: 40, + isSyncing: false, ); expect(plan.assignments, hasLength(1)); @@ -127,11 +134,48 @@ void main() { existing: [live(0, 'Public', 0x11)], incoming: const [], maxChannels: 40, + isSyncing: false, ); expect(plan.assignments, isEmpty); expect(plan.skipped, isEmpty); }); + + // #1021: the connector empties its channel list for the length of a sync, + // so mid-sync `existing` looks empty. Planning then would write every + // incoming channel over slots that are really occupied, starting at 0. + test('mid-sync nothing is written, even into slots that look free', () { + final plan = planChannelImport( + existing: const [], + incoming: [incoming('Weather', 0x22), incoming('Emergency', 0x33)], + maxChannels: 40, + isSyncing: true, + ); + + expect(plan.assignments, isEmpty); + expect(plan.skipped.map((s) => s.name), ['Weather', 'Emergency']); + expect( + plan.skipped.map((s) => s.reason), + everyElement(StockConfigImportIssue.channelsSyncing), + ); + }); + + test('mid-sync every channel is reported as syncing, not as present', () { + // A partial list cannot prove a channel is already there either, so + // "already present" would be a guess. Syncing is the true reason. + final plan = planChannelImport( + existing: [live(0, 'Public', 0x11)], + incoming: [incoming('Public', 0x11)], + maxChannels: 40, + isSyncing: true, + ); + + expect(plan.assignments, isEmpty); + expect( + plan.skipped.single.reason, + StockConfigImportIssue.channelsSyncing, + ); + }); }); group('import result', () { diff --git a/untranslated.json b/untranslated.json index 9f0cceb6..26e30fc0 100644 --- a/untranslated.json +++ b/untranslated.json @@ -211,7 +211,6 @@ "channels_scanQrDesc", "channels_scanQrInstructions", "channels_invalidQr", - "channels_noFreeSlot", "channels_qrAddConfirm", "channels_qrAdded", "channels_qrExists", @@ -319,6 +318,7 @@ "importConfig_resultFailedHeading", "importConfig_reasonAlreadyPresent", "importConfig_reasonNoFreeSlot", + "importConfig_reasonChannelsSyncing", "importConfig_reasonAbsentFromFile", "importConfig_reasonNoReply", "importConfig_reasonUnsupported", @@ -582,7 +582,6 @@ "channels_scanQrDesc", "channels_scanQrInstructions", "channels_invalidQr", - "channels_noFreeSlot", "channels_qrAddConfirm", "channels_qrAdded", "channels_qrExists", @@ -690,6 +689,7 @@ "importConfig_resultFailedHeading", "importConfig_reasonAlreadyPresent", "importConfig_reasonNoFreeSlot", + "importConfig_reasonChannelsSyncing", "importConfig_reasonAbsentFromFile", "importConfig_reasonNoReply", "importConfig_reasonUnsupported", @@ -953,7 +953,6 @@ "channels_scanQrDesc", "channels_scanQrInstructions", "channels_invalidQr", - "channels_noFreeSlot", "channels_qrAddConfirm", "channels_qrAdded", "channels_qrExists", @@ -1061,6 +1060,7 @@ "importConfig_resultFailedHeading", "importConfig_reasonAlreadyPresent", "importConfig_reasonNoFreeSlot", + "importConfig_reasonChannelsSyncing", "importConfig_reasonAbsentFromFile", "importConfig_reasonNoReply", "importConfig_reasonUnsupported", @@ -1324,7 +1324,6 @@ "channels_scanQrDesc", "channels_scanQrInstructions", "channels_invalidQr", - "channels_noFreeSlot", "channels_qrAddConfirm", "channels_qrAdded", "channels_qrExists", @@ -1432,6 +1431,7 @@ "importConfig_resultFailedHeading", "importConfig_reasonAlreadyPresent", "importConfig_reasonNoFreeSlot", + "importConfig_reasonChannelsSyncing", "importConfig_reasonAbsentFromFile", "importConfig_reasonNoReply", "importConfig_reasonUnsupported", @@ -1695,7 +1695,6 @@ "channels_scanQrDesc", "channels_scanQrInstructions", "channels_invalidQr", - "channels_noFreeSlot", "channels_qrAddConfirm", "channels_qrAdded", "channels_qrExists", @@ -1803,6 +1802,7 @@ "importConfig_resultFailedHeading", "importConfig_reasonAlreadyPresent", "importConfig_reasonNoFreeSlot", + "importConfig_reasonChannelsSyncing", "importConfig_reasonAbsentFromFile", "importConfig_reasonNoReply", "importConfig_reasonUnsupported", @@ -2066,7 +2066,6 @@ "channels_scanQrDesc", "channels_scanQrInstructions", "channels_invalidQr", - "channels_noFreeSlot", "channels_qrAddConfirm", "channels_qrAdded", "channels_qrExists", @@ -2174,6 +2173,7 @@ "importConfig_resultFailedHeading", "importConfig_reasonAlreadyPresent", "importConfig_reasonNoFreeSlot", + "importConfig_reasonChannelsSyncing", "importConfig_reasonAbsentFromFile", "importConfig_reasonNoReply", "importConfig_reasonUnsupported", @@ -2437,7 +2437,6 @@ "channels_scanQrDesc", "channels_scanQrInstructions", "channels_invalidQr", - "channels_noFreeSlot", "channels_qrAddConfirm", "channels_qrAdded", "channels_qrExists", @@ -2545,6 +2544,7 @@ "importConfig_resultFailedHeading", "importConfig_reasonAlreadyPresent", "importConfig_reasonNoFreeSlot", + "importConfig_reasonChannelsSyncing", "importConfig_reasonAbsentFromFile", "importConfig_reasonNoReply", "importConfig_reasonUnsupported", @@ -2808,7 +2808,6 @@ "channels_scanQrDesc", "channels_scanQrInstructions", "channels_invalidQr", - "channels_noFreeSlot", "channels_qrAddConfirm", "channels_qrAdded", "channels_qrExists", @@ -2916,6 +2915,7 @@ "importConfig_resultFailedHeading", "importConfig_reasonAlreadyPresent", "importConfig_reasonNoFreeSlot", + "importConfig_reasonChannelsSyncing", "importConfig_reasonAbsentFromFile", "importConfig_reasonNoReply", "importConfig_reasonUnsupported", @@ -3179,7 +3179,6 @@ "channels_scanQrDesc", "channels_scanQrInstructions", "channels_invalidQr", - "channels_noFreeSlot", "channels_qrAddConfirm", "channels_qrAdded", "channels_qrExists", @@ -3287,6 +3286,7 @@ "importConfig_resultFailedHeading", "importConfig_reasonAlreadyPresent", "importConfig_reasonNoFreeSlot", + "importConfig_reasonChannelsSyncing", "importConfig_reasonAbsentFromFile", "importConfig_reasonNoReply", "importConfig_reasonUnsupported", @@ -3550,7 +3550,6 @@ "channels_scanQrDesc", "channels_scanQrInstructions", "channels_invalidQr", - "channels_noFreeSlot", "channels_qrAddConfirm", "channels_qrAdded", "channels_qrExists", @@ -3658,6 +3657,7 @@ "importConfig_resultFailedHeading", "importConfig_reasonAlreadyPresent", "importConfig_reasonNoFreeSlot", + "importConfig_reasonChannelsSyncing", "importConfig_reasonAbsentFromFile", "importConfig_reasonNoReply", "importConfig_reasonUnsupported", @@ -3921,7 +3921,6 @@ "channels_scanQrDesc", "channels_scanQrInstructions", "channels_invalidQr", - "channels_noFreeSlot", "channels_qrAddConfirm", "channels_qrAdded", "channels_qrExists", @@ -4029,6 +4028,7 @@ "importConfig_resultFailedHeading", "importConfig_reasonAlreadyPresent", "importConfig_reasonNoFreeSlot", + "importConfig_reasonChannelsSyncing", "importConfig_reasonAbsentFromFile", "importConfig_reasonNoReply", "importConfig_reasonUnsupported", @@ -4292,7 +4292,6 @@ "channels_scanQrDesc", "channels_scanQrInstructions", "channels_invalidQr", - "channels_noFreeSlot", "channels_qrAddConfirm", "channels_qrAdded", "channels_qrExists", @@ -4400,6 +4399,7 @@ "importConfig_resultFailedHeading", "importConfig_reasonAlreadyPresent", "importConfig_reasonNoFreeSlot", + "importConfig_reasonChannelsSyncing", "importConfig_reasonAbsentFromFile", "importConfig_reasonNoReply", "importConfig_reasonUnsupported", @@ -4663,7 +4663,6 @@ "channels_scanQrDesc", "channels_scanQrInstructions", "channels_invalidQr", - "channels_noFreeSlot", "channels_qrAddConfirm", "channels_qrAdded", "channels_qrExists", @@ -4771,6 +4770,7 @@ "importConfig_resultFailedHeading", "importConfig_reasonAlreadyPresent", "importConfig_reasonNoFreeSlot", + "importConfig_reasonChannelsSyncing", "importConfig_reasonAbsentFromFile", "importConfig_reasonNoReply", "importConfig_reasonUnsupported", @@ -5034,7 +5034,6 @@ "channels_scanQrDesc", "channels_scanQrInstructions", "channels_invalidQr", - "channels_noFreeSlot", "channels_qrAddConfirm", "channels_qrAdded", "channels_qrExists", @@ -5142,6 +5141,7 @@ "importConfig_resultFailedHeading", "importConfig_reasonAlreadyPresent", "importConfig_reasonNoFreeSlot", + "importConfig_reasonChannelsSyncing", "importConfig_reasonAbsentFromFile", "importConfig_reasonNoReply", "importConfig_reasonUnsupported", @@ -5405,7 +5405,6 @@ "channels_scanQrDesc", "channels_scanQrInstructions", "channels_invalidQr", - "channels_noFreeSlot", "channels_qrAddConfirm", "channels_qrAdded", "channels_qrExists", @@ -5513,6 +5512,7 @@ "importConfig_resultFailedHeading", "importConfig_reasonAlreadyPresent", "importConfig_reasonNoFreeSlot", + "importConfig_reasonChannelsSyncing", "importConfig_reasonAbsentFromFile", "importConfig_reasonNoReply", "importConfig_reasonUnsupported", @@ -5776,7 +5776,6 @@ "channels_scanQrDesc", "channels_scanQrInstructions", "channels_invalidQr", - "channels_noFreeSlot", "channels_qrAddConfirm", "channels_qrAdded", "channels_qrExists", @@ -5884,6 +5883,7 @@ "importConfig_resultFailedHeading", "importConfig_reasonAlreadyPresent", "importConfig_reasonNoFreeSlot", + "importConfig_reasonChannelsSyncing", "importConfig_reasonAbsentFromFile", "importConfig_reasonNoReply", "importConfig_reasonUnsupported", @@ -6147,7 +6147,6 @@ "channels_scanQrDesc", "channels_scanQrInstructions", "channels_invalidQr", - "channels_noFreeSlot", "channels_qrAddConfirm", "channels_qrAdded", "channels_qrExists", @@ -6255,6 +6254,7 @@ "importConfig_resultFailedHeading", "importConfig_reasonAlreadyPresent", "importConfig_reasonNoFreeSlot", + "importConfig_reasonChannelsSyncing", "importConfig_reasonAbsentFromFile", "importConfig_reasonNoReply", "importConfig_reasonUnsupported",