-
Notifications
You must be signed in to change notification settings - Fork 10
/
app.js
234 lines (210 loc) · 6.45 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
const electron = require('electron');
const AutoLaunch = require('auto-launch');
const contribution = require('contribution');
const { CronJob, CronTime } = require('cron');
const icon = require('./icon');
const store = require('./store');
const pjson = require('../package.json');
const {
app,
BrowserWindow,
ipcMain,
Menu,
Notification,
shell,
Tray,
} = electron;
app.on('ready', () => {
const autoLauncher = new AutoLaunch({ name: pjson.name });
const tray = new Tray(icon.done);
let preferencesWindow = null;
function createPreferencesWindow() {
preferencesWindow = new BrowserWindow({
title: `${pjson.name} - Preferences`,
width: 300,
height: 320,
resizable: false,
maximizable: false,
minimizable: false,
fullscreenable: false,
show: false,
});
preferencesWindow.loadURL(
`file://${__dirname}/preferences/preferences.html`,
);
preferencesWindow.on('ready-to-show', () => {
preferencesWindow.show();
if (process.platform === 'darwin') {
app.dock.show();
}
});
preferencesWindow.on('closed', () => {
preferencesWindow = null;
if (process.platform === 'darwin') {
app.dock.hide();
}
});
}
function onPreferencesClick() {
if (preferencesWindow === null) {
return createPreferencesWindow();
}
preferencesWindow.focus();
}
function createTrayMenu(data = { streak: {}, contributions: {} }) {
return Menu.buildFromTemplate([
{
label: store.get('username') || 'Set username...',
enabled: !store.get('username'),
click: onPreferencesClick,
},
{ type: 'separator' },
{ label: 'Streak:', enabled: false },
{ label: ` Best: ${data.streak.best || 0}`, enabled: false },
{ label: ` Current: ${data.streak.current || 0}`, enabled: false },
{ label: 'Contributions:', enabled: false },
{ label: ` Best: ${data.contributions.best || 0}`, enabled: false },
{
label: ` Current: ${data.contributions.current || 0}`,
enabled: false,
},
{
label: ` Total: ${data.contributions.total || 0}`,
enabled: false,
},
{ type: 'separator' },
{
label: 'Reload',
enabled: !!store.get('username'),
accelerator: 'CmdOrCtrl+R',
click: requestContributionData,
},
{
label: 'Open GitHub Profile...',
enabled: !!store.get('username'),
accelerator: 'CmdOrCtrl+O',
click: () =>
shell.openExternal(`https://github.com/${store.get('username')}`),
},
{ type: 'separator' },
{
label: 'Preferences...',
accelerator: 'CmdOrCtrl+,',
click: onPreferencesClick,
},
{
label: `About ${pjson.name}...`,
click: () => shell.openExternal(pjson.homepage),
},
{
label: 'Feedback && Support...',
click: () => shell.openExternal(pjson.bugs.url),
},
{ type: 'separator' },
{
label: `Quit ${pjson.name}`,
accelerator: 'CmdOrCtrl+Q',
click: () => app.quit(),
},
]);
}
async function requestContributionData() {
const username = store.get('username');
if (!username) {
tray.setImage(icon.fail);
tray.setContextMenu(createTrayMenu());
return;
}
tray.setImage(icon.load);
tray.setContextMenu(createTrayMenu());
setTimeout(requestContributionData, 1000 * 60 * store.get('syncInterval'));
try {
const data = await contribution(username);
tray.setContextMenu(createTrayMenu(data));
tray.setImage(data.streak.current > 0 ? icon.done : icon.todo);
return data;
} catch (error) {
tray.setContextMenu(createTrayMenu());
tray.setImage(icon.fail);
throw new Error(error);
}
}
async function setUsername(event, username) {
try {
store.set('username', username);
await requestContributionData();
store.set('userExists', !!username);
event.sender.send('usernameSet', !!username);
} catch (error) {
store.set('userExists', false);
event.sender.send('usernameSet', false);
}
}
function setSyncInterval(event, interval) {
store.set('syncInterval', interval);
event.sender.send('syncIntervalSet');
}
function activateLaunchAtLogin(event, isEnabled) {
store.set('autoLaunch', isEnabled);
isEnabled ? autoLauncher.enable() : autoLauncher.disable();
event.sender.send('activateLaunchAtLoginSet');
}
function activateNotifications(event, isEnabled) {
store.set('notification.isEnabled', isEnabled);
if (isEnabled) {
const time = store.get('notification.time');
const timeArray = time.split(':');
const cronTime = `0 ${timeArray[1]} ${timeArray[0]} * * *`;
job.setTime(new CronTime(cronTime));
job.start();
} else {
job.stop();
}
event.sender.send('activateNotificationsSet');
}
function setNotificationTime(event, time) {
store.set('notification.time', time);
const timeArray = time.split(':');
const cronTime = `0 ${timeArray[1]} ${timeArray[0]} * * *`;
job.setTime(new CronTime(cronTime));
job.start();
event.sender.send('NotificationTimeSet');
}
const job = new CronJob({
cronTime: '0 0 20 00 * *',
async onTick() {
const data = await contribution(store.get('username'));
if (data.streak.current === 0 && Notification.isSupported()) {
new Notification({
title: pjson.name,
body: "You haven't contributed today",
}).show();
}
},
});
if (store.get('notification.isEnabled')) {
const time = store.get('notification.time');
const timeArray = time.split(':');
const cronTime = `0 ${timeArray[1]} ${timeArray[0]} * * *`;
job.setTime(new CronTime(cronTime));
job.start();
}
process.on('uncaughtException', () => {
tray.setContextMenu(createTrayMenu());
tray.setImage(icon.fail);
});
if (process.platform === 'darwin') {
app.dock.hide();
}
app.on('window-all-closed', () => {});
tray.on('right-click', requestContributionData);
ipcMain.on('setUsername', setUsername);
ipcMain.on('setSyncInterval', setSyncInterval);
ipcMain.on('activateLaunchAtLogin', activateLaunchAtLogin);
ipcMain.on('activateNotifications', activateNotifications);
ipcMain.on('setNotificationTime', setNotificationTime);
requestContributionData();
if (!store.get('username')) {
createPreferencesWindow();
}
});