History: update on deleting a folder (#11090)

reviewable/pr11088/r5
hius07 7 months ago committed by GitHub
parent 17a4aa962f
commit 4044c81a5a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -988,6 +988,8 @@ function FileManager:deleteFile(file, is_file)
if is_file then if is_file then
DocSettings.updateLocation(file) DocSettings.updateLocation(file)
ReadHistory:fileDeleted(file) ReadHistory:fileDeleted(file)
else
ReadHistory:folderDeleted(file)
end end
ReadCollection:removeItemByPath(file, not is_file) ReadCollection:removeItemByPath(file, not is_file)
return true return true

@ -275,7 +275,7 @@ function FileManagerMenu:setUpdateItemTable()
end, end,
callback = function() callback = function()
G_reader_settings:flipNilOrFalse("history_datetime_short") G_reader_settings:flipNilOrFalse("history_datetime_short")
require("readhistory"):reload(true) require("readhistory"):updateDateTimeString()
end, end,
}, },
{ {

@ -91,7 +91,7 @@ function ReadHistory:_flush()
for _, v in ipairs(self.hist) do for _, v in ipairs(self.hist) do
table.insert(content, { table.insert(content, {
time = v.time, time = v.time,
file = v.file file = v.file,
}) })
end end
util.writeToFile(dump(content), history_file, true, true) util.writeToFile(dump(content), history_file, true, true)
@ -143,14 +143,10 @@ function ReadHistory:_readLegacyHistory()
os.remove(history_dir) os.remove(history_dir)
end end
function ReadHistory:_init()
self:reload()
end
function ReadHistory:ensureLastFile() function ReadHistory:ensureLastFile()
local last_existing_file local last_existing_file
for _, v in ipairs(self.hist) do 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 last_existing_file = v.file
break break
end end
@ -167,7 +163,7 @@ function ReadHistory:getPreviousFile(current_file)
end end
for _, v in ipairs(self.hist) do for _, v in ipairs(self.hist) do
-- skip current document and deleted items kept in history -- 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 return v.file
end end
end end
@ -191,7 +187,6 @@ function ReadHistory:updateItemByPath(old_path, new_path)
self.hist[index].file = new_path self.hist[index].file = new_path
self.hist[index].text = new_path:gsub(".*/", "") self.hist[index].text = new_path:gsub(".*/", "")
self:_flush() self:_flush()
self:reload(true)
end end
end end
@ -212,14 +207,42 @@ function ReadHistory:updateItemsByPath(old_path, new_path)
end end
--- Updates the history list after deleting a file. --- Updates the history list after deleting a file.
function ReadHistory:fileDeleted(path) function ReadHistory:fileDeleted(path_or_index)
local index = self:getIndexByFile(path) 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 index then
if G_reader_settings:isTrue("autoremove_deleted_items_from_history") 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 else
self.hist[index].dim = true self.hist[index].dim = true
self.hist[index].select_enabled = false 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() self:ensureLastFile()
end end
end end
@ -308,6 +331,12 @@ function ReadHistory:updateLastBookTime(no_flush)
end end
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. --- Reloads history from history_file and legacy history folder.
function ReadHistory:reload(force_read) function ReadHistory:reload(force_read)
if self:_read(force_read) then if self:_read(force_read) then
@ -319,6 +348,10 @@ function ReadHistory:reload(force_read)
end end
end end
function ReadHistory:_init()
self:reload()
end
ReadHistory:_init() ReadHistory:_init()
return ReadHistory return ReadHistory

Loading…
Cancel
Save