From bbb94bfa46014e7c976e0da239e0614a4f577e3f Mon Sep 17 00:00:00 2001 From: Johannes Millan Date: Sat, 10 Mar 2018 00:18:10 +0100 Subject: [PATCH] feat(gDriveSync): handle loading conditions correctly --- app-src/scripts/_app.js | 10 ++-- app-src/scripts/constants.js | 4 +- .../main/global-services/app-storage-s.js | 1 + .../global-services/google-drive-sync-s.js | 50 +++++++++++++++---- 4 files changed, 51 insertions(+), 14 deletions(-) diff --git a/app-src/scripts/_app.js b/app-src/scripts/_app.js index a3fe3a0a8e..ae6cfa30b5 100644 --- a/app-src/scripts/_app.js +++ b/app-src/scripts/_app.js @@ -49,7 +49,7 @@ .run(initElectronErrorHandling) .run(sendAppReadyToElectron) .run(preventMultipleInstances) - .run(setStartedTime) + .run(setStartedTimes) .run(checkIfLatestVersion) .run(showWelcomeDialog) //.run(goToWorkViewIfTasks); @@ -301,9 +301,13 @@ }); } - function setStartedTime($rootScope, $timeout) { + function setStartedTimes($rootScope, $timeout) { $timeout(() => { + const moment = window.moment; + const now = moment(); + + // set started time today as used by the time sheet export const startedTime = $rootScope.r.startedTimeToday; if (startedTime && moment(startedTime).isSame(moment(), 'day')) { @@ -311,7 +315,7 @@ $rootScope.r.startedTimeToday = moment(startedTime); } else { // set to now - $rootScope.r.startedTimeToday = moment(); + $rootScope.r.startedTimeToday = now; } }); } diff --git a/app-src/scripts/constants.js b/app-src/scripts/constants.js index cca4dd3709..c2a6183af0 100644 --- a/app-src/scripts/constants.js +++ b/app-src/scripts/constants.js @@ -24,7 +24,8 @@ 'config', 'keys', 'googleDriveSync', - 'googleTokens' + 'googleTokens', + 'lastActiveTime', ]) .constant('ON_DEMAND_LS_FIELDS', [ 'doneBacklogTasks', @@ -68,6 +69,7 @@ theme: undefined, currentTask: undefined, lastActiveTaskTask: undefined, + lastActiveTime: undefined, startedTimeToday: undefined, currentProject: undefined, currentSession: { diff --git a/app-src/scripts/main/global-services/app-storage-s.js b/app-src/scripts/main/global-services/app-storage-s.js index 8a6393c9a3..2f13530c78 100644 --- a/app-src/scripts/main/global-services/app-storage-s.js +++ b/app-src/scripts/main/global-services/app-storage-s.js @@ -42,6 +42,7 @@ initUnloadSave() { window.onbeforeunload = window.onunload = () => { + this.$rootScope.r.lastActiveTime = new Date(); this.saveToLs(); }; } 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 dff57d2e3d..6751581ec2 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 @@ -30,14 +30,36 @@ if (this.config.isAutoSync) { } - if (this.config.isLoadRemoteDataOnStartup) { + if (this.config.isLoadRemoteDataOnStartup) { + this.GoogleApi.getFileInfo(this.data.backupDocId) + .then((res) => { + const lastModifiedRemote = res.data.modifiedDate; + console.log(lastModifiedRemote, this.data.lastLocalUpdate); + + if (this._isNewerThan(lastModifiedRemote, this.data.lastLocalUpdate)) { + console.log('GoogleDriveSync: HAS CHANGED, TRYING TO UPDATE'); + const lastActiveTime = this.$rootScope.r.lastActiveTime; + const isSkipConfirm = this._isNewerThan(lastModifiedRemote, lastActiveTime); + console.log('GoogleDriveSync: Skipping Dialog', isSkipConfirm); + this.loadFrom(isSkipConfirm); + } + }); } } _import(loadRes) { - this.data.lastLocalUpdate = loadRes.meta.modifiedDate; - this.AppStorage.importData(loadRes.backup); + const backupData = loadRes.backup; + const metaData = loadRes.meta; + + // we also need to update the backup to persist it also after the import + backupData.googleDriveSync.lastLocalUpdate = this.data.lastLocalUpdate = metaData.modifiedDate; + // and we also need to update last sync to remote, as it kind of happened now + backupData.googleDriveSync.lastSyncToRemote = this.data.lastSyncToRemote = metaData.modifiedDate; + // also needs to be set to prevent double upgrades + backupData.lastActiveTime = new Date(); + + this.AppStorage.importData(backupData); } _isNewerThan(strDate1, strDate2) { @@ -56,7 +78,7 @@ } _formatDate(date) { - return window.moment(date).format('DD-MM-YYYY hh:mm:ss') + return window.moment(date).format('DD-MM-YYYY hh:mm:ss'); } _confirmSaveDialog(remoteModified) { @@ -64,20 +86,20 @@ .title('Overwrite unsaved data on Google Drive?') .textContent(` There seem to be some changes on Google Drive, that you don\'t have locally. Do you want to overwrite them anyway? - \nRemote data last saved change: ${this._formatDate(remoteModified)}; - \nLast sync to remote from this app instance: ${this._formatDate(this.data.lastSyncToRemote)}.`) + -- Last modification of remote data: ${this._formatDate(remoteModified)} + -- Last sync to remote from this app instance: ${this._formatDate(this.data.lastSyncToRemote)}.`) .ok('Please do it!') .cancel('No'); return this.$mdDialog.show(confirm); } - _confirmLoadDialog(remoteChanged) { + _confirmLoadDialog(remoteModified) { const confirm = this.$mdDialog.confirm() - .title('Overwrite unsaved local changes?') + .title('Update from Google Drive Backup') .textContent(` - All data will be lost forever. - Last modification of remote data: ${this._formatDate(remoteChanged)}`) + Overwrite unsaved local changes? All data will be lost forever. + -- Last modification of remote data: ${this._formatDate(remoteModified)}`) .ok('Please do it!') .cancel('No'); @@ -107,6 +129,14 @@ }); } + initAutoSyncInterval() { + + } + + cancelAutoSyncInterval() { + + } + saveTo() { const defer = this.$q.defer();