Skip to content

Commit 657d8ab

Browse files
committed
update: Settings page now functional
1 parent 8884390 commit 657d8ab

File tree

8 files changed

+304
-41
lines changed

8 files changed

+304
-41
lines changed

global/types.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
//These are the global types i will be using in both processes
2+
3+
type SettingsStoreType = {
4+
interval: number;
5+
message: string;
6+
};

main/background.ts

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import { Menu, Notification, Tray, app } from "electron";
2-
import log from "electron-log";
1+
import { Menu, Tray, app } from "electron";
32
import serve from "electron-serve";
43
import { createWindow, getAppPath } from "./helpers";
4+
import NotificationHandler from "./helpers/notificationHandler";
5+
import { loadIpcHandlers } from "./ipc";
56
const isProd: boolean = process.env.NODE_ENV === "production";
67

78
if (isProd) {
@@ -13,9 +14,6 @@ if (isProd) {
1314
(async () => {
1415
await app.whenReady();
1516
const iconpath = getAppPath("resources/icon.ico");
16-
console.log(iconpath);
17-
log.info("Log from the main process");
18-
log.info(`IconPath: ${iconpath}`);
1917
const mainWindow = createWindow("main", {
2018
width: 600,
2119
height: 700,
@@ -41,24 +39,20 @@ if (isProd) {
4139
mainWindow.show();
4240
});
4341
tray.setContextMenu(contextMenu);
42+
//* Load the IPC Handlers
43+
loadIpcHandlers();
44+
//* Initialize the Notification handler
45+
NotificationHandler.init();
4446

47+
//* Load our Page
4548
if (isProd) {
4649
await mainWindow.loadURL("app://./home.html");
4750
} else {
4851
const port = process.argv[2];
4952
await mainWindow.loadURL(`http://localhost:${port}/home`);
5053
// mainWindow.webContents.openDevTools();
5154
}
52-
setInterval(showNotification, 120000);
5355
})();
54-
const showNotification = () => {
55-
const NOTIFICATION_TITLE = "Look Away 🔔";
56-
const NOTIFICATION_BODY = "Avert yer eyeees!!!!";
57-
new Notification({
58-
title: NOTIFICATION_TITLE,
59-
body: NOTIFICATION_BODY,
60-
}).show();
61-
};
6256
app.on("window-all-closed", () => {
6357
app.quit();
6458
});

main/helpers/notificationHandler.ts

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
import { Notification } from "electron";
2+
import { getStoredNumber, getStoredString } from "./storageHelper";
3+
4+
class NotificationHandler {
5+
private static instance: NotificationHandler | null = null;
6+
private intervalId: NodeJS.Timeout | null;
7+
8+
// Interval in seconds
9+
private interval: number;
10+
private message: string;
11+
12+
constructor(interval: number, message?: string) {
13+
this.intervalId = null;
14+
this.interval = interval;
15+
this.message = message;
16+
}
17+
18+
static init(): NotificationHandler {
19+
if (!NotificationHandler.instance) {
20+
const interval = getStoredNumber("interval", 120);
21+
const message = getStoredString("message", "Time for a little break");
22+
NotificationHandler.instance = new NotificationHandler(interval, message);
23+
}
24+
NotificationHandler.instance.start();
25+
return NotificationHandler.instance;
26+
}
27+
28+
static getInstance(): NotificationHandler {
29+
if (!NotificationHandler.instance) {
30+
throw new Error(
31+
"NotificationHandler has not been initialized please use the init() function first"
32+
);
33+
}
34+
return NotificationHandler.instance;
35+
}
36+
37+
sendNotification(): void {
38+
const NOTIFICATION_TITLE = "Look Away 🔔";
39+
const NOTIFICATION_BODY = this.message;
40+
const notif = new Notification({
41+
title: NOTIFICATION_TITLE,
42+
body: NOTIFICATION_BODY,
43+
});
44+
notif.show();
45+
//? remove the notification after 20 seconds to stop them from pilling up
46+
setTimeout(() => notif.close(), 20000);
47+
}
48+
49+
updateTime(newInterval: number): void {
50+
this.interval = newInterval;
51+
if (this.isRunning()) {
52+
this.restart();
53+
}
54+
}
55+
56+
update(newInterval: number, message: string): void {
57+
this.interval = newInterval;
58+
this.message = message;
59+
if (this.isRunning()) {
60+
this.restart();
61+
}
62+
}
63+
64+
updateMessage(message: string): void {
65+
this.message = message;
66+
if (this.isRunning()) {
67+
this.restart();
68+
}
69+
}
70+
71+
start(): void {
72+
if (!this.isRunning()) {
73+
this.intervalId = setInterval(() => {
74+
this.sendNotification();
75+
}, this.interval * 1000);
76+
}
77+
}
78+
79+
stop(): void {
80+
if (this.isRunning()) {
81+
clearInterval(this.intervalId!);
82+
this.intervalId = null;
83+
}
84+
}
85+
86+
isRunning(): boolean {
87+
return this.intervalId !== null;
88+
}
89+
90+
private restart(): void {
91+
this.stop();
92+
this.start();
93+
}
94+
}
95+
96+
export default NotificationHandler;

main/helpers/storageHelper.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import Store from "electron-store";
2+
3+
export const settingsStore = new Store<SettingsStoreType>({
4+
schema: {
5+
interval: {
6+
type: "number",
7+
minimum: 1,
8+
},
9+
message: {
10+
type: "string",
11+
},
12+
},
13+
defaults: {
14+
interval: 120,
15+
message: "Time for a little break",
16+
},
17+
});
18+
19+
export function getStoredNumber(key: string, defaultValue?: number): number {
20+
try {
21+
return settingsStore.get(key, defaultValue);
22+
} catch (error) {
23+
console.error(`Error retrieving value for key "${key}":`, error);
24+
throw error;
25+
}
26+
}
27+
28+
export function setStoredNumber(key: string, value: number): void {
29+
try {
30+
settingsStore.set(key, value);
31+
} catch (error) {
32+
console.error(`Error saving value for key "${key}":`, error);
33+
throw error;
34+
}
35+
}
36+
37+
export function getStoredString(key: string, defaultValue?: string): string {
38+
try {
39+
const val = settingsStore.get(key, defaultValue);
40+
return val;
41+
} catch (error) {
42+
console.error(`Error retrieving value for key "${key}":`, error);
43+
throw error;
44+
}
45+
}
46+
47+
export function setStoredString(key: string, value: string): void {
48+
try {
49+
settingsStore.set(key, value);
50+
} catch (error) {
51+
console.error(`Error saving value for key "${key}":`, error);
52+
throw error;
53+
}
54+
}

main/ipc.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { ipcMain } from "electron";
2+
import NotificationHandler from "./helpers/notificationHandler";
3+
import { settingsStore } from "./helpers/storageHelper";
4+
5+
export function loadIpcHandlers() {
6+
ipcMain.handle("getSettingValue", (event, key: string) => {
7+
return settingsStore.get(key);
8+
});
9+
10+
ipcMain.handle("updateSettings", (event, settings: SettingsStoreType) => {
11+
try {
12+
settingsStore.set("interval", settings.interval);
13+
settingsStore.set("message", settings.message);
14+
const notificationHandler = NotificationHandler.getInstance();
15+
notificationHandler.update(settings.interval, settings.message);
16+
console.log(
17+
`Set interval to ${settings.interval} and message to ${settings.message}`
18+
);
19+
return true;
20+
} catch (error) {
21+
return false;
22+
}
23+
});
24+
25+
ipcMain.on("show:testNotification", (event, arg) => {
26+
NotificationHandler.getInstance().sendNotification();
27+
});
28+
}

renderer/pages/home.tsx

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,24 @@
11
import { Layout, Result, Row } from "antd";
2+
import { ipcRenderer } from "electron";
23
import Head from "next/head";
34
import Link from "next/link";
4-
import React from "react";
5+
import React, { useEffect, useState } from "react";
56

67
const { Header, Content } = Layout;
78

9+
const ipc = ipcRenderer || false;
10+
811
function Home() {
12+
const [intervalSetting, setIntervalSetting] = useState<number>();
13+
14+
useEffect(() => {
15+
if (ipc) {
16+
ipc
17+
.invoke("getSettingValue", "interval")
18+
.then((v) => setIntervalSetting(v));
19+
}
20+
}, []);
21+
922
return (
1023
<React.Fragment>
1124
{/* @ts-ignore */}
@@ -28,9 +41,7 @@ function Home() {
2841
title="LAR"
2942
subTitle="Look away Reminder"
3043
/>
31-
<p>
32-
Will show a notification every 2 minutes or 120 seconds by default.
33-
</p>
44+
<p>We will show a notification every {intervalSetting} seconds.</p>
3445
</div>
3546
</Content>
3647
</React.Fragment>

0 commit comments

Comments
 (0)