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 */ 15import ConfigData from '../../common/constants'; 16import Log from '../../../../../../../../../common/src/main/ets/default/Log'; 17 18const TAG = 'ManagementComponent-SwitchComponent'; 19 20@Component 21export default struct SwitchComponent { 22 @Link title: Resource; 23 describe: Resource; 24 private initializationAction: (params?) => Promise<any> 25 private settingAction: (params?) => void 26 private register?: (listener) => void 27 @State initState: boolean = false 28 29 build() { 30 Flex({ justifyContent: FlexAlign.SpaceBetween }) { 31 Row() { 32 Column() { 33 Text(this.title) 34 .fontColor($r('sys.color.ohos_id_color_text_primary')) 35 .fontSize($r('sys.float.ohos_id_text_size_body1')) 36 .fontWeight(FontWeight.Medium) 37 .align(Alignment.Start) 38 Text(this.describe) 39 .fontColor($r('sys.color.ohos_id_color_text_primary')) 40 .fontSize($r('sys.float.ohos_id_text_size_body2')) 41 .align(Alignment.Start) 42 .visibility((this.describe ? true : false) ? Visibility.Visible : Visibility.None) 43 }.alignItems(HorizontalAlign.Start) 44 } 45 .flexShrink(1) 46 .height($r('app.float.switchComp_height')) 47 .alignItems(VerticalAlign.Center) 48 .align(Alignment.Start) 49 .padding({ right: $r('app.float.switchComp_padding_r') }) 50 .margin({ left: $r('app.float.page_notice_info_label_margin') }) 51 52 Row() { 53 Toggle({ type: ToggleType.Switch, isOn: this.initState }) 54 .align(Alignment.End) 55 .offset({ x: $r("app.float.noDisturb_offset_x"), y: 0 }) 56 .selectedColor($r("app.color.font_color_007DFF")) 57 .width($r('app.float.toggle_comp_width')) 58 .height($r('app.float.toggle_comp_height')) 59 .onChange((data) => { 60 Log.showInfo(TAG, `Toggle onChange data:${data}`); 61 this.initState = data ? true : false; 62 this.settingAction(data); 63 }) 64 } 65 .flexShrink(0) 66 .height($r('app.float.switchComp_height')) 67 .alignItems(VerticalAlign.Center) 68 .align(Alignment.End) 69 .padding({ right: $r('app.float.switchComp_padding_r') }) 70 }.width(ConfigData.WH_100_100) 71 .height($r('app.float.switchComp_height')) 72 .border({ width: $r('app.float.border_width'), color: Color.White, 73 radius: $r('app.float.border_radius') }) 74 .backgroundColor(Color.White) 75 } 76 77 aboutToAppear(): void{ 78 Log.showInfo(TAG, `aboutToAppear`); 79 this.initializationAction().then((data) => { 80 Log.showInfo(TAG, `initializationAction:${data}`); 81 this.initState = data; 82 }) 83 } 84}