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

+1 -1
View File
@@ -80,7 +80,7 @@ Groups are stored per radio identity (scoped by public key).
| Add/Remove Favorite | All types | Toggles the favorite flag |
| Share Contact | All types | Requests advert from device → copies `meshcore://<hex>` URI to clipboard |
| Share Contact Zero-Hop | All types | Broadcasts the contact's advertisement one hop |
| Delete Contact | All types | Confirmation dialog → removes from device and clears messages |
| Delete Contact | All types | Confirmation dialog → removes from device and clears messages. If auto-add is on for that type, the radio adds it back the next time it advertises; the dialog warns about this |
## App Bar Menus
+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 (_) {
@@ -65,7 +65,12 @@ void main() {
waitForReply: true,
);
queue.takeErr(6);
await expectLater(pending!.completer!.future, throwsException);
await expectLater(
pending!.completer!.future,
throwsA(
isA<FirmwareCommandException>().having((e) => e.errCode, 'errCode', 6),
),
);
});
group('timed-out entries', () {
+81
View File
@@ -0,0 +1,81 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:meshcore_open/connector/meshcore_connector.dart';
import 'package:meshcore_open/connector/meshcore_protocol.dart';
import 'package:meshcore_open/review_mode/review_radio.dart';
import 'package:meshcore_open/services/message_retry_service.dart';
import 'package:meshcore_open/services/path_history_service.dart';
import 'package:meshcore_open/services/storage_service.dart';
import 'package:meshcore_open/storage/contact_store.dart';
import 'package:meshcore_open/storage/prefs_manager.dart';
import 'package:shared_preferences/shared_preferences.dart';
Future<void> _waitFor(bool Function() condition, {int seconds = 8}) async {
final end = DateTime.now().add(Duration(seconds: seconds));
while (!condition()) {
if (DateTime.now().isAfter(end)) fail('Timed out waiting for condition');
await Future<void>.delayed(const Duration(milliseconds: 25));
}
}
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
setUp(() async {
SharedPreferences.setMockInitialValues({});
PrefsManager.reset();
await PrefsManager.initialize();
});
test('removed contact stays gone from the radio, list and store', () async {
final connector = MeshCoreConnector();
addTearDown(connector.dispose);
final retryService = MessageRetryService();
addTearDown(retryService.dispose);
connector.initialize(
retryService: retryService,
pathHistoryService: PathHistoryService(StorageService()),
);
connector.reviewRadioFactoryForTest = () =>
ReviewRadio(scheduledMessageInterval: const Duration(hours: 1));
await connector.connectReview();
await _waitFor(
() => connector.hasLoadedContacts && connector.contacts.length >= 6,
);
final contact = connector.contacts.firstWhere((c) => c.name == 'Maya');
await connector.removeContact(contact);
bool listed() =>
connector.contacts.any((c) => c.publicKeyHex == contact.publicKeyHex);
expect(listed(), isFalse);
expect(
connector.discoveredContacts
.firstWhere((c) => c.publicKeyHex == contact.publicKeyHex)
.isActive,
isFalse,
);
// A full resync from the radio must not bring it back.
await connector.getContacts();
await _waitFor(() => connector.hasLoadedContacts);
expect(connector.contacts, isNotEmpty);
expect(listed(), isFalse);
final store = ContactStore()..setPublicKeyHex = connector.selfPublicKeyHex;
final saved = await store.loadContacts();
expect(saved.any((c) => c.publicKeyHex == contact.publicKeyHex), isFalse);
await connector.disconnect(manual: true);
});
test('auto-add config reports which contact types come back', () {
final connector = MeshCoreConnector();
addTearDown(connector.dispose);
connector.handleFrameForTest([respCodeAutoAddConfig, autoAddRepeaterFlag]);
expect(connector.autoAddsContactType(advTypeRepeater), isTrue);
expect(connector.autoAddsContactType(advTypeChat), isFalse);
expect(connector.autoAddsContactType(advTypeRoom), isFalse);
});
}
+34
View File
@@ -1,5 +1,7 @@
{
"bg": [
"contacts_removeAutoAddNote",
"contacts_removeFailed",
"map_pathTraceAddMoreHint",
"map_pathTraceModeRoundTrip",
"map_pathTraceModeRoundTripHint",
@@ -20,6 +22,8 @@
],
"de": [
"contacts_removeAutoAddNote",
"contacts_removeFailed",
"map_pathTraceAddMoreHint",
"map_pathTraceModeRoundTrip",
"map_pathTraceModeRoundTripHint",
@@ -40,6 +44,8 @@
],
"es": [
"contacts_removeAutoAddNote",
"contacts_removeFailed",
"map_pathTraceAddMoreHint",
"map_pathTraceModeRoundTrip",
"map_pathTraceModeRoundTripHint",
@@ -60,6 +66,8 @@
],
"fr": [
"contacts_removeAutoAddNote",
"contacts_removeFailed",
"map_pathTraceAddMoreHint",
"map_pathTraceModeRoundTrip",
"map_pathTraceModeRoundTripHint",
@@ -80,6 +88,8 @@
],
"hu": [
"contacts_removeAutoAddNote",
"contacts_removeFailed",
"map_pathTraceAddMoreHint",
"map_pathTraceModeRoundTrip",
"map_pathTraceModeRoundTripHint",
@@ -100,6 +110,8 @@
],
"it": [
"contacts_removeAutoAddNote",
"contacts_removeFailed",
"map_pathTraceAddMoreHint",
"map_pathTraceModeRoundTrip",
"map_pathTraceModeRoundTripHint",
@@ -120,6 +132,8 @@
],
"ja": [
"contacts_removeAutoAddNote",
"contacts_removeFailed",
"map_pathTraceAddMoreHint",
"map_pathTraceModeRoundTrip",
"map_pathTraceModeRoundTripHint",
@@ -140,6 +154,8 @@
],
"ko": [
"contacts_removeAutoAddNote",
"contacts_removeFailed",
"map_pathTraceAddMoreHint",
"map_pathTraceModeRoundTrip",
"map_pathTraceModeRoundTripHint",
@@ -160,6 +176,8 @@
],
"nl": [
"contacts_removeAutoAddNote",
"contacts_removeFailed",
"map_pathTraceAddMoreHint",
"map_pathTraceModeRoundTrip",
"map_pathTraceModeRoundTripHint",
@@ -180,6 +198,8 @@
],
"pl": [
"contacts_removeAutoAddNote",
"contacts_removeFailed",
"map_pathTraceAddMoreHint",
"map_pathTraceModeRoundTrip",
"map_pathTraceModeRoundTripHint",
@@ -200,6 +220,8 @@
],
"pt": [
"contacts_removeAutoAddNote",
"contacts_removeFailed",
"map_pathTraceAddMoreHint",
"map_pathTraceModeRoundTrip",
"map_pathTraceModeRoundTripHint",
@@ -220,6 +242,8 @@
],
"ru": [
"contacts_removeAutoAddNote",
"contacts_removeFailed",
"map_pathTraceAddMoreHint",
"map_pathTraceModeRoundTrip",
"map_pathTraceModeRoundTripHint",
@@ -240,6 +264,8 @@
],
"sk": [
"contacts_removeAutoAddNote",
"contacts_removeFailed",
"map_pathTraceAddMoreHint",
"map_pathTraceModeRoundTrip",
"map_pathTraceModeRoundTripHint",
@@ -260,6 +286,8 @@
],
"sl": [
"contacts_removeAutoAddNote",
"contacts_removeFailed",
"map_pathTraceAddMoreHint",
"map_pathTraceModeRoundTrip",
"map_pathTraceModeRoundTripHint",
@@ -280,6 +308,8 @@
],
"sv": [
"contacts_removeAutoAddNote",
"contacts_removeFailed",
"map_pathTraceAddMoreHint",
"map_pathTraceModeRoundTrip",
"map_pathTraceModeRoundTripHint",
@@ -300,6 +330,8 @@
],
"uk": [
"contacts_removeAutoAddNote",
"contacts_removeFailed",
"map_pathTraceAddMoreHint",
"map_pathTraceModeRoundTrip",
"map_pathTraceModeRoundTripHint",
@@ -320,6 +352,8 @@
],
"zh": [
"contacts_removeAutoAddNote",
"contacts_removeFailed",
"map_pathTraceAddMoreHint",
"map_pathTraceModeRoundTrip",
"map_pathTraceModeRoundTripHint",