feat(gDriveSync): handle loading conditions correctly

This commit is contained in:
Johannes Millan 2018-03-10 00:18:10 +01:00
parent d3dee47e51
commit bbb94bfa46
4 changed files with 51 additions and 14 deletions

View file

@ -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;
}
});
}

View file

@ -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: {

View file

@ -42,6 +42,7 @@
initUnloadSave() {
window.onbeforeunload = window.onunload = () => {
this.$rootScope.r.lastActiveTime = new Date();
this.saveToLs();
};
}

View file

@ -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();