Skip to content

Commit

Permalink
feat(gDriveSync): add a way to specify the file name used
Browse files Browse the repository at this point in the history
  • Loading branch information
johannesjo committed Mar 10, 2018
1 parent 51af762 commit 7c693fc
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 25 deletions.
1 change: 1 addition & 0 deletions app-src/scripts/constants.js
Expand Up @@ -137,6 +137,7 @@
isAutoLogin: false,
isLoadRemoteDataOnStartup: false,
isAutoSyncToRemote: false,
syncFileName: 'SUPER_PRODUCTIVITY_SYNC.json',
syncInterval: moment.duration(1, 'minutes'),
},
automaticBackups: {
Expand Down
20 changes: 20 additions & 0 deletions app-src/scripts/main/global-services/google-api-s.js
Expand Up @@ -297,6 +297,26 @@
});
}

findFile(fileName) {
if (!fileName) {
this.SimpleToast('ERROR', 'GoogleApi: No file name specified');
return this.$q.reject('No file name given');
}

return this.$http({
method: 'GET',
url: `https://content.googleapis.com/drive/v2/files`,
params: {
'key': this.GOOGLE.API_KEY,
// should be called name officially instead of title
q: `title='${fileName}' and trashed=false`,
},
headers: {
'Authorization': `Bearer ${this.accessToken}`,
},
}).catch(this.handleError.bind(this));
}

loadFile(fileId) {
if (!fileId) {
this.SimpleToast('ERROR', 'GoogleApi: No file id specified');
Expand Down
89 changes: 64 additions & 25 deletions app-src/scripts/main/global-services/google-drive-sync-s.js
Expand Up @@ -9,7 +9,7 @@
(() => {
'use strict';

const SYNC_FILE_NAME = 'SUPER_PRODUCTIVITY_SYNC.json';
const DEFAULT_SYNC_FILE_NAME = 'SUPER_PRODUCTIVITY_SYNC.json';

class GoogleDriveSync {
/* @ngInject */
Expand Down Expand Up @@ -102,25 +102,38 @@
return this.$mdDialog.show(confirm);
}

_confirmUsingExistingFileDialog(fileName) {
const confirm = this.$mdDialog.confirm()
.title(`Use existing file "${fileName}" as sync file?`)
.textContent(`
We found a file with the name you specified. Do you want to use it as your sync file? If not please change the Sync file name.`)
.ok('Please do it!')
.cancel('Abort');

return this.$mdDialog.show(confirm);
}

_save() {
const completeData = this._getLocalAppData();

return this.GoogleApi.saveFile(completeData, {
title: SYNC_FILE_NAME,
title: this.config.syncFileName,
id: this.data.backupDocId,
editable: true
})
.then((res) => {
if (res && res.data) {
this.data.backupDocId = res.data.id;
this.data.lastSyncToRemote = res.data.modifiedDate;
// also needs to be updated
this.data.lastLocalUpdate = res.data.modifiedDate;
}
});
}

_load() {
if (!this.config.syncFileName) {
return this.$q.reject('No file name specified');
}

return this.GoogleApi.loadFile(this.data.backupDocId)
.then((res) => {
return this.$q.when(res);
Expand Down Expand Up @@ -154,30 +167,56 @@
}

saveTo() {
const defer = this.$q.defer();

// CREATE OR FIND
// ---------------------------
// when we have no backup file we create one directly
if (!this.data.backupDocId) {
this.SimpleToast('CUSTOM', 'GoogleDriveSync: Creating new file for backups, as none was found');
return this._save();
}
if (!this.config.syncFileName) {
this.config.syncFileName = DEFAULT_SYNC_FILE_NAME;
}

// otherwise update
const defer = this.$q.defer();
this.GoogleApi.getFileInfo(this.data.backupDocId)
.then((res) => {
const lastModifiedRemote = res.data.modifiedDate;

if (this._isNewerThan(lastModifiedRemote, this.data.lastSyncToRemote)) {
// remote has an update so prompt what to do
this._confirmSaveDialog(lastModifiedRemote)
.then(() => {
this._save().then(defer.resolve);
}, defer.reject);
} else {
// all clear just save
this._save().then(defer.resolve);
}
})
.catch(defer.reject);
this.GoogleApi.findFile(this.config.syncFileName)
.then((res) => {
const filesFound = res.data.items;
if (!filesFound || filesFound.length === 0) {
this.SimpleToast('CUSTOM', `GoogleDriveSync: No file with the name "${this.config.syncFileName}" found. Creating it now...`, 'file_upload');
this._save().then(defer.resolve);
} else if (filesFound.length > 1) {
this.SimpleToast('ERROR', `GoogleDriveSync: Multiple files with the name "${this.config.syncFileName}" found. Please delete all but one or choose a different name.`);
defer.reject();
} else if (filesFound.length === 1) {
this._confirmUsingExistingFileDialog(this.config.syncFileName)
.then(() => {
const fileToUpdate = filesFound[0];
this.data.backupDocId = fileToUpdate.id;
this._save().then(defer.resolve);
}, defer.reject);
}
});

// JUST UPDATE
// ---------------------------
// otherwise update
} else {
this.GoogleApi.getFileInfo(this.data.backupDocId)
.then((res) => {
const lastModifiedRemote = res.data.modifiedDate;

if (this._isNewerThan(lastModifiedRemote, this.data.lastSyncToRemote)) {
// remote has an update so prompt what to do
this._confirmSaveDialog(lastModifiedRemote)
.then(() => {
this._save().then(defer.resolve);
}, defer.reject);
} else {
// all clear just save
this._save().then(defer.resolve);
}
})
.catch(defer.reject);
}

return defer.promise;
}
Expand Down
11 changes: 11 additions & 0 deletions app-src/scripts/settings/backup-settings/backup-settings-d.html
Expand Up @@ -116,6 +116,17 @@ <h3 class="md-caption">Sync via Google Drive</h3>
</div>
<!-- todo add sync doc id -->

<md-input-container class="md-block md-icon-float"
ng-show="vm.GoogleApi.isLoggedIn">
<label>Sync file name</label>
<ng-md-icon icon="file_upload"
aria-label="file_upload"></ng-md-icon>
<input type="text"
ng-model-options="{ debounce: 250 }"
ng-change="vm.resetSync();"
ng-model="vm.settings.googleDriveSync.syncFileName">
</md-input-container>

<md-input-container class="md-block md-icon-float"
ng-show="vm.settings.googleDriveSync.isAutoSyncToRemote">
<label>Sync interval (sync every x)</label>
Expand Down

0 comments on commit 7c693fc

Please sign in to comment.