• 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 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMMON_RESOURCE_PATTERN_REOURCE_MANAGER_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMMON_RESOURCE_PATTERN_REOURCE_MANAGER_H
18 
19 #include <functional>
20 #include <map>
21 #include <string>
22 #include <unordered_map>
23 
24 #include "base/memory/ace_type.h"
25 
26 #include "core/common/resource/resource_object.h"
27 #include "core/components/common/properties/text_style.h"
28 
29 namespace OHOS::Ace {
30 enum class ValueType {
31     UNKNOWN = 0,
32     CALDIMENSION,
33     COLOR,
34     DOUBLE,
35     DIMENSION,
36     FLOAT,
37     FONT_WEIGHT,
38     MEDIA,
39     STRING,
40     U16STRING,
41     VECTOR_STRING
42 };
43 
44 using VariantValue = std::variant<std::string, std::u16string, float, double, Color, CalcDimension,
45     std::vector<std::string>, FontWeight>;
46 
47 class PropertyValueBase : public virtual AceType {
48     DECLARE_ACE_TYPE(PropertyValueBase, AceType);
49 public:
50     virtual ~PropertyValueBase() = default;
GetValue()51     VariantValue& GetValue()
52     {
53         return value_;
54     }
GetValueType()55     ValueType GetValueType()
56     {
57         return valueType_;
58     }
SetValue(const VariantValue & value)59     void SetValue(const VariantValue& value)
60     {
61         value_ = value;
62     }
SetValueType(const ValueType & valueType)63     void SetValueType(const ValueType& valueType)
64     {
65         valueType_ = valueType;
66     }
67 private:
68     ValueType valueType_;
69     VariantValue value_;
70 };
71 
72 class PatternResourceManager final : public AceType {
73     DECLARE_ACE_TYPE(PatternResourceManager, AceType);
74 
75 public:
76     PatternResourceManager() = default;
77     ~PatternResourceManager() override = default;
78 
79     void AddResource(
80         const std::string& key,
81         const RefPtr<ResourceObject>& resObj,
82         std::function<void(const RefPtr<ResourceObject>&)>&& updateFunc);
83 
84     void AddResCache(const std::string& key, const std::string& value);
85 
86     std::string GetResCacheMapByKey(const std::string& key);
87 
88     void RemoveResource(const std::string& key);
89 
90     void ReloadResources();
91 
92     bool Empty();
93 
94     template<typename T>
UpdateProperty(std::function<void (const std::string &,const RefPtr<PropertyValueBase> &)> && propUpdateFunc,const std::string & key,const RefPtr<ResourceObject> & resObj)95     void UpdateProperty(std::function<void(const std::string&, const RefPtr<PropertyValueBase>&)>&& propUpdateFunc,
96         const std::string& key, const RefPtr<ResourceObject>& resObj)
97     {
98         auto value = AceType::MakeRefPtr<PropertyValueBase>();
99         if constexpr (std::is_same_v<T, std::string>) {
100             value->SetValueType(ValueType::STRING);
101         } else if (std::is_same_v<T, std::u16string>) {
102             value->SetValueType(ValueType::U16STRING);
103         } else if constexpr(std::is_same_v<T, Color>) {
104             value->SetValueType(ValueType::COLOR);
105         } else if constexpr(std::is_same_v<T, double>) {
106             value->SetValueType(ValueType::DOUBLE);
107         } else if constexpr(std::is_same_v<T, CalcDimension>) {
108             value->SetValueType(ValueType::CALDIMENSION);
109         } else if constexpr(std::is_same_v<T, float>) {
110             value->SetValueType(ValueType::FLOAT);
111         } else if constexpr(std::is_same_v<T, std::vector<std::string>>) {
112             value->SetValueType(ValueType::VECTOR_STRING);
113         }
114         ParsePropertyValue(resObj, value);
115         if (propUpdateFunc) {
116             propUpdateFunc(key, value);
117         }
118     }
119 
120     template<typename T>
RegisterResource(std::function<void (const std::string &,const RefPtr<PropertyValueBase> &)> && propUpdateFunc,const std::string & key,const RefPtr<ResourceObject> & resObj,T value)121     void RegisterResource(std::function<void(const std::string&, const RefPtr<PropertyValueBase>&)>&& propUpdateFunc,
122         const std::string& key, const RefPtr<ResourceObject>& resObj, T value)
123     {
124         auto&& updateFunc = [weakptr = AceType::WeakClaim(this), propUpdateFunc, key](
125                                 const RefPtr<ResourceObject>& resObj) mutable {
126             auto manager = weakptr.Upgrade();
127             if (manager) {
128                 manager->UpdateProperty<T>(std::move(propUpdateFunc), key, resObj);
129             }
130         };
131         AddResource(key, resObj, std::move(updateFunc));
132     }
133 
134     void ParsePropertyValue(const RefPtr<ResourceObject>& resObj, RefPtr<PropertyValueBase> value);
135 private:
136     struct ResourceUpdater {
137         RefPtr<ResourceObject> obj;
138         std::function<void(const RefPtr<ResourceObject>&)> updateFunc;
139     };
140     std::unordered_map<std::string, ResourceUpdater> resMap_;
141     std::vector<std::string> resKeyArray_;
142     std::unordered_map<std::string, std::map<bool, std::string>> resCacheMap_;
143 };
144 }
145 #endif
146