From 3e4d4e9e0df367e796b330ecf1b348dc462667cf Mon Sep 17 00:00:00 2001 From: Johannes Millan Date: Fri, 16 Mar 2018 13:10:59 +0100 Subject: [PATCH] feat: sync to google drive before closing app --- app-src/scripts/_app.js | 33 +++++++++++++++++++ .../scripts/daily-summary/daily-summary-d.js | 3 +- .../global-services/google-drive-sync-s.js | 27 ++++++++++----- electron/main.js | 16 ++++----- 4 files changed, 60 insertions(+), 19 deletions(-) diff --git a/app-src/scripts/_app.js b/app-src/scripts/_app.js index 14d598681f..8e06ed65dd 100644 --- a/app-src/scripts/_app.js +++ b/app-src/scripts/_app.js @@ -53,6 +53,7 @@ .run(checkIfLatestVersion) .run(showWelcomeDialog) .run(initUnloadActions) + .run(initElectronOnBeforeQuit) //.run(goToWorkViewIfTasks); /* @ngInject */ @@ -378,6 +379,38 @@ } } + function initElectronOnBeforeQuit(IS_ELECTRON, GoogleDriveSync, $mdDialog) { + const ON_BEFORE_QUIT_EV = 'ON_BEFORE_QUIT'; + const SHUTDOWN_NOW_EV = 'SHUTDOWN_NOW'; + + const confirmQuitAnyWay = () => { + const confirm = $mdDialog.confirm() + .title(`Some error occurred while syncing`) + .textContent(` + Some error occurred while syncing to Google Drive (check the console). Do you want to quit anyway?`) + .ok('Yes') + .cancel('No!'); + $mdDialog.show(confirm) + .then(() => { + window.ipcRenderer.send(SHUTDOWN_NOW_EV, {}); + }); + }; + + if (IS_ELECTRON) { + window.ipcRenderer.on(ON_BEFORE_QUIT_EV, () => { + if (GoogleDriveSync.config.isAutoSyncToRemote) { + GoogleDriveSync.saveForSyncIfEnabled() + .then(() => { + window.ipcRenderer.send(SHUTDOWN_NOW_EV, {}); + }) + .catch(confirmQuitAnyWay); + } else { + window.ipcRenderer.send(SHUTDOWN_NOW_EV, {}); + } + }); + } + } + function sendAppReadyToElectron(IS_ELECTRON) { const APP_READY = 'APP_READY'; if (IS_ELECTRON) { diff --git a/app-src/scripts/daily-summary/daily-summary-d.js b/app-src/scripts/daily-summary/daily-summary-d.js index fda23b4257..ff7146f008 100644 --- a/app-src/scripts/daily-summary/daily-summary-d.js +++ b/app-src/scripts/daily-summary/daily-summary-d.js @@ -73,7 +73,8 @@ // save everything AppStorage.saveToLs(); - if (GoogleDriveSync.config && GoogleDriveSync.config.isAutoSyncToRemote) { + + if (!IS_ELECTRON && GoogleDriveSync.config && GoogleDriveSync.config.isAutoSyncToRemote) { SimpleToast('CUSTOM', `Syncing Data to Google Drive.`, 'file_upload'); GoogleDriveSync.saveTo(); } diff --git a/app-src/scripts/main/global-services/google-drive-sync-s.js b/app-src/scripts/main/global-services/google-drive-sync-s.js index 97b084ee17..6f4a3a6863 100644 --- a/app-src/scripts/main/global-services/google-drive-sync-s.js +++ b/app-src/scripts/main/global-services/google-drive-sync-s.js @@ -230,15 +230,7 @@ this.autoSyncInterval = this.$interval(() => { // only sync if not in the middle of something - if (this._isCurrentPromisePending()) { - this._log('SYNC OMITTED because of promise', this.currentPromise, this.currentPromise.$$state.status); - } else { - this._log('SYNC'); - this.saveTo() - .then(() => { - this.SimpleToast('SUCCESS', `Successfully synced Data to Google drive.`, 'file_upload'); - }); - } + this.saveForSyncIfEnabled(); }, interval); } @@ -279,6 +271,23 @@ return defer.promise; } + saveForSyncIfEnabled() { + if (!this.config.isAutoSyncToRemote) { + return this.$q.resolve(); + } + + if (this._isCurrentPromisePending()) { + this._log('SYNC OMITTED because of promise', this.currentPromise, this.currentPromise.$$state.status); + return this.$q.reject(); + } else { + this._log('SYNC'); + return this.saveTo() + .then(() => { + this.SimpleToast('SUCCESS', `Successfully synced Data to Google drive.`, 'file_upload'); + }); + } + } + saveTo() { // don't execute sync interactions at the same time if (this._isCurrentPromisePending()) { diff --git a/electron/main.js b/electron/main.js index 6414efdead..be51e3d866 100644 --- a/electron/main.js +++ b/electron/main.js @@ -51,7 +51,7 @@ let shouldQuitBecauseAppIsAnotherInstance = app.makeSingleInstance(() => { }); if (shouldQuitBecauseAppIsAnotherInstance) { - quitApp(); + quitAppNow(); return; } @@ -109,6 +109,8 @@ app.on('before-quit', () => { // FRONTEND EVENTS // --------------- +electron.ipcMain.on('SHUTDOWN_NOW', quitAppNow); + electron.ipcMain.on('SHUTDOWN', quitApp); electron.ipcMain.on('REGISTER_GLOBAL_SHORTCUT', (ev, shortcutPassed) => { @@ -182,19 +184,15 @@ function registerShowAppShortCut(shortcutPassed) { } } -function showIdleDialog(idleTimeInMs) { - // first show, then send again - mainWin.webContents.send('WAS_IDLE', ({ - idleTimeInMs: idleTimeInMs, - minIdleTimeInMs: CONFIG.MIN_IDLE_TIME - })); -} - function showApp() { showOrFocus(mainWin); } function quitApp() { + mainWin.webContents.send('ON_BEFORE_QUIT'); +} + +function quitAppNow() { app.isQuiting = true; app.quit(); }