Trim an oversized list when enabling eviction
This commit is contained in:
1 parent
1dbb487fe7
commit
1323bf13b6
2 files changed
+17
-3
No files matched your search
@@ -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();
|
||||
|
||||
@@ -1269,6 +1269,9 @@ class _SettingsScreenState extends State<SettingsScreen> {
|
||||
await appSettingsService.setEvictDiscoveredContactsEnabled(
|
||||
evictDiscoveredContactsEnabled,
|
||||
);
|
||||
if (evictDiscoveredContactsEnabled) {
|
||||
await connector.trimDiscoveredContactsToLimit();
|
||||
}
|
||||
_sendSettings(
|
||||
connector,
|
||||
autoAddChat,
|
||||
|
||||
Reference in new issue
Block a user