Skip to content

Commit 67d03d7

Browse files
committed
feat(gDriveSync): add settings and improve structure
1 parent 528020c commit 67d03d7

File tree

9 files changed

+141
-32
lines changed

9 files changed

+141
-32
lines changed

app-src/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@
132132
<script src="scripts/main/global-services/git-log-s.js"></script>
133133
<script src="scripts/main/global-services/git-s.js"></script>
134134
<script src="scripts/main/global-services/google-api-s.js"></script>
135+
<script src="scripts/main/global-services/google-drive-sync-s.js"></script>
135136
<script src="scripts/main/global-services/init-global-models-s.js"></script>
136137
<script src="scripts/main/global-services/jira-s.js"></script>
137138
<script src="scripts/main/global-services/notifier-s.js"></script>

app-src/scripts/constants.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@
2222
'keys',
2323
'isShowWelcomeDialog',
2424
'config',
25-
'keys'
25+
'keys',
26+
'googleDriveSync',
27+
'googleTokens'
2628
])
2729
.constant('ON_DEMAND_LS_FIELDS', [
2830
'doneBacklogTasks',
@@ -82,6 +84,11 @@
8284
refreshToken: undefined,
8385
expiresAt: undefined
8486
},
87+
googleDriveSync: {
88+
backupDocId: undefined,
89+
lastLocalUpdate: undefined,
90+
lastSyncToRemote: undefined,
91+
},
8592
tasks: [],
8693
backlogTasks: [],
8794
doneBacklogTasks: [],
@@ -124,6 +131,12 @@
124131
togglePlay: 'y',
125132
},
126133
config: {
134+
googleDriveSync: {
135+
isAutoLogin: undefined,
136+
isAutoSyncToRemote: false,
137+
isAutoSyncFromRemote: false,
138+
syncInterval: moment.duration(1, 'minutes'),
139+
},
127140
automaticBackups: {
128141
isEnabled: false,
129142
path: '~/backup-{date}.json',

app-src/scripts/main/global-services/app-storage-s.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,17 @@
124124
}
125125
}
126126

