#1723 Add time to read into the status bar

Add new statuses:
TB - book time to read
TC - chapter time to read

Fix backward compatible in statistics plugin
pull/1727/head
Alex Pletnev 8 years ago
parent 239ee06fb8
commit ce4e32e01c

@ -16,6 +16,7 @@ local Event = require("ui/event")
local Font = require("ui/font")
local DEBUG = require("dbg")
local _ = require("gettext")
local util = require("util")
local ReaderFooter = InputContainer:new{
mode = 1,
@ -48,6 +49,8 @@ function ReaderFooter:init()
page_progress = true,
pages_left = true,
percentage = true,
book_time_to_read = true,
chapter_time_to_read = true,
}
local text_default = ""
if self.settings.all_at_once then
@ -67,6 +70,12 @@ function ReaderFooter:init()
if self.settings.percentage then
table.insert(info, "R:100%")
end
if self.settings.book_time_to_read then
table.insert(info, "TB: 00:00")
end
if self.settings.chapter_time_to_read then
table.insert(info, "TC: 00:00")
end
text_default = table.concat(info, " | ")
else
text_default = string.format(" %d / %d ", self.pages, self.pages)
@ -159,6 +168,8 @@ local options = {
page_progress = _("Current page"),
pages_left = _("Pages left in this chapter"),
percentage = _("Progress percentage"),
book_time_to_read = _("Book time to read"),
chapter_time_to_read = _("Chapter time to read"),
}
function ReaderFooter:addToMainMenu(tab_item_table)
@ -190,6 +201,8 @@ function ReaderFooter:addToMainMenu(tab_item_table)
get_minibar_option("page_progress"),
get_minibar_option("pages_left"),
get_minibar_option("percentage"),
get_minibar_option("book_time_to_read"),
get_minibar_option("chapter_time_to_read"),
}
})
end
@ -216,6 +229,26 @@ function ReaderFooter:getProgressPercentage()
return string.format("R:%1.f%%", self.progress_bar.percentage * 100)
end
function ReaderFooter:getBookTimeToRead()
return self:getDataFromStatistics("TB: ", self.pages)
end
function ReaderFooter:getChapterTimeToRead()
local left = self.ui.toc:getChapterPagesLeft(self.pageno, self.toc_level)
return self:getDataFromStatistics("TC: ", (left and left or self.pages - self.pageno))
end
function ReaderFooter:getDataFromStatistics(title, pages)
local statistics_data = self.ui.doc_settings:readSetting("stats")
if statistics_data and statistics_data.performance_in_pages then
local read_pages = util.tablelength(statistics_data.performance_in_pages)
local average_time_per_page = statistics_data.total_time_in_sec / read_pages
return title .. util.secondsToClock(pages * average_time_per_page, true)
end
end
function ReaderFooter:updateFooterPage()
if type(self.pageno) ~= "number" then return end
self.progress_bar.percentage = self.pageno / self.pages
@ -236,6 +269,12 @@ function ReaderFooter:updateFooterPage()
if self.settings.percentage then
table.insert(info, self:getProgressPercentage())
end
if self.settings.book_time_to_read then
table.insert(info, self:getBookTimeToRead())
end
if self.settings.chapter_time_to_read then
table.insert(info, self:getChapterTimeToRead())
end
self.progress_text.text = table.concat(info, " | ")
else
local info = ""
@ -249,6 +288,10 @@ function ReaderFooter:updateFooterPage()
info = self:getBatteryInfo()
elseif self.mode == 5 then
info = self:getProgressPercentage()
elseif self.mode == 6 then
info = self:getBookTimeToRead()
elseif self.mode == 7 then
info = self:getChapterTimeToRead()
end
self.progress_text.text = info
end
@ -291,6 +334,8 @@ function ReaderFooter:applyFooterMode(mode)
-- 3 for footer next_chapter info
-- 4 for battery status
-- 5 for progress percentage
-- 6 for from statistics book time to read
-- 7 for from statistics chapter time to read
if mode ~= nil then self.mode = mode end
if self.mode == 0 then
self.view.footer_visible = false
@ -318,7 +363,7 @@ function ReaderFooter:onTapFooter(arg, ges)
self.ui:handleEvent(Event:new("GotoPercentage", percentage))
end
else
self.mode = (self.mode + 1) % 6
self.mode = (self.mode + 1) % 8
if self.settings.all_at_once and (self.mode > 1) then
self.mode = 0
end
@ -335,6 +380,12 @@ function ReaderFooter:onTapFooter(arg, ges)
self.mode = 5
end
if (self.mode == 5) and not self.settings.percentage then
self.mode = 6
end
if (self.mode == 6) and not self.settings.book_time_to_read then
self.mode = 7
end
if (self.mode == 7) and not self.settings.chapter_time_to_read then
self.mode = 0
end
self:applyFooterMode()

