Add removed item name in "Remove this item from history" and fix wrong items deleted issue. (#2164)

* Add removed item name in "Remove this item from history".

* Fix #2162, wrong history items deleted.
pull/2168/head
Hzj_jie 8 years ago committed by Qingping Hou
parent 6a7f55f812
commit 7493fc89f6

@ -4,6 +4,7 @@ local ButtonDialog = require("ui/widget/buttondialog")
local UIManager = require("ui/uimanager") local UIManager = require("ui/uimanager")
local Menu = require("ui/widget/menu") local Menu = require("ui/widget/menu")
local Screen = require("device").screen local Screen = require("device").screen
local util = require("ffi/util")
local _ = require("gettext") local _ = require("gettext")
local FileManagerHistory = InputContainer:extend{ local FileManagerHistory = InputContainer:extend{
@ -38,7 +39,8 @@ function FileManagerHistory:onMenuHold(item)
buttons = { buttons = {
{ {
{ {
text = _("Remove this item from history"), text = util.template(_("Remove \"%1\" from history"),
item.text),
callback = function() callback = function()
require("readhistory"):removeItem(item) require("readhistory"):removeItem(item)
self._manager:updateItemTable() self._manager:updateItemTable()

@ -25,16 +25,21 @@ function DocSettings:getHistoryPath(fullpath)
end end
function DocSettings:getPathFromHistory(hist_name) function DocSettings:getPathFromHistory(hist_name)
if hist_name == nil or hist_name == '' then return nil end
-- 1. select everything included in brackets -- 1. select everything included in brackets
local s = string.match(hist_name,"%b[]") local s = string.match(hist_name,"%b[]")
if s == nil or s == '' then return nil end
-- 2. crop the bracket-sign from both sides -- 2. crop the bracket-sign from both sides
-- 3. and finally replace decorative signs '#' to dir-char '/' -- 3. and finally replace decorative signs '#' to dir-char '/'
return string.gsub(string.sub(s,2,-3),"#","/") return string.gsub(string.sub(s,2,-3),"#","/")
end end
function DocSettings:getNameFromHistory(hist_name) function DocSettings:getNameFromHistory(hist_name)
if hist_name == nil or hist_name == '' then return nil end
hist_name = string.match(hist_name, "%b[]")
if hist_name == nil or hist_name == '' then return nil end
-- at first, search for path length -- at first, search for path length
local s = string.len(string.match(hist_name,"%b[]")) local s = string.len(hist_name)
-- and return the rest of string without 4 last characters (".lua") -- and return the rest of string without 4 last characters (".lua")
return string.sub(hist_name, s+2, -5) return string.sub(hist_name, s+2, -5)
end end

@ -22,6 +22,13 @@ local function buildEntry(input_time, input_file)
} }
end end
function ReadHistory:_indexing(start)
-- TODO(Hzj_jie): Use binary search to find an item when deleting it.
for i = start, #self.hist, 1 do
self.hist[i].index = i
end
end
function ReadHistory:_sort() function ReadHistory:_sort()
for i = #self.hist, 1, -1 do for i = #self.hist, 1, -1 do
if lfs.attributes(self.hist[i].file, "mode") ~= "file" then if lfs.attributes(self.hist[i].file, "mode") ~= "file" then
@ -40,10 +47,7 @@ function ReadHistory:_sort()
end end
end end
table.sort(self.hist, function(v1, v2) return v1.time > v2.time end) table.sort(self.hist, function(v1, v2) return v1.time > v2.time end)
-- TODO(zijiehe): Use binary search to find an item when deleting it. self:_indexing(1)
for i = 1, #self.hist, 1 do
self.hist[i].index = i
end
end end
-- Reduces total count in hist list to a reasonable number by removing last -- Reduces total count in hist list to a reasonable number by removing last
@ -84,10 +88,16 @@ function ReadHistory:_readLegacyHistory()
for f in lfs.dir(history_dir) do for f in lfs.dir(history_dir) do
local path = joinPath(history_dir, f) local path = joinPath(history_dir, f)
if lfs.attributes(path, "mode") == "file" then if lfs.attributes(path, "mode") == "file" then
local file = joinPath(DocSettings:getPathFromHistory(f), path = DocSettings:getPathFromHistory(f)
DocSettings:getNameFromHistory(f)) if path ~= nil and path ~= "" then
table.insert(self.hist, local file = DocSettings:getNameFromHistory(f)
buildEntry(lfs.attributes(path, "modification"), file)) if file ~= nil and file ~= "" then
table.insert(
self.hist,
buildEntry(lfs.attributes(path, "modification"),
joinPath(path, file)))
end
end
end end
end end
end end
@ -102,6 +112,7 @@ end
function ReadHistory:removeItem(item) function ReadHistory:removeItem(item)
table.remove(self.hist, item.index) table.remove(self.hist, item.index)
os.remove(DocSettings:getHistoryPath(item.file)) os.remove(DocSettings:getHistoryPath(item.file))
self:_indexing(item.index)
self:_flush() self:_flush()
end end

Loading…
Cancel
Save