Skip to content

Commit

Permalink
feat(gDriveSync): add basic sync
Browse files Browse the repository at this point in the history
  • Loading branch information
johannesjo committed Mar 10, 2018
1 parent bbb94bf commit f6ebeb8
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 11 deletions.
2 changes: 1 addition & 1 deletion app-src/scripts/constants.js
Expand Up @@ -136,7 +136,7 @@
googleDriveSync: {
isAutoLogin: false,
isLoadRemoteDataOnStartup: false,
isAutoSync: false,
isAutoSyncToRemote: false,
syncInterval: moment.duration(1, 'minutes'),
},
automaticBackups: {
Expand Down
31 changes: 25 additions & 6 deletions app-src/scripts/main/global-services/google-drive-sync-s.js
Expand Up @@ -13,12 +13,13 @@

class GoogleDriveSync {
/* @ngInject */
constructor(AppStorage, GoogleApi, $rootScope, SimpleToast, $mdDialog, $q) {
constructor(AppStorage, GoogleApi, $rootScope, SimpleToast, $mdDialog, $q, $interval) {
this.AppStorage = AppStorage;
this.GoogleApi = GoogleApi;
this.$rootScope = $rootScope;
this.SimpleToast = SimpleToast;
this.$mdDialog = $mdDialog;
this.$interval = $interval;
this.$q = $q;

this.data = this.$rootScope.r.googleDriveSync;
Expand All @@ -27,8 +28,8 @@
if (this.config.isAutoLogin) {
GoogleApi.login();
}
if (this.config.isAutoSync) {

if (this.config.isAutoSyncToRemote) {
this.resetAutoSyncToRemoteInterval();
}

if (this.config.isLoadRemoteDataOnStartup) {
Expand Down Expand Up @@ -129,12 +130,30 @@
});
}

initAutoSyncInterval() {
resetAutoSyncToRemoteInterval() {
// always unset if set
this.cancelAutoSyncToRemoteIntervalIfSet();
if (!this.config.isAutoSyncToRemote) {
return;
}

}
const interval = window.moment.duration(this.config.syncInterval).asMilliseconds();

cancelAutoSyncInterval() {
if (interval < 5000) {
console.log('GoogleDriveSync: Interval too low');
return;
}

this.autoSyncInterval = this.$interval(() => {
console.log('GoogleDriveSync: SYNC');
this.saveTo();
}, interval);
}

cancelAutoSyncToRemoteIntervalIfSet() {
if (this.autoSyncInterval) {
this.$interval.cancel(this.autoSyncInterval);
}
}

saveTo() {
Expand Down
11 changes: 7 additions & 4 deletions app-src/scripts/settings/backup-settings/backup-settings-d.html
Expand Up @@ -108,20 +108,23 @@ <h3 class="md-caption">Sync via Google Drive</h3>
</md-switch>
</div>
<div>
<md-switch ng-model="vm.settings.googleDriveSync.isAutoSync"
aria-label="Auto sync data">
Auto sync data
<md-switch ng-model="vm.settings.googleDriveSync.isAutoSyncToRemote"
ng-change="vm.resetSync();"
aria-label="Auto sync data TO remote">
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.settings.googleDriveSync.isAutoSyncToRemote || vm.settings.googleDriveSync.isAutoSyncFromRemote">
ng-show="vm.settings.googleDriveSync.isAutoSyncToRemote">
<label>Sync interval (sync every x)</label>
<ng-md-icon icon="timer"
aria-label="timer"></ng-md-icon>
<input type="text"
input-duration
ng-model-options="{ debounce: 250 }"
ng-change="vm.resetSync();"
ng-model="vm.settings.googleDriveSync.syncInterval">
</md-input-container>

Expand Down
4 changes: 4 additions & 0 deletions app-src/scripts/settings/backup-settings/backup-settings-d.js
Expand Up @@ -55,6 +55,10 @@
return GoogleApi.logout();
};

vm.resetSync = () => {
GoogleDriveSync.resetAutoSyncToRemoteInterval();
};

vm.GoogleApi = GoogleApi;
}

Expand Down

0 comments on commit f6ebeb8

Please sign in to comment.