Trim an oversized list when enabling eviction

This commit is contained in:
ericz committed 2026-09-08 18:05:11 +02:00
1 parent 1dbb487fe7
commit 1323bf13b6
2 files changed
+17 -3

No files matched your search

+14 -3
View File
@@ -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<Contact> contacts) {
if (contacts.length <= _maxDiscoveredContacts) return false;
contacts.sort((a, b) => b.lastSeen.compareTo(a.lastSeen));
contacts.removeRange(_maxDiscoveredContacts, contacts.length);
return true;
}
Future<void> trimDiscoveredContactsToLimit() async {
if (!_trimDiscoveredContactsToLimit(_discoveredContacts)) return;
await _persistDiscoveredContacts();
notifyListeners();
}
Future<void> loadChannelSettings({int? maxChannels}) async {
_channelSmazEnabled.clear();
_channelCyr2LatEnabled.clear();
+3
View File
@@ -1269,6 +1269,9 @@ class _SettingsScreenState extends State<SettingsScreen> {
await appSettingsService.setEvictDiscoveredContactsEnabled(
evictDiscoveredContactsEnabled,
);
if (evictDiscoveredContactsEnabled) {
await connector.trimDiscoveredContactsToLimit();
}
_sendSettings(
connector,
autoAddChat,