Skip to content

Commit

Permalink
Add update check
Browse files Browse the repository at this point in the history
  • Loading branch information
mishamyrt committed Aug 7, 2023
1 parent 880bd77 commit 702b8d1
Show file tree
Hide file tree
Showing 9 changed files with 114 additions and 10 deletions.
1 change: 0 additions & 1 deletion .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ linters:
- nestif
- nilerr
- nilnil
- noctx
- nolintlint
- nonamedreturns
- nosprintfhostport
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog][],
and this project adheres to [Semantic Versioning][].

## 1.0.0-beta7

## Add

* Mechanism for searching for application updates

## [1.0.0-beta6]

## Changed
Expand Down
15 changes: 14 additions & 1 deletion app/frontend/src/App.svelte
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
<script lang="ts">
import { onDestroy, onMount } from 'svelte'
import LoadingView from './views/LoadingView.svelte'
import { BrowserOpenURL } from '../wailsjs/runtime'
import { focused, version } from '@stores/app'
import SidebarItem from './components/SidebarItem.svelte'
import { view, connected } from './stores/app'
import Button from './components/Button.svelte'
import { view, connected, updateUrl } from './stores/app'
import LightsView from './views/LightsView.svelte'
import DeviceView from './views/DeviceView.svelte'
Expand All @@ -14,6 +16,14 @@
let hideLoading = false
function openUpdate (): void {
const url = $updateUrl
if (!url) {
return
}
BrowserOpenURL(url)
}
onMount(() => {
unsubscribeConnected = connected.subscribe(isConnected => {
if (isConnected) {
Expand Down Expand Up @@ -43,6 +53,9 @@
</div>
</div>
<div>
{#if $updateUrl}
<Button on:click={openUpdate} label="Update available" autosize variant="warning" />
{/if}
<span class="version">{appVersion}</span>
</div>
</div>
Expand Down
24 changes: 22 additions & 2 deletions app/frontend/src/components/Button.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,22 @@
import { createEventDispatcher } from 'svelte'
export let label: string = ''
export let primary = false
export let variant: 'warning' | 'primary' | 'default' = 'default'
export let disabled = false
export let autosize = false
const dispatch = createEventDispatcher()
function handleClick (): void {
dispatch('click')
}
</script>

<button {disabled} on:click={handleClick} class:primary>{label}</button>
<button {disabled}
on:click={handleClick}
class:autosize
class:primary={variant === 'primary'}
class:warning={variant === 'warning'}
>{label}</button>

<style lang="scss">
button {
Expand All @@ -23,6 +29,11 @@
width: 64px;
border-top: 0.5px solid rgb(255 255 255 / 22%);
&.autosize {
width: unset;
padding: 2px 10px;
}
&:disabled {
opacity: 0.5;
pointer-events: none;
Expand All @@ -39,5 +50,14 @@
background: #457AEB;
}
}
&.warning {
background: linear-gradient(180deg, #E26A4F 0%, #B84228 100%);
&:active {
background: #AB432C;
}
}
}
</style>
2 changes: 1 addition & 1 deletion app/frontend/src/components/ColorPickerModal.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ async function applyColor (): Promise<void> {
</div>
<div class="actions">
<Button label="Cancel" on:click={hideModal} />
<Button label="Done" primary on:click={applyColor} />
<Button label="Done" variant="primary" on:click={applyColor} />
</div>
</div>
</div>
Expand Down
11 changes: 9 additions & 2 deletions app/frontend/src/stores/app.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { GetVersion, SetMode } from '../../wailsjs/go/nuga/App'
import { GetVersion, SetMode, CheckUpdates } from '../../wailsjs/go/nuga/App'
import { EventsOn } from '../../wailsjs/runtime'
import { atom } from 'nanostores'

export type SettingsView = 'lights' | 'device' | 'keys'
Expand All @@ -10,6 +11,7 @@ export const osMode = atom<OSMode>('mac')
export const version = atom<string>('dev')
export const individualSettings = atom<boolean>(false)
export const focused = atom<boolean>(true)
export const updateUrl = atom<string | undefined>()

window.addEventListener('blur', () => focused.set(false))
window.addEventListener('focus', () => focused.set(true))
Expand All @@ -23,7 +25,12 @@ export async function setMode (): Promise<void> {
await SetMode(mode)
}

export async function loadVersion (): Promise<void> {
export async function loadApp (): Promise<void> {
const appVersion = await GetVersion()
version.set(appVersion)
await CheckUpdates()
}

EventsOn('update', value => {
updateUrl.set(value)
})
6 changes: 3 additions & 3 deletions app/frontend/src/views/LoadingView.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { onMount } from 'svelte'
import cable from '../assets/images/cable.connector.svg'
import { connect, startSimulation } from '@stores/device'
import { connected, loadVersion } from '@stores/app'
import { connected, loadApp } from '@stores/app'
import { sleep } from '@utils/timing'
import { onHotkey } from '@utils/hotkey'
import { sync } from '@stores/lights/actions'
Expand All @@ -17,7 +17,7 @@
showHelp = true
}, 1500)
await Promise.all([
loadVersion(),
loadApp(),
connect(),
sleep(1000)
]).catch(console.error)
Expand All @@ -30,7 +30,7 @@
<div class="help" class:show={showHelp}>
<img alt="Wails logo" id="logo" src="{cable}">
<h1>Looking for compatible keyboard</h1>
<p>Make sure your device is plugged in</p>
<p>Make sure that the device is connected with a wire</p>
</div>
<div class="spinner-container">
<div class="spinner">
Expand Down
15 changes: 15 additions & 0 deletions app/internal/nuga/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"nuga/pkg/hid"
"nuga/pkg/light"
"nuga/pkg/light/effect"
"nuga_ui/internal/updates"
"os"

"github.com/wailsapp/wails/v2/pkg/runtime"
Expand All @@ -32,6 +33,20 @@ func (a *App) OnStartup(ctx context.Context) {
a.ctx = ctx
}

// CheckUpdates starts update check in background
func (a *App) CheckUpdates() {
go func() {
updater := updates.GitHubUpdater{FullName: "mishamyrt/Nuga"}
latest, err := updater.Latest()
if err != nil || len(latest) == 0 {
return
}
if AppVersion != latest {
runtime.EventsEmit(a.ctx, "update", updater.ReleaseURL(latest))
}
}()
}

// OnShutdown is called when the app closes
func (a *App) OnShutdown(_ context.Context) {
err := hid.Exit()
Expand Down
44 changes: 44 additions & 0 deletions app/internal/updates/github.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Package updates contains tools for update checking
package updates

import (
"encoding/json"
"io/ioutil"
"net/http"
)

type gitHubTag struct {
Name string `json:"name"`
}

// GitHubUpdater represents GitHub update checker
type GitHubUpdater struct {
FullName string
}

// ReleaseURL formats release page URL from tag
func (u *GitHubUpdater) ReleaseURL(tag string) string {
return "https://github.com/" + u.FullName + "/releases/tag/" + tag
}

// Latest returns latest tag
func (u *GitHubUpdater) Latest() (string, error) {
response, err := http.Get("https://api.github.com/repos/" + u.FullName + "/tags")
if err != nil {
return "", err
}
defer response.Body.Close()
data, err := ioutil.ReadAll(response.Body)
if err != nil {
return "", err
}
var tags []gitHubTag
err = json.Unmarshal(data, &tags)
if err != nil {
return "", err
}
if len(tags) < 1 {
return "", nil
}
return tags[0].Name, nil
}

0 comments on commit 702b8d1

Please sign in to comment.