From a653333c9bc681d801430854e153648ab6f59b25 Mon Sep 17 00:00:00 2001 From: Nejc Zdovc Date: Fri, 21 Oct 2016 16:36:41 +0200 Subject: [PATCH] Removed unnecessary services (#125) They have been replaced with Store. --- src/app.module.ts | 6 +-- .../popover/tab/macro/macro-tab.component.ts | 22 +++++++--- .../svg/keyboard/svg-keyboard.component.ts | 26 ++++++++--- .../svg-keyboard-key.component.ts | 31 +++++++++---- src/services/data-provider.service.ts | 43 ------------------- src/services/uhk-configuration.service.ts | 20 --------- src/store/storage/index.ts | 26 +++++++---- src/store/storage/local.ts | 6 +-- 8 files changed, 78 insertions(+), 102 deletions(-) delete mode 100644 src/services/data-provider.service.ts delete mode 100644 src/services/uhk-configuration.service.ts diff --git a/src/app.module.ts b/src/app.module.ts index 4781b4bd..7fabba0b 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -58,16 +58,14 @@ import { SvgModuleComponent } from './components/svg/module'; import { SvgKeyboardWrapComponent } from './components/svg/wrap'; import { MainAppComponent, appRoutingProviders, routing } from './main-app'; -import { DataProviderService } from './services/data-provider.service'; import { MapperService } from './services/mapper.service'; -import { UhkConfigurationService } from './services/uhk-configuration.service'; import { KeymapEffects, MacroEffects } from './store/effects'; import { keymapReducer, macroReducer, presetReducer } from './store/reducers'; import { DataStorage } from './store/storage'; // Create DataStorage dependency injection -const storageProvider = ReflectiveInjector.resolve([DataStorage, DataProviderService]); +const storageProvider = ReflectiveInjector.resolve([DataStorage]); const storageInjector = ReflectiveInjector.fromResolvedProviders(storageProvider); const storageService: DataStorage = storageInjector.get(DataStorage); @@ -143,8 +141,6 @@ const storeConfig = { EffectsModule.runAfterBootstrap(MacroEffects) ], providers: [ - DataProviderService, - UhkConfigurationService, MapperService, appRoutingProviders ], diff --git a/src/components/popover/tab/macro/macro-tab.component.ts b/src/components/popover/tab/macro/macro-tab.component.ts index 0905cdd9..86a4f21b 100644 --- a/src/components/popover/tab/macro/macro-tab.component.ts +++ b/src/components/popover/tab/macro/macro-tab.component.ts @@ -1,4 +1,8 @@ -import { Component, Input, OnInit } from '@angular/core'; +import { Component, Input, OnDestroy, OnInit } from '@angular/core'; + +import { Store } from '@ngrx/store'; + +import { Subscription } from 'rxjs/Subscription'; import { Select2OptionData } from 'ng2-select2/ng2-select2'; @@ -7,29 +11,30 @@ import { Macro } from '../../../../config-serializer/config-items/Macro'; import { Tab } from '../tab'; -import { UhkConfigurationService } from '../../../../services/uhk-configuration.service'; +import { AppState } from '../../../../store/index'; +import { getMacroEntities } from '../../../../store/reducers/macro'; @Component({ selector: 'macro-tab', template: require('./macro-tab.component.html'), styles: [require('./macro-tab.component.scss')] }) -export class MacroTabComponent implements OnInit, Tab { +export class MacroTabComponent implements OnInit, OnDestroy, Tab { @Input() defaultKeyAction: KeyAction; private macros: Macro[]; private macroOptions: Array; private selectedMacroIndex: number; + private subscription: Subscription; - constructor(private uhkConfigurationService: UhkConfigurationService) { - this.macros = []; + constructor(private store: Store) { + this.subscription = store.let(getMacroEntities()) + .subscribe((macros: Macro[]) => this.macros = macros); this.macroOptions = []; this.selectedMacroIndex = -1; } ngOnInit() { - this.macros = this.uhkConfigurationService.getUhkConfiguration().macros; - this.macroOptions.push({ id: '-1', text: 'Select macro' @@ -72,4 +77,7 @@ export class MacroTabComponent implements OnInit, Tab { return keymapAction; } + ngOnDestroy() { + this.subscription.unsubscribe(); + } } diff --git a/src/components/svg/keyboard/svg-keyboard.component.ts b/src/components/svg/keyboard/svg-keyboard.component.ts index e7289658..b0e3fe33 100644 --- a/src/components/svg/keyboard/svg-keyboard.component.ts +++ b/src/components/svg/keyboard/svg-keyboard.component.ts @@ -3,8 +3,6 @@ import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core'; import { Module } from '../../../config-serializer/config-items/Module'; import { SvgModule } from '../module'; -import { DataProviderService } from '../../../services/data-provider.service'; - @Component({ selector: 'svg-keyboard', template: require('./svg-keyboard.component.html'), @@ -18,13 +16,13 @@ export class SvgKeyboardComponent implements OnInit { private modules: SvgModule[]; private svgAttributes: { viewBox: string, transform: string, fill: string }; - constructor(private dps: DataProviderService) { + constructor() { this.modules = []; - this.svgAttributes = this.dps.getKeyboardSvgAttributes(); + this.svgAttributes = this.getKeyboardSvgAttributes(); } ngOnInit() { - this.modules = this.dps.getSvgModules(); + this.modules = this.getSvgModules(); } onKeyClick(moduleId: number, keyId: number): void { @@ -43,4 +41,22 @@ export class SvgKeyboardComponent implements OnInit { }); } + private getKeyboardSvgAttributes(): { viewBox: string, transform: string, fill: string } { + let svg: any = this.getBaseLayer(); + return { + viewBox: svg.$.viewBox, + transform: svg.g[0].$.transform, + fill: svg.g[0].$.fill + }; + } + + private getSvgModules(): SvgModule[] { + let modules = this.getBaseLayer().g[0].g.map((obj: any) => new SvgModule(obj)); + return [modules[1], modules[0]]; // TODO: remove if the svg will be correct + } + + private getBaseLayer(): any { + return require('xml!../../../../images/base-layer.svg').svg; + } + } diff --git a/src/components/svg/keys/svg-keyboard-key/svg-keyboard-key.component.ts b/src/components/svg/keys/svg-keyboard-key/svg-keyboard-key.component.ts index b9f6079a..2bbf024e 100644 --- a/src/components/svg/keys/svg-keyboard-key/svg-keyboard-key.component.ts +++ b/src/components/svg/keys/svg-keyboard-key/svg-keyboard-key.component.ts @@ -1,4 +1,8 @@ -import { Component, Input, OnChanges, OnInit, SimpleChange } from '@angular/core'; +import {Component, Input, OnChanges, OnDestroy, OnInit, SimpleChange } from '@angular/core'; + +import { Store } from '@ngrx/store'; + +import { Subscription } from 'rxjs/Subscription'; import { KeyAction, @@ -10,10 +14,12 @@ import { SwitchLayerAction } from '../../../../config-serializer/config-items/key-action'; import { KeyModifiers } from '../../../../config-serializer/config-items/KeyModifiers'; -import { UhkConfiguration } from '../../../../config-serializer/config-items/UhkConfiguration'; +import { Macro } from '../../../../config-serializer/config-items/Macro'; import { MapperService } from '../../../../services/mapper.service'; -import { UhkConfigurationService } from '../../../../services/uhk-configuration.service'; + +import { AppState } from '../../../../store/index'; +import { getMacroEntities } from '../../../../store/reducers/macro'; enum LabelTypes { KeystrokeKey, @@ -31,7 +37,7 @@ enum LabelTypes { template: require('./svg-keyboard-key.component.html'), styles: [require('./svg-keyboard-key.component.scss')] }) -export class SvgKeyboardKeyComponent implements OnInit, OnChanges { +export class SvgKeyboardKeyComponent implements OnInit, OnChanges, OnDestroy { @Input() id: string; @Input() rx: string; @Input() ry: string; @@ -46,8 +52,13 @@ export class SvgKeyboardKeyComponent implements OnInit, OnChanges { private labelSource: any; private labelType: LabelTypes; + private macros: Macro[]; + private subscription: Subscription; - constructor(private mapperService: MapperService, private uhkConfigurationService: UhkConfigurationService) { } + constructor(private mapperService: MapperService, private store: Store) { + this.subscription = store.let(getMacroEntities()) + .subscribe((macros: Macro[]) => this.macros = macros); + } ngOnInit() { this.setLabels(); @@ -61,6 +72,10 @@ export class SvgKeyboardKeyComponent implements OnInit, OnChanges { /* tslint:enable:no-string-literal */ } + ngOnDestroy() { + this.subscription.unsubscribe(); + } + private setLabels(): void { if (!this.keyAction) { this.labelSource = undefined; @@ -150,11 +165,11 @@ export class SvgKeyboardKeyComponent implements OnInit, OnChanges { this.labelSource = keyAction.keymapAbbreviation; } else if (this.keyAction instanceof PlayMacroAction) { let keyAction: PlayMacroAction = this.keyAction as PlayMacroAction; + let macro: Macro = this.macros.find((macro: Macro) => macro.id === keyAction.macroId); this.labelType = LabelTypes.IconText; - let uhkConfiguration: UhkConfiguration = this.uhkConfigurationService.getUhkConfiguration(); this.labelSource = { icon: this.mapperService.getIcon('macro'), - text: uhkConfiguration.getMacro(keyAction.macroId).name + text: macro.name }; } else if (this.keyAction instanceof MouseAction) { this.labelType = LabelTypes.MouseKey; @@ -162,7 +177,5 @@ export class SvgKeyboardKeyComponent implements OnInit, OnChanges { } else { this.labelSource = undefined; } - } - } diff --git a/src/services/data-provider.service.ts b/src/services/data-provider.service.ts deleted file mode 100644 index 603eb8a1..00000000 --- a/src/services/data-provider.service.ts +++ /dev/null @@ -1,43 +0,0 @@ -import { Injectable } from '@angular/core'; - -import { Keymap } from '../config-serializer/config-items/Keymap'; -import { UhkConfiguration } from '../config-serializer/config-items/UhkConfiguration'; - -import { SvgModule } from '../components/svg/module/svg-module.model'; - -@Injectable() -export class DataProviderService { - - private uhkConfiguration: UhkConfiguration; - - constructor() { - this.uhkConfiguration = new UhkConfiguration().fromJsObject(require('json!../config-serializer/uhk-config.json')); - } - - getUHKConfig(): UhkConfiguration { - return this.uhkConfiguration; - } - - getDefaultKeymaps(): Keymap[] { - return (require('json!../config-serializer/preset-keymaps.json')).map(keymap => new Keymap().fromJsObject(keymap)); - } - - getKeyboardSvgAttributes(): { viewBox: string, transform: string, fill: string } { - let svg: any = this.getBaseLayer(); - return { - viewBox: svg.$.viewBox, - transform: svg.g[0].$.transform, - fill: svg.g[0].$.fill - }; - } - - getSvgModules(): SvgModule[] { - let modules = this.getBaseLayer().g[0].g.map((obj: any) => new SvgModule(obj)); - return [modules[1], modules[0]]; // TODO: remove if the svg will be correct - } - - private getBaseLayer(): any { - return require('xml!../../images/base-layer.svg').svg; - } - -} diff --git a/src/services/uhk-configuration.service.ts b/src/services/uhk-configuration.service.ts deleted file mode 100644 index ae9abd07..00000000 --- a/src/services/uhk-configuration.service.ts +++ /dev/null @@ -1,20 +0,0 @@ -import { Injectable } from '@angular/core'; - -import { UhkConfiguration } from '../config-serializer/config-items/UhkConfiguration'; - -import { DataProviderService } from './data-provider.service'; - -@Injectable() -export class UhkConfigurationService { - - private configuration: UhkConfiguration; - - constructor(private dataProviderService: DataProviderService) { - this.configuration = new UhkConfiguration().fromJsObject(this.dataProviderService.getUHKConfig()); - } - - getUhkConfiguration(): UhkConfiguration { - return this.configuration; - } - -} diff --git a/src/store/storage/index.ts b/src/store/storage/index.ts index 470441ae..9bee37aa 100644 --- a/src/store/storage/index.ts +++ b/src/store/storage/index.ts @@ -4,25 +4,27 @@ import { Action } from '@ngrx/store'; import { Keymap } from '../../config-serializer/config-items/Keymap'; import { Macro } from '../../config-serializer/config-items/Macro'; +import { UhkConfiguration } from '../../config-serializer/config-items/UhkConfiguration'; + import { KeymapActions, MacroActions } from '../actions'; import { AppState } from '../index'; import { Electron } from './electron'; import { Local } from './local'; -import { UhkConfiguration } from '../../config-serializer/config-items/UhkConfiguration'; -import { DataProviderService } from '../../services/data-provider.service'; - @Injectable() export class DataStorage { private _environment: Local | Electron; + private uhkConfiguration: UhkConfiguration; + private uhkPresets: Keymap[]; - constructor(private dataProvider: DataProviderService) { + constructor() { + this.initUHKJson(); this.detectEnvironment(); } initialState(): AppState { - let config: UhkConfiguration = this._environment.getConfig() || this.dataProvider.getUHKConfig(); + let config: UhkConfiguration = this._environment.getConfig() || this.uhkConfiguration; return { keymaps: { entities: config.keymaps @@ -30,7 +32,7 @@ export class DataStorage { macros: { entities: config.macros }, - presetKeymaps: this.dataProvider.getDefaultKeymaps() + presetKeymaps: this.uhkPresets }; } @@ -42,7 +44,7 @@ export class DataStorage { } // Local storage else { - this._environment = new Local(this.dataProvider); + this._environment = new Local(); } } @@ -60,7 +62,7 @@ export class DataStorage { (state.entities && state.entities.length && state.entities[0] instanceof Keymap) ) ) { - config = this._environment.getConfig(); + config = this._environment.getConfig() || this.uhkConfiguration; config.keymaps = Object.values(nextState.entities); this._environment.saveConfig(config); } else if ( @@ -70,11 +72,17 @@ export class DataStorage { (state.entities && state.entities.length && state.entities[0] instanceof Macro) ) ) { - config = this._environment.getConfig(); + config = this._environment.getConfig() || this.uhkConfiguration; config.macros = Object.values(nextState.entities); this._environment.saveConfig(config); } return nextState; }; } + + initUHKJson() { + this.uhkConfiguration = new UhkConfiguration().fromJsObject(require('json!../../config-serializer/uhk-config.json')); + this.uhkPresets = (require('json!../../config-serializer/preset-keymaps.json')) + .map(keymap => new Keymap().fromJsObject(keymap)); + } } diff --git a/src/store/storage/local.ts b/src/store/storage/local.ts index bf967b4e..c5d73be8 100644 --- a/src/store/storage/local.ts +++ b/src/store/storage/local.ts @@ -1,17 +1,15 @@ import { UhkConfiguration } from '../../config-serializer/config-items/UhkConfiguration'; -import { DataProviderService } from '../../services/data-provider.service'; export class Local { - constructor(private dataProvider: DataProviderService) { } + constructor() { } getConfig(): UhkConfiguration { let configJson = localStorage.getItem('config'); let config: UhkConfiguration; + if (configJson) { config = new UhkConfiguration().fromJsObject(JSON.parse(configJson)); - } else { - config = this.dataProvider.getUHKConfig(); } return config;