From 5c9ba533532a452240b4e99fcbfb4995f38fa1c0 Mon Sep 17 00:00:00 2001 From: hius07 <62179190+hius07@users.noreply.github.com> Date: Thu, 23 Feb 2023 19:23:30 +0200 Subject: [PATCH] History: update last book access time (#10156) --- frontend/apps/reader/readerui.lua | 1 + frontend/datetime.lua | 3 ++ frontend/readhistory.lua | 44 +++++++++++++--------------- frontend/ui/widget/touchmenu.lua | 2 +- plugins/systemstat.koplugin/main.lua | 7 ++--- 5 files changed, 28 insertions(+), 29 deletions(-) diff --git a/frontend/apps/reader/readerui.lua b/frontend/apps/reader/readerui.lua index 3775bd176..11a369b0e 100644 --- a/frontend/apps/reader/readerui.lua +++ b/frontend/apps/reader/readerui.lua @@ -792,6 +792,7 @@ function ReaderUI:onClose(full_refresh) self:saveSettings() end if self.document ~= nil then + require("readhistory"):updateLastBookTime(self.tearing_down) -- Serialize the most recently displayed page for later launch DocCache:serialize(self.document.file) logger.dbg("closing document") diff --git a/frontend/datetime.lua b/frontend/datetime.lua index 2852401d9..6477b2ab6 100644 --- a/frontend/datetime.lua +++ b/frontend/datetime.lua @@ -295,6 +295,9 @@ end ---- @treturn string date+time function datetime.secondsToDateTime(seconds, twelve_hour_clock, use_locale) seconds = seconds or os.time() + if twelve_hour_clock == nil then + twelve_hour_clock = G_reader_settings:isTrue("twelve_hour_clock") + end local BD = require("ui/bidi") local date_string = datetime.secondsToDate(seconds, use_locale) local time_string = datetime.secondsToHour(seconds, twelve_hour_clock, not use_locale) diff --git a/frontend/readhistory.lua b/frontend/readhistory.lua index 277e3eda3..ab9a4a96d 100644 --- a/frontend/readhistory.lua +++ b/frontend/readhistory.lua @@ -23,30 +23,14 @@ end local function buildEntry(input_time, input_file) local file_path = realpath(input_file) or input_file -- keep orig file path of deleted files + local is_file_deleted = lfs.attributes(file_path, "mode") ~= "file" return { time = input_time, file = file_path, text = input_file:gsub(".*/", ""), - dim = lfs.attributes(file_path, "mode") ~= "file", -- "dim", as expected by Menu - mandatory_func = function() -- Show the last read time - local readerui_instance = require("apps/reader/readerui"):_getRunningInstance() - local currently_opened_file = readerui_instance and readerui_instance.document and readerui_instance.document.file - local last_read_ts - if file_path == currently_opened_file then - -- Don't use the sidecar file date which is updated regularly while - -- reading: keep showing the opening time for the current document. - last_read_ts = input_time - else - -- For past documents, the last save time of the settings is better - -- as last read time than input_time (its last opening time, that - -- we fallback to if no sidecar file) - last_read_ts = DocSettings:getLastSaveTime(file_path) or input_time - end - return datetime.secondsToDateTime(last_read_ts, G_reader_settings:isTrue("twelve_hour_clock")) - end, - select_enabled_func = function() - return lfs.attributes(file_path, "mode") == "file" - end, + dim = is_file_deleted, + mandatory = datetime.secondsToDateTime(input_time), + select_enabled = not is_file_deleted, callback = function() selectCallback(input_file) end, @@ -225,6 +209,7 @@ function ReadHistory:fileDeleted(path) self:removeItem(self.hist[index], index) else self.hist[index].dim = true + self.hist[index].select_enabled = false self:ensureLastFile() end end @@ -269,7 +254,7 @@ end --- Adds new item (last opened document) to the top of the history list. -- If item time (ts) is passed, add item to the history list at this time position. -function ReadHistory:addItem(file, ts, no_flash) +function ReadHistory:addItem(file, ts, no_flush) if file ~= nil and lfs.attributes(file, "mode") == "file" then local index = self:getIndexByFile(realpath(file)) if ts and index and self.hist[index].time == ts then @@ -277,9 +262,10 @@ function ReadHistory:addItem(file, ts, no_flash) end local now = ts or os.time() local mtime = lfs.attributes(file, "modification") - lfs.touch(file, now, mtime) - if index == 1 and not ts then -- last book + lfs.touch(file, now, mtime) -- update book access time for sorting by last read date + if index == 1 and not ts then -- last book, update access time only self.hist[1].time = now + self.hist[1].mandatory = datetime.secondsToDateTime(now) else -- old or new book if index then -- old book table.remove(self.hist, index) @@ -287,7 +273,7 @@ function ReadHistory:addItem(file, ts, no_flash) index = ts and self:getIndexByTime(ts, file:gsub(".*/", "")) or 1 table.insert(self.hist, index, buildEntry(now, file)) end - if not no_flash then + if not no_flush then self:_reduce() self:_flush() end @@ -295,6 +281,16 @@ function ReadHistory:addItem(file, ts, no_flash) end end +--- Updates last book access time on closing the document. +function ReadHistory:updateLastBookTime(no_flush) + local now = os.time() + self.hist[1].time = now + self.hist[1].mandatory = datetime.secondsToDateTime(now) + if not no_flush then + self:_flush() + end +end + --- Reloads history from history_file and legacy history folder. function ReadHistory:reload(force_read) if self:_read(force_read) then diff --git a/frontend/ui/widget/touchmenu.lua b/frontend/ui/widget/touchmenu.lua index 0af378924..b16265d19 100644 --- a/frontend/ui/widget/touchmenu.lua +++ b/frontend/ui/widget/touchmenu.lua @@ -569,7 +569,7 @@ function TouchMenu:init() text_font_bold = false, callback = function() UIManager:show(InfoMessage:new{ - text = datetime.secondsToDateTime(os.time(), G_reader_settings:isTrue("twelve_hour_clock"), true), + text = datetime.secondsToDateTime(nil, nil, true), }) end, hold_callback = function() diff --git a/plugins/systemstat.koplugin/main.lua b/plugins/systemstat.koplugin/main.lua index 98ff9d56b..48a532ad7 100644 --- a/plugins/systemstat.koplugin/main.lua +++ b/plugins/systemstat.koplugin/main.lua @@ -53,21 +53,20 @@ function SystemStat:putSeparator() end function SystemStat:appendCounters() - local use_twelve_hour_clock = G_reader_settings:isTrue("twelve_hour_clock") self:put({ _("KOReader started at"), - datetime.secondsToDateTime(time.to_s(self.start_time), use_twelve_hour_clock, true) + datetime.secondsToDateTime(time.to_s(self.start_time), nil, true) }) if self.suspend_time then self:put({ " " .. _("Last suspend time"), - datetime.secondsToDateTime(time.to_s(self.suspend_time), use_twelve_hour_clock, true) + datetime.secondsToDateTime(time.to_s(self.suspend_time), nil, true) }) end if self.resume_time then self:put({ " " .. _("Last resume time"), - datetime.secondsToDateTime(time.to_s(self.resume_time), use_twelve_hour_clock, true) + datetime.secondsToDateTime(time.to_s(self.resume_time), nil, true) }) end local uptime = time.boottime_or_realtime_coarse() - self.start_monotonic_time