Skip to content

Commit

Permalink
Refactor some data classes into their own files. Rename profile manager.
Browse files Browse the repository at this point in the history
  • Loading branch information
gbmhunter committed Jun 18, 2024
1 parent c2f54bd commit 62131c7
Show file tree
Hide file tree
Showing 16 changed files with 156 additions and 146 deletions.
6 changes: 3 additions & 3 deletions src/model/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -115,7 +115,7 @@ export class App {

fakePortController: FakePortsController = new FakePortsController(this);

profileManager: ProfileManager;
profileManager: AppDataManager;

selectionController: SelectionController = new SelectionController();

Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect, test, describe, beforeEach } from "vitest";

import { ProfileManager } from "./ProfileManager";
import { AppDataManager } from "./AppDataManager";
import { App } from "../App";

beforeEach(() => {
Expand All @@ -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");
Expand Down
Original file line number Diff line number Diff line change
@@ -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<SerialPortInfo> = {};
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();
Expand Down
43 changes: 43 additions & 0 deletions src/model/AppDataManager/DataClasses/AppData.ts
Original file line number Diff line number Diff line change
@@ -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);
}
}
27 changes: 27 additions & 0 deletions src/model/AppDataManager/DataClasses/Profile.ts
Original file line number Diff line number Diff line change
@@ -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);
}
}
52 changes: 52 additions & 0 deletions src/model/AppDataManager/DataClasses/RootConfig.ts
Original file line number Diff line number Diff line change
@@ -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(),
};
}
6 changes: 3 additions & 3 deletions src/model/Settings/DisplaySettings/DisplaySettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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));
Expand All @@ -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();
Expand Down
6 changes: 3 additions & 3 deletions src/model/Settings/GeneralSettings/GeneralSettings.ts
Original file line number Diff line number Diff line change
@@ -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 {
/**
Expand All @@ -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(() => {
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -90,7 +90,7 @@ export class PortConfigurationConfigV3 {
export default class PortConfiguration {

app: App
profileManager: ProfileManager;
profileManager: AppDataManager;

baudRateInputValue: string;

Expand Down
Loading

0 comments on commit 62131c7

Please sign in to comment.