ReaderToc: rework getChapterPageCount/Left/Done()

Rework the way they are computed, fixing issues with hidden
flows when they don't coincide with chapter starts.
reviewable/pr10990/r1
poire-z 7 months ago
parent e6ac74c1df
commit 5ec736ae96

@ -543,78 +543,54 @@ function ReaderToc:isChapterEnd(cur_pageno)
end end
function ReaderToc:getChapterPageCount(pageno) function ReaderToc:getChapterPageCount(pageno)
if self.ui.document:hasHiddenFlows() then local next_chapter = self:getNextChapter(pageno) or self.ui.document:getPageCount() + 1
-- Count pages until new chapter, starting by going backwards to the beginning of the current chapter if necessary local previous_chapter = self:isChapterStart(pageno) and pageno or self:getPreviousChapter(pageno) or 1
local page_count = 1 local page_count = next_chapter - previous_chapter
if not self:isChapterStart(pageno) then if self.ui.document:hasHiddenFlows() and self.ui.document:getPageFlow(pageno) == 0 then
local test_page = self.ui.document:getPrevPage(pageno) -- If current page in a hidden flow, return the full amount of pages in this chapter.
while test_page > 0 do -- Otherwise, count only pages in the main flow
page_count = page_count + 1 for page = previous_chapter, next_chapter - 1 do
if self:isChapterStart(test_page) then if self.ui.document:getPageFlow(page) ~= 0 then
break page_count = page_count - 1
end
test_page = self.ui.document:getPrevPage(test_page)
end end
end end
-- Then forward
local test_page = self.ui.document:getNextPage(pageno)
while test_page > 0 do
page_count = page_count + 1
if self:isChapterStart(test_page) then
return page_count - 1
end
test_page = self.ui.document:getNextPage(test_page)
end
else
local next_chapter = self:getNextChapter(pageno) or self.ui.document:getPageCount() + 1
local previous_chapter = self:isChapterStart(pageno) and pageno or self:getPreviousChapter(pageno) or 1
local page_count = next_chapter - previous_chapter
return page_count
end end
return page_count
end end
function ReaderToc:getChapterPagesLeft(pageno) function ReaderToc:getChapterPagesLeft(pageno)
if self.ui.document:hasHiddenFlows() then local next_chapter = self:getNextChapter(pageno)
-- Count pages until new chapter if not next_chapter then
local pages_left = 0 -- (ReaderFooter deals itself with nil and pageno in last chapter)
local test_page = self.ui.document:getNextPage(pageno) return
while test_page > 0 do end
pages_left = pages_left + 1 local pages_left = next_chapter - pageno - 1
if self:isChapterStart(test_page) then if self.ui.document:hasHiddenFlows() and self.ui.document:getPageFlow(pageno) == 0 then
return pages_left - 1 for page = pageno, next_chapter - 1 do
if self.ui.document:getPageFlow(page) ~= 0 then
pages_left = pages_left - 1
end end
test_page = self.ui.document:getNextPage(test_page)
end
else
local next_chapter = self:getNextChapter(pageno)
if next_chapter then
next_chapter = next_chapter - pageno - 1
end end
return next_chapter
end end
return pages_left
end end
function ReaderToc:getChapterPagesDone(pageno) function ReaderToc:getChapterPagesDone(pageno)
if self:isChapterStart(pageno) then return 0 end if self:isChapterStart(pageno) then return 0 end
if self.ui.document:hasHiddenFlows() then local previous_chapter = self:getPreviousChapter(pageno)
-- Count pages until chapter start if not previous_chapter then
local pages_done = 0 -- (ReaderFooter deals itself with nil and pageno not yet in first chapter)
local test_page = self.ui.document:getPrevPage(pageno) return
while test_page > 0 do end
pages_done = pages_done + 1 local pages_done = pageno - previous_chapter
if self:isChapterStart(test_page) then if self.ui.document:hasHiddenFlows() and self.ui.document:getPageFlow(pageno) == 0 then
return pages_done for page = previous_chapter, pageno - 1 do
if self.ui.document:getPageFlow(page) ~= 0 then
pages_done = pages_done - 1
end end
test_page = self.ui.document:getPrevPage(test_page)
end
else
local previous_chapter = self:getPreviousChapter(pageno)
if previous_chapter then
previous_chapter = pageno - previous_chapter
end end
return previous_chapter
end end
return pages_done
end end
function ReaderToc:updateCurrentNode() function ReaderToc:updateCurrentNode()

@ -76,7 +76,10 @@ describe("Readertoc module", function()
it("should get page left of chapter", function() it("should get page left of chapter", function()
assert.truthy(toc:getChapterPagesLeft(10) > 10) assert.truthy(toc:getChapterPagesLeft(10) > 10)
assert.truthy(toc:getChapterPagesLeft(95) > 10) assert.truthy(toc:getChapterPagesLeft(95) > 10)
assert.are.same(nil, toc:getChapterPagesLeft(290)) -- assert.are.same(nil, toc:getChapterPagesLeft(290))
-- Previous line somehow fails, but not if written this way:
local pagesleft = toc:getChapterPagesLeft(290)
assert.are.same(nil, pagesleft)
end) end)
it("should get page done of chapter", function() it("should get page done of chapter", function()
assert.truthy(toc:getChapterPagesDone(11) < 5) assert.truthy(toc:getChapterPagesDone(11) < 5)

Loading…
Cancel
Save