From cb1f677f314838fd85844dc2fb2bc3093884e0d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Farkas=20J=C3=B3zsef?= Date: Sun, 25 Sep 2016 08:51:06 +0200 Subject: [PATCH] refactor: UhkConfiguration --- src/components/macro/macro.component.ts | 2 +- .../popover/tab/macro/macro-tab.component.ts | 2 +- .../config-items/UhkConfiguration.ts | 40 +++++++++---------- src/store/storage/local.ts | 8 ++-- 4 files changed, 26 insertions(+), 26 deletions(-) diff --git a/src/components/macro/macro.component.ts b/src/components/macro/macro.component.ts index 742bdf2f..72d77f75 100644 --- a/src/components/macro/macro.component.ts +++ b/src/components/macro/macro.component.ts @@ -48,7 +48,7 @@ export class MacroComponent implements OnInit, OnDestroy { getMacro(id: number): Macro { const config = this.uhkConfigurationService.getUhkConfiguration(); - const macro: Macro = config.macros.elements.find(item => item.id === id); + const macro: Macro = config.macros.find(item => item.id === id); if (macro) { // Clone macro for editing return new Macro().fromJsObject(macro.toJsObject()); diff --git a/src/components/popover/tab/macro/macro-tab.component.ts b/src/components/popover/tab/macro/macro-tab.component.ts index 91701b3a..0905cdd9 100644 --- a/src/components/popover/tab/macro/macro-tab.component.ts +++ b/src/components/popover/tab/macro/macro-tab.component.ts @@ -28,7 +28,7 @@ export class MacroTabComponent implements OnInit, Tab { } ngOnInit() { - this.macros = this.uhkConfigurationService.getUhkConfiguration().macros.elements; + this.macros = this.uhkConfigurationService.getUhkConfiguration().macros; this.macroOptions.push({ id: '-1', diff --git a/src/config-serializer/config-items/UhkConfiguration.ts b/src/config-serializer/config-items/UhkConfiguration.ts index 3ac498ee..2a6c25d1 100644 --- a/src/config-serializer/config-items/UhkConfiguration.ts +++ b/src/config-serializer/config-items/UhkConfiguration.ts @@ -2,10 +2,8 @@ import { assertUInt32, assertUInt8 } from '../assert'; import { Serializable } from '../Serializable'; import { UhkBuffer } from '../UhkBuffer'; import { Keymap } from './Keymap'; -import { Keymaps } from './Keymaps'; import { Macro } from './Macro'; -import { Macros } from './Macros'; -import { ModuleConfigurations } from './ModuleConfigurations'; +import { ModuleConfiguration } from './ModuleConfiguration'; export class UhkConfiguration extends Serializable { @@ -23,11 +21,11 @@ export class UhkConfiguration extends Serializable { @assertUInt8 brandId: number; - moduleConfigurations: ModuleConfigurations; + moduleConfigurations: ModuleConfiguration[]; - keymaps: Keymaps; + keymaps: Keymap[]; - macros: Macros; + macros: Macro[]; @assertUInt32 epilogue: number; @@ -38,9 +36,11 @@ export class UhkConfiguration extends Serializable { this.prologue = jsObject.prologue; this.hardwareId = jsObject.hardwareId; this.brandId = jsObject.brandId; - this.moduleConfigurations = new ModuleConfigurations().fromJsObject(jsObject.moduleConfigurations); - this.keymaps = new Keymaps().fromJsObject(jsObject.keymaps); - this.macros = new Macros().fromJsObject(jsObject.macros); + this.moduleConfigurations = jsObject.moduleConfigurations.map((moduleConfiguration: any) => { + return new ModuleConfiguration().fromJsObject(moduleConfiguration); + }); + this.keymaps = jsObject.keymaps.map((keymap: any) => new Keymap().fromJsObject(keymap)); + this.macros = jsObject.macros.map((macro: any) => new Macro().fromJsObject(macro)); this.epilogue = jsObject.epilogue; return this; } @@ -51,9 +51,9 @@ export class UhkConfiguration extends Serializable { this.prologue = buffer.readUInt32(); this.hardwareId = buffer.readUInt8(); this.brandId = buffer.readUInt8(); - this.moduleConfigurations = new ModuleConfigurations().fromBinary(buffer); - this.keymaps = new Keymaps().fromBinary(buffer); - this.macros = new Macros().fromBinary(buffer); + this.moduleConfigurations = buffer.readArray(ModuleConfiguration); + this.keymaps = buffer.readArray(Keymap); + this.macros = buffer.readArray(Macro); this.epilogue = buffer.readUInt32(); return this; } @@ -65,9 +65,9 @@ export class UhkConfiguration extends Serializable { prologue: this.prologue, hardwareId: this.hardwareId, brandId: this.brandId, - moduleConfigurations: this.moduleConfigurations.toJsObject(), - keymaps: this.keymaps.toJsObject(), - macros: this.macros.toJsObject(), + moduleConfigurations: this.moduleConfigurations.map(moduleConfiguration => moduleConfiguration.toJsObject()), + keymaps: this.keymaps.map(keymap => keymap.toJsObject()), + macros: this.macros.map(macro => macro.toJsObject()), epilogue: this.epilogue }; } @@ -78,9 +78,9 @@ export class UhkConfiguration extends Serializable { buffer.writeUInt32(this.prologue); buffer.writeUInt8(this.hardwareId); buffer.writeUInt8(this.brandId); - this.moduleConfigurations.toBinary(buffer); - this.keymaps.toBinary(buffer); - this.macros.toBinary(buffer); + buffer.writeArray(this.moduleConfigurations); + buffer.writeArray(this.keymaps); + buffer.writeArray(this.macros); buffer.writeUInt32(this.epilogue); } @@ -89,10 +89,10 @@ export class UhkConfiguration extends Serializable { } getKeymap(keymapAbbreviation: string): Keymap { - return this.keymaps.elements.find(keymap => keymapAbbreviation === keymap.abbreviation); + return this.keymaps.find(keymap => keymapAbbreviation === keymap.abbreviation); } getMacro(macroId: number): Macro { - return this.macros.elements.find(macro => macroId === macro.id); + return this.macros.find(macro => macroId === macro.id); } } diff --git a/src/store/storage/local.ts b/src/store/storage/local.ts index 595f5657..480f661c 100644 --- a/src/store/storage/local.ts +++ b/src/store/storage/local.ts @@ -35,8 +35,8 @@ export class Local { } return { - keymaps: config.keymaps.elements, - macros: config.macros.elements, + keymaps: config.keymaps, + macros: config.macros, presetKeymaps: presetAll.elements }; } @@ -51,13 +51,13 @@ export class Local { config = new UhkConfiguration().fromJsObject( JSON.parse(localStorage.getItem('config')) ); - config.keymaps.elements = Object.values(nextState); + config.keymaps = Object.values(nextState); localStorage.setItem('config', JSON.stringify(config.toJsObject())); } else if (action.type.startsWith(MacroActions.PREFIX) && state.length && state[0] instanceof Macro) { config = new UhkConfiguration().fromJsObject( JSON.parse(localStorage.getItem('config')) ); - config.macros.elements = Object.values(nextState); + config.macros = Object.values(nextState); localStorage.setItem('config', JSON.stringify(config.toJsObject())); }