Update statistics plugin to support docless mode

pull/2481/head
Hzj_jie 7 years ago committed by Qingping Hou
parent f95ad00b9e
commit dbdce45129

@ -21,7 +21,6 @@ local statistics_dir = DataStorage:getDataDir() .. "/statistics/"
local page_max_time local page_max_time
local ReaderStatistics = Widget:extend{ local ReaderStatistics = Widget:extend{
is_doc_only = true,
last_time = nil, last_time = nil,
page_min_read_sec = 5, page_min_read_sec = 5,
page_max_read_sec = 90, page_max_read_sec = 90,
@ -41,8 +40,12 @@ local ReaderStatistics = Widget:extend{
}, },
} }
function ReaderStatistics:isDocless()
return self.ui == nil or self.ui.document == nil
end
function ReaderStatistics:init() function ReaderStatistics:init()
if self.ui.document.is_pic then if not self:isDocless() and self.ui.document.is_pic then
return return
end end
@ -69,22 +72,23 @@ function ReaderStatistics:getBookProperties()
end end
function ReaderStatistics:initData(config) function ReaderStatistics:initData(config)
if self:isDocless() or not self.is_enabled then
return
end
-- first execution -- first execution
if self.is_enabled then if not self.data then
if not self.data then self.data = { performance_in_pages= {} }
self.data = { performance_in_pages= {} } self:inplaceMigration(); -- first time merge data
self:inplaceMigration(); -- first time merge data end
end
local book_properties = self:getBookProperties() local book_properties = self:getBookProperties()
self.data.title = book_properties.title self.data.title = book_properties.title
self.data.authors = book_properties.authors self.data.authors = book_properties.authors
self.data.language = book_properties.language self.data.language = book_properties.language
self.data.series = book_properties.series self.data.series = book_properties.series
self.data.pages = self.view.document:getPageCount() self.data.pages = self.view.document:getPageCount()
return return
end
end end
local function generateReadBooksTable(title, dates) local function generateReadBooksTable(title, dates)
@ -104,13 +108,13 @@ function ReaderStatistics:getStatisticEnabledMenuItem()
checked_func = function() return self.is_enabled end, checked_func = function() return self.is_enabled end,
callback = function() callback = function()
-- if was enabled, have to save data to file -- if was enabled, have to save data to file
if self.last_time and self.is_enabled then if self.last_time and self.is_enabled and not self:isDocless() then
self.ui.doc_settings:saveSetting("stats", self.data) self.ui.doc_settings:saveSetting("stats", self.data)
end end
self.is_enabled = not self.is_enabled self.is_enabled = not self.is_enabled
-- if was disabled have to get data from file -- if was disabled have to get data from file
if self.is_enabled then if self.is_enabled and not self:isDocless() then
self:initData(self.ui.doc_settings) self:initData(self.ui.doc_settings)
end end
self:saveSettings() self:saveSettings()
@ -178,7 +182,8 @@ function ReaderStatistics:addToMainMenu(tab_item_table)
title = _("Statistics"), title = _("Statistics"),
kv_pairs = self:getCurrentStat(), kv_pairs = self:getCurrentStat(),
}) })
end end,
enabled = not self:isDocless()
}, },
{ {
text = _("All books"), text = _("All books"),
@ -445,18 +450,21 @@ local function getDatesForBook(book)
end end
function ReaderStatistics:getTotalStats() function ReaderStatistics:getTotalStats()
local total_stats = { local total_stats = {}
{ if not self:isDocless() then
self.data.title, total_stats = {
util.secondsToClock(self.data.total_time_in_sec, false), {
callback = function() self.data.title,
UIManager:show(KeyValuePage:new{ util.secondsToClock(self.data.total_time_in_sec, false),
title = self.data.title, callback = function()
kv_pairs = getDatesForBook(self.data), UIManager:show(KeyValuePage:new{
}) title = self.data.title,
end, kv_pairs = getDatesForBook(self.data),
})
end,
}
} }
} end
-- find stats for all other books in history -- find stats for all other books in history
local proceded_titles, total_books_time = self:getStatisticsFromHistory(total_stats) local proceded_titles, total_books_time = self:getStatisticsFromHistory(total_stats)
total_books_time = total_books_time + self:getOldStatisticsFromDirectory(proceded_titles, total_stats) total_books_time = total_books_time + self:getOldStatisticsFromDirectory(proceded_titles, total_stats)
@ -522,30 +530,31 @@ function ReaderStatistics:getOldStatisticsFromDirectory(exlude_titles, total_sta
end end
function ReaderStatistics:onPageUpdate(pageno) function ReaderStatistics:onPageUpdate(pageno)
if self.is_enabled then if self:isDocless() or not self.is_enabled then
local curr_time = TimeVal:now() return
local diff_time = curr_time.sec - self.last_time.sec end
local curr_time = TimeVal:now()
-- if last update was more then 10 minutes then current period set to 0 local diff_time = curr_time.sec - self.last_time.sec
if (diff_time > 600) then
self.current_period = 0
self.pages_current_period = 0
end
if diff_time >= self.page_min_read_sec and diff_time <= self.page_max_read_sec then -- if last update was more then 10 minutes then current period set to 0
self.current_period = self.current_period + diff_time if (diff_time > 600) then
self.pages_current_period = self.pages_current_period + 1 self.current_period = 0
self.data.total_time_in_sec = self.data.total_time_in_sec + diff_time self.pages_current_period = 0
self.data.performance_in_pages[curr_time.sec] = pageno end
-- we cannot save stats each time this is a page update event,
-- because the self.data may not even be initialized when such a event
-- comes, which will render a blank stats written into doc settings
-- and all previous stats are totally wiped out.
self.ui.doc_settings:saveSetting("stats", self.data)
end
self.last_time = curr_time if diff_time >= self.page_min_read_sec and diff_time <= self.page_max_read_sec then
self.current_period = self.current_period + diff_time
self.pages_current_period = self.pages_current_period + 1
self.data.total_time_in_sec = self.data.total_time_in_sec + diff_time
self.data.performance_in_pages[curr_time.sec] = pageno
-- we cannot save stats each time this is a page update event,
-- because the self.data may not even be initialized when such a event
-- comes, which will render a blank stats written into doc settings
-- and all previous stats are totally wiped out.
self.ui.doc_settings:saveSetting("stats", self.data)
end end
self.last_time = curr_time
end end
-- For backward compatibility -- For backward compatibility
@ -576,7 +585,7 @@ function ReaderStatistics:importFromFile(base_path, item)
end end
function ReaderStatistics:onCloseDocument() function ReaderStatistics:onCloseDocument()
if self.last_time and self.is_enabled then if not self:isDocless() and self.last_time and self.is_enabled then
self.ui.doc_settings:saveSetting("stats", self.data) self.ui.doc_settings:saveSetting("stats", self.data)
end end
end end
@ -592,10 +601,11 @@ end
-- in case when screensaver starts -- in case when screensaver starts
function ReaderStatistics:onSaveSettings() function ReaderStatistics:onSaveSettings()
self:saveSettings() self:saveSettings()
self.ui.doc_settings:saveSetting("stats", self.data) if not self:isDocless() then
self.current_period = 0 self.ui.doc_settings:saveSetting("stats", self.data)
self.pages_current_period = 0 self.current_period = 0
self.pages_current_period = 0
end
end end
-- screensaver off -- screensaver off

Loading…
Cancel
Save