Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve search #824

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions cozy/ui/search_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,19 @@ def __init__(self):
def _connect_view_model(self):
self.view_model.bind_to("search_open", self._on_search_open_changed)

def search(self, user_search: str):
def search(self, query: str):
# we need the main context to call methods in the main thread after the search is finished
main_context = GLib.MainContext.default()

query_lower = query.lower()

books = list({
book
for book
in self.view_model.books
if user_search.lower() in book.name.lower()
or user_search.lower() in book.author.lower()
or user_search.lower() in book.reader.lower()
if query_lower in book.name.lower()
or query_lower in book.author.lower()
or query_lower in book.reader.lower()
})
books = sorted(books, key=lambda book: book.name.lower())
if self.search_thread_stop.is_set():
Expand All @@ -74,7 +76,7 @@ def search(self, user_search: str):
author
for author
in self.view_model.authors
if user_search.lower() in author.lower()
if query_lower in author.lower()
})
if self.search_thread_stop.is_set():
return
Expand All @@ -85,14 +87,14 @@ def search(self, user_search: str):
reader
for reader
in self.view_model.readers
if user_search.lower() in reader.lower()
if query_lower in reader.lower()
})
if self.search_thread_stop.is_set():
return
main_context.invoke_full(
GLib.PRIORITY_DEFAULT, self.__on_reader_search_finished, readers)

if len(readers) < 1 and len(authors) < 1 and len(books) < 1:
if not any((readers, authors, books)):
main_context.invoke_full(
GLib.PRIORITY_DEFAULT, self.stack.set_visible_child_name, "nothing")

Expand Down