From cfa45f8d883d434f3e69297578a082ba6e798767 Mon Sep 17 00:00:00 2001 From: poire-z Date: Sat, 29 Aug 2020 18:25:36 +0200 Subject: [PATCH] History: show last read date instead of file size In classic and list display modes. --- frontend/docsettings.lua | 7 ++++++ frontend/readhistory.lua | 25 ++++++++++++++++--- frontend/ui/widget/menu.lua | 6 +++-- plugins/coverbrowser.koplugin/listmenu.lua | 29 ++++++++++++++-------- 4 files changed, 51 insertions(+), 16 deletions(-) diff --git a/frontend/docsettings.lua b/frontend/docsettings.lua index 66223f54b..efbd618b6 100644 --- a/frontend/docsettings.lua +++ b/frontend/docsettings.lua @@ -83,6 +83,13 @@ function DocSettings:getNameFromHistory(hist_name) return string.sub(hist_name, string.len(s)+2, -5) end +function DocSettings:getLastSaveTime(doc_path) + local attr = lfs.attributes(self:getSidecarFile(doc_path)) + if attr and attr.mode == "file" then + return attr.modification + end +end + function DocSettings:ensureSidecar(sidecar) if lfs.attributes(sidecar, "mode") ~= "directory" then lfs.mkdir(sidecar) diff --git a/frontend/readhistory.lua b/frontend/readhistory.lua index 31ce10280..ff1a8a93e 100644 --- a/frontend/readhistory.lua +++ b/frontend/readhistory.lua @@ -2,7 +2,7 @@ local DataStorage = require("datastorage") local DocSettings = require("docsettings") local dump = require("dump") local ffiutil = require("ffi/util") -local getFriendlySize = require("util").getFriendlySize +local util = require("util") local joinPath = ffiutil.joinPath local lfs = require("libs/libkoreader-lfs") local realpath = ffiutil.realpath @@ -15,13 +15,30 @@ local ReadHistory = { } local function buildEntry(input_time, input_file) - local file_exists = lfs.attributes(input_file, "mode") == "file" + local file_path = realpath(input_file) or input_file -- keep orig file path of deleted files + local file_exists = lfs.attributes(file_path, "mode") == "file" return { time = input_time, text = input_file:gsub(".*/", ""), - file = realpath(input_file) or input_file, -- keep orig file path of deleted files + file = file_path, dim = not file_exists, -- "dim", as expected by Menu - mandatory = file_exists and getFriendlySize(lfs.attributes(input_file, "size") or 0), + -- mandatory = file_exists and require("util").getFriendlySize(lfs.attributes(input_file, "size") or 0), + mandatory_func = function() -- Show the last read time (rather than file size) + local readerui_instance = require("apps/reader/readerui"):_getRunningInstance() + local currently_opened_file = readerui_instance 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 it no sidecar file) + last_read_ts = DocSettings:getLastSaveTime(file_path) or input_time + end + return util.secondsToDate(last_read_ts, G_reader_settings:nilOrTrue("twelve_hour_clock")) + end, callback = function() local ReaderUI = require("apps/reader/readerui") ReaderUI:showReader(input_file) diff --git a/frontend/ui/widget/menu.lua b/frontend/ui/widget/menu.lua index 51329f0bb..e4f372cd9 100644 --- a/frontend/ui/widget/menu.lua +++ b/frontend/ui/widget/menu.lua @@ -236,12 +236,13 @@ function MenuItem:init() -- Padding before mandatory local text_mandatory_padding = 0 local text_ellipsis_mandatory_padding = 0 - if self.mandatory then + local mandatory = self.mandatory_func and self.mandatory_func() or self.mandatory + if mandatory then text_mandatory_padding = Size.span.horizontal_default -- Smaller padding when ellipsis for better visual feeling text_ellipsis_mandatory_padding = Size.span.horizontal_small end - local mandatory = self.mandatory and ""..self.mandatory or "" + mandatory = mandatory and ""..mandatory or "" local mandatory_widget = TextWidget:new{ text = mandatory, face = self.info_face, @@ -1014,6 +1015,7 @@ function Menu:updateItems(select_number) text = Menu.getMenuText(self.item_table[i]), bidi_wrap_func = self.item_table[i].bidi_wrap_func, mandatory = self.item_table[i].mandatory, + mandatory_func = self.item_table[i].mandatory_func, bold = self.item_table.current == i or self.item_table[i].bold == true, dim = self.item_table[i].dim, font = "smallinfofont", diff --git a/plugins/coverbrowser.koplugin/listmenu.lua b/plugins/coverbrowser.koplugin/listmenu.lua index be92a625c..7191430ff 100644 --- a/plugins/coverbrowser.koplugin/listmenu.lua +++ b/plugins/coverbrowser.koplugin/listmenu.lua @@ -224,7 +224,7 @@ function ListMenuItem:update() self.is_directory = true -- nb items on the right, directory name on the left local wright = TextWidget:new{ - text = self.mandatory, + text = self.mandatory_func and self.mandatory_func() or self.mandatory, face = Font:getFace("infont", math.min(max_fontsize_fileinfo, _fontSize(15))), } local pad_width = Screen:scaleBySize(10) -- on the left, in between, and on the right @@ -357,15 +357,23 @@ function ListMenuItem:update() -- pages read / nb of pages (not available for crengine doc not opened) local directory, filename = util.splitFilePathName(self.filepath) -- luacheck: no unused local filename_without_suffix, filetype = util.splitFileNameSuffix(filename) - local fileinfo_str = filetype - if self.mandatory then - fileinfo_str = self.mandatory .. " " .. BD.wrap(fileinfo_str) - end - if bookinfo._no_provider then - -- for unspported files: don't show extension on the right, - -- keep it in filename - filename_without_suffix = filename - fileinfo_str = self.mandatory + local fileinfo_str + if self.mandatory_func then + -- Currently only provided by History, giving the last time read. + -- Just show this date, without the file extension + fileinfo_str = self.mandatory_func() + else + if self.mandatory then + fileinfo_str = BD.wrap(self.mandatory) .. " " .. BD.wrap(filetype) + else + fileinfo_str = filetype + end + if bookinfo._no_provider then + -- for unspported files: don't show extension on the right, + -- keep it in filename + filename_without_suffix = filename + fileinfo_str = self.mandatory + end end -- Current page / pages are available or more accurate in .sdr/metadata.lua -- We use a cache (cleaned at end of this browsing session) to store @@ -949,6 +957,7 @@ function ListMenu:_updateItemsBuildUI() text = getMenuText(entry), show_parent = self.show_parent, mandatory = entry.mandatory, + mandatory_func = entry.mandatory_func, dimen = self.item_dimen:new(), shortcut = item_shortcut, shortcut_style = shortcut_style,