From a4e8c31a1687937b1b981e5d6cbc9f929feaca21 Mon Sep 17 00:00:00 2001 From: pelgraine <140762863+pelgraine@users.noreply.github.com> Date: Sun, 12 Apr 2026 20:42:45 +1000 Subject: [PATCH] fiiiiixed the contacts add and delete and toggle fav issues for real this time --- examples/companion_radio/MyMesh.cpp | 11 ++++++++++- examples/companion_radio/MyMesh.h | 4 ++++ examples/companion_radio/main.cpp | 2 +- 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/examples/companion_radio/MyMesh.cpp b/examples/companion_radio/MyMesh.cpp index c28f0ac6..d18c77c5 100644 --- a/examples/companion_radio/MyMesh.cpp +++ b/examples/companion_radio/MyMesh.cpp @@ -315,6 +315,7 @@ bool MyMesh::isAutoAddEnabled() const { } bool MyMesh::shouldAutoAddContactType(uint8_t contact_type) const { + if (_forceNextImport) return true; // explicit user add from Last Heard / Discovery if ((_prefs.manual_add_contacts & 1) == 0) { return true; } @@ -360,6 +361,7 @@ void MyMesh::onContactsFull() { } void MyMesh::onDiscoveredContact(ContactInfo &contact, bool is_new, uint8_t path_len, const uint8_t* path) { + _forceNextImport = false; // clear force-add flag (set by forceImportContact) if (_serial->isConnected()) { if (is_new) { writeContactRespFrame(PUSH_CODE_NEW_ADVERT, contact); @@ -3259,6 +3261,13 @@ void MyMesh::stopDiscovery() { _discoveryActive = false; } +bool MyMesh::forceImportContact(const uint8_t* blob, uint8_t len) { + _forceNextImport = true; + bool ok = importContact(blob, len); + if (!ok) _forceNextImport = false; // clear if importContact failed (no loopback queued) + return ok; +} + bool MyMesh::addDiscoveredToContacts(int idx) { if (idx < 0 || idx >= _discoveredCount) return false; if (_discovered[idx].already_in_contacts) return true; // already there @@ -3267,7 +3276,7 @@ bool MyMesh::addDiscoveredToContacts(int idx) { uint8_t buf[256]; int plen = getBlobByKey(_discovered[idx].contact.id.pub_key, PUB_KEY_SIZE, buf); if (plen > 0) { - bool ok = importContact(buf, (uint8_t)plen); + bool ok = forceImportContact(buf, (uint8_t)plen); if (ok) { _discovered[idx].already_in_contacts = true; dirty_contacts_expiry = futureMillis(LAZY_CONTACTS_WRITE_DELAY); diff --git a/examples/companion_radio/MyMesh.h b/examples/companion_radio/MyMesh.h index e30778c9..a477c503 100644 --- a/examples/companion_radio/MyMesh.h +++ b/examples/companion_radio/MyMesh.h @@ -131,6 +131,9 @@ public: int getContactBlob(const uint8_t key[], int key_len, uint8_t dest_buf[]) { return getBlobByKey(key, key_len, dest_buf); } + // Force-add a contact from a raw advert blob, bypassing auto-add settings. + // Used by Last Heard and Discovery when the user explicitly selects a node to add. + bool forceImportContact(const uint8_t* blob, uint8_t len); // Queue a sent channel message for BLE app sync void queueSentChannelMessage(uint8_t channel_idx, uint32_t timestamp, const char* sender, const char* text); @@ -261,6 +264,7 @@ private: NodePrefs _prefs; VoiceRawHandler _voiceHandler = nullptr; VoiceEnvelopeHandler _voiceEnvHandler = nullptr; + mutable bool _forceNextImport = false; bool _deferSaves = false; uint32_t pending_login; uint32_t pending_status; diff --git a/examples/companion_radio/main.cpp b/examples/companion_radio/main.cpp index bbdfc744..cd94e8dd 100644 --- a/examples/companion_radio/main.cpp +++ b/examples/companion_radio/main.cpp @@ -1111,7 +1111,7 @@ static void lastHeardToggleContact() { uint8_t blob[256]; int blobLen = the_mesh.getContactBlob(entry->pubkey_prefix, 8, blob); if (blobLen > 0) { - the_mesh.importContact(blob, blobLen); + the_mesh.forceImportContact(blob, blobLen); the_mesh.scheduleLazyContactSave(); char alertBuf[40]; snprintf(alertBuf, sizeof(alertBuf), "Added: %s", entry->name);