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 
16 #include "frameworks/bridge/js_frontend/engine/jsi/jsi_clock_bridge.h"
17 
18 namespace OHOS::Ace::Framework {
19 
ParseClockConfig(const shared_ptr<JsRuntime> & runtime,const shared_ptr<JsValue> & valObject)20 void JsiClockBridge::ParseClockConfig(const shared_ptr<JsRuntime>& runtime, const shared_ptr<JsValue>& valObject)
21 {
22     if (!runtime) {
23         LOGE("runtime is null");
24         return;
25     }
26     if (!valObject->IsObject(runtime)) {
27         LOGE("none found attrs");
28         return;
29     }
30     shared_ptr<JsValue> propertyNames;
31     int32_t len = 0;
32     valObject->GetPropertyNames(runtime, propertyNames, len);
33     for (auto i = 0; i < len; i++) {
34         shared_ptr<JsValue> key = propertyNames->GetElement(runtime, i);
35         if (!key) {
36             LOGW("key is null. Ignoring!");
37             continue;
38         }
39         std::string keyStr = key->ToString(runtime);
40         shared_ptr<JsValue> item = valObject->GetProperty(runtime, key);
41         if (!item) {
42             LOGW("item value is null. Ignoring!");
43             continue;
44         }
45         if (item->IsString(runtime) || item->IsNumber(runtime)) {
46             std::string valStr = item->ToString(runtime);
47             // static linear map must be sorted by key.
48             static const LinearMapNode<void (*)(std::string&, JsiClockBridge&)> clockConfigOperators[] = {
49                 { DOM_DIGIT_COLOR,
50                     [](std::string& valStr, JsiClockBridge& clockBridge) {
51                         clockBridge.clockConfig_.digitColor_ = valStr;
52                     } },
53                 { DOM_DIGIT_COLOR_NIGHT,
54                     [](std::string& valStr, JsiClockBridge& clockBridge) {
55                         clockBridge.clockConfig_.digitColorNight_ = valStr;
56                     } },
57                 { DOM_DIGIT_RADIUS_RATIO,
58                     [](std::string& valStr, JsiClockBridge& clockBridge) {
59                         clockBridge.clockConfig_.digitRadiusRatio_ = StringToDouble(valStr);
60                     } },
61                 { DOM_DIGIT_SIZE_RATIO,
62                     [](std::string& valStr, JsiClockBridge& clockBridge) {
63                         clockBridge.clockConfig_.digitSizeRatio_ = StringToDouble(valStr);
64                     } },
65                 { DOM_CLOCK_FACE_SOURCE,
66                     [](std::string& valStr, JsiClockBridge& clockBridge) {
67                         clockBridge.clockConfig_.clockFaceSrc_ = valStr;
68                     } },
69                 { DOM_CLOCK_FACE_SOURCE_NIGHT,
70                     [](std::string& valStr, JsiClockBridge& clockBridge) {
71                         clockBridge.clockConfig_.clockFaceNightSrc_ = valStr;
72                     } },
73                 { DOM_HOUR_HAND_SOURCE,
74                     [](std::string& valStr, JsiClockBridge& clockBridge) {
75                         clockBridge.clockConfig_.hourHandSrc_ = valStr;
76                     } },
77                 { DOM_HOUR_HAND_SOURCE_NIGHT,
78                     [](std::string& valStr, JsiClockBridge& clockBridge) {
79                         clockBridge.clockConfig_.hourHandNightSrc_ = valStr;
80                     } },
81                 { DOM_MINUTE_HAND_SOURCE,
82                     [](std::string& valStr, JsiClockBridge& clockBridge) {
83                         clockBridge.clockConfig_.minuteHandSrc_ = valStr;
84                     } },
85                 { DOM_MINUTE_HAND_SOURCE_NIGHT,
86                     [](std::string& valStr, JsiClockBridge& clockBridge) {
87                         clockBridge.clockConfig_.minuteHandNightSrc_ = valStr;
88                     } },
89                 { DOM_SECOND_HAND_SOURCE,
90                     [](std::string& valStr, JsiClockBridge& clockBridge) {
91                         clockBridge.clockConfig_.secondHandSrc_ = valStr;
92                     } },
93                 { DOM_SECOND_HAND_SOURCE_NIGHT,
94                     [](std::string& valStr, JsiClockBridge& clockBridge) {
95                         clockBridge.clockConfig_.secondHandNightSrc_ = valStr;
96                     } },
97             };
98             auto operatorIter =
99                 BinarySearchFindIndex(clockConfigOperators, ArraySize(clockConfigOperators), keyStr.c_str());
100             if (operatorIter != -1) {
101                 clockConfigOperators[operatorIter].value(valStr, *this);
102             }
103         }
104     }
105 }
106 
107 } // namespace OHOS::Ace::Framework
108