1 /*
2 * Copyright (c) 2024 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/components_ng/pattern/overlay/group_manager.h"
17
18 #include "core/components_ng/pattern/stage/page_event_hub.h"
19 #include "core/components_ng/pattern/checkboxgroup/checkboxgroup_pattern.h"
20 #include "core/components_ng/pattern/radio/radio_pattern.h"
21 #include "core/pipeline_ng/pipeline_context.h"
22
23 namespace OHOS::Ace::NG {
24
AddRadioToGroup(const std::string & group,int32_t radioId)25 void GroupManager::AddRadioToGroup(const std::string& group, int32_t radioId)
26 {
27 radioGroupNotify_[group].push_back(radioId);
28 }
29
RemoveRadioFromGroup(const std::string & group,int32_t radioId)30 void GroupManager::RemoveRadioFromGroup(const std::string& group, int32_t radioId)
31 {
32 radioGroupNotify_[group].remove(radioId);
33 }
34
HasRadioId(const std::string & group,int32_t radioId)35 bool GroupManager::HasRadioId(const std::string& group, int32_t radioId)
36 {
37 auto list = radioGroupNotify_[group];
38 auto it = find(list.begin(), list.end(), radioId);
39 return it != list.end();
40 }
41
UpdateRadioGroupValue(const std::string & group,int32_t radioId)42 void GroupManager::UpdateRadioGroupValue(const std::string& group, int32_t radioId)
43 {
44 const auto& list = radioGroupNotify_[group];
45 for (auto&& item : list) {
46 if (item == radioId) {
47 continue;
48 }
49 auto node = DynamicCast<FrameNode>(ElementRegister::GetInstance()->GetNodeById(item));
50 if (!node) {
51 continue;
52 }
53 auto pattern = node->GetPattern<RadioPattern>();
54 if (!pattern) {
55 continue;
56 }
57 pattern->UpdateUncheckStatus(node);
58 }
59 }
60
AddCheckBoxToGroup(const std::string & group,const WeakPtr<FrameNode> & checkBoxNode)61 void GroupManager::AddCheckBoxToGroup(const std::string& group, const WeakPtr<FrameNode>& checkBoxNode)
62 {
63 checkBoxListMap_[group].push_back(checkBoxNode);
64 checkboxChangedMap_[group] = true;
65 }
66
AddCheckBoxGroup(const std::string & group,const WeakPtr<FrameNode> & checkBoxGroupNode)67 void GroupManager::AddCheckBoxGroup(const std::string& group, const WeakPtr<FrameNode>& checkBoxGroupNode)
68 {
69 auto frameNode = checkBoxGroupNode.Upgrade();
70 CHECK_NULL_VOID(frameNode);
71 auto pattern = frameNode->GetPattern<CheckBoxGroupPattern>();
72 CHECK_NULL_VOID(pattern);
73 if (checkBoxGroupMap_[group].Upgrade()) {
74 pattern->SetIsAddToMap(false);
75 } else {
76 pattern->SetIsAddToMap(true);
77 checkBoxGroupMap_[group] = frameNode;
78 }
79 }
80
RemoveCheckBoxFromGroup(const std::string & group,int32_t checkBoxId)81 void GroupManager::RemoveCheckBoxFromGroup(const std::string& group, int32_t checkBoxId)
82 {
83 auto& checkboxList = checkBoxListMap_[group];
84 for (std::list<WeakPtr<FrameNode>>::iterator iter = checkboxList.begin(); iter != checkboxList.end();) {
85 auto frameNode = (*iter).Upgrade();
86 if (!frameNode || frameNode->GetId() == checkBoxId) {
87 iter = checkboxList.erase(iter);
88 checkboxChangedMap_[group] = true;
89 } else {
90 ++iter;
91 }
92 }
93 }
94
RemoveCheckBoxGroup(const std::string & group,int32_t checkBoxGroupId)95 void GroupManager::RemoveCheckBoxGroup(const std::string& group, int32_t checkBoxGroupId)
96 {
97 auto frameNode = checkBoxGroupMap_[group].Upgrade();
98 if (frameNode && frameNode->GetId() == checkBoxGroupId) {
99 checkBoxGroupMap_.erase(group);
100 }
101 }
102
GetCheckboxList(const std::string & group)103 std::list<RefPtr<FrameNode>> GroupManager::GetCheckboxList(const std::string& group)
104 {
105 auto& checkboxList = checkBoxListMap_[group];
106 std::list<RefPtr<FrameNode>> result;
107 for (std::list<WeakPtr<FrameNode>>::iterator iter = checkboxList.begin(); iter != checkboxList.end();) {
108 auto frameNode = (*iter).Upgrade();
109 if (!frameNode) {
110 iter = checkboxList.erase(iter);
111 } else {
112 result.push_back(frameNode);
113 ++iter;
114 }
115 }
116 return result;
117 }
118
GetCheckboxGroup(const std::string & group)119 RefPtr<FrameNode> GroupManager::GetCheckboxGroup(const std::string& group)
120 {
121 return checkBoxGroupMap_[group].Upgrade();
122 }
123
GetGroupManager()124 WeakPtr<GroupManager> GroupManager::GetGroupManager()
125 {
126 WeakPtr<GroupManager> groupManager;
127 auto context = PipelineContext::GetCurrentContext();
128 CHECK_NULL_RETURN(context, nullptr);
129 if (Container::IsInSubContainer()) {
130 auto overlayManager = context->GetOverlayManager();
131 CHECK_NULL_RETURN(overlayManager, nullptr);
132 groupManager = overlayManager->GetGroupManager();
133 } else {
134 auto stageManager = context->GetStageManager();
135 CHECK_NULL_RETURN(stageManager, nullptr);
136 auto pageNode = stageManager->GetLastPage();
137 CHECK_NULL_RETURN(pageNode, nullptr);
138 auto pageEventHub = pageNode->GetOrCreateEventHub<NG::PageEventHub>();
139 CHECK_NULL_RETURN(pageEventHub, nullptr);
140 groupManager = pageEventHub->GetGroupManager();
141 }
142 return groupManager;
143 }
144 } // namespace OHOS::Ace::NG
145