Don't store preset in local storage

This commit is contained in:
Farkas József
2016-10-12 21:07:22 +02:00
parent 6381f90111
commit b16e83fc30
4 changed files with 72 additions and 83 deletions

View File

@@ -67,14 +67,14 @@ import { keymapReducer, macroReducer, presetReducer } from './store/reducers';
import { DataStorage } from './store/storage';
// Create DataStorage dependency injection
const storageProvider = ReflectiveInjector.resolve([DataStorage]);
const storageProvider = ReflectiveInjector.resolve([DataStorage, DataProviderService]);
const storageInjector = ReflectiveInjector.fromResolvedProviders(storageProvider);
const storageService: DataStorage = storageInjector.get(DataStorage);
// All reducers that are used in application
const storeConfig = {
keymaps: storageService.saveSate(keymapReducer),
macros: storageService.saveSate(macroReducer),
keymaps: storageService.saveState(keymapReducer),
macros: storageService.saveState(macroReducer),
presetKeymaps: presetReducer
};

View File

@@ -1,10 +1,13 @@
import { UhkConfiguration } from '../../config-serializer/config-items/UhkConfiguration';
export class Electron {
initialState() {
getConfig(): UhkConfiguration {
// TODO implement load logic
return;
}
/* tslint:disable:no-unused-variable */
saveSate(reducer: any): any {
saveConfig(config: UhkConfiguration): void {
// TODO implement save logic
}
}

View File

@@ -2,36 +2,79 @@ import { Injectable } from '@angular/core';
import { Action } from '@ngrx/store';
import { Keymap } from '../../config-serializer/config-items/Keymap';
import { Macro } from '../../config-serializer/config-items/Macro';
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;
constructor() {
constructor(private dataProvider: DataProviderService) {
this.detectEnvironment();
}
initialState() {
return this._environment.initialState();
initialState(): AppState {
let config: UhkConfiguration = this._environment.getConfig() || this.dataProvider.getUHKConfig();
return {
keymaps: {
entities: config.keymaps
},
macros: {
entities: config.macros
},
presetKeymaps: this.dataProvider.getDefaultKeymaps()
};
}
detectEnvironment() {
detectEnvironment(): void {
// Electron
// TODO check if we can remove <any> when electron will be implemented (maybe use process.versions['electron'])
if (typeof window !== 'undefined' && (<any>window).process && (<any>window).process.type === 'renderer') {
this._environment = new Electron();
this._environment = new Electron();
}
// Local storage
else {
this._environment = new Local();
this._environment = new Local(this.dataProvider);
}
}
saveSate(reducer: any): (state: AppState, action: Action) => AppState {
return this._environment.saveSate(reducer);
// TODO: Add type for state
saveState(reducer: any): (state: any, action: Action) => AppState {
return (state: any, action: Action) => {
let nextState = reducer(state, action);
let config: UhkConfiguration;
// Save elements to the UhkConfiguration
if (
action.type.startsWith(KeymapActions.PREFIX) &&
(
(nextState.entities && nextState.entities.length && nextState.entities[0] instanceof Keymap) ||
(state.entities && state.entities.length && state.entities[0] instanceof Keymap)
)
) {
config = this._environment.getConfig();
config.keymaps = Object.values(nextState.entities);
this._environment.saveConfig(config);
} else if (
action.type.startsWith(MacroActions.PREFIX) &&
(
(nextState.entities && nextState.entities.length && nextState.entities[0] instanceof Macro) ||
(state.entities && state.entities.length && state.entities[0] instanceof Macro)
)
) {
config = this._environment.getConfig();
config.macros = Object.values(nextState.entities);
this._environment.saveConfig(config);
}
return nextState;
};
}
}

View File

@@ -1,80 +1,23 @@
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 { DataProviderService } from '../../services/data-provider.service';
export class Local {
initialState(): AppState {
constructor(private dataProvider: DataProviderService) { }
getConfig(): UhkConfiguration {
let configJson = localStorage.getItem('config');
let config: UhkConfiguration;
let presetAll: Keymap[];
// Load data from json
if (!localStorage.getItem('config')) {
const jsonUser: JSON = require('json!../../config-serializer/uhk-config.json');
const presets: any[] = require('json!../../config-serializer/preset-keymaps.json');
config = new UhkConfiguration().fromJsObject(jsonUser);
presetAll = presets.map(keymap => new Keymap().fromJsObject(keymap));
// Save to local storage
localStorage.setItem('config', JSON.stringify(config.toJsObject()));
localStorage.setItem('preset', JSON.stringify(presetAll.map(preset => preset.toJsObject())));
}
// Load data from local storage
else {
config = new UhkConfiguration().fromJsObject(
JSON.parse(localStorage.getItem('config'))
);
presetAll = JSON.parse(localStorage.getItem('preset')).map((keymap: any) => new Keymap().fromJsObject(keymap));
if (configJson) {
config = new UhkConfiguration().fromJsObject(JSON.parse(configJson));
} else {
config = this.dataProvider.getUHKConfig();
}
return {
keymaps: {
entities: config.keymaps
},
macros: {
entities: config.macros
},
presetKeymaps: presetAll
};
return config;
}
saveSate(reducer: any): (state: any, action: Action) => AppState {
return function (state: any, action: Action) {
let nextState = reducer(state, action);
let config: UhkConfiguration;
// Save elements to the UhkConfiguration
if (
action.type.startsWith(KeymapActions.PREFIX) &&
(
(nextState.entities && nextState.entities.length && nextState.entities[0] instanceof Keymap) ||
(state.entities && state.entities.length && state.entities[0] instanceof Keymap)
)
) {
config = new UhkConfiguration().fromJsObject(
JSON.parse(localStorage.getItem('config'))
);
config.keymaps = Object.values(nextState.entities);
localStorage.setItem('config', JSON.stringify(config.toJsObject()));
} else if (
action.type.startsWith(MacroActions.PREFIX) &&
(
(nextState.entities && nextState.entities.length && nextState.entities[0] instanceof Macro) ||
(state.entities && state.entities.length && state.entities[0] instanceof Macro)
)
) {
config = new UhkConfiguration().fromJsObject(
JSON.parse(localStorage.getItem('config'))
);
config.macros = Object.values(nextState.entities);
localStorage.setItem('config', JSON.stringify(config.toJsObject()));
}
return nextState;
};
saveConfig(config: UhkConfiguration): void {
localStorage.setItem('config', JSON.stringify(config.toJsObject()));
}
}