From 1406231f8f57791d46dcbcfcdfa84fbb389e5393 Mon Sep 17 00:00:00 2001 From: Nicole O'Brien Date: Fri, 26 Apr 2024 16:45:58 +0100 Subject: [PATCH 1/2] refactor: move json file functions to json file manager --- .../electron/managers/json-file.manager.ts | 29 +++++++++++++++ .../lib/electron/processes/main.process.ts | 35 ++++--------------- 2 files changed, 35 insertions(+), 29 deletions(-) create mode 100644 packages/desktop/lib/electron/managers/json-file.manager.ts diff --git a/packages/desktop/lib/electron/managers/json-file.manager.ts b/packages/desktop/lib/electron/managers/json-file.manager.ts new file mode 100644 index 0000000000..746cd42dc9 --- /dev/null +++ b/packages/desktop/lib/electron/managers/json-file.manager.ts @@ -0,0 +1,29 @@ +import { IError } from '@core/error/interfaces' +import { app } from 'electron' +import fs from 'fs' +import path from 'path' + +export class JsonFileManager { + public static saveJsonToFile(filename: string, data: object): void { + try { + fs.writeFileSync(JsonFileManager.getFilePath(filename), JSON.stringify(data)) + } catch (err) { + console.error(err) + } + } + + public static loadJsonFromFile(filename: string): object | undefined { + try { + return JSON.parse(fs.readFileSync(JsonFileManager.getFilePath(filename)).toString()) + } catch (err) { + if (!(err as IError).message?.includes('ENOENT')) { + console.error(err) + } + } + } + + private static getFilePath(filename: string): string { + const userDataPath = app.getPath('userData') + return path.join(userDataPath, filename) + } +} diff --git a/packages/desktop/lib/electron/processes/main.process.ts b/packages/desktop/lib/electron/processes/main.process.ts index 5ed9625924..dcc90c83fe 100644 --- a/packages/desktop/lib/electron/processes/main.process.ts +++ b/packages/desktop/lib/electron/processes/main.process.ts @@ -36,7 +36,7 @@ import { getMachineId } from '../utils/os.utils' import { registerPowerMonitorListeners } from '../listeners' import ThirdPartyAppManager from '../managers/third-party-profiles.manager' import { ITransakWindowData } from '@core/app/interfaces' -import { IError } from '@core/error' +import { JsonFileManager } from '../managers/json-file.manager' export let appIsReady = false @@ -652,7 +652,7 @@ function windowStateKeeper(windowName: string, settingsFilename: string): IAppSt } function setBounds(): void { - const settings = loadJsonConfig(settingsFilename) + const settings = JsonFileManager.loadJsonFromFile(settingsFilename) if (settings && settings.windowState && settings.windowState[windowName]) { windowState = settings.windowState[windowName] @@ -666,13 +666,13 @@ function windowStateKeeper(windowName: string, settingsFilename: string): IAppSt windowState = window.getBounds() as IAppState } - let settings = loadJsonConfig(settingsFilename) as ISettings + let settings = JsonFileManager.loadJsonFromFile(settingsFilename) as ISettings settings = settings || {} settings.windowState = settings.windowState || {} settings.windowState[windowName] = windowState - saveJsonConfig(settingsFilename, settings) + JsonFileManager.saveJsonToFile(settingsFilename, settings) } function track(win: BrowserWindow): void { @@ -712,37 +712,14 @@ interface IAppState { function updateSettings(data: object): void { const filename = 'settings.json' - const config = loadJsonConfig(filename) + const config = JsonFileManager.loadJsonFromFile(filename) /** * CAUTION: We must be careful saving properties to this file, as * once we decide to save it there then it will be there forever * even if the name changes later. */ - saveJsonConfig(filename, { ...config, ...data }) -} - -function saveJsonConfig(filename: string, data: object): void { - try { - fs.writeFileSync(getJsonConfig(filename), JSON.stringify(data)) - } catch (err) { - console.error(err) - } -} - -function loadJsonConfig(filename: string): object | undefined { - try { - return JSON.parse(fs.readFileSync(getJsonConfig(filename)).toString()) - } catch (err) { - if (!(err as IError).message?.includes('ENOENT')) { - console.error(err) - } - } -} - -function getJsonConfig(filename: string): string { - const userDataPath = app.getPath('userData') - return path.join(userDataPath, filename) + JsonFileManager.saveJsonToFile(filename, { ...config, ...data }) } export function updateAppVersionDetails(details: object): void { From 6a6c175dcd9f2873ae977f4045ccb94e778630ef Mon Sep 17 00:00:00 2001 From: Nicole O'Brien Date: Fri, 26 Apr 2024 17:05:25 +0100 Subject: [PATCH 2/2] refactor: move code from main to electron settings manager --- .../interfaces/electron-settings.interface.ts | 5 ++ .../desktop/lib/electron/interfaces/index.ts | 2 + .../interfaces/window-state.interface.ts | 11 ++++ .../managers/electron-settings.manager.ts | 20 ++++++++ .../lib/electron/processes/main.process.ts | 51 +++++-------------- 5 files changed, 51 insertions(+), 38 deletions(-) create mode 100644 packages/desktop/lib/electron/interfaces/electron-settings.interface.ts create mode 100644 packages/desktop/lib/electron/interfaces/index.ts create mode 100644 packages/desktop/lib/electron/interfaces/window-state.interface.ts create mode 100644 packages/desktop/lib/electron/managers/electron-settings.manager.ts diff --git a/packages/desktop/lib/electron/interfaces/electron-settings.interface.ts b/packages/desktop/lib/electron/interfaces/electron-settings.interface.ts new file mode 100644 index 0000000000..d4ec0ee091 --- /dev/null +++ b/packages/desktop/lib/electron/interfaces/electron-settings.interface.ts @@ -0,0 +1,5 @@ +import { IWindowState } from './window-state.interface' + +export interface IElectronSettings { + windowState: IWindowState +} diff --git a/packages/desktop/lib/electron/interfaces/index.ts b/packages/desktop/lib/electron/interfaces/index.ts new file mode 100644 index 0000000000..379e42f5ef --- /dev/null +++ b/packages/desktop/lib/electron/interfaces/index.ts @@ -0,0 +1,2 @@ +export * from './electron-settings.interface' +export * from './window-state.interface' diff --git a/packages/desktop/lib/electron/interfaces/window-state.interface.ts b/packages/desktop/lib/electron/interfaces/window-state.interface.ts new file mode 100644 index 0000000000..2ad28433a6 --- /dev/null +++ b/packages/desktop/lib/electron/interfaces/window-state.interface.ts @@ -0,0 +1,11 @@ +import { BrowserWindow } from 'electron' + +export interface IWindowState { + x?: number + y?: number + width: number + height: number + isMaximized?: boolean + + track?(window: BrowserWindow): void +} diff --git a/packages/desktop/lib/electron/managers/electron-settings.manager.ts b/packages/desktop/lib/electron/managers/electron-settings.manager.ts new file mode 100644 index 0000000000..6b012ef2e4 --- /dev/null +++ b/packages/desktop/lib/electron/managers/electron-settings.manager.ts @@ -0,0 +1,20 @@ +import { JsonFileManager } from './json-file.manager' +import { IElectronSettings } from '../interfaces' + +const SETTINGS_FILE_NAME = 'settings.json' + +export class ElectronSettingsManager { + public static saveSettings(settings: IElectronSettings): void { + JsonFileManager.saveJsonToFile(SETTINGS_FILE_NAME, settings) + } + + public static loadSettings(): IElectronSettings { + const settings = JsonFileManager.loadJsonFromFile(SETTINGS_FILE_NAME) as IElectronSettings | undefined + return settings ? settings : { windowState: { width: 800, height: 600 } } + } + + public static updateSettings(settings: IElectronSettings): void { + const config = ElectronSettingsManager.loadSettings() + ElectronSettingsManager.saveSettings({ ...config, ...settings }) + } +} diff --git a/packages/desktop/lib/electron/processes/main.process.ts b/packages/desktop/lib/electron/processes/main.process.ts index dcc90c83fe..7df542116e 100644 --- a/packages/desktop/lib/electron/processes/main.process.ts +++ b/packages/desktop/lib/electron/processes/main.process.ts @@ -36,7 +36,8 @@ import { getMachineId } from '../utils/os.utils' import { registerPowerMonitorListeners } from '../listeners' import ThirdPartyAppManager from '../managers/third-party-profiles.manager' import { ITransakWindowData } from '@core/app/interfaces' -import { JsonFileManager } from '../managers/json-file.manager' +import { ElectronSettingsManager } from '../managers/electron-settings.manager' +import { IElectronSettings, IWindowState } from '../interfaces' export let appIsReady = false @@ -179,7 +180,7 @@ function tryOpenExternalUrl(e: Event, url: string): void { } export function createMainWindow(): BrowserWindow { - const mainWindowState = windowStateKeeper('main', 'settings.json') + const mainWindowState = windowStateKeeper('main') // Create the browser window windows.main = new BrowserWindow({ @@ -206,7 +207,7 @@ export function createMainWindow(): BrowserWindow { windows.main.maximize() } - mainWindowState.track(windows.main) + mainWindowState.track?.(windows.main) if (!app.isPackaged) { // Enable dev tools only in developer mode @@ -490,7 +491,7 @@ ipcMain.handle('handle-error', (_e, errorType, error) => { ipcMain.handle('get-machine-id', () => getMachineId()) // Settings -ipcMain.handle('update-app-settings', (_e, settings) => updateSettings(settings)) +ipcMain.handle('update-app-settings', (_e, settings) => ElectronSettingsManager.updateSettings(settings)) // Theme nativeTheme.on('updated', () => { @@ -642,9 +643,9 @@ export function closeErrorWindow(): void { } } -function windowStateKeeper(windowName: string, settingsFilename: string): IAppState { +function windowStateKeeper(windowName: string): IWindowState { let window: BrowserWindow - let windowState = { + let windowState = { x: 0, y: 0, width: 1280, @@ -652,7 +653,7 @@ function windowStateKeeper(windowName: string, settingsFilename: string): IAppSt } function setBounds(): void { - const settings = JsonFileManager.loadJsonFromFile(settingsFilename) + const settings = ElectronSettingsManager.loadSettings() if (settings && settings.windowState && settings.windowState[windowName]) { windowState = settings.windowState[windowName] @@ -663,16 +664,16 @@ function windowStateKeeper(windowName: string, settingsFilename: string): IAppSt function saveState(): void { windowState.isMaximized = window.isMaximized() if (!windowState.isMaximized) { - windowState = window.getBounds() as IAppState + windowState = window.getBounds() as IWindowState } - let settings = JsonFileManager.loadJsonFromFile(settingsFilename) as ISettings + let settings = ElectronSettingsManager.loadSettings() - settings = settings || {} - settings.windowState = settings.windowState || {} + settings = settings || {} + settings.windowState = settings.windowState || {} settings.windowState[windowName] = windowState - JsonFileManager.saveJsonToFile(settingsFilename, settings) + ElectronSettingsManager.saveSettings(settings) } function track(win: BrowserWindow): void { @@ -696,32 +697,6 @@ function windowStateKeeper(windowName: string, settingsFilename: string): IAppSt } } -interface ISettings { - windowState: IAppState -} - -interface IAppState { - x: number - y: number - width: number - height: number - isMaximized: boolean - - track(window: BrowserWindow): void -} - -function updateSettings(data: object): void { - const filename = 'settings.json' - const config = JsonFileManager.loadJsonFromFile(filename) - - /** - * CAUTION: We must be careful saving properties to this file, as - * once we decide to save it there then it will be there forever - * even if the name changes later. - */ - JsonFileManager.saveJsonToFile(filename, { ...config, ...data }) -} - export function updateAppVersionDetails(details: object): void { versionDetails = Object.assign({}, versionDetails, details)