class BookGenre(MenuScreen): """ Bokk genre screen """ def __init__(self, util, listeners, go_book_by_genre, genres, base_url, voice_assistant, d): self.util = util self.go_book_by_genre = go_book_by_genre self.config = util.config self.genres_list = genres self.base_url = base_url self.factory = Factory(util) MenuScreen.__init__(self, util, listeners, MENU_ROWS, MENU_COLUMNS, voice_assistant, d, self.turn_page) self.total_pages = math.ceil(len(genres) / PAGE_SIZE) self.title = self.config[LABELS][KEY_CHOOSE_GENRE] m = self.factory.create_book_genre_menu_button self.genre_menu = MultiPageMenu(util, self.next_page, self.previous_page, self.set_title, self.reset_title, self.go_to_page, m, MENU_ROWS, MENU_COLUMNS, None, (0, 0, 0), self.menu_layout) self.set_menu(self.genre_menu) self.turn_page() def turn_page(self): """ Turn book genre page """ start = (self.current_page - 1) * PAGE_SIZE end = self.current_page * PAGE_SIZE page = self.genres_list[start:end] self.genres_dict = self.factory.create_book_genre_items( page, self.base_url) self.genre_menu.set_items(self.genres_dict, 0, self.go_book_by_genre, False) self.genre_menu.align_labels(ALIGN_LEFT) self.genre_menu.select_by_index(0) self.genre_menu.clean_draw_update() self.navigator.left_button.change_label(str(self.current_page - 1)) self.navigator.right_button.change_label( str(self.total_pages - self.current_page)) self.set_title(self.current_page) def add_screen_observers(self, update_observer, redraw_observer): """ Add screen observers :param update_observer: observer for updating the screen :param redraw_observer: observer to redraw the whole screen """ self.navigator.add_observers(update_observer, redraw_observer) self.genre_menu.add_menu_observers(update_observer, redraw_observer, release=False)
class BookAuthor(MenuScreen): """ Authors screen """ def __init__(self, util, listeners, ch, f, go_author, parser, base_url, voice_assistant, d): """ Initializer :param util: utility object :param listeners: screen listeners :param ch: selected character :param f: selected filter :param go_authors: callback :param parser: authors parser :param base_url: url :param d: dictionary with menu button flags """ self.util = util self.factory = Factory(util) self.base_url = base_url self.config = util.config self.parser = parser self.current_author_char = ch self.current_author_filter = f self.go_author = go_author self.author_cache = {} self.title = self.config[LABELS][KEY_AUTHORS] MenuScreen.__init__(self, util, listeners, MENU_ROWS, MENU_COLUMNS, voice_assistant, d, self.turn_page) m = self.factory.create_book_author_menu_button self.authors_menu = MultiPageMenu(util, self.next_page, self.previous_page, self.set_title, self.reset_title, self.go_to_page, m, MENU_ROWS, MENU_COLUMNS, None, (0, 0, 0), self.menu_layout) self.set_menu(self.authors_menu) def set_current(self, ch=None, f=None): """ Apply selected character and filter :param ch: selected character :param f: selected filter """ if not ch and not f: return self.set_loading(self.config[LABELS][KEY_AUTHORS]) self.total_pages = 0 self.current_page = 1 self.current_author_char = ch self.current_author_filter = f try: self.author_cache[ch] except: self.get_authors() self.turn_page() self.reset_loading() def get_authors(self): """ Get authors from parser """ self.parser.author_parser.current_author_char = self.current_author_char self.parser.author_parser.url = self.base_url authors = self.parser.get_authors() if authors: self.author_cache[self.current_author_char] = authors def turn_page(self): """ Turn authors page """ self.authors_menu.set_items({}, 0, self.go_author) filtered_authors = [] for a in self.author_cache[self.current_author_char]: if self.current_author_filter: if a[AUTHOR_NAME].startswith(self.current_author_filter): filtered_authors.append(a) else: filtered_authors.append(a) start = (self.current_page - 1) * PAGE_SIZE end = self.current_page * PAGE_SIZE page = filtered_authors[start : end] self.author_dict = self.factory.create_book_author_items(page) self.authors_menu.set_items(self.author_dict, 0, self.go_author, False) self.authors_menu.align_labels(ALIGN_LEFT) self.authors_menu.select_by_index(0) self.authors_menu.clean_draw_update() self.total_pages = math.ceil(len(filtered_authors) / PAGE_SIZE) left = str(self.current_page - 1) if self.total_pages == 0: right = "0" else: right = str(self.total_pages - self.current_page) self.navigator.left_button.change_label(left) self.navigator.right_button.change_label(right) self.set_title(self.current_page) def add_screen_observers(self, update_observer, redraw_observer): """ Add screen observers :param update_observer: observer for updating the screen :param redraw_observer: observer to redraw the whole screen """ self.navigator.add_observers(update_observer, redraw_observer) self.authors_menu.add_menu_loaded_listener(redraw_observer) self.authors_menu.add_menu_observers(update_observer, redraw_observer, release=False)