1/* 2 * Copyright (c) 2021-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 16import Constants from '../common/constants' 17import Log from '../../../../../../../common/src/main/ets/default/Log' 18import { TintContentInfo } from '../../../../../../../common/src/main/ets/default/TintStateManager' 19import StyleConfiguration, { BatteryPicStyle } from '../common/StyleConfiguration' 20import ViewModel from '../viewmodel/BatteryVM' 21 22const TAG = 'BatteryComponent-batteryPic'; 23 24@Component 25export default struct BatteryPic { 26 @StorageLink('batterySoc') batterySoc: number = 100 27 @StorageLink('batteryCharging') batteryCharging: boolean = false 28 @State mTintContentInfo: TintContentInfo = ViewModel.getTintContentInfo() 29 @State style: BatteryPicStyle = StyleConfiguration.getBatteryPicStyle() 30 31 aboutToAppear() { 32 Log.showInfo(TAG, 'aboutToAppear Start'); 33 } 34 35 aboutToDisappear() { 36 Log.showInfo(TAG, 'aboutToDisappear'); 37 } 38 39 build() { 40 41 Row() { 42 Row() { 43 Row() { 44 45 } 46 .height('100%') 47 .width((this.batterySoc < 100 ? this.batterySoc : 100) + '%') 48 .backgroundColor(this.getBatteryColor(this.batterySoc, this.batteryCharging)) 49 } 50 .width(this.style.picBodyWidth) 51 .height(this.style.picBodyHeight) 52 .backgroundColor($r('app.color.battery_background')) 53 .border({ width: this.style.picBodyBorderWidth, 54 color: this.mTintContentInfo.contentColor, 55 radius: this.style.picBorderRadius, 56 style: BorderStyle.Solid }) 57 .padding(this.style.picBodyPadding) 58 59 Row() { 60 61 } 62 .width(this.style.picGap) 63 .height(1) 64 65 Row() { 66 67 } 68 .width(this.style.picHeadWidth) 69 .height(this.style.picHeadHeight) 70 .backgroundColor(this.mTintContentInfo.contentColor) 71 .borderRadius(this.style.picHeadBorderRadius) 72 } 73 .margin({ 74 top: $r("app.float.battery_component_margin"), 75 bottom: $r("app.float.battery_component_margin"), 76 left: $r("app.float.battery_component_margin"), 77 right: $r("app.float.battery_component_margin") 78 }) 79 } 80 81 private getBatteryColor(val, charging) { 82 Log.showInfo(TAG, `getBatteryColor, val: ${val} charging: ${charging} `); 83 if (charging) { 84 return this.style.picChargingColor; 85 } else if (val <= Constants.BATTERY_LEVEL_LOW) { 86 return this.style.picLevelLowColor; 87 } else { 88 return this.mTintContentInfo.contentColor; 89 } 90 } 91} 92