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 StyleConfiguration from '../common/StyleConfiguration' 19 20const TAG = 'BatteryComponent-batteryPic' 21 22@Component 23export default 24struct BatteryPic { 25 @StorageLink('batterySoc') batterySoc: number = 100 26 @StorageLink('batteryCharging') batteryCharging : boolean = false 27 @State mContentColor: string = "#FFFFFFFF" 28 @State style: any = StyleConfiguration.getBatteryPicStyle() 29 30 aboutToAppear(){ 31 Log.showInfo(TAG,'aboutToAppear Start'); 32 } 33 34 aboutToDisappear(){ 35 Log.showInfo(TAG,'aboutToDisappear'); 36 } 37 38 build() { 39 40 Row() { 41 Row() { 42 Row() { 43 44 } 45 .height('100%') 46 .width((this.batterySoc < 100 ? this.batterySoc: 100) + '%') 47 .backgroundColor(this.getBatteryColor(this.batterySoc, this.batteryCharging)) 48 } 49 .width(this.style.picBodyWidth) 50 .height(this.style.picBodyHeight) 51 .backgroundColor($r('app.color.battery_background')) 52 .border({ width: this.style.picBodyBorderWidth, 53 color: this.mContentColor, 54 radius: this.style.picBorderRadius, 55 style: BorderStyle.Solid }) 56 .padding(this.style.picBodyPadding) 57 58 Row() { 59 60 } 61 .width(this.style.picGap) 62 .height(1) 63 64 Row() { 65 66 } 67 .width(this.style.picHeadWidth) 68 .height(this.style.picHeadHeight) 69 .backgroundColor(this.mContentColor) 70 .borderRadius(this.style.picHeadBorderRadius) 71 } 72 } 73 74 private getBatteryColor(val, charging) { 75 Log.showDebug(TAG, `getBatteryColor, val: ${ val } charging: ${ charging } `); 76 if (charging) { 77 return this.style.picChargingColor; 78 } else if (val <= Constants.BATTERY_LEVEL_LOW) { 79 return this.style.picLevelLowColor; 80 } else { 81 return this.mContentColor; 82 } 83 } 84} 85