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

View File

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

View File

@@ -2,36 +2,79 @@ import { Injectable } from '@angular/core';
import { Action } from '@ngrx/store'; 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 { AppState } from '../index';
import { Electron } from './electron'; import { Electron } from './electron';
import { Local } from './local'; import { Local } from './local';
import { UhkConfiguration } from '../../config-serializer/config-items/UhkConfiguration';
import { DataProviderService } from '../../services/data-provider.service';
@Injectable() @Injectable()
export class DataStorage { export class DataStorage {
private _environment: Local | Electron; private _environment: Local | Electron;
constructor() { constructor(private dataProvider: DataProviderService) {
this.detectEnvironment(); this.detectEnvironment();
} }
initialState() { initialState(): AppState {
return this._environment.initialState(); 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 // Electron
// TODO check if we can remove <any> when electron will be implemented (maybe use process.versions['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') { if (typeof window !== 'undefined' && (<any>window).process && (<any>window).process.type === 'renderer') {
this._environment = new Electron(); this._environment = new Electron();
} }
// Local storage // Local storage
else { else {
this._environment = new Local(); this._environment = new Local(this.dataProvider);
} }
} }
saveSate(reducer: any): (state: AppState, action: Action) => AppState { // TODO: Add type for state
return this._environment.saveSate(reducer); 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 { UhkConfiguration } from '../../config-serializer/config-items/UhkConfiguration';
import { DataProviderService } from '../../services/data-provider.service';
import { KeymapActions, MacroActions } from '../actions';
import { AppState } from '../index';
export class Local { export class Local {
initialState(): AppState {
constructor(private dataProvider: DataProviderService) { }
getConfig(): UhkConfiguration {
let configJson = localStorage.getItem('config');
let config: UhkConfiguration; let config: UhkConfiguration;
let presetAll: Keymap[]; if (configJson) {
config = new UhkConfiguration().fromJsObject(JSON.parse(configJson));
// Load data from json } else {
if (!localStorage.getItem('config')) { config = this.dataProvider.getUHKConfig();
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));
} }
return { return config;
keymaps: {
entities: config.keymaps
},
macros: {
entities: config.macros
},
presetKeymaps: presetAll
};
} }
saveSate(reducer: any): (state: any, action: Action) => AppState { saveConfig(config: UhkConfiguration): void {
return function (state: any, action: Action) { localStorage.setItem('config', JSON.stringify(config.toJsObject()));
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;
};
} }
} }