From 9e2df29e63d15ed7144660e90b42a40734716c3c Mon Sep 17 00:00:00 2001 From: Oleh Aloshkin Date: Thu, 18 Jun 2020 15:12:00 +0200 Subject: [PATCH 1/4] Add automatic check for udpates --- src/app/autoUpdate/available.js | 38 +++++++++++++++ .../autoUpdate/checkForUpdateAutomatically.js | 14 ++++++ src/app/autoUpdate/downloaded.js | 45 +----------------- src/app/autoUpdate/notAvailable.js | 2 + src/app/autoUpdate/onUpdateDownloaded.js | 47 +++++++++++++++++++ src/app/autoUpdate/resetUpdateTimer.js | 15 ++++++ src/app/autoUpdate/startUpdateTimer.js | 33 +++++++++++++ src/app/initApp.js | 2 + src/constants/delays.js | 4 ++ src/constants/index.js | 12 ++++- src/state/index.js | 4 ++ 11 files changed, 172 insertions(+), 44 deletions(-) create mode 100644 src/app/autoUpdate/checkForUpdateAutomatically.js create mode 100644 src/app/autoUpdate/onUpdateDownloaded.js create mode 100644 src/app/autoUpdate/resetUpdateTimer.js create mode 100644 src/app/autoUpdate/startUpdateTimer.js create mode 100644 src/constants/delays.js diff --git a/src/app/autoUpdate/available.js b/src/app/autoUpdate/available.js index c85f15b2..45f90ed1 100644 --- a/src/app/autoUpdate/available.js +++ b/src/app/autoUpdate/available.js @@ -3,6 +3,7 @@ import { dialog, shell } from 'electron' import { autoUpdater } from 'electron-updater' import state from '../../state' +import resetUpdateTimer from './resetUpdateTimer' import { isWindowsStore } from '../../constants' const messages = { @@ -18,6 +19,10 @@ const messages = { id: 'updateCancel', defaultMessage: 'Cancel', }, + later: { + id: 'updateLater', + defaultMessage: 'Later', + }, update: { id: 'updateUpdateGrape', defaultMessage: 'Update Grape', @@ -29,6 +34,39 @@ export default () => { const { formatMessage } = require('../../i18n') autoUpdater.on('update-available', () => { + if (state.checkingForUpdateAutomatically) { + dialog + .showMessageBox({ + type: 'question', + title: formatMessage(messages.newVersionAvailable), + message: formatMessage(messages.install), + buttons: [ + formatMessage(messages.later), + formatMessage(messages.update), + ], + }) + .then(({ response }) => { + if (response === 0) { + resetUpdateTimer(true) + return + } + + if (response === 1) { + if (isWindowsStore) { + shell.openExternal( + 'ms-windows-store://pdp/?ProductId=9P28KPMR8L2Z', + ) + return + } + + resetUpdateTimer(true) + autoUpdater.downloadUpdate() + } + }) + + return + } + if (state.isInitialUpdateCheck) return dialog diff --git a/src/app/autoUpdate/checkForUpdateAutomatically.js b/src/app/autoUpdate/checkForUpdateAutomatically.js new file mode 100644 index 00000000..bdc680fe --- /dev/null +++ b/src/app/autoUpdate/checkForUpdateAutomatically.js @@ -0,0 +1,14 @@ +import { autoUpdater } from 'electron-updater' + +// eslint-disable-next-line import/no-cycle +import resetUpdateTimer from './resetUpdateTimer' +import state from '../../state' + +export default () => { + state.checkingForUpdateAutomatically = true + autoUpdater.autoDownload = false + autoUpdater.checkForUpdates().then(() => { + state.checkingForUpdateAutomatically = false + if (!state.shouldCheckUpdateLater) resetUpdateTimer() + }) +} diff --git a/src/app/autoUpdate/downloaded.js b/src/app/autoUpdate/downloaded.js index e944b154..f03f7565 100644 --- a/src/app/autoUpdate/downloaded.js +++ b/src/app/autoUpdate/downloaded.js @@ -1,53 +1,12 @@ -// eslint-disable-next-line import/no-extraneous-dependencies -import { Menu, dialog } from 'electron' import { autoUpdater } from 'electron-updater' import state from '../../state' -import quitAndInstall from './quitAndInstall' -import { getMenuTemplate } from '../menu' - -const messages = { - newVersion: { - id: 'updateNewVersionReady', - defaultMessage: 'New version is ready!', - }, - restart: { - id: 'updateRestart', - defaultMessage: 'Restart the app to apply the update.', - }, - later: { - id: 'updateLater', - defaultMessage: 'Later', - }, - restartAndUpdate: { - id: 'updateRestartAndUpdate', - defaultMessage: 'Restart and update', - }, -} +import onUpdateDownloaded from './onUpdateDownloaded' export default () => { - // eslint-disable-next-line global-require - const { formatMessage } = require('../../i18n') - autoUpdater.on('update-downloaded', () => { state.isUpdateDownloaded = true - Menu.setApplicationMenu(Menu.buildFromTemplate(getMenuTemplate())) - - dialog - .showMessageBox({ - type: 'info', - title: formatMessage(messages.newVersion), - message: formatMessage(messages.restart), - buttons: [ - formatMessage(messages.later), - formatMessage(messages.restartAndUpdate), - ], - }) - .then(({ response }) => { - if (response === 1) { - quitAndInstall() - } - }) + onUpdateDownloaded() }) } diff --git a/src/app/autoUpdate/notAvailable.js b/src/app/autoUpdate/notAvailable.js index b160d7d2..a22bbb4e 100644 --- a/src/app/autoUpdate/notAvailable.js +++ b/src/app/autoUpdate/notAvailable.js @@ -24,6 +24,8 @@ export default () => { const { formatMessage } = require('../../i18n') autoUpdater.on('update-not-available', () => { + if (state.checkingForUpdateAutomatically) return + if (!state.isInitialUpdateChecked) { state.isInitialUpdateChecked = true return diff --git a/src/app/autoUpdate/onUpdateDownloaded.js b/src/app/autoUpdate/onUpdateDownloaded.js new file mode 100644 index 00000000..62be934a --- /dev/null +++ b/src/app/autoUpdate/onUpdateDownloaded.js @@ -0,0 +1,47 @@ +// eslint-disable-next-line import/no-extraneous-dependencies +import { Menu, dialog } from 'electron' + +import quitAndInstall from './quitAndInstall' +import { getMenuTemplate } from '../menu' + +const messages = { + newVersion: { + id: 'updateNewVersionReady', + defaultMessage: 'New version is ready!', + }, + restart: { + id: 'updateRestart', + defaultMessage: 'Restart the app to apply the update.', + }, + later: { + id: 'updateLater', + defaultMessage: 'Later', + }, + restartAndUpdate: { + id: 'updateRestartAndUpdate', + defaultMessage: 'Restart and update', + }, +} + +export default () => { + // eslint-disable-next-line global-require + const { formatMessage } = require('../../i18n') + + Menu.setApplicationMenu(Menu.buildFromTemplate(getMenuTemplate())) + + dialog + .showMessageBox({ + type: 'info', + title: formatMessage(messages.newVersion), + message: formatMessage(messages.restart), + buttons: [ + formatMessage(messages.later), + formatMessage(messages.restartAndUpdate), + ], + }) + .then(({ response }) => { + if (response === 1) { + quitAndInstall() + } + }) +} diff --git a/src/app/autoUpdate/resetUpdateTimer.js b/src/app/autoUpdate/resetUpdateTimer.js new file mode 100644 index 00000000..6a2745ba --- /dev/null +++ b/src/app/autoUpdate/resetUpdateTimer.js @@ -0,0 +1,15 @@ +// eslint-disable-next-line import/no-cycle +import startUpdateTimer from './startUpdateTimer' +import state from '../../state' + +export default checkForUpdateLater => { + state.updateCounter = 0 + state.shouldCheckUpdateLater = false + + if (checkForUpdateLater) { + state.shouldCheckUpdateLater = true + } + + clearInterval(state.updateInterval) + startUpdateTimer() +} diff --git a/src/app/autoUpdate/startUpdateTimer.js b/src/app/autoUpdate/startUpdateTimer.js new file mode 100644 index 00000000..fe81678c --- /dev/null +++ b/src/app/autoUpdate/startUpdateTimer.js @@ -0,0 +1,33 @@ +// eslint-disable-next-line import/no-cycle +import checkForUpdateAutomatically from './checkForUpdateAutomatically' +import onUpdateDownloaded from './onUpdateDownloaded' +import state from '../../state' +import { delays } from '../../constants' + +export default () => { + if (state.isUpdateDownloaded) { + onUpdateDownloaded() + return + } + + state.updateInterval = setInterval(() => { + if (!state.checkingForUpdateAutomatically) { + if (state.shouldCheckUpdateLater) { + if (state.updateCounter === delays.checkLaterDelay) { + checkForUpdateAutomatically() + return + } + + state.updateCounter += 1 + return + } + + if (state.updateCounter === delays.defaultDelay) { + checkForUpdateAutomatically() + return + } + + state.updateCounter += 1 + } + }, 1000) +} diff --git a/src/app/initApp.js b/src/app/initApp.js index 1889993d..e78db4ef 100644 --- a/src/app/initApp.js +++ b/src/app/initApp.js @@ -26,6 +26,7 @@ import state from '../state' import { images, isDevelopment, isMas, isWindowsStore } from '../constants' import { getOsType, getChatUrl } from '../utils' import showMainWindow from './menu/actions/showMainWindow' +import startUpdateTimer from './autoUpdate/startUpdateTimer' const { trayIcon, @@ -47,6 +48,7 @@ const messages = { export default url => { if (getOsType !== 'linux' && !isMas && !isWindowsStore) autoUpdate() global.store = store.get() || env + startUpdateTimer() const { width, height } = screen.getPrimaryDisplay().workAreaSize diff --git a/src/constants/delays.js b/src/constants/delays.js new file mode 100644 index 00000000..8cc76133 --- /dev/null +++ b/src/constants/delays.js @@ -0,0 +1,4 @@ +export default { + defaultDelay: 10800, // 3 hours in seconds + checkLaterDelay: 86400, // 24 hours in seconds +} diff --git a/src/constants/index.js b/src/constants/index.js index 6ebfd8a9..8d6b9d15 100644 --- a/src/constants/index.js +++ b/src/constants/index.js @@ -5,5 +5,15 @@ import isDevelopment from './isDevelopment' import isMas from './isMas' import isMac from './isMac' import isWindowsStore from './isWindowsStore' +import delays from './delays' -export { blobs, images, pages, isDevelopment, isMas, isMac, isWindowsStore } +export { + blobs, + images, + pages, + isDevelopment, + isMas, + isMac, + isWindowsStore, + delays, +} diff --git a/src/state/index.js b/src/state/index.js index 773829a3..0d7cbd38 100644 --- a/src/state/index.js +++ b/src/state/index.js @@ -2,4 +2,8 @@ export default { isChatOpened: false, isUpdateDownloaded: false, isInitialUpdateChecked: false, + checkingForUpdateAutomatically: false, + updateCounter: 0, + shouldCheckUpdateLater: false, + updateInterval: null, } From c0ba085ddbf65de8354d4c286041eddc221b6ebc Mon Sep 17 00:00:00 2001 From: Oleh Aloshkin Date: Thu, 18 Jun 2020 17:20:36 +0200 Subject: [PATCH 2/4] Added checkUpdateLater key for translation --- src/app/autoUpdate/available.js | 2 +- src/i18n/de.js | 1 + src/i18n/en.js | 1 + 3 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/app/autoUpdate/available.js b/src/app/autoUpdate/available.js index 45f90ed1..970d8b6e 100644 --- a/src/app/autoUpdate/available.js +++ b/src/app/autoUpdate/available.js @@ -20,7 +20,7 @@ const messages = { defaultMessage: 'Cancel', }, later: { - id: 'updateLater', + id: 'checkUpdateLater', defaultMessage: 'Later', }, update: { diff --git a/src/i18n/de.js b/src/i18n/de.js index 49dad445..9d6098bd 100644 --- a/src/i18n/de.js +++ b/src/i18n/de.js @@ -70,6 +70,7 @@ export default { "updateNewVersionReady": "Neue Version steht bereit!", "updateRestart": "Starten Sie die App zum Anwenden der Aktualisierung neu.", "updateLater": "Später", + "checkUpdateLater": "Später", "updateRestartAndUpdate": "Neu starten und aktualisieren", "updateUpToDate": "Sie sind auf dem neuesten Stand!", "updateLatest": "Sie benutzen bereits die neueste Version von Grape.", diff --git a/src/i18n/en.js b/src/i18n/en.js index 9a82db74..e1c1ce00 100644 --- a/src/i18n/en.js +++ b/src/i18n/en.js @@ -70,6 +70,7 @@ export default { "updateNewVersionReady": "New version is ready!", "updateRestart": "Restart the app to apply the update.", "updateLater": "Later", + "checkUpdateLater": "Later", "updateRestartAndUpdate": "Restart and update", "updateUpToDate": "You're up to date!", "updateLatest": "You're already using the latest version of Grape.", From cfffde13e782650fe009b001ef5ca39b026941a2 Mon Sep 17 00:00:00 2001 From: Oleh Aloshkin Date: Thu, 18 Jun 2020 17:30:51 +0200 Subject: [PATCH 3/4] Replaced checkUpdateLater with downloadUpdateLater --- src/app/autoUpdate/available.js | 2 +- src/i18n/de.js | 2 +- src/i18n/en.js | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/app/autoUpdate/available.js b/src/app/autoUpdate/available.js index 970d8b6e..3fcc04be 100644 --- a/src/app/autoUpdate/available.js +++ b/src/app/autoUpdate/available.js @@ -20,7 +20,7 @@ const messages = { defaultMessage: 'Cancel', }, later: { - id: 'checkUpdateLater', + id: 'downloadUpdateLater', defaultMessage: 'Later', }, update: { diff --git a/src/i18n/de.js b/src/i18n/de.js index 9d6098bd..00bb8e7b 100644 --- a/src/i18n/de.js +++ b/src/i18n/de.js @@ -70,7 +70,7 @@ export default { "updateNewVersionReady": "Neue Version steht bereit!", "updateRestart": "Starten Sie die App zum Anwenden der Aktualisierung neu.", "updateLater": "Später", - "checkUpdateLater": "Später", + "downloadUpdateLater": "Später", "updateRestartAndUpdate": "Neu starten und aktualisieren", "updateUpToDate": "Sie sind auf dem neuesten Stand!", "updateLatest": "Sie benutzen bereits die neueste Version von Grape.", diff --git a/src/i18n/en.js b/src/i18n/en.js index e1c1ce00..aba26a40 100644 --- a/src/i18n/en.js +++ b/src/i18n/en.js @@ -70,7 +70,7 @@ export default { "updateNewVersionReady": "New version is ready!", "updateRestart": "Restart the app to apply the update.", "updateLater": "Later", - "checkUpdateLater": "Later", + "downloadUpdateLater": "Later", "updateRestartAndUpdate": "Restart and update", "updateUpToDate": "You're up to date!", "updateLatest": "You're already using the latest version of Grape.", From 30b7586277437bbd8d206412338ad821fb8c4712 Mon Sep 17 00:00:00 2001 From: Oleh Aloshkin Date: Thu, 18 Jun 2020 17:49:22 +0200 Subject: [PATCH 4/4] Correctly added downloadUpdateLater key --- i18n/translations/de.json | 3 ++- i18n/translations/en.json | 3 ++- i18n/translations/pl.json | 3 ++- src/i18n/pl.js | 1 + 4 files changed, 7 insertions(+), 3 deletions(-) diff --git a/i18n/translations/de.json b/i18n/translations/de.json index 1a9562e9..890fde04 100644 --- a/i18n/translations/de.json +++ b/i18n/translations/de.json @@ -64,6 +64,7 @@ "updateNewVersionReady": "Neue Version steht bereit!", "updateRestart": "Starten Sie die App zum Anwenden der Aktualisierung neu.", "updateLater": "Später", + "downloadUpdateLater": "Später", "updateRestartAndUpdate": "Neu starten und aktualisieren", "updateUpToDate": "Sie sind auf dem neuesten Stand!", "updateLatest": "Sie benutzen bereits die neueste Version von Grape.", @@ -71,4 +72,4 @@ "logOutTitle": "Sind Sie sicher, dass Sie sich abmelden möchten?", "logOutCancel": "Abbrechen", "logOutConfirm": "Abmelden" -} \ No newline at end of file +} diff --git a/i18n/translations/en.json b/i18n/translations/en.json index f2478a9d..3b833865 100644 --- a/i18n/translations/en.json +++ b/i18n/translations/en.json @@ -64,6 +64,7 @@ "updateNewVersionReady": "New version is ready!", "updateRestart": "Restart the app to apply the update.", "updateLater": "Later", + "downloadUpdateLater": "Later", "updateRestartAndUpdate": "Restart and update", "updateUpToDate": "You're up to date!", "updateLatest": "You're already using the latest version of Grape.", @@ -71,4 +72,4 @@ "logOutTitle": "Are you sure you want to log out?", "logOutCancel": "Cancel", "logOutConfirm": "Log out" -} \ No newline at end of file +} diff --git a/i18n/translations/pl.json b/i18n/translations/pl.json index f619bd4b..8712cd04 100644 --- a/i18n/translations/pl.json +++ b/i18n/translations/pl.json @@ -64,6 +64,7 @@ "updateNewVersionReady": "Nowa wersja jest gotowa!", "updateRestart": "Zrestartuj aplikację, aby zastosować aktualizację.", "updateLater": "Później", + "downloadUpdateLater": "Później", "updateRestartAndUpdate": "Restart i aktualizacja", "updateUpToDate": "Jesteś na bieżąco!", "updateLatest": "Używasz już najnowszej wersji Grape.", @@ -71,4 +72,4 @@ "logOutTitle": "Jesteś pewien, że chcesz się wylogować?", "logOutCancel": "Anuluj", "logOutConfirm": "Wyloguj się" -} \ No newline at end of file +} diff --git a/src/i18n/pl.js b/src/i18n/pl.js index 5cef837e..ebfdf443 100644 --- a/src/i18n/pl.js +++ b/src/i18n/pl.js @@ -70,6 +70,7 @@ export default { "updateNewVersionReady": "Nowa wersja jest gotowa!", "updateRestart": "Zrestartuj aplikację, aby zastosować aktualizację.", "updateLater": "Później", + "downloadUpdateLater": "Później", "updateRestartAndUpdate": "Restart i aktualizacja", "updateUpToDate": "Jesteś na bieżąco!", "updateLatest": "Używasz już najnowszej wersji Grape.",