Merge branch 'notification' into 'dev'

Notification

See merge request crimata/electron-app!8
This commit is contained in:
Enrique Hernandez 2021-07-29 12:31:19 +00:00
commit 9de4ff0805
4 changed files with 61 additions and 11 deletions

View file

@ -9,6 +9,7 @@ import { app, protocol } from "electron";
import createWindow from "./window";
import main from "./main";
import { backgroundMitt } from '@/composables/useEmitter';
import {setWindow, getWindow } from './store';
console.log('Starting Crimata electron app.');
@ -19,11 +20,9 @@ protocol.registerSchemesAsPrivileged([
const isDev = require('electron-is-dev');
let win: boolean;
// Listen for window creation.
backgroundMitt.on('window-active', (state: boolean) => {
win = state;
setWindow(state)
});
/* Start main process on ready */
@ -41,7 +40,7 @@ app.on("window-all-closed", () => {
// When user clicks app icon (re-open)
app.on("activate", () => {
if (!win) createWindow();
if (!getWindow()) createWindow();
});
// Exit cleanly on request from parent process in development mode.

View file

@ -13,7 +13,6 @@ import initIpcMain from "@/ipc/index";
import { accountAuth } from "./account";
import createWindow from "./window";
export default async function main() {
/* initiate controls for frontend to use when needed */

View file

@ -4,13 +4,27 @@ import UIState from "@/uiState";
import { config } from "@/config";
import useWebsockets from "./composables/useWebsockets";
import { backgroundMitt, ipcEmit } from "@/composables/useEmitter";
import { Notification } from 'electron';
import { getWindow } from './store';
function showNotification (options: {
title: string;
subtitle?: string;
body: string;
}) {
new Notification(
{
title: options.title,
subtitle: options.subtitle,
body: options.body,
}).show()
}
// UI state
const uiState = new UIState()
const uiState = new UIState();
// let audio = new Audio();
const onMessageCallback = (payload: string) => {
if (payload === "CLOSE_AUTH_FAIL") {
@ -22,7 +36,23 @@ const onMessageCallback = (payload: string) => {
if (message.header === "init") {
uiState.set(message.body as Init);
} else uiState.update(message.body as Update);
} else {
const body = message.body as Update;
uiState.update(body);
// show notification if window is not active
if (!getWindow()) {
if (body.name === "add") {
const data = body.data as Message;
showNotification({
title: 'New Message',
subtitle: data.context.text as string,
body: data.content[0].text,
});
}
}
}
}
@ -69,3 +99,4 @@ export function endSession() {
uiState.reset();
}

View file

@ -3,7 +3,10 @@ const Store = require('electron-store');
const schema = {
token: {
type: 'string',
}
},
window: {
type: 'boolean'
}
};
const store = new Store({
@ -11,9 +14,12 @@ const store = new Store({
encryptionKey: "super user test"
});
/*
* Token state management
* */
export const setToken = (token: string): void => {
store.set("token", token);
}
}
export const getToken = (): string | undefined => {
return store.get("token");
@ -21,4 +27,19 @@ export const getToken = (): string | undefined => {
export const clearToken = (): void => {
store.delete("token");
}
}
/*
* Window state management
* */
export const setWindow = (state: boolean): void => {
store.set("window", state);
}
export const getWindow = (): boolean | undefined => {
return store.get("window");
}
export const clearWindow = (): void => {
store.delete("window");
}