fix(#1023): every add-channel path chooses its slot at the write
The channels screen was fixed in #1022, but two more surfaces chose a channel slot on their own and had the same race. Community QR scanner: - A channel share link picked its slot before the confirm dialog and wrote it after, so a 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, and a refusal keeps the screen open with the reason showing. - Joining a community skipped the public channel silently when the radio was full and still showed the green "joined" toast. A refusal now explains itself, and the green toast shows only when the channel was actually written. - Its private slot finder is deleted. Config import: - planChannelImport takes a required isSyncing. Mid-sync the connector's channel list is empty, so the planner would have written every incoming channel from slot 0 up, over Public. It now plans nothing and reports each channel as skipped with a new channelsSyncing reason. The parameter is required so a caller cannot forget it and get the unsafe behavior by default. Both screens now share allocateChannelSlotOrExplain (lib/helpers/channel_slot_ui.dart), so the "which slot is free" question has a single answer. channels_noFreeSlot is retired; its only caller moved to the shared message. Tests: two planner tests for the syncing case, mutation-checked (with the guard disabled both fail). Full suite 1781 pass, analyze clean. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>fix/1896-client-slot0-public
parent
0506738925
commit
9a5cd0caf7
@ -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);
|
||||
}
|
||||
Loading…
Reference in new issue