From 76232c1ad75a0321ab8d5bb50adda8dde1b8514a Mon Sep 17 00:00:00 2001 From: ericz Date: Sun, 8 Nov 2026 03:15:32 +0100 Subject: [PATCH] clear contacts and channels before restoring. --- documentation/settings.md | 2 +- lib/l10n/app_en.arb | 2 +- lib/l10n/app_localizations.dart | 2 +- lib/l10n/app_localizations_bg.dart | 2 +- lib/l10n/app_localizations_de.dart | 2 +- lib/l10n/app_localizations_en.dart | 2 +- lib/l10n/app_localizations_es.dart | 2 +- lib/l10n/app_localizations_fr.dart | 2 +- lib/l10n/app_localizations_hu.dart | 2 +- lib/l10n/app_localizations_it.dart | 2 +- lib/l10n/app_localizations_ja.dart | 2 +- lib/l10n/app_localizations_ko.dart | 2 +- lib/l10n/app_localizations_nl.dart | 2 +- lib/l10n/app_localizations_pl.dart | 2 +- lib/l10n/app_localizations_pt.dart | 2 +- lib/l10n/app_localizations_ru.dart | 2 +- lib/l10n/app_localizations_sk.dart | 2 +- lib/l10n/app_localizations_sl.dart | 2 +- lib/l10n/app_localizations_sv.dart | 2 +- lib/l10n/app_localizations_uk.dart | 2 +- lib/l10n/app_localizations_zh.dart | 2 +- lib/screens/backup_restore_screen.dart | 2 + lib/services/backup_service.dart | 77 +++++++++++++++++++++----- 23 files changed, 85 insertions(+), 36 deletions(-) diff --git a/documentation/settings.md b/documentation/settings.md index 628fd35c..45d15b2b 100644 --- a/documentation/settings.md +++ b/documentation/settings.md @@ -212,7 +212,7 @@ The settings card links to **Backup & Restore** and **Export** subpages. ### Backup & Restore -Choose individual items to include in a backup or restore. Location settings include the saved coordinates, GPS enable state and interval, contact discovery options, and privacy/adverts settings. When importing, only items found in the selected file can be selected. Backups from the official MeshCore app are supported; those files do not include app preferences, groups, filters, or message history, so those choices are unavailable. A radio connection is required to restore radio settings and contact or channel lists. Contact and channel lists are restored to the connected radio; message history and groups are restored in the app. +Choose individual items to include in a backup or restore. Location settings include the saved coordinates, GPS enable state and interval, contact discovery options, and privacy/adverts settings. When importing, only items found in the selected file can be selected. Backups from the official MeshCore app are supported; those files do not include app preferences, groups, filters, or message history, so those choices are unavailable. A radio connection is required to restore radio settings and contact or channel lists. Restoring contacts or channels replaces the selected list on the radio, then reloads it from the device. If you also restore the radio identity, reconnect afterward to reload its data. Message history and groups are restored in the app. The private-key option is off by default because it changes the radio's identity. Select it only when you intend to restore that identity; the app asks for confirmation before proceeding. Some radios do not allow identity backup or restore, and the app will let you know if this feature is unavailable. diff --git a/lib/l10n/app_en.arb b/lib/l10n/app_en.arb index b345cc1f..909eceb0 100644 --- a/lib/l10n/app_en.arb +++ b/lib/l10n/app_en.arb @@ -234,7 +234,7 @@ "backupRestore_backup": "Create Backup", "backupRestore_restore": "Restore Selected", "backupRestore_backupComplete": "Backup file created.", - "backupRestore_restoreComplete": "Restore complete. Reconnect the radio to refresh its data.", + "backupRestore_restoreComplete": "Restore complete. Reconnect if you restored the radio identity.", "backupRestore_failed": "Backup operation failed: {error}", "@backupRestore_failed": { "placeholders": { diff --git a/lib/l10n/app_localizations.dart b/lib/l10n/app_localizations.dart index 6c42cc51..7728370c 100644 --- a/lib/l10n/app_localizations.dart +++ b/lib/l10n/app_localizations.dart @@ -1081,7 +1081,7 @@ abstract class AppLocalizations { /// No description provided for @backupRestore_restoreComplete. /// /// In en, this message translates to: - /// **'Restore complete. Reconnect the radio to refresh its data.'** + /// **'Restore complete. Reconnect if you restored the radio identity.'** String get backupRestore_restoreComplete; /// No description provided for @backupRestore_failed. diff --git a/lib/l10n/app_localizations_bg.dart b/lib/l10n/app_localizations_bg.dart index 1525fd45..0e1f606e 100644 --- a/lib/l10n/app_localizations_bg.dart +++ b/lib/l10n/app_localizations_bg.dart @@ -542,7 +542,7 @@ class AppLocalizationsBg extends AppLocalizations { @override String get backupRestore_restoreComplete => - 'Restore complete. Reconnect the radio to refresh its data.'; + 'Restore complete. Reconnect if you restored the radio identity.'; @override String backupRestore_failed(String error) { diff --git a/lib/l10n/app_localizations_de.dart b/lib/l10n/app_localizations_de.dart index 7a98cc9a..367660db 100644 --- a/lib/l10n/app_localizations_de.dart +++ b/lib/l10n/app_localizations_de.dart @@ -545,7 +545,7 @@ class AppLocalizationsDe extends AppLocalizations { @override String get backupRestore_restoreComplete => - 'Restore complete. Reconnect the radio to refresh its data.'; + 'Restore complete. Reconnect if you restored the radio identity.'; @override String backupRestore_failed(String error) { diff --git a/lib/l10n/app_localizations_en.dart b/lib/l10n/app_localizations_en.dart index 741c6a86..fb11ecfb 100644 --- a/lib/l10n/app_localizations_en.dart +++ b/lib/l10n/app_localizations_en.dart @@ -538,7 +538,7 @@ class AppLocalizationsEn extends AppLocalizations { @override String get backupRestore_restoreComplete => - 'Restore complete. Reconnect the radio to refresh its data.'; + 'Restore complete. Reconnect if you restored the radio identity.'; @override String backupRestore_failed(String error) { diff --git a/lib/l10n/app_localizations_es.dart b/lib/l10n/app_localizations_es.dart index 8249dd75..eede141c 100644 --- a/lib/l10n/app_localizations_es.dart +++ b/lib/l10n/app_localizations_es.dart @@ -543,7 +543,7 @@ class AppLocalizationsEs extends AppLocalizations { @override String get backupRestore_restoreComplete => - 'Restore complete. Reconnect the radio to refresh its data.'; + 'Restore complete. Reconnect if you restored the radio identity.'; @override String backupRestore_failed(String error) { diff --git a/lib/l10n/app_localizations_fr.dart b/lib/l10n/app_localizations_fr.dart index efae49c7..c0ffdabb 100644 --- a/lib/l10n/app_localizations_fr.dart +++ b/lib/l10n/app_localizations_fr.dart @@ -545,7 +545,7 @@ class AppLocalizationsFr extends AppLocalizations { @override String get backupRestore_restoreComplete => - 'Restore complete. Reconnect the radio to refresh its data.'; + 'Restore complete. Reconnect if you restored the radio identity.'; @override String backupRestore_failed(String error) { diff --git a/lib/l10n/app_localizations_hu.dart b/lib/l10n/app_localizations_hu.dart index 64420984..b6bdb2a2 100644 --- a/lib/l10n/app_localizations_hu.dart +++ b/lib/l10n/app_localizations_hu.dart @@ -541,7 +541,7 @@ class AppLocalizationsHu extends AppLocalizations { @override String get backupRestore_restoreComplete => - 'Restore complete. Reconnect the radio to refresh its data.'; + 'Restore complete. Reconnect if you restored the radio identity.'; @override String backupRestore_failed(String error) { diff --git a/lib/l10n/app_localizations_it.dart b/lib/l10n/app_localizations_it.dart index 998e97c0..e7d4f086 100644 --- a/lib/l10n/app_localizations_it.dart +++ b/lib/l10n/app_localizations_it.dart @@ -545,7 +545,7 @@ class AppLocalizationsIt extends AppLocalizations { @override String get backupRestore_restoreComplete => - 'Restore complete. Reconnect the radio to refresh its data.'; + 'Restore complete. Reconnect if you restored the radio identity.'; @override String backupRestore_failed(String error) { diff --git a/lib/l10n/app_localizations_ja.dart b/lib/l10n/app_localizations_ja.dart index 50eeb3d6..37673dd9 100644 --- a/lib/l10n/app_localizations_ja.dart +++ b/lib/l10n/app_localizations_ja.dart @@ -526,7 +526,7 @@ class AppLocalizationsJa extends AppLocalizations { @override String get backupRestore_restoreComplete => - 'Restore complete. Reconnect the radio to refresh its data.'; + 'Restore complete. Reconnect if you restored the radio identity.'; @override String backupRestore_failed(String error) { diff --git a/lib/l10n/app_localizations_ko.dart b/lib/l10n/app_localizations_ko.dart index 11acc5bc..7fb984ac 100644 --- a/lib/l10n/app_localizations_ko.dart +++ b/lib/l10n/app_localizations_ko.dart @@ -526,7 +526,7 @@ class AppLocalizationsKo extends AppLocalizations { @override String get backupRestore_restoreComplete => - 'Restore complete. Reconnect the radio to refresh its data.'; + 'Restore complete. Reconnect if you restored the radio identity.'; @override String backupRestore_failed(String error) { diff --git a/lib/l10n/app_localizations_nl.dart b/lib/l10n/app_localizations_nl.dart index 0da21c09..012ed113 100644 --- a/lib/l10n/app_localizations_nl.dart +++ b/lib/l10n/app_localizations_nl.dart @@ -541,7 +541,7 @@ class AppLocalizationsNl extends AppLocalizations { @override String get backupRestore_restoreComplete => - 'Restore complete. Reconnect the radio to refresh its data.'; + 'Restore complete. Reconnect if you restored the radio identity.'; @override String backupRestore_failed(String error) { diff --git a/lib/l10n/app_localizations_pl.dart b/lib/l10n/app_localizations_pl.dart index ab7bd293..effb0898 100644 --- a/lib/l10n/app_localizations_pl.dart +++ b/lib/l10n/app_localizations_pl.dart @@ -548,7 +548,7 @@ class AppLocalizationsPl extends AppLocalizations { @override String get backupRestore_restoreComplete => - 'Restore complete. Reconnect the radio to refresh its data.'; + 'Restore complete. Reconnect if you restored the radio identity.'; @override String backupRestore_failed(String error) { diff --git a/lib/l10n/app_localizations_pt.dart b/lib/l10n/app_localizations_pt.dart index 9f8f4943..6bfa701f 100644 --- a/lib/l10n/app_localizations_pt.dart +++ b/lib/l10n/app_localizations_pt.dart @@ -544,7 +544,7 @@ class AppLocalizationsPt extends AppLocalizations { @override String get backupRestore_restoreComplete => - 'Restore complete. Reconnect the radio to refresh its data.'; + 'Restore complete. Reconnect if you restored the radio identity.'; @override String backupRestore_failed(String error) { diff --git a/lib/l10n/app_localizations_ru.dart b/lib/l10n/app_localizations_ru.dart index c0534b20..626351fd 100644 --- a/lib/l10n/app_localizations_ru.dart +++ b/lib/l10n/app_localizations_ru.dart @@ -544,7 +544,7 @@ class AppLocalizationsRu extends AppLocalizations { @override String get backupRestore_restoreComplete => - 'Restore complete. Reconnect the radio to refresh its data.'; + 'Restore complete. Reconnect if you restored the radio identity.'; @override String backupRestore_failed(String error) { diff --git a/lib/l10n/app_localizations_sk.dart b/lib/l10n/app_localizations_sk.dart index 35263c5b..34c98ba8 100644 --- a/lib/l10n/app_localizations_sk.dart +++ b/lib/l10n/app_localizations_sk.dart @@ -544,7 +544,7 @@ class AppLocalizationsSk extends AppLocalizations { @override String get backupRestore_restoreComplete => - 'Restore complete. Reconnect the radio to refresh its data.'; + 'Restore complete. Reconnect if you restored the radio identity.'; @override String backupRestore_failed(String error) { diff --git a/lib/l10n/app_localizations_sl.dart b/lib/l10n/app_localizations_sl.dart index 95e03d47..c397c6a3 100644 --- a/lib/l10n/app_localizations_sl.dart +++ b/lib/l10n/app_localizations_sl.dart @@ -543,7 +543,7 @@ class AppLocalizationsSl extends AppLocalizations { @override String get backupRestore_restoreComplete => - 'Restore complete. Reconnect the radio to refresh its data.'; + 'Restore complete. Reconnect if you restored the radio identity.'; @override String backupRestore_failed(String error) { diff --git a/lib/l10n/app_localizations_sv.dart b/lib/l10n/app_localizations_sv.dart index 9be4b142..9ed2b494 100644 --- a/lib/l10n/app_localizations_sv.dart +++ b/lib/l10n/app_localizations_sv.dart @@ -539,7 +539,7 @@ class AppLocalizationsSv extends AppLocalizations { @override String get backupRestore_restoreComplete => - 'Restore complete. Reconnect the radio to refresh its data.'; + 'Restore complete. Reconnect if you restored the radio identity.'; @override String backupRestore_failed(String error) { diff --git a/lib/l10n/app_localizations_uk.dart b/lib/l10n/app_localizations_uk.dart index a87c8d27..fd931301 100644 --- a/lib/l10n/app_localizations_uk.dart +++ b/lib/l10n/app_localizations_uk.dart @@ -543,7 +543,7 @@ class AppLocalizationsUk extends AppLocalizations { @override String get backupRestore_restoreComplete => - 'Restore complete. Reconnect the radio to refresh its data.'; + 'Restore complete. Reconnect if you restored the radio identity.'; @override String backupRestore_failed(String error) { diff --git a/lib/l10n/app_localizations_zh.dart b/lib/l10n/app_localizations_zh.dart index 5c09fbb1..6edd82ac 100644 --- a/lib/l10n/app_localizations_zh.dart +++ b/lib/l10n/app_localizations_zh.dart @@ -520,7 +520,7 @@ class AppLocalizationsZh extends AppLocalizations { @override String get backupRestore_restoreComplete => - 'Restore complete. Reconnect the radio to refresh its data.'; + 'Restore complete. Reconnect if you restored the radio identity.'; @override String backupRestore_failed(String error) { diff --git a/lib/screens/backup_restore_screen.dart b/lib/screens/backup_restore_screen.dart index f6d50833..e9de609f 100644 --- a/lib/screens/backup_restore_screen.dart +++ b/lib/screens/backup_restore_screen.dart @@ -46,6 +46,8 @@ class _BackupRestoreScreenState extends State { BackupItem.location, BackupItem.contactSettings, BackupItem.privacySettings, + BackupItem.contacts, + BackupItem.channels, }; static const Set _defaultUncheckedOnRestore = { ..._defaultUncheckedItems, diff --git a/lib/services/backup_service.dart b/lib/services/backup_service.dart index 4eb205a2..e24ca0c5 100644 --- a/lib/services/backup_service.dart +++ b/lib/services/backup_service.dart @@ -1,3 +1,4 @@ +import 'dart:async'; import 'dart:convert'; import 'dart:math'; import 'dart:typed_data'; @@ -6,8 +7,8 @@ import 'package:cryptography/cryptography.dart'; import '../connector/meshcore_connector.dart'; import '../connector/meshcore_protocol.dart'; +import '../connector/pending_command_replies.dart'; import '../models/app_settings.dart'; -import '../models/contact.dart'; import 'app_settings_service.dart'; import '../storage/channel_store.dart'; import '../storage/contact_store.dart'; @@ -302,6 +303,8 @@ class BackupService { final restoreChannels = selectedItems.any( (item) => _sectionForItem(item) == BackupSection.channels, ); + final replaceContacts = selectedItems.contains(BackupItem.contacts); + final replaceChannels = selectedItems.contains(BackupItem.channels); if (importingIdentity && (sourcePublicKey.isEmpty || _normalizePublicKey(node?['publicKey'] as String? ?? '') != @@ -315,6 +318,9 @@ class BackupService { throw StateError('Wait for radio identity to finish loading'); } } + if (replaceContacts) await _clearRadioContacts(connector); + if (replaceChannels) await _clearRadioChannels(connector); + if (node != null) { final name = node['name']; if (selectedItems.contains(BackupItem.deviceName) && name is String) { @@ -474,12 +480,61 @@ class BackupService { ); } } - if (selectedItems.contains(BackupItem.contacts)) { + if (replaceContacts) { await _restoreRadioContacts(connector, destinationPublicKey); } - if (selectedItems.contains(BackupItem.channels)) { + if (replaceChannels) { await _restoreRadioChannels(connector, destinationPublicKey); } + if (!importingIdentity) { + if (replaceContacts) await _refreshContactsFromRadio(connector); + if (replaceChannels) await connector.getChannels(force: true); + } + } + + Future _clearRadioContacts(MeshCoreConnector connector) async { + await _refreshContactsFromRadio(connector); + for (final contact in connector.contacts.toList()) { + try { + await connector.sendFrame( + buildRemoveContactFrame(contact.publicKey), + waitForGenericAck: true, + ); + } on FirmwareCommandException { + // A contact removed on the radio since its last sync is already clear. + } + } + } + + Future _clearRadioChannels(MeshCoreConnector connector) async { + await connector.getChannels(force: true); + for (final channel in connector.channels) { + if (channel.isEmpty) continue; + await connector.sendFrame( + buildSetChannelFrame(channel.index, '', Uint8List(16)), + waitForGenericAck: true, + ); + } + } + + Future _refreshContactsFromRadio(MeshCoreConnector connector) async { + final refreshed = Completer(); + void checkComplete() { + if (connector.hasLoadedContacts && + !connector.isLoadingContacts && + !refreshed.isCompleted) { + refreshed.complete(); + } + } + + connector.addListener(checkComplete); + try { + await connector.getContacts(); + checkComplete(); + await refreshed.future.timeout(const Duration(seconds: 30)); + } finally { + connector.removeListener(checkComplete); + } } Future _restoreRadioContacts( @@ -492,22 +547,14 @@ class BackupService { if (contact.publicKeyHex.toUpperCase() == publicKey.toUpperCase()) { continue; } - Contact? existing; - for (final candidate in connector.contacts) { - if (candidate.publicKeyHex == contact.publicKeyHex) { - existing = candidate; - break; - } - } - final pathLengthValue = existing?.pathLength ?? contact.pathLength; - final pathHashWidth = existing?.pathHashWidth ?? contact.pathHashWidth; - final pathLength = pathLengthValue < 0 + final pathLength = contact.pathLength < 0 ? 0xff - : (pathLengthValue & 0x3f) | (((pathHashWidth - 1) & 0x03) << 6); + : (contact.pathLength & 0x3f) | + (((contact.pathHashWidth - 1) & 0x03) << 6); await connector.sendFrame( buildUpdateContactPathFrame( contact.publicKey, - existing?.path ?? contact.path, + contact.path, pathLength, type: contact.type, flags: contact.flags,