class RadioGroupScreen(MenuScreen): """ Radio group screen """ def __init__(self, util, listeners, voice_assistant): self.util = util self.config = util.config self.groups_list = self.util.get_stations_folders() self.factory = Factory(util) d = [MENU_ROWS, MENU_COLUMNS] MenuScreen.__init__(self, util, listeners, MENU_ROWS, MENU_COLUMNS, voice_assistant, d, self.turn_page, page_in_title=False) self.total_pages = math.ceil(len(self.groups_list) / PAGE_SIZE) self.title = util.get_stations_top_folder() m = self.create_genre_menu_button label_area = ( (self.menu_layout.h / MENU_ROWS) / 100) * (100 - ICON_AREA) font_size = int((label_area / 100) * FONT_HEIGHT) self.groups_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, 0), self.menu_layout, align=ALIGN_CENTER, font_size=font_size) self.groups_menu.add_listener(listeners[KEY_GENRE]) self.set_menu(self.groups_menu) color_dark_light = self.config[COLORS][COLOR_DARK_LIGHT] self.navigator = RadioGroupNavigator(self.util, self.layout.BOTTOM, listeners, color_dark_light, self.total_pages) self.add_component(self.navigator) current_name = self.get_current_group_name() if current_name == None: self.current_page = 1 else: try: current_group_index = self.groups_list.index(current_name) self.current_page = int(current_group_index / PAGE_SIZE) + 1 except: current_group_index = 0 self.turn_page() def create_genre_menu_button(self, s, constr, action, scale, font_size): """ Create Genre Menu button :param s: button state :param constr: scaling constraints :param action: button event listener :param scale: True - scale images, False - don't scale images :return: genre menu button """ s.padding = BUTTON_PADDING s.image_area_percent = ICON_AREA s.fixed_height = font_size s.v_align = CENTER return self.factory.create_menu_button(s, constr, action, scale, font_size=font_size) def get_current_group_name(self): key = STATIONS + "." + self.config[CURRENT][LANGUAGE] name = None try: name = self.config[key][CURRENT_STATIONS] except: pass return name def get_page(self): start = (self.current_page - 1) * PAGE_SIZE end = self.current_page * PAGE_SIZE tmp_layout = self.groups_menu.get_layout(self.groups_list) button_rect = tmp_layout.constraints[0] image_box = self.factory.get_icon_bounding_box(button_rect, ICON_LOCATION, ICON_AREA, ICON_SIZE, BUTTON_PADDING) groups_dict = self.util.load_stations_folders(image_box) return self.util.get_radio_group_slice(groups_dict, start, end) def turn_page(self): """ Turn book genre page """ group_page = self.get_page() self.groups_menu.set_items(group_page, 0, self.change_group, False) current_name = self.get_current_group_name() try: self.current_genre = group_page[current_name] self.groups_menu.item_selected(self.current_genre) except: keys = list(group_page.keys()) self.groups_menu.item_selected(group_page[keys[0]]) if self.navigator and self.total_pages > 1: self.navigator.left_button.change_label(str(self.current_page - 1)) self.navigator.right_button.change_label( str(self.total_pages - self.current_page)) for b in self.groups_menu.buttons.values(): b.parent_screen = self self.set_title(self.current_page) self.groups_menu.clean_draw_update() def change_group(self, state): """ Change group event listener :param state: button state """ if not self.visible: return self.current_genre = state key = STATIONS + "." + self.config[CURRENT][LANGUAGE] self.config[key][CURRENT_STATIONS] = state.genre state.source = GENRE self.groups_menu.notify_listeners(state) 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.groups_menu.add_menu_observers(update_observer, redraw_observer, release=False)
class PodcastsScreen(MenuScreen): """ Podcasts Screen """ def __init__(self, util, listeners, voice_assistant): """ Initializer :param util: utility object :param listeners: listeners :param voice_assistant: voice assistant """ self.util = util self.config = util.config self.listeners = listeners self.factory = Factory(util) self.podcasts_util = util.get_podcasts_util() self.bounding_box = util.screen_rect layout = BorderLayout(self.bounding_box) layout.set_percent_constraints(PERCENT_TOP_HEIGHT, PERCENT_BOTTOM_HEIGHT, 0, 0) d = [MENU_ROWS_PODCASTS, MENU_COLUMNS_PODCASTS] MenuScreen.__init__(self, util, listeners, MENU_ROWS_PODCASTS, MENU_COLUMNS_PODCASTS, voice_assistant, d, self.turn_page, page_in_title=False, show_loading=True) self.title = self.config[LABELS][PODCASTS] m = self.factory.create_podcast_menu_button self.podcasts_menu = MultiPageMenu(util, self.next_page, self.previous_page, self.set_title, self.reset_title, self.go_to_page, m, MENU_ROWS_PODCASTS, MENU_COLUMNS_PODCASTS, None, (0, 0, 0), self.menu_layout, align=ALIGN_CENTER) self.set_menu(self.podcasts_menu) self.navigator = PodcastNavigator( self.util, self.layout.BOTTOM, listeners, self.config[COLORS][COLOR_DARK_LIGHT], PAGE_SIZE_PODCASTS + 1) self.components.append(self.navigator) url = self.config[PODCASTS][PODCAST_URL] if url and len(url) > 0: self.current_page = self.podcasts_util.get_podcast_page( url, PAGE_SIZE_PODCASTS) else: self.current_page = 1 self.animated_title = True def set_current(self, state): """ Set current state :param state: button state """ if self.util.connected_to_internet: podcast_links_num = len(self.podcasts_util.get_podcasts_links()) else: podcast_links_num = len(self.podcasts_util.load_podcasts()) self.total_pages = math.ceil(podcast_links_num / PAGE_SIZE_PODCASTS) self.set_loading(self.title) self.turn_page() self.reset_loading() def turn_page(self): """ Turn podcasts page """ page = {} if self.util.connected_to_internet: page = self.podcasts_util.get_podcasts(self.current_page, PAGE_SIZE_PODCASTS) if len(list(page.keys())) == 0 or not self.util.connected_to_internet: page = self.podcasts_util.get_podcasts_from_disk( self.current_page, PAGE_SIZE_PODCASTS) self.podcasts_menu.set_items(page, 0, self.listeners[KEY_PODCAST_EPISODES], False) keys = list(page.keys()) if len(keys) != 0: self.podcasts_menu.item_selected(page[keys[0]]) if self.navigator and self.total_pages > 1: 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) self.podcasts_menu.clean_draw_update() if hasattr(self, "update_observer"): self.podcasts_menu.add_menu_observers(self.update_observer, self.redraw_observer) self.podcasts_menu.unselect() for i, b in enumerate(self.podcasts_menu.buttons.values()): url = self.config[PODCASTS][PODCAST_URL] if url == b.state.url: self.podcasts_menu.select_by_index(i) return self.podcasts_menu.select_by_index(0) 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 """ MenuScreen.add_screen_observers(self, update_observer, redraw_observer) self.update_observer = update_observer self.redraw_observer = redraw_observer self.add_loading_listener(redraw_observer) for b in self.navigator.menu_buttons: self.add_button_observers(b, update_observer, redraw_observer)
class RadioGroupScreen(MenuScreen): """ Radio group screen """ def __init__(self, util, listeners, voice_assistant): self.util = util self.config = util.config self.groups_list = self.util.get_stations_folders() self.factory = Factory(util) d = [MENU_ROWS, MENU_COLUMNS] MenuScreen.__init__(self, util, listeners, MENU_ROWS, MENU_COLUMNS, voice_assistant, d, self.turn_page, page_in_title=False) self.total_pages = math.ceil(len(self.groups_list) / PAGE_SIZE) self.title = util.get_stations_top_folder() m = self.factory.create_genre_menu_button self.groups_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, align=ALIGN_CENTER) self.groups_menu.add_listener(listeners[KEY_GENRE]) self.set_menu(self.groups_menu) color_dark_light = self.config[COLORS][COLOR_DARK_LIGHT] self.navigator = RadioGroupNavigator(self.util, self.layout.BOTTOM, listeners, color_dark_light, self.total_pages) self.components.append(self.navigator) current_name = self.get_current_group_name() if current_name == None: self.current_page = 1 else: try: current_group_index = self.groups_list.index(current_name) self.current_page = int(current_group_index / PAGE_SIZE) + 1 except: current_group_index = 0 self.turn_page() def get_current_group_name(self): key = STATIONS + "." + self.config[CURRENT][LANGUAGE] name = None try: name = self.config[key][CURRENT_STATIONS] except: pass return name def get_page(self): start = (self.current_page - 1) * PAGE_SIZE end = self.current_page * PAGE_SIZE page = self.groups_list[start:end] tmp_layout = self.groups_menu.get_layout(self.groups_list) button_rect = tmp_layout.constraints[0] groups_dict = self.util.load_stations_folders(button_rect) return self.util.get_radio_group_slice(groups_dict, start, end) def turn_page(self): """ Turn book genre page """ group_page = self.get_page() self.groups_menu.set_items(group_page, 0, self.change_group, False) current_name = self.get_current_group_name() try: self.current_genre = group_page[current_name] self.groups_menu.item_selected(self.current_genre) except: keys = list(group_page.keys()) self.groups_menu.item_selected(group_page[keys[0]]) if self.navigator and self.total_pages > 1: 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) self.groups_menu.clean_draw_update() def change_group(self, state): """ Change group event listener :param state: button state """ if not self.visible: return self.current_genre = state key = STATIONS + "." + self.config[CURRENT][LANGUAGE] self.config[key][CURRENT_STATIONS] = state.genre state.source = GENRE self.groups_menu.notify_listeners(state) 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.groups_menu.add_menu_observers(update_observer, redraw_observer, release=False)
class PodcastsScreen(MenuScreen): """ Podcasts Screen """ def __init__(self, util, listeners, voice_assistant): """ Initializer :param util: utility object :param listeners: listeners :param voice_assistant: voice assistant """ self.util = util self.config = util.config self.listeners = listeners self.factory = Factory(util) self.podcasts_util = util.get_podcasts_util() self.bounding_box = util.screen_rect layout = BorderLayout(self.bounding_box) layout.set_percent_constraints(PERCENT_TOP_HEIGHT, PERCENT_BOTTOM_HEIGHT, 0, 0) d = [MENU_ROWS_PODCASTS, MENU_COLUMNS_PODCASTS] MenuScreen.__init__(self, util, listeners, MENU_ROWS_PODCASTS, MENU_COLUMNS_PODCASTS, voice_assistant, d, self.turn_page, page_in_title=False, show_loading=True) self.title = self.config[LABELS][PODCASTS] m = self.create_podcast_menu_button font_size = int( ((self.menu_layout.h / MENU_ROWS_PODCASTS) / 100) * FONT_HEIGHT) self.podcasts_menu = MultiPageMenu(util, self.next_page, self.previous_page, self.set_title, self.reset_title, self.go_to_page, m, MENU_ROWS_PODCASTS, MENU_COLUMNS_PODCASTS, None, (0, 0, 0, 0), self.menu_layout, align=ALIGN_CENTER, font_size=font_size) self.set_menu(self.podcasts_menu) self.navigator = PodcastNavigator(self.util, self.layout.BOTTOM, listeners, PAGE_SIZE_PODCASTS + 1) self.add_component(self.navigator) url = self.config[PODCASTS][PODCAST_URL] if url and len(url) > 0: self.current_page = self.podcasts_util.get_podcast_page( url, PAGE_SIZE_PODCASTS) else: self.current_page = 1 self.animated_title = True def create_podcast_menu_button(self, s, constr, action, scale, font_size): """ Create podcast menu button :param s: button state :param constr: scaling constraints :param action: button event listener :param scale: True - scale images, False - don't scale images :return: genre menu button """ s.bounding_box = constr s.img_x = None s.img_y = None s.auto_update = True s.show_bgr = True s.show_img = True s.show_label = True s.image_location = ICON_LOCATION s.label_location = CENTER s.label_area_percent = 30 s.image_size_percent = ICON_SIZE s.text_color_normal = self.config[COLORS][COLOR_BRIGHT] s.text_color_selected = self.config[COLORS][COLOR_CONTRAST] s.text_color_disabled = self.config[COLORS][COLOR_MEDIUM] s.text_color_current = s.text_color_normal s.scale = scale s.source = None s.v_align = V_ALIGN_TOP s.h_align = H_ALIGN_LEFT s.v_offset = (constr.h / 100) * 5 s.bgr = self.config[BACKGROUND][MENU_BGR_COLOR] s.image_area_percent = ICON_AREA s.fixed_height = font_size button = PodcastButton(self.util, s) button.add_release_listener(action) if not getattr(s, "enabled", True): button.set_enabled(False) elif getattr(s, "icon_base", False) and not getattr(s, "scaled", False): button.components[1].content = s.icon_base button.scaled = scale return button def set_current(self, state): """ Set current state :param state: button state """ if self.util.connected_to_internet: podcast_links_num = len(self.podcasts_util.get_podcasts_links()) else: podcast_links_num = len(self.podcasts_util.load_podcasts()) self.total_pages = math.ceil(podcast_links_num / PAGE_SIZE_PODCASTS) self.set_loading(self.title) self.turn_page() self.reset_loading() def turn_page(self): """ Turn podcasts page """ page = {} if self.util.connected_to_internet: page = self.podcasts_util.get_podcasts(self.current_page, PAGE_SIZE_PODCASTS) if len(list(page.keys())) == 0 or not self.util.connected_to_internet: page = self.podcasts_util.get_podcasts_from_disk( self.current_page, PAGE_SIZE_PODCASTS) self.podcasts_menu.set_items(page, 0, self.listeners[KEY_PODCAST_EPISODES], False) keys = list(page.keys()) if len(keys) != 0: self.podcasts_menu.item_selected(page[keys[0]]) if self.navigator and self.total_pages > 1: 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) self.podcasts_menu.clean_draw_update() if hasattr(self, "update_observer"): self.podcasts_menu.add_menu_observers(self.update_observer, self.redraw_observer) self.podcasts_menu.unselect() for i, b in enumerate(self.podcasts_menu.buttons.values()): url = self.config[PODCASTS][PODCAST_URL] if url == b.state.url: self.podcasts_menu.select_by_index(i) return self.podcasts_menu.select_by_index(0) self.podcasts_menu.set_parent_screen(self) 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 """ MenuScreen.add_screen_observers(self, update_observer, redraw_observer) self.update_observer = update_observer self.redraw_observer = redraw_observer self.add_loading_listener(redraw_observer) for b in self.navigator.menu_buttons: self.add_button_observers(b, update_observer, redraw_observer)