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 ReaderStatistics = Widget:extend{
is_doc_only = true,
last_time = nil,
page_min_read_sec = 5,
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()
if self.ui.document.is_pic then
if not self:isDocless() and self.ui.document.is_pic then
return
end
@ -69,8 +72,10 @@ function ReaderStatistics:getBookProperties()
end
function ReaderStatistics:initData(config)
if self:isDocless() or not self.is_enabled then
return
end
-- first execution
if self.is_enabled then
if not self.data then
self.data = { performance_in_pages= {} }
self:inplaceMigration(); -- first time merge data
@ -85,7 +90,6 @@ function ReaderStatistics:initData(config)
self.data.pages = self.view.document:getPageCount()
return
end
end
local function generateReadBooksTable(title, dates)
local result = {}
@ -104,13 +108,13 @@ function ReaderStatistics:getStatisticEnabledMenuItem()
checked_func = function() return self.is_enabled end,
callback = function()
-- 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)
end
self.is_enabled = not self.is_enabled
-- 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)
end
self:saveSettings()
@ -178,7 +182,8 @@ function ReaderStatistics:addToMainMenu(tab_item_table)
title = _("Statistics"),
kv_pairs = self:getCurrentStat(),
})
end
end,
enabled = not self:isDocless()
},
{
text = _("All books"),
@ -445,7 +450,9 @@ local function getDatesForBook(book)
end
function ReaderStatistics:getTotalStats()
local total_stats = {
local total_stats = {}
if not self:isDocless() then
total_stats = {
{
self.data.title,
util.secondsToClock(self.data.total_time_in_sec, false),
@ -457,6 +464,7 @@ function ReaderStatistics:getTotalStats()
end,
}
}
end
-- find stats for all other books in history
local proceded_titles, total_books_time = self:getStatisticsFromHistory(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
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 diff_time = curr_time.sec - self.last_time.sec
@ -546,7 +556,6 @@ function ReaderStatistics:onPageUpdate(pageno)
self.last_time = curr_time
end
end
-- For backward compatibility
function ReaderStatistics:inplaceMigration()
@ -576,7 +585,7 @@ function ReaderStatistics:importFromFile(base_path, item)
end
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)
end
end
@ -592,10 +601,11 @@ end
-- in case when screensaver starts
function ReaderStatistics:onSaveSettings()
self:saveSettings()
if not self:isDocless() then
self.ui.doc_settings:saveSetting("stats", self.data)
self.current_period = 0
self.pages_current_period = 0
end
end
-- screensaver off

Loading…
Cancel
Save