Wait for the radio before removing a contact, and explain re-adds

removeContact now waits for the radio's reply and only drops the contact
locally when the radio confirms (or reports it doesn't have it), so a
lost command no longer leaves a contact that the next sync restores.
Failures are shown to the user. The delete dialog warns when the radio
auto-adds that contact type, since the contact returns on its next
advert.
This commit is contained in:
zjs81 committed 2026-10-24 21:03:39 -07:00
1 parent 1e786b24ba
commit f2a27f0f71
28 files changed
+352 -10

No files matched your search

+25 -2
View File
@@ -667,6 +667,16 @@ class MeshCoreConnector extends ChangeNotifier {
bool? get autoAddSensors => _autoAddSensors;
bool? get autoAddOverwriteOldest => _overwriteOldest;
/// Whether the radio stores adverts of [type] by itself, so a removed
/// contact of that type returns the next time it advertises.
bool autoAddsContactType(int type) => switch (type) {
advTypeChat => _autoAddUsers,
advTypeRepeater => _autoAddRepeaters,
advTypeRoom => _autoAddRoomServers,
advTypeSensor => _autoAddSensors,
_ => false,
};
/// autoadd_max_hops from GET_AUTOADD_CONFIG (0 = no limit), null when the
/// firmware did not report it (pre-v1.13).
int? get autoAddMaxHops => _autoAddMaxHops;
@@ -5034,13 +5044,26 @@ class MeshCoreConnector extends ChangeNotifier {
}) async {
if (!isConnected) return;
// Only drop the contact locally once the radio confirms; otherwise the
// next contact sync restores it. ERR means the radio does not have it.
try {
await sendFrame(
buildRemoveContactFrame(contact.publicKey),
waitForGenericAck: true,
);
} on FirmwareCommandException catch (e) {
_appDebugLogService?.warn(
'Radio has no contact ${contact.publicKeyHex.substring(0, 8)} '
'(error ${e.errCode}); removing it locally',
tag: 'Connector',
);
}
_handleDiscovery(
contact,
contact.rawPacket ?? Uint8List(0),
noNotify: true,
);
await sendFrame(buildRemoveContactFrame(contact.publicKey));
_contactsStorageFull = false;
_contacts.removeWhere((c) => c.publicKeyHex == contact.publicKeyHex);
_knownContactKeys.remove(contact.publicKeyHex);
+11 -3
View File
@@ -5,6 +5,16 @@ import 'package:flutter/foundation.dart';
import '../services/image_chunk_transport.dart' show cmdSendChannelData;
import 'meshcore_protocol.dart';
/// RESP_CODE_ERR received for a command that was waiting for its reply.
class FirmwareCommandException implements Exception {
final int errCode;
const FirmwareCommandException(this.errCode);
@override
String toString() => 'Exception: Command failed with error code $errCode';
}
/// A command waiting for its reply from the companion firmware.
class PendingCommandReply {
final int commandCode;
@@ -203,9 +213,7 @@ class PendingCommandReplies {
if (_queue.isEmpty) return null;
final pending = _queue.removeAt(0);
if (pending.isTombstone) return null;
pending.completer?.completeError(
Exception('Command failed with error code $errCode'),
);
pending.completer?.completeError(FirmwareCommandException(errCode));
return pending;
}
+2
View File
@@ -608,6 +608,8 @@
},
"contacts_keepChatHistory": "Keep chat history",
"contacts_remove": "Remove",
"contacts_removeAutoAddNote": "Your radio auto-adds this contact type, so it will come back the next time it advertises. Turn off auto-add in Contacts settings to keep it removed.",
"contacts_removeFailed": "The radio did not confirm the removal. The contact was not removed.",
"contacts_discoveredNearby": "Discovered nearby ({count})",
"@contacts_discoveredNearby": {
"placeholders": {
+12
View File
@@ -2314,6 +2314,18 @@ abstract class AppLocalizations {
/// **'Remove'**
String get contacts_remove;
/// No description provided for @contacts_removeAutoAddNote.
///
/// In en, this message translates to:
/// **'Your radio auto-adds this contact type, so it will come back the next time it advertises. Turn off auto-add in Contacts settings to keep it removed.'**
String get contacts_removeAutoAddNote;
/// No description provided for @contacts_removeFailed.
///
/// In en, this message translates to:
/// **'The radio did not confirm the removal. The contact was not removed.'**
String get contacts_removeFailed;
/// No description provided for @contacts_discoveredNearby.
///
/// In en, this message translates to:
+8
View File
@@ -1290,6 +1290,14 @@ class AppLocalizationsBg extends AppLocalizations {
@override
String get contacts_remove => 'Премахни';
@override
String get contacts_removeAutoAddNote =>
'Your radio auto-adds this contact type, so it will come back the next time it advertises. Turn off auto-add in Contacts settings to keep it removed.';
@override
String get contacts_removeFailed =>
'The radio did not confirm the removal. The contact was not removed.';
@override
String contacts_discoveredNearby(int count) {
return 'Открити наблизо ($count)';
+8
View File
@@ -1285,6 +1285,14 @@ class AppLocalizationsDe extends AppLocalizations {
@override
String get contacts_remove => 'Entfernen';
@override
String get contacts_removeAutoAddNote =>
'Your radio auto-adds this contact type, so it will come back the next time it advertises. Turn off auto-add in Contacts settings to keep it removed.';
@override
String get contacts_removeFailed =>
'The radio did not confirm the removal. The contact was not removed.';
@override
String contacts_discoveredNearby(int count) {
return 'In der Nähe entdeckt ($count)';
+8
View File
@@ -1267,6 +1267,14 @@ class AppLocalizationsEn extends AppLocalizations {
@override
String get contacts_remove => 'Remove';
@override
String get contacts_removeAutoAddNote =>
'Your radio auto-adds this contact type, so it will come back the next time it advertises. Turn off auto-add in Contacts settings to keep it removed.';
@override
String get contacts_removeFailed =>
'The radio did not confirm the removal. The contact was not removed.';
@override
String contacts_discoveredNearby(int count) {
return 'Discovered nearby ($count)';
+8
View File
@@ -1282,6 +1282,14 @@ class AppLocalizationsEs extends AppLocalizations {
@override
String get contacts_remove => 'Eliminar';
@override
String get contacts_removeAutoAddNote =>
'Your radio auto-adds this contact type, so it will come back the next time it advertises. Turn off auto-add in Contacts settings to keep it removed.';
@override
String get contacts_removeFailed =>
'The radio did not confirm the removal. The contact was not removed.';
@override
String contacts_discoveredNearby(int count) {
return 'Descubiertos cercanos ($count)';
+8
View File
@@ -1288,6 +1288,14 @@ class AppLocalizationsFr extends AppLocalizations {
@override
String get contacts_remove => 'Supprimer';
@override
String get contacts_removeAutoAddNote =>
'Your radio auto-adds this contact type, so it will come back the next time it advertises. Turn off auto-add in Contacts settings to keep it removed.';
@override
String get contacts_removeFailed =>
'The radio did not confirm the removal. The contact was not removed.';
@override
String contacts_discoveredNearby(int count) {
return 'Découverts à proximité ($count)';
+8
View File
@@ -1278,6 +1278,14 @@ class AppLocalizationsHu extends AppLocalizations {
@override
String get contacts_remove => 'Eltávolítás';
@override
String get contacts_removeAutoAddNote =>
'Your radio auto-adds this contact type, so it will come back the next time it advertises. Turn off auto-add in Contacts settings to keep it removed.';
@override
String get contacts_removeFailed =>
'The radio did not confirm the removal. The contact was not removed.';
@override
String contacts_discoveredNearby(int count) {
return 'Közelben felfedezve ($count)';
+8
View File
@@ -1287,6 +1287,14 @@ class AppLocalizationsIt extends AppLocalizations {
@override
String get contacts_remove => 'Rimuovi';
@override
String get contacts_removeAutoAddNote =>
'Your radio auto-adds this contact type, so it will come back the next time it advertises. Turn off auto-add in Contacts settings to keep it removed.';
@override
String get contacts_removeFailed =>
'The radio did not confirm the removal. The contact was not removed.';
@override
String contacts_discoveredNearby(int count) {
return 'Scoperti nelle vicinanze ($count)';
+8
View File
@@ -1215,6 +1215,14 @@ class AppLocalizationsJa extends AppLocalizations {
@override
String get contacts_remove => '削除';
@override
String get contacts_removeAutoAddNote =>
'Your radio auto-adds this contact type, so it will come back the next time it advertises. Turn off auto-add in Contacts settings to keep it removed.';
@override
String get contacts_removeFailed =>
'The radio did not confirm the removal. The contact was not removed.';
@override
String contacts_discoveredNearby(int count) {
return '近くで発見された連絡先($count)';
+8
View File
@@ -1220,6 +1220,14 @@ class AppLocalizationsKo extends AppLocalizations {
@override
String get contacts_remove => '제거';
@override
String get contacts_removeAutoAddNote =>
'Your radio auto-adds this contact type, so it will come back the next time it advertises. Turn off auto-add in Contacts settings to keep it removed.';
@override
String get contacts_removeFailed =>
'The radio did not confirm the removal. The contact was not removed.';
@override
String contacts_discoveredNearby(int count) {
return '주변에서 발견됨($count)';
+8
View File
@@ -1277,6 +1277,14 @@ class AppLocalizationsNl extends AppLocalizations {
@override
String get contacts_remove => 'Verwijderen';
@override
String get contacts_removeAutoAddNote =>
'Your radio auto-adds this contact type, so it will come back the next time it advertises. Turn off auto-add in Contacts settings to keep it removed.';
@override
String get contacts_removeFailed =>
'The radio did not confirm the removal. The contact was not removed.';
@override
String contacts_discoveredNearby(int count) {
return 'In de buurt ontdekt ($count)';
+8
View File
@@ -1301,6 +1301,14 @@ class AppLocalizationsPl extends AppLocalizations {
@override
String get contacts_remove => 'Usuń';
@override
String get contacts_removeAutoAddNote =>
'Your radio auto-adds this contact type, so it will come back the next time it advertises. Turn off auto-add in Contacts settings to keep it removed.';
@override
String get contacts_removeFailed =>
'The radio did not confirm the removal. The contact was not removed.';
@override
String contacts_discoveredNearby(int count) {
return 'Odkryte w pobliżu ($count)';
+8
View File
@@ -1286,6 +1286,14 @@ class AppLocalizationsPt extends AppLocalizations {
@override
String get contacts_remove => 'Remover';
@override
String get contacts_removeAutoAddNote =>
'Your radio auto-adds this contact type, so it will come back the next time it advertises. Turn off auto-add in Contacts settings to keep it removed.';
@override
String get contacts_removeFailed =>
'The radio did not confirm the removal. The contact was not removed.';
@override
String contacts_discoveredNearby(int count) {
return 'Descobertos nas proximidades ($count)';
+8
View File
@@ -1290,6 +1290,14 @@ class AppLocalizationsRu extends AppLocalizations {
@override
String get contacts_remove => 'Убрать';
@override
String get contacts_removeAutoAddNote =>
'Your radio auto-adds this contact type, so it will come back the next time it advertises. Turn off auto-add in Contacts settings to keep it removed.';
@override
String get contacts_removeFailed =>
'The radio did not confirm the removal. The contact was not removed.';
@override
String contacts_discoveredNearby(int count) {
return 'Обнаружены поблизости ($count)';
+8
View File
@@ -1277,6 +1277,14 @@ class AppLocalizationsSk extends AppLocalizations {
@override
String get contacts_remove => 'Odstrániť';
@override
String get contacts_removeAutoAddNote =>
'Your radio auto-adds this contact type, so it will come back the next time it advertises. Turn off auto-add in Contacts settings to keep it removed.';
@override
String get contacts_removeFailed =>
'The radio did not confirm the removal. The contact was not removed.';
@override
String contacts_discoveredNearby(int count) {
return 'Objavené v blízkosti ($count)';
+8
View File
@@ -1277,6 +1277,14 @@ class AppLocalizationsSl extends AppLocalizations {
@override
String get contacts_remove => 'Odstrani';
@override
String get contacts_removeAutoAddNote =>
'Your radio auto-adds this contact type, so it will come back the next time it advertises. Turn off auto-add in Contacts settings to keep it removed.';
@override
String get contacts_removeFailed =>
'The radio did not confirm the removal. The contact was not removed.';
@override
String contacts_discoveredNearby(int count) {
return 'Odkriti v bližini ($count)';
+8
View File
@@ -1267,6 +1267,14 @@ class AppLocalizationsSv extends AppLocalizations {
@override
String get contacts_remove => 'Ta bort';
@override
String get contacts_removeAutoAddNote =>
'Your radio auto-adds this contact type, so it will come back the next time it advertises. Turn off auto-add in Contacts settings to keep it removed.';
@override
String get contacts_removeFailed =>
'The radio did not confirm the removal. The contact was not removed.';
@override
String contacts_discoveredNearby(int count) {
return 'Upptäckt i närheten ($count)';
+8
View File
@@ -1281,6 +1281,14 @@ class AppLocalizationsUk extends AppLocalizations {
@override
String get contacts_remove => 'Прибрати';
@override
String get contacts_removeAutoAddNote =>
'Your radio auto-adds this contact type, so it will come back the next time it advertises. Turn off auto-add in Contacts settings to keep it removed.';
@override
String get contacts_removeFailed =>
'The radio did not confirm the removal. The contact was not removed.';
@override
String contacts_discoveredNearby(int count) {
return 'Виявлені поблизу ($count)';
+8
View File
@@ -1197,6 +1197,14 @@ class AppLocalizationsZh extends AppLocalizations {
@override
String get contacts_remove => '移除';
@override
String get contacts_removeAutoAddNote =>
'Your radio auto-adds this contact type, so it will come back the next time it advertises. Turn off auto-add in Contacts settings to keep it removed.';
@override
String get contacts_removeFailed =>
'The radio did not confirm the removal. The contact was not removed.';
@override
String contacts_discoveredNearby(int count) {
return '附近发现的($count)';
+27 -2
View File
@@ -1758,6 +1758,16 @@ class _ContactsScreenState extends State<ContactsScreen>
Text(
context.l10n.contacts_removeFromContactsConfirm(contact.name),
),
if (connector.autoAddsContactType(contact.type)) ...[
const SizedBox(height: 12),
Text(
context.l10n.contacts_removeAutoAddNote,
style: TextStyle(
color: Theme.of(dialogContext).colorScheme.error,
fontWeight: FontWeight.w600,
),
),
],
CheckboxListTile(
contentPadding: EdgeInsets.zero,
controlAffinity: ListTileControlAffinity.leading,
@@ -1774,9 +1784,24 @@ class _ContactsScreenState extends State<ContactsScreen>
child: Text(context.l10n.common_cancel),
),
TextButton(
onPressed: () {
onPressed: () async {
Navigator.pop(dialogContext);
connector.removeContact(contact, keepMessages: keepHistory);
try {
await connector.removeContact(
contact,
keepMessages: keepHistory,
);
} catch (e) {
appLogger.warn(
'Remove contact failed: $e',
tag: 'ContactsScreen',
);
if (!context.mounted) return;
showDismissibleSnackBar(
context,
content: Text(context.l10n.contacts_removeFailed),
);
}
},
child: Text(
context.l10n.contacts_remove,
+9 -1
View File
@@ -316,7 +316,15 @@ class _DiscoveryScreenState extends State<DiscoveryScreen> {
content: Text(context.l10n.discoveredContacts_contactAdded),
action: SnackBarAction(
label: context.l10n.common_undo,
onPressed: () => connector.removeContact(contact),
onPressed: () => unawaited(
connector.removeContact(contact).catchError((Object _) {
if (!context.mounted) return;
showDismissibleSnackBar(
context,
content: Text(context.l10n.contacts_removeFailed),
);
}),
),
),
);
} catch (_) {