• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 "core/common/resource/pattern_resource_manager.h"
17 
18 #include "base/utils/device_config.h"
19 #include "core/common/container.h"
20 #include "core/common/resource/resource_parse_utils.h"
21 
22 namespace OHOS::Ace {
AddResource(const std::string & key,const RefPtr<ResourceObject> & resObj,std::function<void (const RefPtr<ResourceObject> &)> && updateFunc)23 void PatternResourceManager::AddResource(
24     const std::string& key,
25     const RefPtr<ResourceObject>& resObj,
26     std::function<void(const RefPtr<ResourceObject>&)>&& updateFunc)
27 {
28     if (resObj == nullptr || !updateFunc) {
29         return;
30     }
31     if (resMap_.count(key) > 0) {
32         resCacheMap_.clear();
33         resKeyArray_.erase(std::remove(resKeyArray_.begin(), resKeyArray_.end(), key), resKeyArray_.end());
34     }
35     resMap_[key] = { resObj, std::move(updateFunc) };
36     resKeyArray_.emplace_back(key);
37 }
38 
AddResCache(const std::string & key,const std::string & value)39 void PatternResourceManager::AddResCache(const std::string& key, const std::string& value)
40 {
41     if (key.empty() || value.empty()) {
42         return;
43     }
44     bool darkMode = Container::CurrentColorMode() == ColorMode::DARK;
45     resCacheMap_[key][darkMode] = value;
46 }
47 
GetResCacheMapByKey(const std::string & key)48 std::string PatternResourceManager::GetResCacheMapByKey(const std::string& key)
49 {
50     bool darkMode = Container::CurrentColorMode() == ColorMode::DARK;
51     if (!resCacheMap_.count(key) || !resCacheMap_[key].count(darkMode)) {
52         return "";
53     }
54     return resCacheMap_[key][darkMode];
55 }
56 
RemoveResource(const std::string & key)57 void PatternResourceManager::RemoveResource(const std::string& key)
58 {
59     auto iter = resMap_.find(key);
60     if (iter != resMap_.end()) {
61         resMap_.erase(iter);
62     }
63     auto it = std::find(resKeyArray_.begin(), resKeyArray_.end(), key);
64     if (it != resKeyArray_.end()) {
65         resKeyArray_.erase(it);
66     }
67     resCacheMap_.clear();
68 }
69 
ReloadResources()70 void PatternResourceManager::ReloadResources()
71 {
72     for (const auto& key : resKeyArray_) {
73         if (!resMap_.count(key)) {
74             continue;
75         }
76         auto resourceUpdater = resMap_[key];
77         resourceUpdater.updateFunc(resourceUpdater.obj);
78     }
79 }
80 
Empty()81 bool PatternResourceManager::Empty()
82 {
83     return resMap_.empty();
84 }
85 
ParsePropertyValue(const RefPtr<ResourceObject> & resObj,RefPtr<PropertyValueBase> valueBase)86 void PatternResourceManager::ParsePropertyValue(
87     const RefPtr<ResourceObject>& resObj, RefPtr<PropertyValueBase> valueBase)
88 {
89     if (valueBase->GetValueType() == ValueType::STRING) {
90         std::string value;
91         if (ResourceParseUtils::ParseResString(resObj, value)) {
92             valueBase->SetValue(value);
93         } else {
94             ResourceParseUtils::ParseResMedia(resObj, value);
95             valueBase->SetValue(value);
96             valueBase->SetValueType(ValueType::MEDIA);
97         }
98     } else if (valueBase->GetValueType() == ValueType::U16STRING) {
99         std::u16string value;
100         ResourceParseUtils::ParseResString(resObj, value);
101         valueBase->SetValue(value);
102     } else if (valueBase->GetValueType() == ValueType::COLOR) {
103         Color value;
104         ResourceParseUtils::ParseResColor(resObj, value);
105         valueBase->SetValue(value);
106     } else if (valueBase->GetValueType() == ValueType::DOUBLE) {
107         double value;
108         ResourceParseUtils::ParseResDouble(resObj, value);
109         valueBase->SetValue(value);
110     } else if (valueBase->GetValueType() == ValueType::CALDIMENSION) {
111         CalcDimension value;
112         ResourceParseUtils::ParseResDimensionFpNG(resObj, value);
113         valueBase->SetValue(value);
114     } else if (valueBase->GetValueType() == ValueType::FLOAT) {
115         double value;
116         ResourceParseUtils::ParseResDouble(resObj, value);
117         valueBase->SetValue(static_cast<float>(value));
118     } else if (valueBase->GetValueType() == ValueType::VECTOR_STRING) {
119         std::vector<std::string> value;
120         ResourceParseUtils::ParseResFontFamilies(resObj, value);
121         valueBase->SetValue(value);
122     }
123 }
124 }
125