diff --git a/main.lua b/main.lua index 7e12438..ab4eacd 100644 --- a/main.lua +++ b/main.lua @@ -23,7 +23,7 @@ local lfs = require("libs/libkoreader-lfs") local IrcChatView = TextViewer:extend{ title = _("IRC Chat"), text = "", - add_default_buttons = true, + add_default_buttons = false, monospace_font = true, text_type = "code", keep_running = true, -- keep connection alive when UI is closed @@ -44,6 +44,7 @@ local IrcChatView = TextViewer:extend{ _history_dir = nil, _history_server_dir = nil, _history_preload_lines = 500, + _ui_open = false, } function IrcChatView:init() @@ -253,7 +254,7 @@ function IrcChatView:appendLine(line, target) -- Persist to history self:writeHistory(target, prefix .. line) 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:scrollToBottom() end @@ -600,6 +601,7 @@ end function IrcChatView:onClose() -- If keep_running, only close the UI; keep socket and receive loop alive in background if self.keep_running then + self._ui_open = false TextViewer.onClose(self) return end @@ -622,6 +624,24 @@ function IrcChatView:onClose() TextViewer.onClose(self) 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. function IrcChatView:showChannelSwitcher() 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 table.insert(buttons, { { @@ -1006,6 +1037,8 @@ function IRC:connectToServer(server) local nick = server.nick or self.username or "koreader" -- Reuse background session if already running for this host/port 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) return end @@ -1024,6 +1057,8 @@ function IRC:connectToServer(server) self._bg_view = view UIManager:show(view) end + -- Mark UI open + self._ui_open = true -- Ensure wifi/network is up before proceeding if NetworkMgr:willRerunWhenConnected(function() open_chat(channel) end) then return