Skip to content

Commit

Permalink
updater progress steps
Browse files Browse the repository at this point in the history
  • Loading branch information
0o-de-lally committed Jan 3, 2024
1 parent d7f5047 commit ee24bc8
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 34 deletions.
38 changes: 20 additions & 18 deletions src/components/about/UpgradeApp.svelte
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@
<script lang="ts">
import { onDestroy, onMount } from 'svelte'
import { updateStatus } from '../../modules/updater'
import { onDestroy } from 'svelte'
import { updateStatus, whatUpdateStep } from '../../modules/updater'
import { debugMode } from '../../modules/debug'
import UpgradeButtonManual from './UpgradeButtonManual.svelte'
import CardAlert from '../layout/CardAlert.svelte'
import { onUpdaterEvent } from '@tauri-apps/api/updater'
let unlisten
// onMount(async () => {
// unlisten = await
// })
onUpdaterEvent(({ error, status }) => {
// This will log all updater events, including status updates and errors.
updateStatus.update((u) => {
u.error = error ?? null
u.status = status
return u
})
console.log('onMount Updater event', error, status)
// This will log all updater events, including status updates and errors.
updateStatus.update((u) => {
u.error = error ?? null
u.status = status
return u
})
console.log('onMount Updater event', error, status)
})
onDestroy(() => {
// don't duplicate listener next time we navigate here
Expand All @@ -28,13 +25,15 @@
</script>

<main class="uk-padding">
{whatUpdateStep($updateStatus)}

Upgrade App
{JSON.stringify($updateStatus)}

{#if $updateStatus?.manifest}
<CardAlert>
<div slot="title">
{#if $updateStatus.refreshing}
{#if !$updateStatus}
<div class="uk-text-center">
<span class="uk-text-muted">Checking for upgrade</span>
<span class="uk-padding" uk-spinner="ratio: 0.66"></span>
Expand All @@ -46,11 +45,14 @@
{/if}
</div>

<div slot="body">
{#if !$updateStatus?.refreshing}
<div class="uk-padding uk-flex uk-grid">
<div class="uk-margin">
<!-- <h4>Update Available {$updateStatus?.manifest.version}</h4> -->
<div slot="body" class="uk-padding">
{#if $updateStatus?.refreshing}
<div class="uk-margin">
<progress class="uk-progress" value={whatUpdateStep($updateStatus)} max="4" />
<p>{$updateStatus.msg}</p>
</div>
<div class="uk-flex uk-grid">
<div>
<h5 class="uk-text-uppercase">Update Notes</h5>
<p>{$updateStatus?.manifest.body}</p>
</div>
Expand Down
6 changes: 3 additions & 3 deletions src/components/about/UpgradeButtonManual.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
<div>
<p>
{#if upToDate}
{$_('about.upgrade_uptodate')}
{$_('upgrade.uptodate')}
{:else}
{$_('about.upgrade_available')}
{$_('upgrade.available')}
{/if}
</p>
<button class="uk-button uk-button-default" on:click={tryUpdate} disabled={checking}>{$_('about.upgrade_checkagain')}</button>
<button class="uk-button uk-button-default" on:click={tryUpdate} disabled={checking}>{$_('upgrade.checkagain')}</button>
</div>
</main>
11 changes: 7 additions & 4 deletions src/lang/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,13 @@
"release": "Release",
"version": "Version",
"branch": "Branch",
"commit": "Commit",
"upgrade_uptodate": "You are on the current version",
"upgrade_available": "There is an upgrade available",
"upgrade_checkagain": "Check Upgrade"
"commit": "Commit"
},
"upgrade": {
"uptodate": "You are on the current version",
"available": "There is an upgrade available",
"check_again": "Check Upgrade",
"restart": "You can now restart the app"
},
"settings": {
"title": "SETTINGS",
Expand Down
29 changes: 20 additions & 9 deletions src/modules/updater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
} from '@tauri-apps/api/updater'
import { relaunch } from '@tauri-apps/api/process'
import { get, writable } from 'svelte/store'
import { _ } from 'svelte-i18n'

interface WhatsUpdate {
refreshing: boolean
Expand All @@ -28,31 +29,28 @@ export const tryUpdate = async () => {
updateStatus.set({ refreshing: true })
try {
const { shouldUpdate, manifest } = await checkUpdate()

const u = get(updateStatus)
if (shouldUpdate) {
u.refreshing = false

u.msg = 'upgrade is available'
if (shouldUpdate) {
u.msg = get(_('upgrade.available')) // use internationalization to have consistent strings
u.manifest = manifest

updateStatus.set(u)
// You could show a dialog asking the user if they want to install the update here.
console.log(`Installing update ${manifest?.version}, ${manifest?.date}, ${manifest?.body}`)

u.msg = 'attempting install'
updateStatus.set(u)
// Install the update. This will also restart the app on Windows!
await installUpdate()

u.msg = 'attempting relaunch'
u.msg = get(_('upgrade.restart'))
updateStatus.set(u)
// On macOS and Linux you will need to restart the app manually.
// You could use this step to display another confirmation dialog.
await relaunch()
u.refreshing = false

} else {
u.msg = 'You are on the latest version'
u.refreshing = false
u.msg = get(_('upgrade.uptodate'))
}
updateStatus.set(u)
} catch (e) {
Expand All @@ -76,3 +74,16 @@ export const parseErrorString = (msg: string): UpdateErrors => {
}
return errEnum
}

export const whatUpdateStep = (u: WhatsUpdate): number => {
switch (u.status) {
case 'PENDING':
return 1
case 'DOWNLOADED':
return 2
case 'DONE':
return 3
default:
return 0
}
}

0 comments on commit ee24bc8

Please sign in to comment.