From ce00471fd84745dcbbcb9f2d9ef434acb3d0fd2b Mon Sep 17 00:00:00 2001 From: dec05eba Date: Mon, 14 Mar 2022 18:41:31 +0100 Subject: Image scroll view: add first/last image to allow scrolling to previous/next chapter, load next chapters page when reaching bottom --- src/QuickMedia.cpp | 64 +++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 44 insertions(+), 20 deletions(-) (limited to 'src/QuickMedia.cpp') diff --git a/src/QuickMedia.cpp b/src/QuickMedia.cpp index e6e8e68..4bdf4d6 100644 --- a/src/QuickMedia.cpp +++ b/src/QuickMedia.cpp @@ -2210,18 +2210,50 @@ namespace QuickMedia { page_navigation = image_continuous_page(manga_images_page); } - if(page_navigation == -1) { + if(page_navigation == -1) { // previous chapter // TODO: Make this work if the list is sorted differently than from newest to oldest. - chapters_body->select_next_item(); - select_episode(chapters_body->get_selected(), !continue_left_off); - if(!continue_left_off) - image_index = 99999; // Start at the page that shows we are at the end of the chapter - manga_images_page->change_chapter(chapters_body->get_selected()->get_title(), chapters_body->get_selected()->url); - } else if(page_navigation == 1) { + + if(chapters_body->select_next_item()) { + select_episode(chapters_body->get_selected(), !continue_left_off); + if(!continue_left_off) + image_index = 99999; // Start at the page that shows we are at the end of the chapter + manga_images_page->change_chapter(chapters_body->get_selected()->get_title(), chapters_body->get_selected()->url); + } else if(!tab_associated_data[selected_tab].fetching_next_page_failed) { + BodyItems new_body_items; + const int fetch_page = tab_associated_data[selected_tab].fetched_page + 1; + TaskResult load_next_page_result = run_task_with_loading_screen([&] { + if(tabs[selected_tab].page->get_page("", fetch_page, new_body_items) != PluginResult::OK) { + fprintf(stderr, "Failed to get next page (page %d)\n", fetch_page); + return false; + } + return true; + }); + + fprintf(stderr, "Finished fetching page %d, num new items: %zu\n", fetch_page, new_body_items.size()); + size_t num_new_messages = new_body_items.size(); + if(num_new_messages > 0) { + tabs[selected_tab].body->append_items(new_body_items); + tab_associated_data[selected_tab].fetched_page++; + + select_episode(chapters_body->get_selected(), !continue_left_off); + if(!continue_left_off) + image_index = 99999; // Start at the page that shows we are at the end of the chapter + manga_images_page->change_chapter(chapters_body->get_selected()->get_title(), chapters_body->get_selected()->url); + } else { + tab_associated_data[selected_tab].fetching_next_page_failed = true; + } + + if(load_next_page_result == TaskResult::CANCEL) { + current_page = pop_page_stack(); + break; + } + } + } else if(page_navigation == 1) { // next chapter // TODO: Make this work if the list is sorted differently than from newest to oldest. - chapters_body->select_previous_item(); - select_episode(chapters_body->get_selected(), !continue_left_off); - manga_images_page->change_chapter(chapters_body->get_selected()->get_title(), chapters_body->get_selected()->url); + if(chapters_body->select_previous_item()) { + select_episode(chapters_body->get_selected(), !continue_left_off); + manga_images_page->change_chapter(chapters_body->get_selected()->get_title(), chapters_body->get_selected()->url); + } } } } @@ -3118,7 +3150,6 @@ namespace QuickMedia { std::string audio_url; bool has_embedded_audio = true; - std::string prev_start_time; std::vector media_chapters; auto load_video_error_check = [&](std::string start_time = "", bool reuse_media_source = false) mutable { @@ -3219,7 +3250,6 @@ namespace QuickMedia { const bool is_resume_go_back = !start_time.empty(); if(start_time.empty()) start_time = video_page->get_url_timestamp(); - prev_start_time = start_time; watched_videos.insert(video_page->get_url()); // TODO: Sync sequences @@ -4081,12 +4111,6 @@ namespace QuickMedia { image_index = num_manga_pages; goto end_of_images_page; } - } else if((event.key.control && event.key.code == mgl::Keyboard::Up) || (event.key.alt && event.key.control && event.key.code == mgl::Keyboard::K)) { - page_navigation = -1; - goto end_of_images_page; - } else if((event.key.control && event.key.code == mgl::Keyboard::Down) || (event.key.alt && event.key.control && event.key.code == mgl::Keyboard::J)) { - page_navigation = 1; - goto end_of_images_page; } else if(event.key.code == mgl::Keyboard::Escape) { current_page = pop_page_stack(); } else if(event.key.code == mgl::Keyboard::I) { @@ -4200,7 +4224,7 @@ namespace QuickMedia { Json::Value json_chapter; int current_read_page; save_manga_progress(images_page, json_chapters, json_chapter, current_read_page); - ImageViewer image_viewer(&window, num_manga_pages, images_page->manga_name, images_page->get_chapter_name(), image_index, content_cache_dir, &fit_image_to_window); + ImageViewer image_viewer(&window, num_manga_pages, images_page->manga_name, images_page->get_chapter_name(), std::min(num_manga_pages - 1, image_index), content_cache_dir, &fit_image_to_window); idle_active_handler(); @@ -4234,7 +4258,7 @@ namespace QuickMedia { image_index = focused_page - 1; if(focused_page != current_read_page) { current_read_page = focused_page; - json_chapter["current"] = current_read_page; + json_chapter["current"] = std::max(0, std::min(current_read_page, num_manga_pages)); json_chapters[images_page->get_chapter_name()] = json_chapter; content_storage_json["chapters"] = json_chapters; if(!save_json_to_file_atomic(content_storage_file, content_storage_json)) { -- cgit v1.2.3