1fb299fa2Sopenharmony_ci/*
2fb299fa2Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
3fb299fa2Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4fb299fa2Sopenharmony_ci * you may not use this file except in compliance with the License.
5fb299fa2Sopenharmony_ci * You may obtain a copy of the License at
6fb299fa2Sopenharmony_ci *
7fb299fa2Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8fb299fa2Sopenharmony_ci *
9fb299fa2Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10fb299fa2Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11fb299fa2Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12fb299fa2Sopenharmony_ci * See the License for the specific language governing permissions and
13fb299fa2Sopenharmony_ci * limitations under the License.
14fb299fa2Sopenharmony_ci */
15fb299fa2Sopenharmony_ci
16fb299fa2Sopenharmony_ci#include "sub_page.h"
17fb299fa2Sopenharmony_ci#include "dock/focus_manager.h"
18fb299fa2Sopenharmony_ci#include "log/log.h"
19fb299fa2Sopenharmony_ci#include "scope_guard.h"
20fb299fa2Sopenharmony_ci
21fb299fa2Sopenharmony_cinamespace Updater {
22fb299fa2Sopenharmony_ciusing namespace OHOS;
23fb299fa2Sopenharmony_ciSubPage::SubPage() : basePage_ {}, pageId_ {}, comsId_ {}, isVisible_ {false}, color_ {Color::Black()}
24fb299fa2Sopenharmony_ci{
25fb299fa2Sopenharmony_ci}
26fb299fa2Sopenharmony_ci
27fb299fa2Sopenharmony_ciSubPage::SubPage(const std::shared_ptr<Page> &basePage, const std::string &pageId)
28fb299fa2Sopenharmony_ci    : basePage_ {basePage}, pageId_ {pageId}, comsId_ {}, isVisible_ {false}, color_ {Color::Black()}
29fb299fa2Sopenharmony_ci{
30fb299fa2Sopenharmony_ci}
31fb299fa2Sopenharmony_ci
32fb299fa2Sopenharmony_civoid SubPage::Reset()
33fb299fa2Sopenharmony_ci{
34fb299fa2Sopenharmony_ci    isVisible_ = false;
35fb299fa2Sopenharmony_ci    focusedView_ = nullptr;
36fb299fa2Sopenharmony_ci}
37fb299fa2Sopenharmony_ci
38fb299fa2Sopenharmony_cibool SubPage::BuildSubPage(UxSubPageInfo &subpageInfo)
39fb299fa2Sopenharmony_ci{
40fb299fa2Sopenharmony_ci    if (!IsValid()) {
41fb299fa2Sopenharmony_ci        return false;
42fb299fa2Sopenharmony_ci    }
43fb299fa2Sopenharmony_ci    Reset();
44fb299fa2Sopenharmony_ci    color_ = StrToColor(subpageInfo.bgColor);
45fb299fa2Sopenharmony_ci    comsId_ = std::move(subpageInfo.coms);
46fb299fa2Sopenharmony_ci    int minY = INT16_MAX;
47fb299fa2Sopenharmony_ci    std::string focusedId {};
48fb299fa2Sopenharmony_ci    for (auto &id : comsId_) {
49fb299fa2Sopenharmony_ci        if (!basePage_->IsValidCom(id)) {
50fb299fa2Sopenharmony_ci            LOG(ERROR) << "invalid id for subpage: " << id;
51fb299fa2Sopenharmony_ci            return false;
52fb299fa2Sopenharmony_ci        }
53fb299fa2Sopenharmony_ci        if ((*basePage_)[id]->IsFocusable() && (*basePage_)[id]->GetY() < minY) {
54fb299fa2Sopenharmony_ci            minY = (*basePage_)[id]->GetY();
55fb299fa2Sopenharmony_ci            focusedId = id;
56fb299fa2Sopenharmony_ci        }
57fb299fa2Sopenharmony_ci    }
58fb299fa2Sopenharmony_ci    if (!focusedId.empty()) {
59fb299fa2Sopenharmony_ci        focusedView_ = (*basePage_)[focusedId].As();
60fb299fa2Sopenharmony_ci    }
61fb299fa2Sopenharmony_ci    return true;
62fb299fa2Sopenharmony_ci}
63fb299fa2Sopenharmony_ci
64fb299fa2Sopenharmony_cibool SubPage::IsPageInfoValid(const UxSubPageInfo &info)
65fb299fa2Sopenharmony_ci{
66fb299fa2Sopenharmony_ci    if (info.id.empty()) {
67fb299fa2Sopenharmony_ci        LOG(ERROR) << "sub page id is empty";
68fb299fa2Sopenharmony_ci        return false;
69fb299fa2Sopenharmony_ci    }
70fb299fa2Sopenharmony_ci
71fb299fa2Sopenharmony_ci    if (!CheckColor(info.bgColor)) {
72fb299fa2Sopenharmony_ci        LOG(ERROR) << "sub page color not valid, bgcolor: " << info.bgColor;
73fb299fa2Sopenharmony_ci        return false;
74fb299fa2Sopenharmony_ci    }
75fb299fa2Sopenharmony_ci    return true;
76fb299fa2Sopenharmony_ci}
77fb299fa2Sopenharmony_ci
78fb299fa2Sopenharmony_cistd::string SubPage::GetPageId()
79fb299fa2Sopenharmony_ci{
80fb299fa2Sopenharmony_ci    return pageId_;
81fb299fa2Sopenharmony_ci}
82fb299fa2Sopenharmony_ci
83fb299fa2Sopenharmony_civoid SubPage::SetVisible(bool isVisible)
84fb299fa2Sopenharmony_ci{
85fb299fa2Sopenharmony_ci    if (!IsValid()) {
86fb299fa2Sopenharmony_ci        return;
87fb299fa2Sopenharmony_ci    }
88fb299fa2Sopenharmony_ci    isVisible_ = isVisible;
89fb299fa2Sopenharmony_ci    const auto &view = basePage_->GetView();
90fb299fa2Sopenharmony_ci    if (view == nullptr) {
91fb299fa2Sopenharmony_ci        LOG(ERROR) << "basepage's view is nullptr";
92fb299fa2Sopenharmony_ci        return;
93fb299fa2Sopenharmony_ci    }
94fb299fa2Sopenharmony_ci
95fb299fa2Sopenharmony_ci    for (const auto &id : comsId_) {
96fb299fa2Sopenharmony_ci        (*basePage_)[id]->SetVisible(isVisible);
97fb299fa2Sopenharmony_ci    }
98fb299fa2Sopenharmony_ci    view->SetVisible(isVisible);
99fb299fa2Sopenharmony_ci    if (isVisible) {
100fb299fa2Sopenharmony_ci        // change background
101fb299fa2Sopenharmony_ci        view->SetStyle(OHOS::STYLE_BACKGROUND_COLOR, color_.full);
102fb299fa2Sopenharmony_ci        view->SetStyle(OHOS::STYLE_BACKGROUND_OPA, color_.alpha);
103fb299fa2Sopenharmony_ci    }
104fb299fa2Sopenharmony_ci    UpdateFocus(isVisible);
105fb299fa2Sopenharmony_ci}
106fb299fa2Sopenharmony_ci
107fb299fa2Sopenharmony_cibool SubPage::IsVisible() const
108fb299fa2Sopenharmony_ci{
109fb299fa2Sopenharmony_ci    return isVisible_;
110fb299fa2Sopenharmony_ci}
111fb299fa2Sopenharmony_ci
112fb299fa2Sopenharmony_ciOHOS::UIViewGroup *SubPage::GetView() const
113fb299fa2Sopenharmony_ci{
114fb299fa2Sopenharmony_ci    if (!IsValid()) {
115fb299fa2Sopenharmony_ci        return nullptr;
116fb299fa2Sopenharmony_ci    }
117fb299fa2Sopenharmony_ci    return basePage_->GetView();
118fb299fa2Sopenharmony_ci}
119fb299fa2Sopenharmony_ci
120fb299fa2Sopenharmony_cibool SubPage::IsValid() const
121fb299fa2Sopenharmony_ci{
122fb299fa2Sopenharmony_ci    if (basePage_ == nullptr || !basePage_->IsValid()) {
123fb299fa2Sopenharmony_ci        LOG(ERROR) << "basepage of subpage is null";
124fb299fa2Sopenharmony_ci        return false;
125fb299fa2Sopenharmony_ci    }
126fb299fa2Sopenharmony_ci    return true;
127fb299fa2Sopenharmony_ci}
128fb299fa2Sopenharmony_ci
129fb299fa2Sopenharmony_cibool SubPage::IsValidCom(const std::string &id) const
130fb299fa2Sopenharmony_ci{
131fb299fa2Sopenharmony_ci    if (!IsValid()) {
132fb299fa2Sopenharmony_ci        return false;
133fb299fa2Sopenharmony_ci    }
134fb299fa2Sopenharmony_ci    if (std::find(comsId_.begin(), comsId_.end(), id) == comsId_.end()) {
135fb299fa2Sopenharmony_ci        return false;
136fb299fa2Sopenharmony_ci    }
137fb299fa2Sopenharmony_ci    return basePage_->IsValidCom(id);
138fb299fa2Sopenharmony_ci}
139fb299fa2Sopenharmony_ci
140fb299fa2Sopenharmony_ciViewProxy &SubPage::operator[](const std::string &id)
141fb299fa2Sopenharmony_ci{
142fb299fa2Sopenharmony_ci    static ViewProxy dummy;
143fb299fa2Sopenharmony_ci    if (!IsValid()) {
144fb299fa2Sopenharmony_ci        return dummy;
145fb299fa2Sopenharmony_ci    }
146fb299fa2Sopenharmony_ci    return (*basePage_)[id];
147fb299fa2Sopenharmony_ci}
148fb299fa2Sopenharmony_ci} // namespace Updater
149