1 Commits

Author SHA1 Message Date
559e0fea36 Implement some form of caching entries while scrolling 2025-06-09 08:49:48 +02:00
2 changed files with 1049 additions and 206 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -348,10 +348,14 @@ Future<Note> createNote(String content) async {
} }
} }
final mostFrequentLetter = // Handle the case where there are no alphanumeric characters
letterFrequency.entries.reduce((a, b) => a.value > b.value ? a : b).key; String mostFrequentLetter = 'a'; // Default value
final mostFrequentLetterCount = double mostFrequentLetterCount = 0.0; // Default value
letterFrequency[mostFrequentLetter]! / trimmedContent.length;
if (letterFrequency.isNotEmpty) {
mostFrequentLetter = letterFrequency.entries.reduce((a, b) => a.value > b.value ? a : b).key;
mostFrequentLetterCount = letterFrequency[mostFrequentLetter]! / (trimmedContent.length > 0 ? trimmedContent.length : 1);
}
final document = { final document = {
'id': generateRandomString(32), 'id': generateRandomString(32),
@@ -431,10 +435,14 @@ Future<void> updateNote(Note note) async {
} }
} }
final mostFrequentLetter = // Handle the case where there are no alphanumeric characters
letterFrequency.entries.reduce((a, b) => a.value > b.value ? a : b).key; String mostFrequentLetter = 'a'; // Default value
final mostFrequentLetterRatio = double mostFrequentLetterRatio = 0.0; // Default value
letterFrequency[mostFrequentLetter]! / trimmedContent.length;
if (letterFrequency.isNotEmpty) {
mostFrequentLetter = letterFrequency.entries.reduce((a, b) => a.value > b.value ? a : b).key;
mostFrequentLetterRatio = letterFrequency[mostFrequentLetter]! / (trimmedContent.length > 0 ? trimmedContent.length : 1);
}
final document = { final document = {
'id': note.id, 'id': note.id,
@@ -527,3 +535,101 @@ Future<Scratch> createScratch(String content) async {
content: document['content'] as String, content: document['content'] as String,
); );
} }
Future<List<Note>> getNotesBefore(int epochTime, {int limit = 50}) async {
final endpoint = await _getEndpoint();
final headers = await _getHeaders();
final searchCondition = MeilisearchQuery(
q: '',
filter: 'date < $epochTime',
sort: ['date:desc'],
limit: limit,
);
final response = await http.post(
Uri.parse('$endpoint/indexes/$noteIndex/search'),
headers: headers,
body: jsonEncode(searchCondition.toJson()),
);
if (response.statusCode != 200) {
throw Exception(
'Failed to get notes before timestamp, backend responded with ${response.statusCode}',
);
}
final responseJson = MeilisearchResponse.fromJson(jsonDecode(response.body));
return responseJson.hits
.map(
(hit) => Note(
id: hit['id'] as String,
epochTime: hit['date'] as int,
content: hit['content'] as String,
),
)
.toList();
}
Future<List<Note>> getNotesAfter(int epochTime, {int limit = 50}) async {
final endpoint = await _getEndpoint();
final headers = await _getHeaders();
final searchCondition = MeilisearchQuery(
q: '',
filter: 'date > $epochTime',
sort: ['date:asc'],
limit: limit,
);
final response = await http.post(
Uri.parse('$endpoint/indexes/$noteIndex/search'),
headers: headers,
body: jsonEncode(searchCondition.toJson()),
);
if (response.statusCode != 200) {
throw Exception(
'Failed to get notes after timestamp, backend responded with ${response.statusCode}',
);
}
final responseJson = MeilisearchResponse.fromJson(jsonDecode(response.body));
return responseJson.hits
.map(
(hit) => Note(
id: hit['id'] as String,
epochTime: hit['date'] as int,
content: hit['content'] as String,
),
)
.toList();
}
Future<Duration> getPopupInterval() async {
final value = await getSetting('popupInterval');
if (value == null) {
return const Duration(minutes: 20);
}
return Duration(minutes: int.parse(value));
}
Future<void> setPopupInterval(Duration interval) async {
await setSetting('popupInterval', interval.inMinutes.toString());
}
Future<int> getCacheSizeBefore() async {
final value = await getSetting('cacheSizeBefore');
if (value == null) {
return 50; // Default value
}
return int.parse(value);
}
Future<void> setCacheSizeBefore(int size) async {
await setSetting('cacheSizeBefore', size.toString());
}
Future<int> getCacheSizeAfter() async {
final value = await getSetting('cacheSizeAfter');
if (value == null) {
return 50; // Default value
}
return int.parse(value);
}
Future<void> setCacheSizeAfter(int size) async {
await setSetting('cacheSizeAfter', size.toString());
}