diff --git a/lib/connector/meshcore_connector.dart b/lib/connector/meshcore_connector.dart index e6969d99..6db7f11e 100644 --- a/lib/connector/meshcore_connector.dart +++ b/lib/connector/meshcore_connector.dart @@ -1112,9 +1112,7 @@ class MeshCoreConnector extends ChangeNotifier { // Trim a previously-saved oversized list down to the freshest entries so a // device that grew unbounded before the cap existed recovers on load. if (_appSettingsService?.settings.evictDiscoveredContactsEnabled == true && - cached.length > _maxDiscoveredContacts) { - cached.sort((a, b) => b.lastSeen.compareTo(a.lastSeen)); - cached.removeRange(_maxDiscoveredContacts, cached.length); + _trimDiscoveredContactsToLimit(cached)) { unawaited(_discoveryContactStore.saveContacts(cached)); } _discoveredContacts @@ -1122,6 +1120,19 @@ class MeshCoreConnector extends ChangeNotifier { ..addAll(cached); } + bool _trimDiscoveredContactsToLimit(List contacts) { + if (contacts.length <= _maxDiscoveredContacts) return false; + contacts.sort((a, b) => b.lastSeen.compareTo(a.lastSeen)); + contacts.removeRange(_maxDiscoveredContacts, contacts.length); + return true; + } + + Future trimDiscoveredContactsToLimit() async { + if (!_trimDiscoveredContactsToLimit(_discoveredContacts)) return; + await _persistDiscoveredContacts(); + notifyListeners(); + } + Future loadChannelSettings({int? maxChannels}) async { _channelSmazEnabled.clear(); _channelCyr2LatEnabled.clear(); diff --git a/lib/screens/settings_screen.dart b/lib/screens/settings_screen.dart index 838ebfb4..4e2cb8ff 100644 --- a/lib/screens/settings_screen.dart +++ b/lib/screens/settings_screen.dart @@ -1269,6 +1269,9 @@ class _SettingsScreenState extends State { await appSettingsService.setEvictDiscoveredContactsEnabled( evictDiscoveredContactsEnabled, ); + if (evictDiscoveredContactsEnabled) { + await connector.trimDiscoveredContactsToLimit(); + } _sendSettings( connector, autoAddChat,