Skip to content

Commit

Permalink
feat(gDriveSync): improve sync file selection
Browse files Browse the repository at this point in the history
  • Loading branch information
johannesjo committed Mar 11, 2018
1 parent 8aa264f commit 52722c7
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 32 deletions.
66 changes: 48 additions & 18 deletions app-src/scripts/main/global-services/google-drive-sync-s.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,17 @@
return this.$mdDialog.show(confirm);
}

_confirmSaveNewFile(fileName) {
const confirm = this.$mdDialog.confirm()
.title(`Create "${fileName}" as sync file on Google Drive?`)
.textContent(`
No file with the name you specified was found. Do you want to create it?`)
.ok('Please do it!')
.cancel('Abort');

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

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

Expand All @@ -185,6 +196,8 @@
editable: true
})
.then((res) => {
console.log(res);
//this.config.syncFileName
this.data.backupDocId = res.data.id;
this.data.lastSyncToRemote = res.data.modifiedDate;
// also needs to be updated
Expand Down Expand Up @@ -234,6 +247,37 @@
}
}

changeSyncFileName(newSyncFileName) {
const defer = this.$q.defer();

this.GoogleApi.findFile(newSyncFileName)
.then((res) => {
const filesFound = res.data.items;
if (!filesFound || filesFound.length === 0) {
this._confirmSaveNewFile(newSyncFileName)
.then(() => {
this.config.syncFileName = newSyncFileName;
// we need to unset to save to a new file
this.data.backupDocId = undefined;
this._save().then(defer.resolve);
}, defer.reject);
} else if (filesFound.length > 1) {
this.SimpleToast('ERROR', `Multiple files with the name "${newSyncFileName}" found. Please delete all but one or choose a different name.`);
defer.reject();
} else if (filesFound.length === 1) {
this._confirmUsingExistingFileDialog(newSyncFileName)
.then(() => {
const fileToUpdate = filesFound[0];
this.data.backupDocId = fileToUpdate.id;
this.config.syncFileName = newSyncFileName;
defer.resolve(this.data.backupDocId);
}, defer.reject);
}
});

return defer.promise;
}

saveTo() {
// don't execute sync interactions at the same time
if (this._isCurrentPromisePending()) {
Expand All @@ -252,24 +296,10 @@
this.config.syncFileName = DEFAULT_SYNC_FILE_NAME;
}

this.GoogleApi.findFile(this.config.syncFileName)
.then((res) => {
const filesFound = res.data.items;
if (!filesFound || filesFound.length === 0) {
this.SimpleToast('CUSTOM', `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', `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);
}
});
this.changeSyncFileName(this.config.syncFileName)
.then(() => {
this._save().then(defer.resolve);
}, defer.reject);

// JUST UPDATE
// ---------------------------
Expand Down
12 changes: 11 additions & 1 deletion app-src/scripts/settings/backup-settings/_backup-settings-d.scss
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,17 @@ backup-settings {
}

.sync-from-btn {
ng-md-icon{
ng-md-icon {
transform: rotate(180deg);
}
}

.sync-file-wrapper {
display: flex;
flex-direction: row;
//justify-content: center;
align-items: center;
md-input-container{
flex: 1;
}
}
33 changes: 21 additions & 12 deletions app-src/scripts/settings/backup-settings/backup-settings-d.html
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ <h3 class="md-caption">File import/export</h3>

<section>
<h3 class="md-caption">Sync via Google Drive</h3>
<p><strong>NOTE:</strong> This is
<strong>a highly experimental feature!!!</strong> Take care! Make a backup! Changes to the sync settings require you to restart the application to take effect.
</p>


<div>
<md-button class="md-raised"
promise-btn
Expand Down Expand Up @@ -95,6 +100,22 @@ <h3 class="md-caption">Sync via Google Drive</h3>
</md-button>
</div>

<div ng-show="vm.GoogleApi.isLoggedIn"
class="sync-file-wrapper">
<md-input-container class="md-block md-icon-float">
<label>Sync file name</label>
<ng-md-icon icon="file_upload"
aria-label="file_upload"></ng-md-icon>
<input type="text"
ng-model="vm.tmpSyncFile">
</md-input-container>

<md-button class="md-raised md-primary md-icon-button"
ng-click="vm.changeSyncFileName(vm.tmpSyncFile)">
<ng-md-icon icon="save"></ng-md-icon>
</md-button>
</div>

<div>
<md-switch ng-model="vm.settings.googleDriveSync.isAutoLogin"
aria-label="Auto login at when starting app">
Expand All @@ -114,18 +135,6 @@ <h3 class="md-caption">Sync via Google Drive</h3>
Auto sync data TO remote
</md-switch>
</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.isAutoLogin && vm.settings.googleDriveSync.isAutoSyncToRemote">
Expand Down
10 changes: 9 additions & 1 deletion app-src/scripts/settings/backup-settings/backup-settings-d.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,14 @@
}

/* @ngInject */
function BackupSettingsCtrl(AppStorage, IS_ELECTRON, GoogleApi, GoogleDriveSync, SimpleToast) {
function BackupSettingsCtrl(AppStorage, IS_ELECTRON, GoogleApi, GoogleDriveSync, SimpleToast, $timeout) {
let vm = this;
vm.IS_ELECTRON = IS_ELECTRON;

$timeout(() => {
vm.tmpSyncFile = vm.settings.googleDriveSync.syncFileName;
});

// import/export stuff
vm.importSettings = (uploadSettingsTextarea) => {
let settings = JSON.parse(uploadSettingsTextarea);
Expand Down Expand Up @@ -59,6 +63,10 @@
GoogleDriveSync.resetAutoSyncToRemoteInterval();
};

vm.changeSyncFileName = (newSyncFile) => {
GoogleDriveSync.changeSyncFileName(newSyncFile);
};

vm.GoogleApi = GoogleApi;
}

Expand Down

0 comments on commit 52722c7

Please sign in to comment.