Skip to content

Commit

Permalink
Add version check
Browse files Browse the repository at this point in the history
  • Loading branch information
ubavic committed Dec 7, 2024
1 parent d4af3c1 commit 3be4963
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 2 deletions.
3 changes: 3 additions & 0 deletions embed/translation/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
"about.moreAboutProgram": "More about the program at",
"about.title": "Baš Čelik - Program for Reading Smart Card Documents",
"about.version": "Version",
"about.checkVersion": "Check version",
"about.newVersionAvailable": "New version %s is available.",
"about.youHaveLatestVersion": "You have the latest version.",
"error.dataUpdate": "Error updating data.",
"error.driver": "Error with smart card driver.",
"error.driverExplanation": "Does the program have the required permissions? Please restart the program.",
Expand Down
3 changes: 3 additions & 0 deletions embed/translation/sr-cyrillic.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
"about.moreAboutProgram": "Више о програму на адреси",
"about.title": "Баш Челик - програм за очитавање електронских докумената",
"about.version": "Верзија",
"about.checkVersion": "Провери верзију",
"about.newVersionAvailable": "Нова верзија %s је доступна.",
"about.youHaveLatestVersion": "Поседујете најновију верзију програма.",
"error.dataUpdate": "Грешка приликом ажурирања података",
"error.driver": "Грешка при употреби драјвера за паметне картице.",
"error.driverExplanation": "Да ли програм има неопходне дозволе? Рестартујте апликацију.",
Expand Down
3 changes: 3 additions & 0 deletions embed/translation/sr-latin.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
"about.moreAboutProgram": "Više o programu na adresi",
"about.title": "Baš Čelik - program za očitavanje elektronskih dokumenata",
"about.version": "Verzija",
"about.checkVersion": "Proveri verziju",
"about.newVersionAvailable": "Nova verzija %s je dostupna.",
"about.youHaveLatestVersion": "Posedujete najnoviju verziju programa.",
"error.dataUpdate": "Greška prilikom ažuriranja podataka",
"error.driver": "Greška pri upotrebi drajvera za pametne kartice.",
"error.driverExplanation": "Da li program ima neophodne dozvole? Restartujte aplikaciju.",
Expand Down
55 changes: 53 additions & 2 deletions internal/gui/about.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package gui

import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"time"

"fyne.io/fyne/v2"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/dialog"
"fyne.io/fyne/v2/widget"
"github.com/ubavic/bas-celik/internal/gui/widgets"
"github.com/ubavic/bas-celik/internal/logger"
)

func showAboutBox(win fyne.Window, version string) func() {
Expand All @@ -16,8 +21,23 @@ func showAboutBox(win fyne.Window, version string) func() {
url, _ := url.Parse("https://github.com/ubavic/bas-celik")
linkLabel := widget.NewHyperlink("github.com/ubavic/bas-celik", url)
spacer := widgets.NewSpacer()
hBox := container.NewHBox(moreLabel, linkLabel)
vBox := container.NewVBox(verLabel, hBox, spacer)
verButton := widget.NewButton(t("about.checkVersion"), func() {
newVersion, err := checkForUpdate()
if err != nil || newVersion == "" {
logger.Error(err)
return
}

if newVersion != "v"+version {
dialog.ShowInformation(t("about.version"), fmt.Sprintf(t("about.newVersionAvailable"), newVersion), win)
} else {
dialog.ShowInformation(t("about.version"), t("about.youHaveLatestVersion"), win)
}
})

hBox0 := container.NewHBox(verLabel, verButton)
hBox1 := container.NewHBox(moreLabel, linkLabel)
vBox := container.NewVBox(hBox0, hBox1, spacer)

return func() {
dialog.ShowCustom(
Expand All @@ -28,3 +48,34 @@ func showAboutBox(win fyne.Window, version string) func() {
)
}
}

func checkForUpdate() (string, error) {
client := http.Client{
Timeout: time.Second,
}

url := `https://api.github.com/repos/ubavic/bas-celik/releases/latest`
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return "", fmt.Errorf("creating request: %w", err)
}

res, err := client.Do(req)
if err != nil {
return "", fmt.Errorf("executing request: %w", err)
}

if res.Body != nil {
defer res.Body.Close()
}

response := struct {
TagName string `json:"tag_name"`
}{}
err = json.NewDecoder(res.Body).Decode(&response)
if err != nil {
return "", fmt.Errorf("decoding response: %w", err)
}

return response.TagName, nil
}

0 comments on commit 3be4963

Please sign in to comment.