irc: add Disconnect action; avoid double Close by removing default buttons; guard UI updates when closed; reinit view when reopening background session; rename server action when connected
Signed-off-by: Slendi <slendi@socopon.com>
This commit is contained in:
39
main.lua
39
main.lua
@@ -23,7 +23,7 @@ local lfs = require("libs/libkoreader-lfs")
|
|||||||
local IrcChatView = TextViewer:extend{
|
local IrcChatView = TextViewer:extend{
|
||||||
title = _("IRC Chat"),
|
title = _("IRC Chat"),
|
||||||
text = "",
|
text = "",
|
||||||
add_default_buttons = true,
|
add_default_buttons = false,
|
||||||
monospace_font = true,
|
monospace_font = true,
|
||||||
text_type = "code",
|
text_type = "code",
|
||||||
keep_running = true, -- keep connection alive when UI is closed
|
keep_running = true, -- keep connection alive when UI is closed
|
||||||
@@ -44,6 +44,7 @@ local IrcChatView = TextViewer:extend{
|
|||||||
_history_dir = nil,
|
_history_dir = nil,
|
||||||
_history_server_dir = nil,
|
_history_server_dir = nil,
|
||||||
_history_preload_lines = 500,
|
_history_preload_lines = 500,
|
||||||
|
_ui_open = false,
|
||||||
}
|
}
|
||||||
|
|
||||||
function IrcChatView:init()
|
function IrcChatView:init()
|
||||||
@@ -253,7 +254,7 @@ function IrcChatView:appendLine(line, target)
|
|||||||
-- Persist to history
|
-- Persist to history
|
||||||
self:writeHistory(target, prefix .. line)
|
self:writeHistory(target, prefix .. line)
|
||||||
if target == (self._current_target or "*") then
|
if target == (self._current_target or "*") then
|
||||||
if self.scroll_text_w and self.scroll_text_w.text_widget then
|
if self._ui_open and self.scroll_text_w and self.scroll_text_w.text_widget then
|
||||||
self.scroll_text_w.text_widget:setText(buf)
|
self.scroll_text_w.text_widget:setText(buf)
|
||||||
self.scroll_text_w:scrollToBottom()
|
self.scroll_text_w:scrollToBottom()
|
||||||
end
|
end
|
||||||
@@ -600,6 +601,7 @@ end
|
|||||||
function IrcChatView:onClose()
|
function IrcChatView:onClose()
|
||||||
-- If keep_running, only close the UI; keep socket and receive loop alive in background
|
-- If keep_running, only close the UI; keep socket and receive loop alive in background
|
||||||
if self.keep_running then
|
if self.keep_running then
|
||||||
|
self._ui_open = false
|
||||||
TextViewer.onClose(self)
|
TextViewer.onClose(self)
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
@@ -622,6 +624,24 @@ function IrcChatView:onClose()
|
|||||||
TextViewer.onClose(self)
|
TextViewer.onClose(self)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function IrcChatView:disconnect()
|
||||||
|
-- Terminate receive loop and close socket
|
||||||
|
if self._receive_task then
|
||||||
|
UIManager:unschedule(self._receive_task)
|
||||||
|
self._receive_task = nil
|
||||||
|
end
|
||||||
|
if self._sock then
|
||||||
|
pcall(function()
|
||||||
|
self:sendRaw("QUIT :bye\r\n")
|
||||||
|
self._sock:close()
|
||||||
|
end)
|
||||||
|
self._sock = nil
|
||||||
|
end
|
||||||
|
self._connected = false
|
||||||
|
self._closing = true
|
||||||
|
self:appendLine(_("Disconnected."))
|
||||||
|
end
|
||||||
|
|
||||||
-- Extend the hamburger menu to add a list of joined channels/targets.
|
-- Extend the hamburger menu to add a list of joined channels/targets.
|
||||||
function IrcChatView:showChannelSwitcher()
|
function IrcChatView:showChannelSwitcher()
|
||||||
local Menu = require("ui/widget/menu")
|
local Menu = require("ui/widget/menu")
|
||||||
@@ -674,6 +694,17 @@ function IrcChatView:onShowMenu()
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
-- Disconnect action
|
||||||
|
table.insert(buttons, {
|
||||||
|
{
|
||||||
|
text = _("Disconnect"),
|
||||||
|
enabled_func = function() return self._sock ~= nil end,
|
||||||
|
callback = function()
|
||||||
|
self:disconnect()
|
||||||
|
end,
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
-- Font size
|
-- Font size
|
||||||
table.insert(buttons, {
|
table.insert(buttons, {
|
||||||
{
|
{
|
||||||
@@ -1006,6 +1037,8 @@ function IRC:connectToServer(server)
|
|||||||
local nick = server.nick or self.username or "koreader"
|
local nick = server.nick or self.username or "koreader"
|
||||||
-- Reuse background session if already running for this host/port
|
-- Reuse background session if already running for this host/port
|
||||||
if self._bg_view and self._bg_view._sock then
|
if self._bg_view and self._bg_view._sock then
|
||||||
|
if self._bg_view.reinit then self._bg_view:reinit() end
|
||||||
|
self._bg_view._ui_open = true
|
||||||
UIManager:show(self._bg_view)
|
UIManager:show(self._bg_view)
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
@@ -1024,6 +1057,8 @@ function IRC:connectToServer(server)
|
|||||||
self._bg_view = view
|
self._bg_view = view
|
||||||
UIManager:show(view)
|
UIManager:show(view)
|
||||||
end
|
end
|
||||||
|
-- Mark UI open
|
||||||
|
self._ui_open = true
|
||||||
-- Ensure wifi/network is up before proceeding
|
-- Ensure wifi/network is up before proceeding
|
||||||
if NetworkMgr:willRerunWhenConnected(function() open_chat(channel) end) then
|
if NetworkMgr:willRerunWhenConnected(function() open_chat(channel) end) then
|
||||||
return
|
return
|
||||||
|
|||||||
Reference in New Issue
Block a user