@ -1,4 +1,3 @@
local util = {}
function util.stripePunctuations(word)
@ -47,4 +46,30 @@ function util.gsplit(str, pattern, capture)
end)
end
--https://gist.github.com/jesseadams/791673
function util.secondsToClock(seconds, withoutSeconds)
local seconds = tonumber(seconds)
if seconds == 0 or seconds ~= seconds then
if withoutSeconds then
return "00:00";
else
return "00:00:00";
end
else
local hours = string.format("%02.f", math.floor(seconds / 3600));
local mins = string.format("%02.f", math.floor(seconds / 60 - (hours * 60)));
if withoutSeconds then
return hours .. ":" .. mins
end
local secs = string.format("%02.f", math.floor(seconds - hours * 3600 - mins * 60));
return hours .. ":" .. mins .. ":" .. secs
end
end
function util.tablelength(T)
local count = 0
for _ in pairs(T) do count = count + 1 end
return count
end
return util

@ -13,6 +13,7 @@ local lfs = require("libs/libkoreader-lfs")
local DEBUG = require("dbg")
local T = require("ffi/util").template
local _ = require("gettext")
local util = require("util")
local tableutil = require("tableutil")
local statistics_dir = DataStorage:getDataDir() .. "/statistics/"
@ -234,16 +235,16 @@ function ReaderStatistics:updateCurrentStat()
dates[os.date("%Y-%m-%d", k)] = ""
end
local read_pages = tableutil.tablelength(self.data.performance_in_pages)
local read_pages = util.tablelength(self.data.performance_in_pages)
local average_time_per_page = self.data.total_time_in_sec / read_pages
table.insert(stats, { text = _("Current period"), mandatory = self:secondsToClock(self.current_period) })
table.insert(stats, { text = _("Time to read"), mandatory = self:secondsToClock(self.data.pages * average_time_per_page) })
table.insert(stats, { text = _("Total time"), mandatory = self:secondsToClock(self.data.total_time_in_sec) })
table.insert(stats, { text = _("Current period"), mandatory = util.secondsToClock(self.current_period, false) })
table.insert(stats, { text = _("Time to read"), mandatory = util.secondsToClock(self.data.pages * average_time_per_page, false) })
table.insert(stats, { text = _("Total time"), mandatory = util.secondsToClock(self.data.total_time_in_sec, false) })
table.insert(stats, { text = _("Total highlights"), mandatory = self.data.highlights })
table.insert(stats, { text = _("Total notes"), mandatory = self.data.notes })
table.insert(stats, { text = _("Total days"), mandatory = tableutil.tablelength(dates) })
table.insert(stats, { text = _("Average time per page"), mandatory = self:secondsToClock(average_time_per_page) })
table.insert(stats, { text = _("Total days"), mandatory = util.tablelength(dates) })
table.insert(stats, { text = _("Average time per page"), mandatory = util.secondsToClock(average_time_per_page, false) })
table.insert(stats, { text = _("Read pages/Total pages"), mandatory = read_pages .. "/" .. self.data.pages })
return stats
end
@ -301,7 +302,7 @@ function ReaderStatistics:generateReadBooksTable(title, dates)
local result = {}
table.insert(result, { text = title })
for k, v in tableutil.spairs(dates, function(t, a, b) return t[b].date < t[a].date end) do
table.insert(result, { text = k, mandatory = T(_("Pages (%1) Time: %2"), v.count, self:secondsToClock(v.read)) })
table.insert(result, { text = k, mandatory = T(_("Pages (%1) Time: %2"), v.count, util.secondsToClock(v.read, false)) })
end
return result
end
@ -316,11 +317,12 @@ function ReaderStatistics:updateTotalStat()
total_books_time = total_books_time + tonumber(self.data.total_time_in_sec)
table.insert(total_stats, 1, { text = _("Total hours read"), mandatory = self:secondsToClock(total_books_time) })
DEBUG ("TOTALSTATS", total_stats)
table.insert(total_stats, 1, { text = _("Total hours read"), mandatory = util.secondsToClock(total_books_time, false) })
table.insert(total_stats, 2, { text = _("----------------------------------------------------") })
table.insert(total_stats, 3, {
text = self.data.title,
mandatory = self:secondsToClock(self.data.total_time_in_sec),
mandatory = util.secondsToClock(self.data.total_time_in_sec, false),
callback = function()
self.total_status:swithItemTable(nil, self:getDatesForBook(self.data))
UIManager:show(self.total_menu)
@ -341,7 +343,7 @@ function ReaderStatistics:getStatisticsFromHistory(total_stats, total_books_time
titles[book_stats.title] = true
table.insert(total_stats, {
text = book_stats.title,
mandatory = self:secondsToClock(book_stats.total_time_in_sec),
mandatory = util.secondsToClock(book_stats.total_time_in_sec, false),
callback = function()
self.total_status:swithItemTable(nil, self:getDatesForBook(book_stats))
UIManager:show(self.total_menu)
@ -367,33 +369,19 @@ function ReaderStatistics:getOldStatisticsFromDirectory(exlude_titles, total_sta
if book_result and book_result.title ~= self.data.title and not exlude_titles[book_result.title] then
table.insert(total_stats, {
text = book_result.title,
mandatory = self:secondsToClock(book_result.total_time_in_sec),
mandatory = util.secondsToClock(book_result.total_time, false),
callback = function()
self.total_status:swithItemTable(nil, self:getDatesForBookOldFormat(book_result))
UIManager:show(self.total_menu)
return true
end,
})
total_books_time = total_books_time + tonumber(book_result.total_time_in_sec)
total_books_time = total_books_time + tonumber(book_result.total_time)
end
end
end
end
--https://gist.github.com/jesseadams/791673
function ReaderStatistics:secondsToClock(seconds)
local seconds = tonumber(seconds)
if seconds == 0 or seconds ~= seconds then
return "00:00:00";
else
local hours = string.format("%02.f", math.floor(seconds / 3600));
local mins = string.format("%02.f", math.floor(seconds / 60 - (hours * 60)));
local secs = string.format("%02.f", math.floor(seconds - hours * 3600 - mins * 60));
return hours .. ":" .. mins .. ":" .. secs
end
end
function ReaderStatistics:getBookProperties()
local props = self.view.document:getProps()
if props.title == "No document" or props.title == "" then --sometime crengine returns "No document" try to get one more time
@ -419,6 +407,7 @@ function ReaderStatistics:onPageUpdate(pageno)
end
self.last_time = curr_time
self.ui.doc_settings:saveSetting("stats", self.data)
end
end

@ -25,10 +25,4 @@ function tableutil.spairs(t, order)
end
end
function tableutil.tablelength(T)
local count = 0
for _ in pairs(T) do count = count + 1 end
return count
end
return tableutil

Loading…
Cancel
Save