Skip to content

Commit

Permalink
feat: add events autoUpdater
Browse files Browse the repository at this point in the history
  • Loading branch information
UrijHoruzij committed Jan 11, 2023
1 parent 728756a commit 4525bb0
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 35 deletions.
7 changes: 5 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
name: Release

on: workflow_dispatch

on:
workflow_run:
workflows: ['Semantic']
types:
- completed
jobs:
release-windows:
name: Release Windows
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ node_modules
.next
app
dist
out
3 changes: 2 additions & 1 deletion electron-builder.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
"nsis": {
"allowToChangeInstallationDirectory": true,
"oneClick": false,
"runAfterFinish": true
"runAfterFinish": true,
"perMachine": true
},
"files": ["main", "out"],
"publish": [{ "provider": "github", "repo": "super-resolution", "owner": "UrijHoruzij" }]
Expand Down
5 changes: 2 additions & 3 deletions main/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,12 @@ if (!isDev) {
serve({ directory: 'out' });
}

autoUpdate();

let mainWindow;
let splashWindow;
let files = [];
let saveDirectory = [];
let flag = false;
app.on('ready', async () => {
autoUpdater.checkForUpdatesAndNotify();
const local = await settings.get('local.name');
if (!!!local) {
await settings.set('local', {
Expand Down Expand Up @@ -91,6 +88,8 @@ app.on('ready', async () => {
} else {
await mainWindow.loadURL('app:https://./index.html');
}
autoUpdate(mainWindow);
autoUpdater.checkForUpdatesAndNotify();

ipcMain.on('upscayl', async () => {
if (!flag && files.length > 0 && saveDirectory.length > 0) {
Expand Down
51 changes: 26 additions & 25 deletions main/helpers/autoUpdate.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,31 @@
const { autoUpdater } = require('electron-updater');
const { dialog } = require('electron');
const { ipcMain } = require('electron');

const autoUpdate = () => {
autoUpdater.on('checking-for-update', () => {});
autoUpdater.on('update-available', ({ releaseNotes, releaseName }) => {
const dialogOpts = {
type: 'info',
buttons: ['Ok'],
title: 'Application Update',
message: process.platform === 'win32' ? releaseNotes : releaseName,
detail: 'A new version is being downloaded.',
};
dialog.showMessageBox(dialogOpts).then((returnValue) => {});
});
autoUpdater.on('update-not-available', () => {});
autoUpdater.on('update-downloaded', (event) => {
const dialogOpts = {
type: 'info',
buttons: ['Restart', 'Later'],
title: 'Application Update',
message: process.platform === 'win32' ? event.releaseNotes : event.releaseName,
detail: 'A new version has been downloaded. Restart the application to apply the updates.',
};
dialog.showMessageBox(dialogOpts).then((returnValue) => {
if (returnValue.response === 0) autoUpdater.quitAndInstall();
});
const autoUpdate = (mainWindow) => {
ipcMain.on('check-update', () => {
mainWindow.webContents.send('update-status', 'checking-for-update');
autoUpdater.checkForUpdatesAndNotify();
});
autoUpdater.on('checking-for-update', () => {
mainWindow.webContents.send('update-status', 'checking-for-update');
});
autoUpdater.on('update-available', () => {
mainWindow.webContents.send('update-status', 'update-available');
});
autoUpdater.on('update-not-available', () => {
mainWindow.webContents.send('update-status', 'update-not-available');
});
autoUpdater.on('update-downloaded', () => {
mainWindow.webContents.send('update-status', 'update-downloaded');
});
autoUpdater.on('error', () => {
mainWindow.webContents.send('update-status', 'error');
});
autoUpdater.on('download-progress', ({ bytesPerSecond, percent, total }) => {
mainWindow.webContents.send('update-status', 'download-progress', bytesPerSecond, percent, total);
});
ipcMain.on('update', () => {
autoUpdater.quitAndInstall();
});
};
module.exports = autoUpdate;
2 changes: 1 addition & 1 deletion src/components/Splash/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const Splash = (props) => {
</div>
<div className={styles.version}>v{version}</div>
</div>
<Image className={styles.splashImage} src={splashImage} alt="splash" />
<Image className={styles.splashImage} priority src={splashImage} alt="splash" />
</div>
);
};
Expand Down
12 changes: 11 additions & 1 deletion src/locale/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,17 @@
"settings": {
"title": "Settings",
"changeLang": "Change language:",
"changeDirectory": "Select the default folder:"
"changeDirectory": "Select the default folder:",
"autoUpdater": {
"checkingForUpdateBtn": "Check for updates",
"updateBtn": "Update",
"checkingForUpdate": "Checking for updates",
"updateAvailable": "Updates found",
"updateNotAvailable": "No updates found",
"updateDownloaded": "Updates downloaded",
"error": "Update error",
"downloadProgress": "Downloading"
}
},
"help": {
"title": "Help"
Expand Down
12 changes: 11 additions & 1 deletion src/locale/ru.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,17 @@
"settings": {
"title": "Настройки",
"changeLang": "Выбрать язык:",
"changeDirectory": "Выбрать папку по умолчанию:"
"changeDirectory": "Выбрать папку по умолчанию:",
"autoUpdater": {
"checkingForUpdateBtn": "Проверить наличия обновления",
"updateBtn": "Обновить",
"checkingForUpdate": "Проверка наличия обновления",
"updateAvailable": "Обновления найдены",
"updateNotAvailable": "Обновления не найдены",
"updateDownloaded": "Обновления загружены",
"error": "Ошибка обновления",
"downloadProgress": "Скачивание"
}
},
"help": {
"title": "Помощь"
Expand Down
43 changes: 42 additions & 1 deletion src/pages/settings.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import React, { useEffect, useRef, useContext } from 'react';
import React, { useEffect, useRef, useContext, useState } from 'react';
import Head from 'next/head';
import { Header, Sidebar, Main, LangContext } from '../components';
import { Button } from 'ui-forest';

function Settings() {
const [messages, changeLang, lang, setLang] = useContext(LangContext);
const [updateStatus, setUpdateStatus] = useState(null);
const firstUpdate = useRef(true);
const toggleLang = (e) => {
changeLang(e.target.value);
Expand All @@ -18,6 +20,35 @@ function Settings() {
});
}
}, [lang]);
const checkUpdateClick = () => {
window.electron.send('check-update');
};
const updateClick = () => {
window.electron.send('update');
};
const updateMessages = () => {
if (updateStatus) {
switch (updateStatus) {
case 'checking-for-update':
return messages.settings.autoUpdater.checkingForUpdate;
case 'update-available':
return messages.settings.autoUpdater.updateAvailable;
case 'update-not-available':
return messages.settings.autoUpdater.updateNotAvailable;
case 'update-downloaded':
return messages.settings.autoUpdater.updateDownloaded;
case 'error':
return messages.settings.autoUpdater.error;
case 'download-progress':
return messages.settings.autoUpdater.downloadProgress;
}
}
};
useEffect(() => {
window.electron.on('update-status', (event, status) => {
setUpdateStatus(status);
});
});
return (
<>
<Head />
Expand All @@ -38,6 +69,16 @@ function Settings() {
<td>{messages.settings.changeDirectory}</td>
<td></td>
</tr>
<tr>
<td>{updateMessages()}</td>
<td>
{updateStatus === 'update-downloaded' ? (
<Button onClick={updateClick}>{messages.settings.autoUpdater.updateBtn}</Button>
) : (
<Button onClick={checkUpdateClick}>{messages.settings.autoUpdater.checkingForUpdateBtn}</Button>
)}
</td>
</tr>
</table>
</Main>
</>
Expand Down

0 comments on commit 4525bb0

Please sign in to comment.