ReaderStatus: Fix some more lifecycle issues in onEndOfBook

Delay anything that attempts to destroy the current document instance if
we're inside an event handler.

Re: https://github.com/koreader/koreader/issues/11147#issuecomment-1891005869
reviewable/pr11362/r2
NiLuJe 4 months ago
parent 0f5547a128
commit d205c72119

@ -38,7 +38,10 @@ function ReaderStatus:onEndOfBook()
local QuickStart = require("ui/quickstart") local QuickStart = require("ui/quickstart")
local last_file = G_reader_settings:readSetting("lastfile") local last_file = G_reader_settings:readSetting("lastfile")
if last_file == QuickStart.quickstart_filename then if last_file == QuickStart.quickstart_filename then
self:openFileBrowser() -- Like onOpenNextDocumentInFolder, delay this so as not to break instance lifecycle
UIManager:nextTick(function()
self:openFileBrowser()
end)
return return
end end
@ -59,15 +62,15 @@ function ReaderStatus:onEndOfBook()
return self.summary.status == "complete" and _("Mark as reading") or _("Mark as finished") return self.summary.status == "complete" and _("Mark as reading") or _("Mark as finished")
end, end,
callback = function() callback = function()
self:onMarkBook()
UIManager:close(button_dialog) UIManager:close(button_dialog)
self:onMarkBook()
end, end,
}, },
{ {
text = _("Book status"), text = _("Book status"),
callback = function() callback = function()
self:onShowBookStatus()
UIManager:close(button_dialog) UIManager:close(button_dialog)
self:onShowBookStatus()
end, end,
}, },
@ -76,16 +79,16 @@ function ReaderStatus:onEndOfBook()
{ {
text = _("Go to beginning"), text = _("Go to beginning"),
callback = function() callback = function()
self.ui:handleEvent(Event:new("GoToBeginning"))
UIManager:close(button_dialog) UIManager:close(button_dialog)
self.ui:handleEvent(Event:new("GoToBeginning"))
end, end,
}, },
{ {
text = _("Open next file"), text = _("Open next file"),
enabled = next_file_enabled, enabled = next_file_enabled,
callback = function() callback = function()
self:onOpenNextDocumentInFolder()
UIManager:close(button_dialog) UIManager:close(button_dialog)
self:onOpenNextDocumentInFolder()
end, end,
}, },
}, },
@ -93,15 +96,18 @@ function ReaderStatus:onEndOfBook()
{ {
text = _("Delete file"), text = _("Delete file"),
callback = function() callback = function()
self:deleteFile()
UIManager:close(button_dialog) UIManager:close(button_dialog)
self:deleteFile()
end, end,
}, },
{ {
text = _("File browser"), text = _("File browser"),
callback = function() callback = function()
self:openFileBrowser()
UIManager:close(button_dialog) UIManager:close(button_dialog)
-- Ditto
UIManager:nextTick(function()
self:openFileBrowser()
end)
end, end,
}, },
}, },
@ -123,11 +129,7 @@ function ReaderStatus:onEndOfBook()
UIManager:show(info) UIManager:show(info)
UIManager:forceRePaint() UIManager:forceRePaint()
UIManager:close(info) UIManager:close(info)
-- Delay until the next tick, as this will destroy the Document instance, self:onOpenNextDocumentInFolder()
-- but we may not be the final Event caught by said Document...
UIManager:nextTick(function()
self:onOpenNextDocumentInFolder()
end)
else else
UIManager:show(InfoMessage:new{ UIManager:show(InfoMessage:new{
text = _("Could not open next file. Sort by last read date does not support this feature."), text = _("Could not open next file. Sort by last read date does not support this feature."),
@ -172,7 +174,11 @@ function ReaderStatus:onOpenNextDocumentInFolder()
local FileChooser = require("ui/widget/filechooser") local FileChooser = require("ui/widget/filechooser")
local next_file = FileChooser:getNextFile(self.document.file) local next_file = FileChooser:getNextFile(self.document.file)
if next_file then if next_file then
self.ui:switchDocument(next_file) -- Delay until the next tick, as this will destroy the Document instance,
-- but we may not be the final Event caught by said Document...
UIManager:nextTick(function()
self.ui:switchDocument(next_file)
end)
else else
UIManager:show(InfoMessage:new{ UIManager:show(InfoMessage:new{
text = _("This is the last file in the current folder. No next file to open."), text = _("This is the last file in the current folder. No next file to open."),

Loading…
Cancel
Save