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,8 +72,10 @@ 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
@ -84,7 +89,6 @@ function ReaderStatistics:initData(config)
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,7 +450,9 @@ local function getDatesForBook(book)
end end
function ReaderStatistics:getTotalStats() function ReaderStatistics:getTotalStats()
local total_stats = { local total_stats = {}
if not self:isDocless() then
total_stats = {
{ {
self.data.title, self.data.title,
util.secondsToClock(self.data.total_time_in_sec, false), util.secondsToClock(self.data.total_time_in_sec, false),
@ -457,6 +464,7 @@ function ReaderStatistics:getTotalStats()
end, 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,7 +530,9 @@ 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
return
end
local curr_time = TimeVal:now() local curr_time = TimeVal:now()
local diff_time = curr_time.sec - self.last_time.sec local diff_time = curr_time.sec - self.last_time.sec
@ -545,7 +555,6 @@ function ReaderStatistics:onPageUpdate(pageno)
end end
self.last_time = curr_time self.last_time = curr_time
end
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()
if not self:isDocless() then
self.ui.doc_settings:saveSetting("stats", self.data) self.ui.doc_settings:saveSetting("stats", self.data)
self.current_period = 0 self.current_period = 0
self.pages_current_period = 0 self.pages_current_period = 0
end
end end
-- screensaver off -- screensaver off

Loading…
Cancel
Save