Skip to content

Commit

Permalink
feat: Update checker (#16)
Browse files Browse the repository at this point in the history
* When there is a new version available, the update checker will alert the user that a new version is available
  • Loading branch information
drewbaumann authored Oct 25, 2024
1 parent 5a107e1 commit e77efeb
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
2 changes: 1 addition & 1 deletion _meta.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
6 changes: 5 additions & 1 deletion main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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)
Expand Down
40 changes: 40 additions & 0 deletions update_checker.lua
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit e77efeb

Please sign in to comment.