Removed unnecessary services (#125)

They have been replaced with Store.
This commit is contained in:
Nejc Zdovc
2016-10-21 16:36:41 +02:00
committed by József Farkas
parent b16e83fc30
commit a653333c9b
8 changed files with 78 additions and 102 deletions

View File

@@ -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
],

View File

@@ -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<Select2OptionData>;
private selectedMacroIndex: number;
private subscription: Subscription;
constructor(private uhkConfigurationService: UhkConfigurationService) {
this.macros = [];
constructor(private store: Store<AppState>) {
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();
}
}

View File

@@ -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;
}
}

View File

@@ -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<AppState>) {
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;
}
}
}

View File

@@ -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 (<any[]>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;
}
}

View File

@@ -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;
}
}

View File

@@ -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 = (<any[]>require('json!../../config-serializer/preset-keymaps.json'))
.map(keymap => new Keymap().fromJsObject(keymap));
}
}

View File

@@ -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;