Exemplo n.º 1
0
class ServerReadingListsScreen(Screen):
    reading_list_title = StringProperty()
    page_number = NumericProperty()
    max_books_page = NumericProperty()
    dynamic_ids = DictProperty({})  # declare class attribute, dynamic_ids
    sync_bool = BooleanProperty(False)
    so = BooleanProperty()
    new_readinglist = ObjectProperty()

    def __init__(self, **kwargs):
        super(ServerReadingListsScreen, self).__init__(**kwargs)
        self.app = App.get_running_app()
        self.fetch_data = None
        self.readinglist_Id = StringProperty()
        self.readinglist_name = ""
        # self.bind_to_resize()
        self.bind(width=self.my_width_callback)
        self.m_grid = ""
        self.main_stack = ""
        self.prev_button = ""
        self.next_button = ""
        self.base_url = self.app.base_url
        self.api_url = self.app.api_url
        self.api_key = self.app.config.get("General", "api_key")
        self.list_count = ""
        self.paginator_obj = ObjectProperty()
        self.current_page = ObjectProperty()
        self.list_loaded = BooleanProperty()
        self.page_number = 1
        self.list_loaded = False
        self.comic_thumb_height = 240
        self.comic_thumb_width = 156
        self.file_download = True
        self.num_file_done = 0
        self.max_books_page = self.app.max_books_page
        self.please_wait_dialog = None
        self.dialog_load_comic_data = None

    def callback_for_menu_items(self, *args):
        pass

    def setup_screen(self):
        self.api_key = self.app.config.get("General", "api_key")
        self.api_url = self.app.api_url
        self.main_stack = self.ids["main_stack"]
        self.m_grid = self.ids["main_grid"]
        self.prev_button = self.ids["prev_button"]
        self.next_button = self.ids["next_button"]

    def on_pre_enter(self, *args):
        self.app.show_action_bar()
        return super().on_pre_enter(*args)

    def on_leave(self, *args):
        self.app.list_previous_screens.append(self.name)
        return super().on_leave(*args)

    def my_width_callback(self, obj, value):

        for key, val in self.ids.items():
            if key == "main_grid":
                c = val
                c.cols = (Window.width - 10) // self.comic_thumb_width

    def page_turn(self, c_id, new_UserLastPageRead):
        grid = self.m_grid
        for child in grid.children:
            if child.comic_obj.Id == c_id:
                if new_UserLastPageRead == 0:
                    child.percent_read = 0
                else:
                    child.percent_read = round(
                        new_UserLastPageRead /
                        (child.comic_obj.PageCount - 1) * 100)
                child.page_count_text = f"{child.percent_read}%"

    def file_sync_update(self, c_id, state):
        grid = self.m_grid
        for child in grid.children:
            if child.comic_obj.Id == c_id:
                child.has_localfile = state

    def collect_readinglist_data(
        self,
        readinglist_name="",
        readinglist_Id="",
        mode="From Server",
        current_page_num=1,
        *largs,
    ):
        async def collect_readinglist_data():
            self.readinglist_name = readinglist_name
            self.app.set_screen(self.readinglist_name + " Page 1")
            self.reading_list_title = self.readinglist_name + " Page 1"
            self.readinglist_Id = readinglist_Id
            self.page_number = current_page_num
            self.mode = mode
            if self.mode == "From Server":
                self.fetch_data = ComicServerConn()
                lsit_count_url = "{}/Lists/{}/Comics/".format(
                    self.api_url, readinglist_Id)
                # self.fetch_data.get_list_count(lsit_count_url,self)
                self.fetch_data.get_server_data(lsit_count_url, self)
            elif self.mode == "From DataBase":
                self.got_db_data()

        asynckivy.start(collect_readinglist_data())

    def get_page(self, instance):
        page_num = instance.page_num
        self.app.set_screen(self.readinglist_name + f" Page {page_num}")
        self.reading_list_title = self.readinglist_name + f" Page {page_num}"
        page = self.paginator_obj.page(page_num)
        self.current_page = page
        if page.has_next():
            self.next_button.opacity = 1
            self.next_button.disabled = False
            self.next_button.page_num = page.next_page_number()
        else:
            self.next_button.opacity = 0
            self.next_button.disabled = True
            self.next_button.page_num = ""
        if page.has_previous():
            self.prev_button.opacity = 1
            self.prev_button.disabled = False
            self.prev_button.page_num = page.previous_page_number()
        else:
            self.prev_button.opacity = 0
            self.prev_button.disabled = True
            self.prev_button.page_num = ""
        self.build_page(page.object_list)

    def build_page(self, object_lsit):
        async def _build_page():
            grid = self.m_grid
            grid.clear_widgets()
            for comic in object_lsit:
                await asynckivy.sleep(0)
                c = ReadingListComicImage(comic_obj=comic)
                c.lines = 2
                c.readinglist_obj = self.new_readinglist
                c.paginator_obj = self.paginator_obj
                y = self.comic_thumb_height
                thumb_filename = f"{comic.Id}.jpg"
                id_folder = self.app.store_dir
                my_thumb_dir = os.path.join(id_folder, "comic_thumbs")
                t_file = os.path.join(my_thumb_dir, thumb_filename)
                if os.path.isfile(t_file):
                    c_image_source = t_file
                else:
                    part_url = f"/Comics/{comic.Id}/Pages/0?"
                    part_api = "&apiKey={}&height={}".format(
                        self.api_key, round(dp(y)))
                    c_image_source = f"{self.api_url}{part_url}{part_api}"
                    asynckivy.start(save_thumb(comic.Id, c_image_source))
                c.source = c_image_source
                c.PageCount = comic.PageCount
                c.pag_pagenum = self.current_page.number
                grid.add_widget(c)
                grid.cols = (Window.width - 10) // self.comic_thumb_width
                self.dynamic_ids[id] = c
            self.ids.page_count.text = "Page #\n{} of {}".format(
                self.current_page.number, self.paginator_obj.num_pages())
            self.loading_done = True

        asynckivy.start(_build_page())

    def refresh_callback(self, *args):
        """A method that updates the state of reading list"""
        def __refresh_callback(interval):
            self.ids.main_grid.clear_widgets()
            self.collect_readinglist_data(
                self.readinglist_name,
                self.readinglist_Id,
                current_page_num=self.page_number,
                mode="From DataBase",
            )
            #            self.build_page(page.object_list)
            # self.ids.main_scroll.refresh_done()
            self.tick = 0

        Clock.schedule_once(__refresh_callback, 1)

    def got_db_data(self):
        """
        used if rl data is already stored in db.
        """
        async def _do_readinglist():
            self.new_readinglist = ComicReadingList(
                name=self.readinglist_name,
                data="db_data",
                slug=self.readinglist_Id,
            )
            await asynckivy.sleep(0)
            self.so = self.new_readinglist.sw_syn_this_active
            self.setup_options()
            new_readinglist_reversed = self.new_readinglist.comics
            self.paginator_obj = Paginator(new_readinglist_reversed,
                                           self.max_books_page)
            page = self.paginator_obj.page(self.page_number)
            self.current_page = page
            if page.has_next():
                self.next_button.opacity = 1
                self.next_button.disabled = False
                self.next_button.page_num = page.next_page_number()
            else:
                self.next_button.opacity = 0
                self.next_button.disabled = True
                self.next_button.page_num = ""
            if page.has_previous():
                self.prev_button.opacity = 1
                self.prev_button.disabled = False
                self.prev_button.page_num = page.previous_page_number()
            else:
                self.prev_button.opacity = 0
                self.prev_button.disabled = True
                self.prev_button.page_num = ""
            self.build_page(page.object_list)
            self.list_loaded = True

        asynckivy.start(_do_readinglist())

    def got_json(self, req, results):
        print("Start got_json")

        async def _got_json():
            print("Start _got_json")
            self.new_readinglist = ComicReadingList(
                name=self.readinglist_name,
                data=results,
                slug=self.readinglist_Id,
            )
            totalCount = self.new_readinglist.totalCount
            i = 1
            for item in self.new_readinglist.comic_json:
                await asynckivy.sleep(0)
                str_name = "{} #{}".format(item["Series"], item["Number"])
                self.dialog_load_comic_data.name_kv_file = str_name
                self.dialog_load_comic_data.percent = str(i * 100 //
                                                          int(totalCount))
                comic_index = self.new_readinglist.comic_json.index(item)
                new_comic = ComicBook(
                    item,
                    readlist_obj=self.new_readinglist,
                    comic_index=comic_index,
                )
                self.new_readinglist.add_comic(new_comic)
                i += 1
            print("End for item in self.new_readinglist.comic_json:")
            self.setup_options()
            new_readinglist_reversed = self.new_readinglist.comics[::-1]
            self.paginator_obj = Paginator(new_readinglist_reversed,
                                           self.max_books_page)
            page = self.paginator_obj.page(self.page_number)
            self.current_page = page
            if page.has_next():
                self.next_button.opacity = 1
                self.next_button.disabled = False
                self.next_button.page_num = page.next_page_number()
            else:
                self.next_button.opacity = 0
                self.next_button.disabled = True
                self.next_button.page_num = ""
            if page.has_previous():
                self.prev_button.opacity = 1
                self.prev_button.disabled = False
                self.prev_button.page_num = page.previous_page_number()
            else:
                self.prev_button.opacity = 0
                self.prev_button.disabled = True
                self.prev_button.page_num = ""
            self.build_page(page.object_list)
            self.list_loaded = True
            self.dialog_load_comic_data.dismiss()

        self.dialog_load_comic_data = DialogLoadKvFiles()
        self.dialog_load_comic_data.open()
        asynckivy.start(_got_json())

    def show_please_wait_dialog(self):
        def __callback_for_please_wait_dialog(*args):
            pass

        self.please_wait_dialog = MDDialog(
            title="No ReadingList loaded.",
            size_hint=(0.8, 0.4),
            text_button_ok="Ok",
            text=f"No ReadingList loaded.",
            events_callback=__callback_for_please_wait_dialog,
        )
        self.please_wait_dialog.open()

    def setup_options(self):
        self.sync_options = SyncOptionsPopup(
            size_hint=(0.76, 0.76),
            cb_limit_active=self.new_readinglist.cb_limit_active,
            limit_num_text=str(self.new_readinglist.limit_num),
            cb_only_read_active=self.new_readinglist.cb_only_read_active,
            cb_purge_active=self.new_readinglist.cb_purge_active,  # noqa
            cb_optimize_size_active=self.new_readinglist.
            cb_optimize_size_active,  # noqa
            sw_syn_this_active=bool(self.new_readinglist.sw_syn_this_active),
        )
        self.sync_options.ids.limit_num.bind(
            on_text_validate=self.sync_options.check_input,
            focus=self.sync_options.check_input,
        )

        self.sync_options.title = self.new_readinglist.name

    def open_sync_options(self):
        if self.sync_options.ids.sw_syn_this.active is True:
            self.sync_options.ids.syn_on_off_label.text = f""
            self.sync_options.ids.syn_on_off_label.theme_text_color = "Primary"
        self.sync_options.open()

    def sync_readinglist(self):
        if self.sync_options.ids.sw_syn_this.active is False:
            self.sync_options.ids.syn_on_off_label.text = f"Sync Not Turned On"
            self.open_sync_options()
        elif self.sync_options.ids.sw_syn_this.active is True:
            toast(f"Starting sync of {self.new_readinglist.name}")
            self.new_readinglist.do_sync()