127-
saveToFileSystem(fs, path, cb, isSync) {
127+
getCompleteBackupData() {
128128
const data = angular.copy(this.getCurrentAppState());
129-
130129
// also add projects data
131130
data[this.PROJECTS_KEY] = this.getProjects();
132131
// also add backlog tasks
133132
data[this.DONE_BACKLOG_TASKS_KEY] = this.getDoneBacklogTasks();
133+
return data;
134+
}
135+
136+
saveToFileSystem(fs, path, cb, isSync) {
137+
const data = this.getCompleteBackupData();
134138

135139
fs.writeFile(path, JSON.stringify(data), function(err) {
136140
if (err) {

app-src/scripts/main/global-services/google-api-s.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@
285285
});
286286
};
287287

288-
saveFile(metadata, content) {
288+
saveFile(metadata = {}, content) {
289289
//window.gapi.client.setApiKey(this.GOOGLE.API_KEY);
290290

291291
if (!angular.isString(content)) {
@@ -303,16 +303,19 @@
303303
method = 'POST';
304304
}
305305

306+
if (!metadata.mimeType) {
307+
metadata.mimeType = 'application/json';
308+
}
309+
306310
const multipart = new MultiPartBuilder()
307311
.append('application/json', JSON.stringify(metadata))
308-
//.append(metadata.mimeType, content)
309-
.append('application/json', content)
312+
.append(metadata.mimeType, content)
310313
.finish();
311314

312315
return this.$http({
313316
method: method,
314317
url: `https://content.googleapis.com/upload/drive/v2/files/`,
315-
//url: `https://content.googleapis.com/${path}`,
318+
url: `https://content.googleapis.com${path}`,
316319
params: {
317320
'key': this.GOOGLE.API_KEY,
318321
uploadType: 'multipart',
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* @ngdoc service
3+
* @name superProductivity.GoogleDriveSync
4+
* @description
5+
* # GoogleDriveSync
6+
* Service in the superProductivity.
7+
*/
8+
9+
(() => {
10+
'use strict';
11+
12+
const SYNC_FILE_NAME = 'SUPER_PRODUCTIVITY_SYNC.json';
13+
14+
class GoogleDriveSync {
15+
/* @ngInject */
16+
constructor(AppStorage, GoogleApi, $rootScope) {
17+
this.AppStorage = AppStorage;
18+
this.GoogleApi = GoogleApi;
19+
this.$rootScope = $rootScope;
20+
}
21+
22+
getData() {
23+
return this.AppStorage.getCompleteBackupData();
24+
}
25+
26+
saveTo() {
27+
return this.GoogleApi.saveFile({
28+
title: SYNC_FILE_NAME,
29+
id: this.$rootScope.r.googleDriveSync.backupDocId,
30+
editable: true
31+
}, this.getData())
32+
.then((res) => {
33+
if (res && res.data) {
34+
this.$rootScope.r.googleDriveSync.backupDocId = res.data.id;
35+
}
36+
});
37+
}
38+
}
39+
40+
angular
41+
.module('superProductivity')
42+
.service('GoogleDriveSync', GoogleDriveSync);
43+
44+
// hacky fix for ff
45+
GoogleDriveSync.$$ngIsClass = true;
46+
})();
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
'use strict';
2+
3+
describe('Service: GoogleDriveSync', () => {
4+
// load the service's module
5+
beforeEach(module('superProductivity'));
6+
7+
// instantiate service
8+
let GoogleDriveSync;
9+
beforeEach(inject((_GoogleDriveSync_) => {
10+
GoogleDriveSync = _GoogleDriveSync_;
11+
}));
12+
13+
it('should be defined', () => {
14+
expect(true).toBe(true);
15+
});
16+
17+
});

app-src/scripts/routes/settings/settings-c.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
<section class="config-section"
1111
md-whiteframe="2">
12-
<backup-settings settings="r.config.automaticBackups"></backup-settings>
12+
<backup-settings settings="r.config"></backup-settings>
1313
</section>
1414

1515

app-src/scripts/settings/backup-settings/backup-settings-d.html

Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -38,29 +38,61 @@ <h3 class="md-caption">Sync via Google Drive</h3>
3838
<div>
3939
<md-button class="md-raised md-primary"
4040
ng-show="!vm.isLoggedIn"
41-
ng-click="vm.login()">Login
41+
ng-click="vm.login()">
42+
<ng-md-icon icon="login"></ng-md-icon>
43+
Login
4244
</md-button>
4345
<md-button class="md-raised md-primary"
4446
ng-show="vm.isLoggedIn"
45-
ng-click="vm.logout()">Logout
47+
ng-click="vm.logout()">
48+
<ng-md-icon icon="logout"></ng-md-icon>
49+
Logout
4650
</md-button>
51+
4752
<md-button class="md-raised md-primary"
4853
ng-show="vm.isLoggedIn"
4954
external-link
5055
target="_blank"
51-
href="https://myaccount.google.com/permissions">Revoke permissions
56+
href="https://myaccount.google.com/permissions">
57+
<ng-md-icon icon="remove_circle"></ng-md-icon>
58+
Revoke permissions
5259
</md-button>
5360
<md-button class="md-raised md-primary"
5461
ng-show="vm.isLoggedIn"
55-
ng-click="vm.test()">TEST
62+
ng-click="vm.backupNow()">
63+
<ng-md-icon icon="backup"></ng-md-icon>
64+
Save Backup now
5665
</md-button>
5766
</div>
67+
68+
<div>
69+
<md-switch ng-model="vm.settings.googleDriveSync.isAutoLogin"
70+
aria-label="Auto login at when starting app">
71+
Auto login at when starting app
72+
</md-switch>
73+
</div>
5874
<div>
59-
<md-switch ng-model="vm.settings.isAutoStartNextTask"
60-
aria-label="Auto sync tasks">
61-
Auto sync tasks
75+
<md-switch ng-model="vm.settings.googleDriveSync.isAutoSyncToRemote"
76+
aria-label="Auto sync to remote">
77+
Auto sync to remote
6278
</md-switch>
6379
</div>
80+
<div>
81+
<md-switch ng-model="vm.settings.googleDriveSync.isAutoSyncFromRemote"
82+
aria-label="Auto sync from remote">
83+
Auto sync from remote
84+
</md-switch>
85+
</div>
86+
<md-input-container class="md-block md-icon-float"
87+
ng-show="vm.settings.googleDriveSync.isAutoSyncToRemote || vm.settings.googleDriveSync.isAutoSyncFromRemote">
88+
<label>Remind when I worked longer than X without a break</label>
89+
<ng-md-icon icon="timer"
90+
aria-label="timer"></ng-md-icon>
91+
<input type="text"
92+
input-duration
93+
ng-model="vm.settings.googleDriveSync.syncInterval">
94+
</md-input-container>
95+
6496
</section>
6597

6698

@@ -86,7 +118,7 @@ <h3 class="md-caption">Sync via Google Drive</h3>
86118
<section ng-if="vm.IS_ELECTRON">
87119
<h3 class="md-caption">Automatic Backups</h3>
88120
<div>
89-
<md-switch ng-model="vm.settings.isEnabled"
121+
<md-switch ng-model="vm.settings.automaticBackups.isEnabled"
90122
aria-label="Enable automatic backups">
91123
Enable automatic backups
92124
</md-switch>
@@ -95,22 +127,22 @@ <h3 class="md-caption">Automatic Backups</h3>
95127
<p><strong>NOTE:</strong> Changes to automated backup settings require you to restart the application to take effect.
96128
</p>
97129

98-
<div ng-show="vm.settings.isEnabled">
130+
<div ng-show="vm.settings.automaticBackups.isEnabled">
99131
<md-input-container class="md-block">
100132
<label>Interval in seconds to make backups</label>
101133
<input type="number"
102-
ng-model="vm.settings.intervalInSeconds">
134+
ng-model="vm.settings.automaticBackups.intervalInSeconds">
103135
</md-input-container>
104136
<md-input-container class="md-block">
105137
<label>Path to backup (e.g. ~/backup-{date}.json)</label>
106138
<input type="text"
107-
ng-model="vm.settings.path">
139+
ng-model="vm.settings.automaticBackups.path">
108140
</md-input-container>
109141
</div>
110142

111143
<h3 class="md-caption">Automatic Sync</h3>
112144
<div>
113-
<md-switch ng-model="vm.settings.isSyncEnabled"
145+
<md-switch ng-model="vm.settings.automaticBackups.isSyncEnabled"
114146
aria-label="Enable automatic backups">
115147
Enable auto sync
116148
</md-switch>
@@ -120,11 +152,11 @@ <h3 class="md-caption">Automatic Sync</h3>
120152
<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.
121153
</p>
122154

123-
<div ng-show="vm.settings.isSyncEnabled">
155+
<div ng-show="vm.settings.automaticBackups.isSyncEnabled">
124156
<md-input-container class="md-block">
125157
<label>Path to sync file</label>
126158
<input type="text"
127-
ng-model="vm.settings.syncPath">
159+
ng-model="vm.settings.automaticBackups.syncPath">
128160
</md-input-container>
129161
</div>
130162
</section>

app-src/scripts/settings/backup-settings/backup-settings-d.js

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
}
2828

2929
/* @ngInject */
30-
function BackupSettingsCtrl(AppStorage, IS_ELECTRON, GoogleApi) {
30+
function BackupSettingsCtrl(AppStorage, IS_ELECTRON, GoogleApi, GoogleDriveSync) {
3131
let vm = this;
3232
vm.IS_ELECTRON = IS_ELECTRON;
3333

@@ -37,15 +37,8 @@
3737
AppStorage.importData(settings);
3838
};
3939

40-
vm.test = () => {
41-
GoogleApi.saveFile({
42-
title: 'SUPER_PRODUCTIVITY_SYNC.json',
43-
id: null,
44-
editable: true
45-
}, {
46-
test: 'DDDDDDDDDDDDDDDDDDDDDDDDD'
47-
})
48-
.then((res) => console.log(res));
40+
vm.backupNow = () => {
41+
GoogleDriveSync.saveTo();
4942
};
5043

5144
vm.login = () => {

0 commit comments

Comments
 (0)