Skip to content

Commit

Permalink
Persist user defines mode and manual user defines txt in local storage
Browse files Browse the repository at this point in the history
  • Loading branch information
jurgelenas committed Feb 15, 2021
1 parent ed19248 commit 6e81cf1
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 89 deletions.
4 changes: 2 additions & 2 deletions src/ui/components/DeviceOptionsForm/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,8 @@ const DeviceOptionsForm: FunctionComponent<DeviceOptionsFormProps> = (
className={styles.textarea}
multiline
label="user_defines.txt"
onChange={onUserDefinesTxt}
value={deviceOptions.userDefinesTxt}
onBlur={onUserDefinesTxt}
defaultValue={deviceOptions.userDefinesTxt}
fullWidth
rows={10}
/>
Expand Down
36 changes: 0 additions & 36 deletions src/ui/storage/commands/mergeUserDefinesFromStorage.ts

This file was deleted.

55 changes: 55 additions & 0 deletions src/ui/storage/commands/mergeWithDeviceOptionsFromStorage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import {
DeviceTarget,
UserDefine,
UserDefineKey,
} from '../../gql/generated/types';
import { DeviceOptions, IApplicationStorage } from '../index';

const mergeWithDeviceOptionsFromStorage = async (
storage: IApplicationStorage,
deviceTarget: DeviceTarget | null,
deviceOptions: DeviceOptions
): Promise<DeviceOptions> => {
if (deviceTarget === null) {
return deviceOptions;
}
const savedBindingPhrase = await storage.GetBindingPhrase();
const savedTargetOptions = await storage.GetDeviceOptions(deviceTarget);
const userDefineOptions = deviceOptions.userDefineOptions.map(
(deviceOption) => {
if (savedTargetOptions === null) {
return deviceOption;
}
const savedOption = savedTargetOptions?.userDefineOptions.find(
({ key }) => key === deviceOption.key
);
if (savedOption === undefined) {
return deviceOption;
}

const { enabled } = savedOption;
let { value } = savedOption;
if (
deviceOption.key === UserDefineKey.BINDING_PHRASE &&
savedBindingPhrase.length > 0
) {
value = savedBindingPhrase;
}
const opt: UserDefine = {
...deviceOption,
enabled,
value,
};
return opt;
}
);
return {
userDefinesMode:
savedTargetOptions?.userDefinesMode ?? deviceOptions.userDefinesMode,
userDefinesTxt:
savedTargetOptions?.userDefinesTxt ?? deviceOptions?.userDefinesTxt,
userDefineOptions,
};
};

export default mergeWithDeviceOptionsFromStorage;
26 changes: 26 additions & 0 deletions src/ui/storage/commands/persistDeviceOptions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { DeviceTarget, UserDefineKey } from '../../gql/generated/types';
import { DeviceOptions, IApplicationStorage } from '../index';

const persistDeviceOptions = async (
storage: IApplicationStorage,
deviceTarget: DeviceTarget,
deviceOptions: DeviceOptions
): Promise<void> => {
if (deviceTarget !== null) {
await storage.SaveDeviceOptions(deviceTarget, deviceOptions);

const bindingPhrase = deviceOptions.userDefineOptions.find(
({ key }) => key === UserDefineKey.BINDING_PHRASE
);
if (
bindingPhrase !== undefined &&
bindingPhrase.enabled &&
bindingPhrase.value &&
bindingPhrase.value?.length > 0
) {
await storage.SetBindingPhrase(bindingPhrase.value);
}
}
};

export default persistDeviceOptions;
29 changes: 0 additions & 29 deletions src/ui/storage/commands/persistDeviceUserDefines.ts

This file was deleted.

28 changes: 20 additions & 8 deletions src/ui/storage/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
import { DeviceTarget, UserDefine } from '../gql/generated/types';
import {
DeviceTarget,
UserDefine,
UserDefinesMode,
} from '../gql/generated/types';

export interface DeviceOptions {
userDefinesMode: UserDefinesMode;
userDefineOptions: UserDefine[];
userDefinesTxt: string;
}

export interface IApplicationStorage {
SaveDeviceOptions(
deviceTarget: DeviceTarget,
userDefines: UserDefine[]
deviceOptions: DeviceOptions
): Promise<void>;

GetDeviceOptions(deviceTarget: DeviceTarget): Promise<UserDefine[]>;
GetDeviceOptions(deviceTarget: DeviceTarget): Promise<DeviceOptions | null>;

SetBindingPhrase(bindingPhrase: string): Promise<void>;

Expand All @@ -19,23 +29,25 @@ const BINDING_PHRASE_KEY = 'binding_phrase';
export default class ApplicationStorage implements IApplicationStorage {
async SaveDeviceOptions(
deviceTarget: DeviceTarget,
userDefines: UserDefine[]
deviceOptions: DeviceOptions
): Promise<void> {
const key = `${DEVICE_OPTIONS_BY_TARGET_KEYSPACE}.${deviceTarget}`;
localStorage.setItem(key, JSON.stringify(userDefines));
localStorage.setItem(key, JSON.stringify(deviceOptions));
}

async GetDeviceOptions(deviceTarget: DeviceTarget): Promise<UserDefine[]> {
async GetDeviceOptions(
deviceTarget: DeviceTarget
): Promise<DeviceOptions | null> {
const key = `${DEVICE_OPTIONS_BY_TARGET_KEYSPACE}.${deviceTarget}`;
const value = localStorage.getItem(key);
if (value === null) {
return [];
return null;
}
try {
return JSON.parse(value);
} catch (e) {
console.error(`failed to parse ${e}`);
return [];
return null;
}
}

Expand Down
26 changes: 12 additions & 14 deletions src/ui/views/ConfiguratorView/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ import BuildResponse from '../../components/BuildResponse';
import { IpcRequest, OpenFileLocationRequestBody } from '../../../ipc';
import UserDefinesValidator from './UserDefinesValidator';
import ApplicationStorage from '../../storage';
import persistDeviceUserDefines from '../../storage/commands/persistDeviceUserDefines';
import mergeUserDefinesFromStorage from '../../storage/commands/mergeUserDefinesFromStorage';
import persistDeviceOptions from '../../storage/commands/persistDeviceOptions';
import mergeWithDeviceOptionsFromStorage from '../../storage/commands/mergeWithDeviceOptionsFromStorage';

export const validateFirmwareVersionData = (
data: FirmwareVersionDataInput
Expand Down Expand Up @@ -216,15 +216,16 @@ const ConfiguratorView: FunctionComponent = () => {
) {
const handleUpdate = async () => {
const storage = new ApplicationStorage();
const userDefineOptions = await mergeUserDefinesFromStorage(
const userDefineOptions = await mergeWithDeviceOptionsFromStorage(
storage,
deviceTarget,
[...deviceOptionsResponse.targetDeviceOptions]
{
...deviceOptionsFormData,
userDefineOptions: [...deviceOptionsResponse.targetDeviceOptions],
}
);
setDeviceOptionsFormData({
...deviceOptionsFormData,
userDefineOptions,
});
console.log('merged values', userDefineOptions);
setDeviceOptionsFormData(userDefineOptions);
};
handleUpdate().catch((err) => {
console.error(`failed to update device options form data: ${err}`);
Expand All @@ -234,13 +235,10 @@ const ConfiguratorView: FunctionComponent = () => {

const onUserDefines = (data: DeviceOptionsFormData) => {
setDeviceOptionsFormData(data);
if (deviceTarget !== null && data.userDefineOptions.length > 0) {
if (deviceTarget !== null) {
const storage = new ApplicationStorage();
persistDeviceUserDefines(
storage,
deviceTarget,
data.userDefineOptions
).catch((err) => {
console.log('trying to persist', data);
persistDeviceOptions(storage, deviceTarget, data).catch((err) => {
console.error(`failed to persist user defines: ${err}`);
});
}
Expand Down

0 comments on commit 6e81cf1

Please sign in to comment.