app-setup.service.ts 5.66 KiB
import { HttpService } from "../http/http.service";
import { Injectable, Inject } from "@angular/core";
import { Observable } from "jalhyd";
import { StorageService, LOCAL_STORAGE } from "ngx-webstorage-service";
/**
 * Stores app preferences
 */
@Injectable()
export class ApplicationSetupService extends Observable {
    private CONFIG_FILE_PATH = "app/config.json";
    private LOCAL_STORAGE_PREFIX = "nghyd_";
    /** ultimate fallback language (read from config) */
    private _fallbackLanguage = "fr";
    // default builtin values
    public displayPrecision = 0.001;
    public computePrecision = 0.0001;
    public newtonMaxIterations = 50;
    public enableNotifications = true;
    /**
     * just stores the current language preference, does not transmit it to I18nService, that is
     * not available here.
     * @see ApplicationSetupComponent.currentLanguageCode() setter
     * @see I18nService.update() observer
    public language = "fr";
    /** themes to group calculators, for displaying on the front page */
    public themes: any[];
    public warnBeforeTabClose: boolean;
    public constructor(
        private httpService: HttpService,
        @Inject(LOCAL_STORAGE) private storage: StorageService
    ) {
        super();
        // precedence: builtin values >> JSON config >> browser's language >> local storage
        const builtinLanguage = this.language;
        // related to @HostListener("window:beforeunload") in AppComponent
        this.warnBeforeTabClose = true;
        // load JSON config
        this.readValuesFromConfig().then((data) => {
            const configLanguage = this.language;
            this._fallbackLanguage = configLanguage;
            // guess browser's language
            this.language = navigator.language;
            const browserLanguage = this.language;
            // load saved preferences
            const loadedPrefKeys = this.readValuesFromLocalStorage();
            let storageLanguage: string;
            if (loadedPrefKeys.includes("language")) {
                storageLanguage = this.language;
            // notify I18nService
            this.notifyObservers({
                action: "languagePreferenceChanged",
                languages: [ storageLanguage, browserLanguage, configLanguage, builtinLanguage ]
            });
        });
7172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
public get displayDigits() { return -Math.log10(this.displayPrecision); } public get fallbackLanguage() { return this._fallbackLanguage; } /** * Save configuration values into local storage */ public saveValuesIntoLocalStorage() { this.storage.set(this.LOCAL_STORAGE_PREFIX + "displayPrecision", this.displayPrecision); this.storage.set(this.LOCAL_STORAGE_PREFIX + "computePrecision", this.computePrecision); this.storage.set(this.LOCAL_STORAGE_PREFIX + "newtonMaxIterations", this.newtonMaxIterations); this.storage.set(this.LOCAL_STORAGE_PREFIX + "enableNotifications", this.enableNotifications); this.storage.set(this.LOCAL_STORAGE_PREFIX + "language", this.language); } /** * Restore configuration values */ public restoreDefaultValues(): Promise<any> { return this.readValuesFromConfig().then(() => { // notify I18nService this.notifyObservers({ action: "languagePreferenceChanged", languages: [ this.language ] }); }); } /** * Read configuration values from local storage */ private readValuesFromLocalStorage(): string[] { const loadedKeys = []; // get all config values (volontarily non-generic to prevent side-effects) const displayPrecision = this.storage.get(this.LOCAL_STORAGE_PREFIX + "displayPrecision"); if (displayPrecision !== undefined) { this.displayPrecision = displayPrecision; loadedKeys.push("displayPrecision"); } const computePrecision = this.storage.get(this.LOCAL_STORAGE_PREFIX + "computePrecision"); if (computePrecision !== undefined) { this.computePrecision = computePrecision; loadedKeys.push("computePrecision"); } const newtonMaxIterations = this.storage.get(this.LOCAL_STORAGE_PREFIX + "newtonMaxIterations"); if (newtonMaxIterations !== undefined) { this.newtonMaxIterations = newtonMaxIterations; loadedKeys.push("newtonMaxIterations"); } const enableNotifications = this.storage.get(this.LOCAL_STORAGE_PREFIX + "enableNotifications"); if (enableNotifications !== undefined) { this.enableNotifications = enableNotifications; loadedKeys.push("enableNotifications"); } const language = this.storage.get(this.LOCAL_STORAGE_PREFIX + "language"); if (language !== undefined) { this.language = language; loadedKeys.push("language"); } return loadedKeys; } /** * Read configuration values from config (async) */
141142143144145146147148149150151152153154
private readValuesFromConfig(): Promise<any> { return this.httpService.httpGetRequestPromise(this.CONFIG_FILE_PATH).then((data: any) => { // get all config values (volontarily non-generic to prevent side-effects) this.displayPrecision = data.params.displayPrecision; this.computePrecision = data.params.computePrecision; this.newtonMaxIterations = data.params.newtonMaxIterations; this.enableNotifications = data.params.enableNotifications; this.language = data.params.language; // load themes for calculators list page this.themes = data.themes; }); } }