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