import { Injectable } from '@angular/core'; import { IAppConfig } from '../models/appconfig.model'; import { HttpClient, HttpParams } from '@angular/common/http'; import { Utils } from '@app/shared/utils'; import { environment } from '@environments/environment'; import { AppMessageService } from '@app/shared/app-message.service'; import { AuthService } from './auth.service'; import { MatType } from '@app/shared/global'; import { catchError, map } from 'rxjs/operators'; import { of } from 'rxjs'; @Injectable({ providedIn: 'root' }) export class AppConfigService { private readonly appConf = "apcf"; private readonly appDef = "apdf"; private _settings: any; private _default: any = {}; wasSetDefault = false; get appDefault() { if (!this._default) { const storedConf = JSON.parse(sessionStorage.getItem(this.appDef)); if (storedConf) this._default = storedConf; } return this._default; } set appDefault(newDefault) { this._default = newDefault; if (newDefault) sessionStorage.setItem(this.appConf, JSON.stringify(newDefault)); } get settings(): IAppConfig { if (!this._settings) { const storedConf = JSON.parse(sessionStorage.getItem(this.appConf)); if (storedConf) this._settings = storedConf; else this.checkAndSetDefault(this._settings); } return this._settings; } set settings(newSettings) { this._settings = newSettings; sessionStorage.setItem(this.appConf, JSON.stringify(this._settings)); } constructor( private readonly authSvc: AuthService, private readonly http: HttpClient, private readonly appMsgSvc: AppMessageService) { } load() { return this.http.get("/appConfig").pipe( map(res => { if (!environment.production) console.log("App config loaded !"); this.checkAndSetDefault(res); return true; }), catchError(err => { this.appMsgSvc.addFailedMsg('Could not load AppConfig. Please retry or contact Agnav.'); return of(false); }) ); } private checkAndSetDefault(settings) { if (!settings) settings = {}; if (!('measureUnit' in settings)) { settings.measureUnit = this.authSvc.locale === 'us' ? true : false; } if (Utils.isEmptyObj(settings['obstacles'])) { settings['obstacles'] = { ranges: [ { code: "0", color: "black" }, { code: "1", color: "purple" }, { code: "2", color: "blue" }, { code: "3", color: "green" }, { code: "4", color: "yellow" }, { code: "5", color: "orange" }, { code: "6", color: "red" } ], minHeight: 0 }; } if (Utils.isEmptyObj(settings['sprayPath'])) { settings['sprayPath'] = { overlap: 120, dataOp: 0 }; } if (Utils.isEmptyObj(settings['colors'])) { settings['colors'] = { sprayZone: 'blue', fpColor: 'lime' }; } if (!settings.colors.sprayZone) settings.colors['sprayZone'] = 'blue'; if (!settings.colors.fpColor) settings.colors['fpColor'] = 'lime'; if (Utils.isEmptyObj(settings['mapOps'])) { settings['mapOps'] = { base: 'imagery', mlbs: true, msts: false, obs: false, grlines: false, flines: false } } if (Utils.isEmptyObj(settings['track'])) { settings['track'] = { activeTabs: [0, 1, 2] } } if (Utils.isNulOrUndef(settings['matType'])) settings['matType'] = MatType.LIQUID; this.settings = settings; this.wasSetDefault = true; } getDefObsAGL(isUS) { return isUS ? 50 : 15; } save(path: any = null, silent = false) { return new Promise((resolve, reject) => { sessionStorage.setItem(this.appConf, JSON.stringify(this._settings)); const ops = silent ? { params: new HttpParams().set('loader', 'false') } : {}; this.http.post("/appConfig", { settings: path ? path : this._settings }, ops) .subscribe(res => { this.wasSetDefault = false; resolve(true); }, err => { if (!silent) this.appMsgSvc.addFailedMsg('Could not save AppConfig. Please retry or contact Agnav.'); reject(`Could not save AppConfig !': ${JSON.stringify(err)}`); }); }); } }