Skip to content

Commit

Permalink
fix: isolate in findBooks
Browse files Browse the repository at this point in the history
  • Loading branch information
Sivan22 committed Nov 21, 2024
1 parent 44bbfad commit bae3196
Showing 1 changed file with 21 additions and 6 deletions.
27 changes: 21 additions & 6 deletions lib/models/app_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,8 @@ class AppModel with ChangeNotifier {
if (showHebrewBooks.value) {
books += await hebrewBooks;
}

// First filter the books
var filteredBooks = books.where((book) {
final title = book.title.toLowerCase();
return queryWords.every((word) => title.contains(word));
Expand All @@ -458,15 +460,28 @@ class AppModel with ChangeNotifier {
.toList();
}

return Isolate.run(() {
filteredBooks.sort((a, b) {
final scoreA = ratio(query, a.title);
final scoreB = ratio(query, b.title);
// Create a simple data structure that can be sent to isolate
final searchData =
filteredBooks.asMap().map((index, book) => MapEntry(index, {
'index': index,
'title': book.title,
}));

// Sort using isolate with the simplified data
final sortedIndices = await Isolate.run(() {
final entries = searchData.entries.toList();
entries.sort((a, b) {
final scoreA = ratio(query, a.value['title'] as String);
final scoreB = ratio(query, b.value['title'] as String);
return scoreB.compareTo(scoreA);
});

return filteredBooks;
return entries.map((e) => e.value['index'] as int).toList();
});

// Reorder the original filtered books based on the sorted indices
final sortedBooks =
sortedIndices.map((index) => filteredBooks[index]).toList();
return sortedBooks;
}

Future<void> createRefsFromLibrary(int startIndex) async {
Expand Down

0 comments on commit bae3196

Please sign in to comment.