def __populate(self): """ Populate searching items in db based on text entry current text """ self.__clear() self.__header_stack.set_visible_child(self.__spinner) self.__spinner.start() self.__history = [] search_items = [self.__current_search] for item in self.__current_search.split(): if len(item) >= 3 and item not in search_items: search_items.append(item) search = Search() search.get(search_items, self.__cancellable, callback=(self.__on_search_get, ))
def __populate(self): """ Populate searching items in db based on text entry current text """ self.__cancellable.reset() self.__header_stack.set_visible_child(self.__spinner) self.__spinner.start() self.__history = [] if self.__current_search: search = Search() search.get(self.__current_search, self.__cancellable, callback=(self.__on_search_get, )) else: self.__stack.set_visible_child_name("placeholder") self.__set_default_placeholder() self.__header_stack.set_visible_child(self.__new_button) GLib.idle_add(self.__spinner.stop)
class SearchView(View, Gtk.Bin, SignalsHelper): """ View for searching albums/tracks """ @signals_map def __init__(self, initial_search, view_type): """ Init Popover @param initial_search as str """ View.__init__( self, StorageType.COLLECTION | StorageType.SAVED | StorageType.SEARCH | StorageType.EPHEMERAL | StorageType.SPOTIFY_NEW_RELEASES | StorageType.SPOTIFY_SIMILARS, ViewType.SEARCH | ViewType.SCROLLED | ViewType.OVERLAY) Gtk.Bin.__init__(self) self.__timeout_id = None self.__current_search = "" self.__search = Search() self.__search.set_web_search( App().settings.get_value("web-search").get_string()) self.__cancellable = Gio.Cancellable() self._empty_message = _("Search for artists, albums and tracks") self._empty_icon_name = "edit-find-symbolic" self.__cancellable = Gio.Cancellable() self.__banner = SearchBannerWidget() self.__banner.show() self.__stack = SearchStack(self.storage_type) self.__stack.show() self.add_widget(self.__stack, self.__banner) self.__banner.entry.connect("changed", self._on_search_changed) self.show_placeholder(True, _("Search for artists, albums and tracks")) self.set_search(initial_search) return [(self.__search, "match-artist", "_on_match_artist"), (self.__search, "match-album", "_on_match_album"), (self.__search, "match-track", "_on_match_track"), (self.__search, "finished", "_on_search_finished"), (App().settings, "changed::web-search", "_on_web_search_changed")] def populate(self): """ Populate search in db based on text entry current text """ self.cancel() self.__stack.new_current_child() if len(self.__current_search) > 1: self.__banner.spinner.start() current_search = self.__current_search.lower() self.__search.get(current_search, self.__cancellable) else: self.show_placeholder(True, _("Search for artists, albums and tracks")) self.__banner.spinner.stop() def set_search(self, search): """ Set search text @param search as str """ self.__banner.entry.set_text(search) self.__banner.entry.grab_focus() def grab_focus(self): """ Make search entry grab focus """ self.__banner.entry.grab_focus() def cancel(self): """ Cancel current search and replace cancellable """ self.__cancellable.cancel() self.__cancellable = Gio.Cancellable() @property def args(self): """ Get default args for __class__ @return {} """ search = self.__banner.entry.get_text().strip() return {"initial_search": search, "view_type": self.view_type} ####################### # PROTECTED # ####################### def _on_map(self, widget): """ Disable shortcuts and update buttons @param widget as Gtk.Widget """ View._on_map(self, widget) App().enable_special_shortcuts(False) def _on_unmap(self, widget): """ Cancel current loading and enable shortcuts @param widget as Gtk.Widget """ View._on_unmap(self, widget) App().enable_special_shortcuts(True) self.cancel() self.__banner.spinner.stop() def _on_match_artist(self, search, artist_id, storage_type): """ Add a new artist to view @param search as *Search @param artist_id as int @param storage_type as StorageType """ if storage_type & StorageType.SEARCH: self.__stack.current_child.artists_line_view.show() self.__stack.current_child.artists_line_view.add_value(artist_id) self.show_placeholder(False) def _on_match_album(self, search, album_id, storage_type): """ Add a new album to view @param search as *Search @param artist_id as int @param storage_type as StorageType """ if storage_type & StorageType.SEARCH: artist_match = False album = Album(album_id) if album.artists: artist = sql_escape(album.artists[0]) search = sql_escape(self.__current_search) artist_match = artist.find(search) != -1 if artist_match: self._on_match_artist(search, album.artist_ids[0], storage_type) else: self.__stack.current_child.albums_line_view.show() self.__stack.current_child.albums_line_view.add_value(album) self.show_placeholder(False) def _on_match_track(self, search, track_id, storage_type): """ Add a new track to view @param search as *Search @param track_id as int @param storage_type as StorageType """ if storage_type & StorageType.SEARCH: track = Track(track_id) self.__stack.current_child.search_tracks_view.show() self.__stack.current_child.search_tracks_view.append_row(track) self.show_placeholder(False) def _on_search_finished(self, search_handler, last): """ Stop spinner and show placeholder if not result @param search_handler as LocalSearch/SpotifySearch @param last as bool """ tracks_len = len( self.__stack.current_child.search_tracks_view.children) albums_len = len(self.__stack.current_child.albums_line_view.children) artists_len = len( self.__stack.current_child.artists_line_view.children) empty = albums_len == 0 and tracks_len == 0 and artists_len == 0 self.__stack.set_visible_child(self.__stack.current_child) if last: self.__banner.spinner.stop() if empty: self.show_placeholder(True, _("No results for this search")) def _on_web_search_changed(self, settings, value): """ Show/hide labels @param settings as Gio.Settings @param value as GLib.Variant """ self.__search.set_web_search( App().settings.get_value("web-search").get_string()) ####################### # PRIVATE # ####################### def __set_no_result_placeholder(self): """ Set placeholder for no result """ self.__placeholder.set_text() def _on_search_changed(self, widget): """ Timeout filtering @param widget as Gtk.TextEntry """ if self.__timeout_id is not None: GLib.source_remove(self.__timeout_id) self.__timeout_id = GLib.timeout_add(500, self.__on_search_changed_timeout) def __on_search_changed_timeout(self): """ Populate widget """ self.__timeout_id = None new_search = self.__banner.entry.get_text().strip() if self.__current_search != new_search: self.__current_search = new_search self.populate() def __on_button_clicked(self, button): """ Reload search for current button @param button as Gtk.RadioButton """ if button.get_active(): self.__current_search = self.__banner.entry.get_text().strip() if self.__current_search: self.populate()