From 4044c81a5aed0d69651d242ee7477c7339994ae0 Mon Sep 17 00:00:00 2001 From: hius07 <62179190+hius07@users.noreply.github.com> Date: Fri, 10 Nov 2023 07:50:39 +0200 Subject: [PATCH] History: update on deleting a folder (#11090) --- frontend/apps/filemanager/filemanager.lua | 2 + frontend/apps/filemanager/filemanagermenu.lua | 2 +- frontend/readhistory.lua | 55 +++++++++++++++---- 3 files changed, 47 insertions(+), 12 deletions(-) diff --git a/frontend/apps/filemanager/filemanager.lua b/frontend/apps/filemanager/filemanager.lua index 797955ccb..f0b3627c3 100644 --- a/frontend/apps/filemanager/filemanager.lua +++ b/frontend/apps/filemanager/filemanager.lua @@ -988,6 +988,8 @@ function FileManager:deleteFile(file, is_file) if is_file then DocSettings.updateLocation(file) ReadHistory:fileDeleted(file) + else + ReadHistory:folderDeleted(file) end ReadCollection:removeItemByPath(file, not is_file) return true diff --git a/frontend/apps/filemanager/filemanagermenu.lua b/frontend/apps/filemanager/filemanagermenu.lua index 5956ff0a7..f58fbac4f 100644 --- a/frontend/apps/filemanager/filemanagermenu.lua +++ b/frontend/apps/filemanager/filemanagermenu.lua @@ -275,7 +275,7 @@ function FileManagerMenu:setUpdateItemTable() end, callback = function() G_reader_settings:flipNilOrFalse("history_datetime_short") - require("readhistory"):reload(true) + require("readhistory"):updateDateTimeString() end, }, { diff --git a/frontend/readhistory.lua b/frontend/readhistory.lua index 9d582e869..d33c8b17e 100644 --- a/frontend/readhistory.lua +++ b/frontend/readhistory.lua @@ -91,7 +91,7 @@ function ReadHistory:_flush() for _, v in ipairs(self.hist) do table.insert(content, { time = v.time, - file = v.file + file = v.file, }) end util.writeToFile(dump(content), history_file, true, true) @@ -143,14 +143,10 @@ function ReadHistory:_readLegacyHistory() os.remove(history_dir) end -function ReadHistory:_init() - self:reload() -end - function ReadHistory:ensureLastFile() local last_existing_file for _, v in ipairs(self.hist) do - if lfs.attributes(v.file, "mode") == "file" then + if v.select_enabled then last_existing_file = v.file break end @@ -167,7 +163,7 @@ function ReadHistory:getPreviousFile(current_file) end for _, v in ipairs(self.hist) do -- skip current document and deleted items kept in history - if v.file ~= current_file and lfs.attributes(v.file, "mode") == "file" then + if v.file ~= current_file and v.select_enabled then return v.file end end @@ -191,7 +187,6 @@ function ReadHistory:updateItemByPath(old_path, new_path) self.hist[index].file = new_path self.hist[index].text = new_path:gsub(".*/", "") self:_flush() - self:reload(true) end end @@ -212,14 +207,42 @@ function ReadHistory:updateItemsByPath(old_path, new_path) end --- Updates the history list after deleting a file. -function ReadHistory:fileDeleted(path) - local index = self:getIndexByFile(path) +function ReadHistory:fileDeleted(path_or_index) + local index + local is_single = type(path_or_index) == "string" + if is_single then -- deleting single file, path passed + index = self:getIndexByFile(path_or_index) + else -- deleting folder, index passed + index = path_or_index + end if index then if G_reader_settings:isTrue("autoremove_deleted_items_from_history") then - self:removeItem(self.hist[index], index) + self:removeItem(self.hist[index], index, not is_single) -- flush immediately when deleting single file only else self.hist[index].dim = true self.hist[index].select_enabled = false + if is_single then + self:ensureLastFile() + end + end + end +end + +--- Updates the history list after deleting a folder. +function ReadHistory:folderDeleted(path) + local history_updated + for i = #self.hist, 1, -1 do + local file = self.hist[i].file + if util.stringStartsWith(file, path) then + self:fileDeleted(i) + history_updated = true + DocSettings.updateLocation(file) -- remove sdr if not in book location + end + end + if history_updated then + if G_reader_settings:isTrue("autoremove_deleted_items_from_history") then + self:_flush() + else self:ensureLastFile() end end @@ -308,6 +331,12 @@ function ReadHistory:updateLastBookTime(no_flush) end end +function ReadHistory:updateDateTimeString() + for _, v in ipairs(self.hist) do + v.mandatory = getMandatory(v.time) + end +end + --- Reloads history from history_file and legacy history folder. function ReadHistory:reload(force_read) if self:_read(force_read) then @@ -319,6 +348,10 @@ function ReadHistory:reload(force_read) end end +function ReadHistory:_init() + self:reload() +end + ReadHistory:_init() return ReadHistory