diff --git a/_meta.lua b/_meta.lua index dbbdf4c..f2ef3e9 100644 --- a/_meta.lua +++ b/_meta.lua @@ -3,5 +3,5 @@ return { name = "askgpt", fullname = _("AskGPT"), description = _([[Allows the user to query the ChatGPT API for answers to questions about highlighted text.]]), - version = 0.9, + version = 1.0, } diff --git a/main.lua b/main.lua index d842540..2ed3444 100644 --- a/main.lua +++ b/main.lua @@ -4,6 +4,7 @@ local NetworkMgr = require("ui/network/manager") local _ = require("gettext") local showChatGPTDialog = require("dialogs") +local UpdateChecker = require("update_checker") local AskGPT = InputContainer:new { name = "askgpt", @@ -16,7 +17,10 @@ function AskGPT:init() text = _("Ask ChatGPT"), enabled = Device:hasClipboard(), callback = function() - NetworkMgr:runWhenOnline(function() showChatGPTDialog(self.ui, _reader_highlight_instance.selected_text.text) end) + NetworkMgr:runWhenOnline(function() + UpdateChecker.checkForUpdates() + showChatGPTDialog(self.ui, _reader_highlight_instance.selected_text.text) + end) end, } end) diff --git a/update_checker.lua b/update_checker.lua new file mode 100644 index 0000000..8d93d65 --- /dev/null +++ b/update_checker.lua @@ -0,0 +1,40 @@ +local http = require("socket.http") +local ltn12 = require("ltn12") +local json = require("json") +local meta = require("_meta") +local InfoMessage = require("ui/widget/infomessage") +local UIManager = require("ui/uimanager") + +local function checkForUpdates() + local response_body = {} + local _, code = http.request { + url = "https://api.github.com/repos/drewbaumann/AskGPT/releases/latest", + headers = { + ["Accept"] = "application/vnd.github.v3+json" + }, + sink = ltn12.sink.table(response_body) + } + + if code == 200 then + local data = table.concat(response_body) + local parsed_data = json.decode(data) + local latest_version = parsed_data.tag_name -- e.g., "v0.9" + local stripped_latest_version = latest_version:match("^v(.+)$") + -- Compare with current version + if meta.version < tonumber(stripped_latest_version) then + -- Show notification to the user if a new version is available + local message = "A new version of the app (" .. latest_version .. ") is available. Please update!" + local info_message = InfoMessage:new{ + text = message, + timeout = 5 -- Display message for 5 seconds + } + UIManager:show(info_message) + end + else + print("Failed to check for updates. HTTP code:", code) + end +end + +return { + checkForUpdates = checkForUpdates +} \ No newline at end of file