Exemplo n.º 2
0
        def __got_readlist_data(results):
            async def __load_readinglist_scree(paginator_obj=None):
                if tmp_last_comic_type == "local_file":
                    x_readinglists_screen = self.app.manager.get_screen(
                        "local_readinglists_screen"
                    )
                else:
                    x_readinglists_screen = self.app.manager.get_screen(
                        "server_readinglists_screen"
                    )
                x_readinglists_screen.list_loaded = False
                x_readinglists_screen.setup_screen()
                x_readinglists_screen.page_number = tmp_last_pag_pagnum
                x_readinglists_screen.loading_done = False
                self.call_collect(
                    set_mode,
                    x_readinglists_screen,
                    readinglist_name,
                    readinglist_Id,
                )

            tmp_last_comic_id = self.app.config.get("Saved", "last_comic_id")
            tmp_last_comic_type = self.app.config.get(
                "Saved", "last_comic_type"
            )
            tmp_last_pag_pagnum = int(
                self.app.config.get("Saved", "last_pag_pagnum")
            )
            if tmp_last_comic_id == "":
                return
            else:
                query = ReadingList.select().where(
                    ReadingList.slug == readinglist_Id
                )
                if query.exists():
                    Logger.info(f"{readinglist_name} already in Database")
                    set_mode = "From DataBase"
                    mode = ""
                    if tmp_last_comic_type == "local_file":
                        mode = "local_file"
                    self.new_readinglist = ComicReadingList(
                        name=self.readinglist_name,
                        data="db_data",
                        slug=self.readinglist_Id,
                        mode=mode,
                    )

                    # self.new_readinglist.comics_write()
                    max_books_page = int(
                        self.app.config.get("General", "max_books_page")
                    )
                    new_readinglist_reversed = self.new_readinglist.comics
                    paginator_obj = Paginator(
                        new_readinglist_reversed, max_books_page
                    )
                    for x in range(1, paginator_obj.num_pages()):
                        this_page = paginator_obj.page(x)
                        for comic in this_page.object_list:
                            if tmp_last_comic_id == comic.Id:
                                tmp_last_pag_pagnum = this_page.number
                    asynckivy.start(
                        __load_readinglist_scree(paginator_obj=paginator_obj)
                    )
                    if (
                        self.open_last_comic_startup == 1
                        and not self.app.app_started
                    ):
                        for comic in self.new_readinglist.comics:
                            if comic.slug == tmp_last_comic_id:

                                self.open_comic(
                                    tmp_last_comic_id=tmp_last_comic_id,
                                    tmp_last_comic_type=tmp_last_comic_type,
                                    paginator_obj=paginator_obj,
                                    comic=comic,
                                    tmp_last_pag_pagnum=tmp_last_pag_pagnum,
                                )
                    else:
                        grid = self.ids["main_grid"]
                        grid.cols = 1
                        grid.clear_widgets()
                        for comic in self.new_readinglist.comics:
                            if comic.slug == tmp_last_comic_id:
                                c = ReadingListComicImage(comic_obj=comic)
                                c.readinglist_obj = self.new_readinglist
                                c.paginator_obj = paginator_obj
                                x = self.app.comic_thumb_width
                                y = self.app.comic_thumb_height
                                if tmp_last_comic_type == "local_file":
                                    if comic.local_file == "":
                                        return
                                    import os

                                    id_folder = os.path.join(
                                        self.app.sync_folder,
                                        self.new_readinglist.slug,
                                    )
                                    my_thumb_dir = os.path.join(
                                        id_folder, "thumb"
                                    )
                                    thumb_name = f"{comic.Id}.jpg"
                                    t_file = os.path.join(
                                        my_thumb_dir, thumb_name
                                    )
                                    c_image_source = t_file
                                else:
                                    round_y = round(dp(y))
                                    part_url = f"/Comics/{comic.Id}/Pages/0?"
                                    part_api = "&apiKey={}&height={}".format(
                                        self.api_key, round_y
                                    )
                                    c_image_source = "{}{}{}".format(
                                        self.app.api_url, part_url, part_api
                                    )
                                c.source = c_image_source
                                c.PageCount = comic.PageCount
                                c.pag_pagenum = tmp_last_pag_pagnum
                                if tmp_last_comic_type == "local_file":
                                    c.view_mode = "Sync"
                                strtxt = f"{comic.Series} #{comic.Number}"
                                tmp_color = get_hex_from_color((1, 1, 1, 1))
                                c.text = f"[color={tmp_color}]{strtxt}[/color]"
                                grid.add_widget(c)
                                tmp_txt = f"Last Comic Load from \
                                        {self.new_readinglist.name}"
                                self.ids.last_comic_label.text = tmp_txt
                else:
                    Logger.info(
                        f"{readinglist_name} \
                            not in Database This could be a problems"
                    )

                    set_mode = "From Server"