-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update: Settings page now functional
- Loading branch information
1 parent
8884390
commit 657d8ab
Showing
8 changed files
with
304 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
//These are the global types i will be using in both processes | ||
|
||
type SettingsStoreType = { | ||
interval: number; | ||
message: string; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import { Notification } from "electron"; | ||
import { getStoredNumber, getStoredString } from "./storageHelper"; | ||
|
||
class NotificationHandler { | ||
private static instance: NotificationHandler | null = null; | ||
private intervalId: NodeJS.Timeout | null; | ||
|
||
// Interval in seconds | ||
private interval: number; | ||
private message: string; | ||
|
||
constructor(interval: number, message?: string) { | ||
this.intervalId = null; | ||
this.interval = interval; | ||
this.message = message; | ||
} | ||
|
||
static init(): NotificationHandler { | ||
if (!NotificationHandler.instance) { | ||
const interval = getStoredNumber("interval", 120); | ||
const message = getStoredString("message", "Time for a little break"); | ||
NotificationHandler.instance = new NotificationHandler(interval, message); | ||
} | ||
NotificationHandler.instance.start(); | ||
return NotificationHandler.instance; | ||
} | ||
|
||
static getInstance(): NotificationHandler { | ||
if (!NotificationHandler.instance) { | ||
throw new Error( | ||
"NotificationHandler has not been initialized please use the init() function first" | ||
); | ||
} | ||
return NotificationHandler.instance; | ||
} | ||
|
||
sendNotification(): void { | ||
const NOTIFICATION_TITLE = "Look Away 🔔"; | ||
const NOTIFICATION_BODY = this.message; | ||
const notif = new Notification({ | ||
title: NOTIFICATION_TITLE, | ||
body: NOTIFICATION_BODY, | ||
}); | ||
notif.show(); | ||
//? remove the notification after 20 seconds to stop them from pilling up | ||
setTimeout(() => notif.close(), 20000); | ||
} | ||
|
||
updateTime(newInterval: number): void { | ||
this.interval = newInterval; | ||
if (this.isRunning()) { | ||
this.restart(); | ||
} | ||
} | ||
|
||
update(newInterval: number, message: string): void { | ||
this.interval = newInterval; | ||
this.message = message; | ||
if (this.isRunning()) { | ||
this.restart(); | ||
} | ||
} | ||
|
||
updateMessage(message: string): void { | ||
this.message = message; | ||
if (this.isRunning()) { | ||
this.restart(); | ||
} | ||
} | ||
|
||
start(): void { | ||
if (!this.isRunning()) { | ||
this.intervalId = setInterval(() => { | ||
this.sendNotification(); | ||
}, this.interval * 1000); | ||
} | ||
} | ||
|
||
stop(): void { | ||
if (this.isRunning()) { | ||
clearInterval(this.intervalId!); | ||
this.intervalId = null; | ||
} | ||
} | ||
|
||
isRunning(): boolean { | ||
return this.intervalId !== null; | ||
} | ||
|
||
private restart(): void { | ||
this.stop(); | ||
this.start(); | ||
} | ||
} | ||
|
||
export default NotificationHandler; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import Store from "electron-store"; | ||
|
||
export const settingsStore = new Store<SettingsStoreType>({ | ||
schema: { | ||
interval: { | ||
type: "number", | ||
minimum: 1, | ||
}, | ||
message: { | ||
type: "string", | ||
}, | ||
}, | ||
defaults: { | ||
interval: 120, | ||
message: "Time for a little break", | ||
}, | ||
}); | ||
|
||
export function getStoredNumber(key: string, defaultValue?: number): number { | ||
try { | ||
return settingsStore.get(key, defaultValue); | ||
} catch (error) { | ||
console.error(`Error retrieving value for key "${key}":`, error); | ||
throw error; | ||
} | ||
} | ||
|
||
export function setStoredNumber(key: string, value: number): void { | ||
try { | ||
settingsStore.set(key, value); | ||
} catch (error) { | ||
console.error(`Error saving value for key "${key}":`, error); | ||
throw error; | ||
} | ||
} | ||
|
||
export function getStoredString(key: string, defaultValue?: string): string { | ||
try { | ||
const val = settingsStore.get(key, defaultValue); | ||
return val; | ||
} catch (error) { | ||
console.error(`Error retrieving value for key "${key}":`, error); | ||
throw error; | ||
} | ||
} | ||
|
||
export function setStoredString(key: string, value: string): void { | ||
try { | ||
settingsStore.set(key, value); | ||
} catch (error) { | ||
console.error(`Error saving value for key "${key}":`, error); | ||
throw error; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { ipcMain } from "electron"; | ||
import NotificationHandler from "./helpers/notificationHandler"; | ||
import { settingsStore } from "./helpers/storageHelper"; | ||
|
||
export function loadIpcHandlers() { | ||
ipcMain.handle("getSettingValue", (event, key: string) => { | ||
return settingsStore.get(key); | ||
}); | ||
|
||
ipcMain.handle("updateSettings", (event, settings: SettingsStoreType) => { | ||
try { | ||
settingsStore.set("interval", settings.interval); | ||
settingsStore.set("message", settings.message); | ||
const notificationHandler = NotificationHandler.getInstance(); | ||
notificationHandler.update(settings.interval, settings.message); | ||
console.log( | ||
`Set interval to ${settings.interval} and message to ${settings.message}` | ||
); | ||
return true; | ||
} catch (error) { | ||
return false; | ||
} | ||
}); | ||
|
||
ipcMain.on("show:testNotification", (event, arg) => { | ||
NotificationHandler.getInstance().sendNotification(); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.