1/* 2 * Copyright (c) 2022 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 "progress_strategy.h" 17#include <functional> 18#include <string> 19#include <unordered_map> 20#include "component/box_progress_adapter.h" 21#include "component/img_view_adapter.h" 22#include "component/text_label_adapter.h" 23#include "log/log.h" 24#include "updater/updater_const.h" 25#include "updater_ui_env.h" 26#include "updater_ui.h" 27 28namespace Updater { 29std::unique_ptr<ProgressStrategy> ProgressStrategy::Factory(const std::string &type, const ComInfo &id) 30{ 31 using Fun = std::function<std::unique_ptr<ProgressStrategy>(const ComInfo &)>; 32 const static std::unordered_map<std::string, Fun> prgrsMap = { 33 { "txt", [] (const ComInfo &id) { return std::make_unique<TxtProgress>(id); }}, 34 { "bar", [] (const ComInfo &id) { return std::make_unique<BarProgress>(id); }} 35 }; 36 if (auto it = prgrsMap.find(type); it != prgrsMap.end()) { 37 return it->second(id); 38 } 39 LOG(ERROR) << "not recognized progress type " << type; 40 return std::make_unique<BarProgress>(id); 41} 42 43void ProgressStrategy::Show() const 44{ 45 ShowProgress(0); 46 pgMgr_[id_]->SetVisible(true); 47} 48 49void ProgressStrategy::Hide() const 50{ 51 pgMgr_[id_]->SetVisible(false); 52} 53 54void TxtProgress::ShowProgress(float value) const 55{ 56 int intValue = static_cast<int>(value); 57 if (intValue < 0 || intValue > FULL_PERCENT_PROGRESS) { 58 LOG(ERROR) << "progress value invalid:" << intValue; 59 return; 60 } 61 std::string progress = std::to_string(intValue) + "%"; 62 pgMgr_[id_].As<TextLabelAdapter>()->SetText(progress); 63} 64 65BarProgress::BarProgress(const ComInfo &id) : ProgressStrategy(id) 66{ 67 if (!pgMgr_[id_].As<BoxProgressAdapter>()->InitEp()) { 68 LOG(ERROR) << "bar progress's endpoint init failed"; 69 } 70} 71 72void BarProgress::ShowProgress(float value) const 73{ 74 pgMgr_[id_].As<BoxProgressAdapter>()->SetValue(value); 75} 76} // namespace Updater