From 62131c74bd3926b89d30705eea8bd731b7292421 Mon Sep 17 00:00:00 2001 From: Geoffrey Hunter Date: Tue, 18 Jun 2024 21:31:46 +1200 Subject: [PATCH] Refactor some data classes into their own files. Rename profile manager. --- src/model/App.tsx | 6 +- .../AppDataManager.spec.ts} | 6 +- .../AppDataManager.ts} | 118 +----------------- .../AppDataManager/DataClasses/AppData.ts | 43 +++++++ .../AppDataManager/DataClasses/Profile.ts | 27 ++++ .../AppDataManager/DataClasses/RootConfig.ts | 52 ++++++++ .../DisplaySettings/DisplaySettings.ts | 6 +- .../GeneralSettings/GeneralSettings.ts | 6 +- .../PortConfigurationSettings.ts | 4 +- .../ProfileSettings/ProfileSettings.ts | 6 +- src/model/Settings/RxSettings/RxSettings.ts | 6 +- src/model/Settings/Settings.tsx | 2 +- src/model/Settings/TxSettings/TxSettings.ts | 6 +- .../Terminals/RightDrawer/RightDrawer.ts | 4 +- .../SingleTerminal/SingleTerminal.spec.ts | 6 +- .../ProfileSettings/ProfileSettingsView.tsx | 4 +- 16 files changed, 156 insertions(+), 146 deletions(-) rename src/model/{ProfileManager/ProfileManager.spec.ts => AppDataManager/AppDataManager.spec.ts} (83%) rename src/model/{ProfileManager/ProfileManager.ts => AppDataManager/AppDataManager.ts} (77%) create mode 100644 src/model/AppDataManager/DataClasses/AppData.ts create mode 100644 src/model/AppDataManager/DataClasses/Profile.ts create mode 100644 src/model/AppDataManager/DataClasses/RootConfig.ts diff --git a/src/model/App.tsx b/src/model/App.tsx index af701954..d5f599f9 100644 --- a/src/model/App.tsx +++ b/src/model/App.tsx @@ -19,7 +19,7 @@ import SingleTerminal from './Terminals/SingleTerminal/SingleTerminal'; import { BackspaceKeyPressBehavior, DeleteKeyPressBehavior, EnterKeyPressBehavior } from './Settings/TxSettings/TxSettings'; import { SelectionController, SelectionInfo } from './SelectionController/SelectionController'; import { isRunningOnWindows } from './Util/Util'; -import { LastUsedSerialPort, ProfileManager } from './ProfileManager/ProfileManager'; +import { LastUsedSerialPort, AppDataManager } from './AppDataManager/AppDataManager'; declare global { interface String { @@ -115,7 +115,7 @@ export class App { fakePortController: FakePortsController = new FakePortsController(this); - profileManager: ProfileManager; + profileManager: AppDataManager; selectionController: SelectionController = new SelectionController(); @@ -132,7 +132,7 @@ export class App { // Read out the version number from package.json this.version = packageDotJson['version']; - this.profileManager = new ProfileManager(this); + this.profileManager = new AppDataManager(this); this.settings = new Settings(this); this.snackbar = new Snackbar(); diff --git a/src/model/ProfileManager/ProfileManager.spec.ts b/src/model/AppDataManager/AppDataManager.spec.ts similarity index 83% rename from src/model/ProfileManager/ProfileManager.spec.ts rename to src/model/AppDataManager/AppDataManager.spec.ts index 6ad59523..6ebf8acd 100644 --- a/src/model/ProfileManager/ProfileManager.spec.ts +++ b/src/model/AppDataManager/AppDataManager.spec.ts @@ -1,6 +1,6 @@ import { expect, test, describe, beforeEach } from "vitest"; -import { ProfileManager } from "./ProfileManager"; +import { AppDataManager } from "./AppDataManager"; import { App } from "../App"; beforeEach(() => { @@ -12,14 +12,14 @@ beforeEach(() => { describe("profile manager tests", () => { test("default profile should be created", () => { const app = new App(); - const profileManager = new ProfileManager(app); + const profileManager = new AppDataManager(app); expect(profileManager.appData.profiles.length).toEqual(1); expect(profileManager.appData.profiles[0].name).toEqual("Default profile"); }); test("new profile can be created", () => { const app = new App(); - const profileManager = new ProfileManager(app); + const profileManager = new AppDataManager(app); profileManager.newProfile(); expect(profileManager.appData.profiles.length).toEqual(2); expect(profileManager.appData.profiles[1].name).toEqual("New profile 1"); diff --git a/src/model/ProfileManager/ProfileManager.ts b/src/model/AppDataManager/AppDataManager.ts similarity index 77% rename from src/model/ProfileManager/ProfileManager.ts rename to src/model/AppDataManager/AppDataManager.ts index 0c5deea4..a856e356 100644 --- a/src/model/ProfileManager/ProfileManager.ts +++ b/src/model/AppDataManager/AppDataManager.ts @@ -1,134 +1,22 @@ import { makeAutoObservable } from 'mobx'; -import { DisplaySettingsConfig } from '../Settings/DisplaySettings/DisplaySettings'; -import { GeneralSettingsConfig } from '../Settings/GeneralSettings/GeneralSettings'; import { PortConfigurationConfigV2, PortConfigurationConfigV3, PortState } from '../Settings/PortConfigurationSettings/PortConfigurationSettings'; -import { RxSettingsConfig } from '../Settings/RxSettings/RxSettings'; -import { TxSettingsConfig } from '../Settings/TxSettings/TxSettings'; -import { MacroControllerConfig } from '../Terminals/RightDrawer/Macros/MacroController'; import { App } from '../App'; import { VariantType } from 'notistack'; import { RightDrawerConfig } from '../Terminals/RightDrawer/RightDrawer'; +import { AppDataV1, AppDataV2 } from './DataClasses/AppData'; +import { ProfileV3 } from './DataClasses/Profile'; export class LastUsedSerialPort { serialPortInfo: Partial = {}; portState: PortState = PortState.CLOSED; } -/** - * Everything in this class must be POD (plain old data) and serializable to JSON. - */ -export class RootConfigV2 { - version = 2; - - terminal = { - macroController: new MacroControllerConfig(), - rightDrawer: new RightDrawerConfig(), - }; - - lastUsedSerialPort: LastUsedSerialPort = new LastUsedSerialPort(); - - settings = { - portSettings: new PortConfigurationConfigV2(), - txSettings: new TxSettingsConfig(), - rxSettings: new RxSettingsConfig(), - displaySettings: new DisplaySettingsConfig(), - generalSettings: new GeneralSettingsConfig(), - }; -} - -/** - * Everything in this class must be POD (plain old data) and serializable to JSON. - */ -export class RootConfigV3 { - version = 3; - - terminal = { - macroController: new MacroControllerConfig(), - rightDrawer: new RightDrawerConfig(), - }; - - lastUsedSerialPort: LastUsedSerialPort = new LastUsedSerialPort(); - - settings = { - portSettings: new PortConfigurationConfigV3(), - txSettings: new TxSettingsConfig(), - rxSettings: new RxSettingsConfig(), - displaySettings: new DisplaySettingsConfig(), - generalSettings: new GeneralSettingsConfig(), - }; -} - -/** - * This class represents all the data stored in a user profile. It is used to store use-specific - * settings for the application (e.g. all the settings to talk to a particular - * embedded device). The class is serializable to JSON. - */ -export class ProfileV2 { - name: string = ''; - rootConfig: RootConfigV2 = new RootConfigV2(); - - constructor(name: string) { - this.name = name; - makeAutoObservable(this); - } -} - -export class ProfileV3 { - name: string = ''; - rootConfig: RootConfigV3 = new RootConfigV3(); - - constructor(name: string) { - this.name = name; - makeAutoObservable(this); - } -} - -/** - * This class represents all the data that the app needs to store/load from - * local storage (i.e. the root object). It must be serializable to JSON. - */ -export class AppDataV1 { - - version = 1; - - profiles: ProfileV2[] = []; - - /** - * Represents the current application configuration. This is saved regularly so that when the app reloads, - * it can restore the last known configuration. - */ - currentAppConfig: RootConfigV2 = new RootConfigV2(); - - constructor() { - makeAutoObservable(this); - } -} - -export class AppDataV2 { - - version = 2; - - profiles: ProfileV3[] = []; - - /** - * Represents the current application configuration. This is saved regularly so that when the app reloads, - * it can restore the last known configuration. - */ - currentAppConfig: RootConfigV3 = new RootConfigV3(); - - constructor() { - this.profiles = []; - this.profiles.push(new ProfileV3('Default profile')); - makeAutoObservable(this); - } -} - type AppData = AppDataV2; const APP_DATA_STORAGE_KEY = 'appData'; -export class ProfileManager { +export class AppDataManager { app: App; appData: AppDataV2 = new AppDataV2(); diff --git a/src/model/AppDataManager/DataClasses/AppData.ts b/src/model/AppDataManager/DataClasses/AppData.ts new file mode 100644 index 00000000..a026f222 --- /dev/null +++ b/src/model/AppDataManager/DataClasses/AppData.ts @@ -0,0 +1,43 @@ +import { makeAutoObservable } from "mobx"; +import { ProfileV2, ProfileV3 } from "./Profile"; +import { RootConfigV2, RootConfigV3 } from "./RootConfig"; + +/** + * This class represents all the data that the app needs to store/load from + * local storage (i.e. the root object). It must be serializable to JSON. + */ +export class AppDataV1 { + + version = 1; + + profiles: ProfileV2[] = []; + + /** + * Represents the current application configuration. This is saved regularly so that when the app reloads, + * it can restore the last known configuration. + */ + currentAppConfig: RootConfigV2 = new RootConfigV2(); + + constructor() { + makeAutoObservable(this); + } +} + +export class AppDataV2 { + + version = 2; + + profiles: ProfileV3[] = []; + + /** + * Represents the current application configuration. This is saved regularly so that when the app reloads, + * it can restore the last known configuration. + */ + currentAppConfig: RootConfigV3 = new RootConfigV3(); + + constructor() { + this.profiles = []; + this.profiles.push(new ProfileV3('Default profile')); + makeAutoObservable(this); + } +} diff --git a/src/model/AppDataManager/DataClasses/Profile.ts b/src/model/AppDataManager/DataClasses/Profile.ts new file mode 100644 index 00000000..d517b8e1 --- /dev/null +++ b/src/model/AppDataManager/DataClasses/Profile.ts @@ -0,0 +1,27 @@ +import { makeAutoObservable } from "mobx"; +import { RootConfigV2, RootConfigV3 } from "./RootConfig"; + +/** + * This class represents all the data stored in a user profile. It is used to store use-specific + * settings for the application (e.g. all the settings to talk to a particular + * embedded device). The class is serializable to JSON. + */ +export class ProfileV2 { + name: string = ''; + rootConfig: RootConfigV2 = new RootConfigV2(); + + constructor(name: string) { + this.name = name; + makeAutoObservable(this); + } +} + +export class ProfileV3 { + name: string = ''; + rootConfig: RootConfigV3 = new RootConfigV3(); + + constructor(name: string) { + this.name = name; + makeAutoObservable(this); + } +} diff --git a/src/model/AppDataManager/DataClasses/RootConfig.ts b/src/model/AppDataManager/DataClasses/RootConfig.ts new file mode 100644 index 00000000..aa0d57af --- /dev/null +++ b/src/model/AppDataManager/DataClasses/RootConfig.ts @@ -0,0 +1,52 @@ +import { MacroControllerConfig } from "src/model/Terminals/RightDrawer/Macros/MacroController"; +import { RightDrawerConfig } from "src/model/Terminals/RightDrawer/RightDrawer"; +import { LastUsedSerialPort } from "../AppDataManager"; +import { TxSettingsConfig } from "src/model/Settings/TxSettings/TxSettings"; +import { RxSettingsConfig } from "src/model/Settings/RxSettings/RxSettings"; +import { DisplaySettingsConfig } from "src/model/Settings/DisplaySettings/DisplaySettings"; +import { GeneralSettingsConfig } from "src/model/Settings/GeneralSettings/GeneralSettings"; +import { PortConfigurationConfigV2, PortConfigurationConfigV3 } from "src/model/Settings/PortConfigurationSettings/PortConfigurationSettings"; + +/** + * Everything in this class must be POD (plain old data) and serializable to JSON. + */ +export class RootConfigV2 { + version = 2; + + terminal = { + macroController: new MacroControllerConfig(), + rightDrawer: new RightDrawerConfig(), + }; + + lastUsedSerialPort: LastUsedSerialPort = new LastUsedSerialPort(); + + settings = { + portSettings: new PortConfigurationConfigV2(), + txSettings: new TxSettingsConfig(), + rxSettings: new RxSettingsConfig(), + displaySettings: new DisplaySettingsConfig(), + generalSettings: new GeneralSettingsConfig(), + }; +} + +/** + * Everything in this class must be POD (plain old data) and serializable to JSON. + */ +export class RootConfigV3 { + version = 3; + + terminal = { + macroController: new MacroControllerConfig(), + rightDrawer: new RightDrawerConfig(), + }; + + lastUsedSerialPort: LastUsedSerialPort = new LastUsedSerialPort(); + + settings = { + portSettings: new PortConfigurationConfigV3(), + txSettings: new TxSettingsConfig(), + rxSettings: new RxSettingsConfig(), + displaySettings: new DisplaySettingsConfig(), + generalSettings: new GeneralSettingsConfig(), + }; +} diff --git a/src/model/Settings/DisplaySettings/DisplaySettings.ts b/src/model/Settings/DisplaySettings/DisplaySettings.ts index 888664cb..35790ab1 100644 --- a/src/model/Settings/DisplaySettings/DisplaySettings.ts +++ b/src/model/Settings/DisplaySettings/DisplaySettings.ts @@ -2,7 +2,7 @@ import { makeAutoObservable } from "mobx"; import { z } from "zod"; import { ApplyableNumberField } from "src/view/Components/ApplyableTextField"; -import { ProfileManager } from "src/model/ProfileManager/ProfileManager"; +import { AppDataManager } from "src/model/AppDataManager/AppDataManager"; /** Enumerates the different possible ways the TX and RX data * can be displayed. One of these may be active at any one time. @@ -31,7 +31,7 @@ export class DisplaySettingsConfig { export default class DisplaySettings { - profileManager: ProfileManager; + profileManager: AppDataManager; // 14px is a good default size for the terminal text charSizePx = new ApplyableNumberField("14", z.coerce.number().int().min(1)); @@ -47,7 +47,7 @@ export default class DisplaySettings { dataViewConfiguration = DataViewConfiguration.SINGLE_TERMINAL; - constructor(profileManager: ProfileManager) { + constructor(profileManager: AppDataManager) { this.profileManager = profileManager; this.charSizePx.setOnApplyChanged(() => { this._saveConfig(); diff --git a/src/model/Settings/GeneralSettings/GeneralSettings.ts b/src/model/Settings/GeneralSettings/GeneralSettings.ts index b0e54347..12a78658 100644 --- a/src/model/Settings/GeneralSettings/GeneralSettings.ts +++ b/src/model/Settings/GeneralSettings/GeneralSettings.ts @@ -1,5 +1,5 @@ import { makeAutoObservable } from "mobx"; -import { ProfileManager } from "src/model/ProfileManager/ProfileManager"; +import { AppDataManager } from "src/model/AppDataManager/AppDataManager"; export class GeneralSettingsConfig { /** @@ -14,12 +14,12 @@ export class GeneralSettingsConfig { } export default class RxSettings { - profileManager: ProfileManager; + profileManager: AppDataManager; whenPastingOnWindowsReplaceCRLFWithLF = true; whenCopyingToClipboardDoNotAddLFIfRowWasCreatedDueToWrapping = true; - constructor(profileManager: ProfileManager) { + constructor(profileManager: AppDataManager) { this.profileManager = profileManager; this._loadConfig(); this.profileManager.registerOnProfileLoad(() => { diff --git a/src/model/Settings/PortConfigurationSettings/PortConfigurationSettings.ts b/src/model/Settings/PortConfigurationSettings/PortConfigurationSettings.ts index defcc7c0..ab7d4c31 100644 --- a/src/model/Settings/PortConfigurationSettings/PortConfigurationSettings.ts +++ b/src/model/Settings/PortConfigurationSettings/PortConfigurationSettings.ts @@ -1,7 +1,7 @@ import { makeAutoObservable } from 'mobx'; import { z } from 'zod'; -import { ProfileManager } from 'src/model/ProfileManager/ProfileManager'; +import { AppDataManager } from 'src/model/AppDataManager/AppDataManager'; import { App } from 'src/model/App'; export enum PortState { @@ -90,7 +90,7 @@ export class PortConfigurationConfigV3 { export default class PortConfiguration { app: App - profileManager: ProfileManager; + profileManager: AppDataManager; baudRateInputValue: string; diff --git a/src/model/Settings/ProfileSettings/ProfileSettings.ts b/src/model/Settings/ProfileSettings/ProfileSettings.ts index 52afccca..98ba6b69 100644 --- a/src/model/Settings/ProfileSettings/ProfileSettings.ts +++ b/src/model/Settings/ProfileSettings/ProfileSettings.ts @@ -2,10 +2,10 @@ import { GridRowSelectionModel } from '@mui/x-data-grid'; import { makeAutoObservable } from 'mobx'; import { z } from 'zod'; -import { ProfileManager } from 'src/model/ProfileManager/ProfileManager'; +import { AppDataManager } from 'src/model/AppDataManager/AppDataManager'; export default class ProfilesSettings { - profileManager: ProfileManager; + profileManager: AppDataManager; profileNameText = ''; profileNameErrorMsg = ''; @@ -15,7 +15,7 @@ export default class ProfilesSettings { */ selectedProfiles: GridRowSelectionModel = []; - constructor(profileManager: ProfileManager) { + constructor(profileManager: AppDataManager) { this.profileManager = profileManager; makeAutoObservable(this); } diff --git a/src/model/Settings/RxSettings/RxSettings.ts b/src/model/Settings/RxSettings/RxSettings.ts index d5edfde6..31f02c30 100644 --- a/src/model/Settings/RxSettings/RxSettings.ts +++ b/src/model/Settings/RxSettings/RxSettings.ts @@ -2,7 +2,7 @@ import { makeAutoObservable } from "mobx"; import { z } from "zod"; import { ApplyableNumberField, ApplyableTextField } from "src/view/Components/ApplyableTextField"; -import { ProfileManager } from "src/model/ProfileManager/ProfileManager"; +import { AppDataManager } from "src/model/AppDataManager/AppDataManager"; export enum DataType { ASCII, @@ -138,7 +138,7 @@ const CONFIG_KEY = ['settings', 'rx-settings']; export default class RxSettings { - profileManager: ProfileManager; + profileManager: AppDataManager; /** * How to interpret the received data from the serial port. @@ -190,7 +190,7 @@ export default class RxSettings { showWarningOnRxBreakSignal = true; - constructor(profileManager: ProfileManager) { + constructor(profileManager: AppDataManager) { this.profileManager = profileManager; this._loadConfig(); this.profileManager.registerOnProfileLoad(() => { diff --git a/src/model/Settings/Settings.tsx b/src/model/Settings/Settings.tsx index c10c0522..7ec1cfe3 100644 --- a/src/model/Settings/Settings.tsx +++ b/src/model/Settings/Settings.tsx @@ -10,7 +10,7 @@ import PortConfiguration from './PortConfigurationSettings/PortConfigurationSett import GeneralSettings from './GeneralSettings/GeneralSettings'; import FakePortsController from 'src/model/FakePorts/FakePortsController'; import ProfilesSettings from './ProfileSettings/ProfileSettings'; -import { ProfileManager } from '../ProfileManager/ProfileManager'; +import { AppDataManager } from '../AppDataManager/AppDataManager'; import { App } from '../App'; diff --git a/src/model/Settings/TxSettings/TxSettings.ts b/src/model/Settings/TxSettings/TxSettings.ts index d46cadc4..dc439e2d 100644 --- a/src/model/Settings/TxSettings/TxSettings.ts +++ b/src/model/Settings/TxSettings/TxSettings.ts @@ -1,5 +1,5 @@ import { makeAutoObservable } from 'mobx'; -import { ProfileManager } from 'src/model/ProfileManager/ProfileManager'; +import { AppDataManager } from 'src/model/AppDataManager/AppDataManager'; export enum EnterKeyPressBehavior { SEND_LF = 'Send LF', @@ -56,7 +56,7 @@ export class TxSettingsConfig { export default class DataProcessingSettings { - profileManager: ProfileManager; + profileManager: AppDataManager; enterKeyPressBehavior = EnterKeyPressBehavior.SEND_LF; @@ -84,7 +84,7 @@ export default class DataProcessingSettings { */ sendEscCharWhenAltKeyPressed = true; - constructor(profileManager: ProfileManager) { + constructor(profileManager: AppDataManager) { this.profileManager = profileManager; this._loadConfig(); this.profileManager.registerOnProfileLoad(() => { diff --git a/src/model/Terminals/RightDrawer/RightDrawer.ts b/src/model/Terminals/RightDrawer/RightDrawer.ts index a358a4ea..e1ecac87 100644 --- a/src/model/Terminals/RightDrawer/RightDrawer.ts +++ b/src/model/Terminals/RightDrawer/RightDrawer.ts @@ -1,7 +1,7 @@ import { makeAutoObservable } from 'mobx'; import { MacroController } from './Macros/MacroController'; import { App } from 'src/model/App'; -import { ProfileManager } from 'src/model/ProfileManager/ProfileManager'; +import { AppDataManager } from 'src/model/AppDataManager/AppDataManager'; export class RightDrawerConfig { /** @@ -25,7 +25,7 @@ export class RightDrawerConfig { export default class RightDrawer { - profileManager: ProfileManager; + profileManager: AppDataManager; macroController: MacroController; drawerWidth_px = 400; diff --git a/src/model/Terminals/SingleTerminal/SingleTerminal.spec.ts b/src/model/Terminals/SingleTerminal/SingleTerminal.spec.ts index ec0fef54..18e5f604 100644 --- a/src/model/Terminals/SingleTerminal/SingleTerminal.spec.ts +++ b/src/model/Terminals/SingleTerminal/SingleTerminal.spec.ts @@ -7,18 +7,18 @@ import RxSettings, { NonVisibleCharDisplayBehaviors, } from 'src/model/Settings/RxSettings/RxSettings'; import DisplaySettings from 'src/model/Settings/DisplaySettings/DisplaySettings'; -import { ProfileManager } from 'src/model/ProfileManager/ProfileManager'; +import { AppDataManager } from 'src/model/AppDataManager/AppDataManager'; import { App } from 'src/model/App'; describe('single terminal tests', () => { let app: App; - let profileManager: ProfileManager; + let profileManager: AppDataManager; let dataProcessingSettings: RxSettings; let displaySettings: DisplaySettings; let singleTerminal: SingleTerminal; beforeEach(async () => { app = new App(); - profileManager = new ProfileManager(app); + profileManager = new AppDataManager(app); dataProcessingSettings = new RxSettings(profileManager); displaySettings = new DisplaySettings(profileManager); singleTerminal = new SingleTerminal( diff --git a/src/view/Settings/ProfileSettings/ProfileSettingsView.tsx b/src/view/Settings/ProfileSettings/ProfileSettingsView.tsx index 28bc21fd..4c1d6c60 100644 --- a/src/view/Settings/ProfileSettings/ProfileSettingsView.tsx +++ b/src/view/Settings/ProfileSettings/ProfileSettingsView.tsx @@ -7,12 +7,12 @@ import AddIcon from '@mui/icons-material/Add'; import DeleteIcon from '@mui/icons-material/Delete'; import ProfilesSettings from 'src/model/Settings/ProfileSettings/ProfileSettings'; -import { ProfileManager } from 'src/model/ProfileManager/ProfileManager'; +import { AppDataManager } from 'src/model/AppDataManager/AppDataManager'; import PortConfigurationSettings from 'src/model/Settings/PortConfigurationSettings/PortConfigurationSettings'; import RxSettings from 'src/model/Settings/RxSettings/RxSettings'; interface Props { - profileManager: ProfileManager; + profileManager: AppDataManager; profilesSettings: ProfilesSettings; }