• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16class Environment {
17  private static Instance_: Environment = undefined;
18  private static EnvBackend_: IEnvironmentBackend;
19  private props_: Map<string, ObservedPropertyAbstract<any>>;
20
21  private static GetOrCreate(): Environment {
22    if (Environment.Instance_) {
23      // already initialized
24      return Environment.Instance_;
25    }
26
27    Environment.Instance_ = new Environment();
28    return Environment.Instance_;
29  }
30
31  public static ConfigureBackend(envBackend: IEnvironmentBackend): void {
32    Environment.EnvBackend_ = envBackend;
33  }
34
35  public static AboutToBeDeleted(): void {
36    if (!Environment.Instance_) {
37      return;
38    }
39
40    Environment.GetOrCreate().aboutToBeDeleted();
41    Environment.Instance_ = undefined;
42  }
43
44  public static EnvProp<S>(key: string, value: S): boolean {
45    return Environment.GetOrCreate().envProp<S>(key, value);
46  }
47
48  public static EnvProps(props: {
49    key: string,
50    defaultValue: any
51  }[]
52  ): void {
53    Environment.GetOrCreate().envProps(props);
54  }
55
56  static Keys(): Array<string> {
57    return Environment.GetOrCreate().keys();
58  }
59
60  private constructor() {
61    this.props_ = new Map<string, ObservedPropertyAbstract<any>>();
62
63    Environment.EnvBackend_.onValueChanged(this.onValueChanged.bind(this));
64  }
65
66  private envProp<S>(key: string, value: S): boolean {
67    let prop = AppStorage.Prop(key);
68    if (prop) {
69      console.warn(`Environment: envProp '${key}': Property already exists in AppStorage. Not using environment property.`);
70      return false;
71    }
72    let tmp;
73    switch(key) {
74      case "accessibilityEnabled":
75        tmp = Environment.EnvBackend_ .getAccessibilityEnabled();
76        break;
77      case "colorMode":
78        tmp = Environment.EnvBackend_ .getColorMode();
79        break;
80      case "fontScale":
81        tmp = Environment.EnvBackend_ .getFontScale();
82        break;
83      case "fontWeightScale":
84        tmp = Environment.EnvBackend_ .getFontWeightScale().toFixed(2);
85        break;
86      case "layoutDirection":
87        tmp = Environment.EnvBackend_ .getLayoutDirection();
88        break;
89      case "languageCode":
90        tmp = Environment.EnvBackend_ .getLanguageCode();
91        break;
92      default:
93        tmp = value;
94    }
95    prop = AppStorage.SetAndProp(key, tmp);
96    this.props_.set(key, prop);
97    console.debug(`Environment: envProp for '${key}' done.`);
98  }
99
100  private envProps(properties: {
101    key: string,
102    defaultValue: any
103  }[]): void {
104    properties.forEach(property => {
105      this.envProp(property.key, property.defaultValue);
106      console.debug(`Environment: envProps for '${property.key}' done.`);
107    });
108  }
109
110  keys(): Array<string> {
111    let result = [];
112    const it = this.props_.keys();
113    let val = it.next();
114
115    while (!val.done) {
116      result.push(val.value);
117      val = it.next();
118    }
119
120    return result;
121  }
122
123  onValueChanged(key: string, value: any): void {
124    let ok = AppStorage.Set(key, value);
125    if (ok) {
126      console.debug(`Environment: onValueChanged: ${key} changed to ${value}`);
127    } else {
128      console.warn(`Environment: onValueChanged: error changing ${key}! See results above.`);
129    }
130  }
131
132  aboutToBeDeleted() {
133    this.props_.forEach((val, key, map) => {
134      val.aboutToBeDeleted();
135      AppStorage.Delete(key);
136    });
137  }
138}
139