Skip to content

Commit

Permalink
Add basic persistent settings
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephen McCann authored and Stephen McCann committed May 7, 2020
1 parent 0421ed4 commit 14792fa
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 26 deletions.
20 changes: 16 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"dependencies": {
"electron-is-dev": "^1.2.0",
"electron-log": "^4.1.1",
"electron-settings": "^3.2.0",
"electron-updater": "^4.3.1",
"play-sound": "^1.1.3"
}
Expand Down
12 changes: 8 additions & 4 deletions src/ContextMenu.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
const EventEmitter = require("events");
const settings = require("electron-settings");

const { getIconsByTheme } = require("./utils");
const { installUpdateAndRestart } = require("./updater");

class ContextMenu extends EventEmitter {
constructor() {
constructor(userSettings) {
super();

const { playIcon, stopIcon, updateIcon } = getIconsByTheme();
Expand Down Expand Up @@ -38,12 +39,15 @@ class ContextMenu extends EventEmitter {
},
{ type: "separator" },
{
id: "settings",
label: "Settings",
id: "sound",
label: "Play Sound",
type: "checkbox",
checked: userSettings.playSound,
click: () => {
this.emit("showSettings");
settings.set("playSound", !userSettings.playSound);
}
},
{ type: "separator" },
{ label: "Quit", role: "quit" }
];
}
Expand Down
49 changes: 31 additions & 18 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ const {
Tray
} = require("electron");
const log = require("electron-log");
const settings = require("electron-settings");

const { showNotification } = require("./notifications");
const { checkForUpdates } = require("./updater");
const { setProductionAppPreferences } = require("./utils");
const { setProductionAppPreferences, loadSettings } = require("./utils");

setProductionAppPreferences();

Expand All @@ -21,44 +22,56 @@ const ContextMenu = require("./ContextMenu");
let player, contextMenu, menuTemplate;

app.on("ready", () => {
function buildMenu(tray) {
contextMenu = new ContextMenu();
const userSettings = loadSettings();

function buildMenu(tray, userSettings) {
contextMenu = new ContextMenu(userSettings);
menuTemplate = Menu.buildFromTemplate(contextMenu.menuItems);
tray.setContextMenu(menuTemplate);
}

function handlePlay() {
if (settings.get("playSound")) {
player.play();
}

timer.start();

contextMenu.toggleStartStop(menuTemplate);

showNotification({
title: "Time to Work 👨‍💻",
body: "Get it done. It'll feel good later."
});
}

function handleStop({ shouldReset }) {
if (settings.get("playSound")) {
player.stop();
}

timer.resetTimer(shouldReset);
player.stop();
contextMenu.toggleStartStop(menuTemplate);
}

function handleBreak() {
player.stop();
if (settings.get("playSound")) {
player.stop();
}

timer.resetTimer();
timer.start();

showNotification({
title: "Break Time 🧘‍♀️",
body: "10 push ups then chill."
});
}

function handlePlay() {
timer.start();
player.play();
contextMenu.toggleStartStop(menuTemplate);

showNotification({
title: "Time to Work 👨‍💻",
body: "Get it done. It'll feel good later."
body: "15 pushups then chill."
});
}

const tray = new Tray(nativeImage.createEmpty());
const timer = new Timer(tray);
player = new Player(tray);
buildMenu(tray);
buildMenu(tray, userSettings);

contextMenu.on("play", handlePlay);
contextMenu.on("stop", handleStop);
Expand Down
13 changes: 13 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
const { app, dialog, nativeTheme } = require("electron");
const settings = require("electron-settings");
const path = require("path");
const isDev = require("electron-is-dev");

const loadSettings = () => {
// Default play sound to true
if (!settings.has("playSound")) {
settings.set("playSound", true);
}

return {
playSound: settings.get("playSound")
};
};

const joinPath = (relativePath, { extraResource = false } = {}) => {
// Prod: assets is in the same directory (as Resources) so remove the ../
if (extraResource && !isDev) {
Expand Down Expand Up @@ -66,6 +78,7 @@ const TIMES = {

module.exports = {
TIMES,
loadSettings,
joinPath,
getTimerDisplay,
getIconsByTheme,
Expand Down

0 comments on commit 14792fa

Please sign in to comment.