1c41cb6d2Sopenharmony_ci/**
2c41cb6d2Sopenharmony_ci * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3c41cb6d2Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4c41cb6d2Sopenharmony_ci * you may not use this file except in compliance with the License.
5c41cb6d2Sopenharmony_ci * You may obtain a copy of the License at
6c41cb6d2Sopenharmony_ci *
7c41cb6d2Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8c41cb6d2Sopenharmony_ci *
9c41cb6d2Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10c41cb6d2Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11c41cb6d2Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12c41cb6d2Sopenharmony_ci * See the License for the specific language governing permissions and
13c41cb6d2Sopenharmony_ci * limitations under the License.
14c41cb6d2Sopenharmony_ci */
15c41cb6d2Sopenharmony_ci
16c41cb6d2Sopenharmony_ciimport ComponentConfig from './ComponentConfig';
17c41cb6d2Sopenharmony_ciimport SwitchController from './controller/SwitchController';
18c41cb6d2Sopenharmony_ci
19c41cb6d2Sopenharmony_ci/**
20c41cb6d2Sopenharmony_ci * Toggle component
21c41cb6d2Sopenharmony_ci */
22c41cb6d2Sopenharmony_ci@Component
23c41cb6d2Sopenharmony_ciexport default struct SwitchComponent {
24c41cb6d2Sopenharmony_ci  @Link isOn: boolean;
25c41cb6d2Sopenharmony_ci  @State isEnabled?: boolean = true;
26c41cb6d2Sopenharmony_ci  private title: string | Resource = '';
27c41cb6d2Sopenharmony_ci  private controller?: SwitchController;
28c41cb6d2Sopenharmony_ci  private summary?: string | Resource = '';
29c41cb6d2Sopenharmony_ci  private switchHeight: string | number | Resource = '';
30c41cb6d2Sopenharmony_ci
31c41cb6d2Sopenharmony_ci  build() {
32c41cb6d2Sopenharmony_ci    Row() {
33c41cb6d2Sopenharmony_ci      Column() {
34c41cb6d2Sopenharmony_ci        Text(this.title)
35c41cb6d2Sopenharmony_ci          .fontColor($r('sys.color.ohos_fa_text_primary'))
36c41cb6d2Sopenharmony_ci          .fontSize($r("app.float.font_16"))
37c41cb6d2Sopenharmony_ci          .fontWeight(FontWeight.Medium)
38c41cb6d2Sopenharmony_ci
39c41cb6d2Sopenharmony_ci        if (this.summary) {
40c41cb6d2Sopenharmony_ci          Text(this.summary)
41c41cb6d2Sopenharmony_ci            .fontColor($r('sys.color.ohos_fa_text_secondary'))
42c41cb6d2Sopenharmony_ci            .fontSize($r('sys.float.ohos_id_text_size_body2'))
43c41cb6d2Sopenharmony_ci            .fontWeight('sans-serif')
44c41cb6d2Sopenharmony_ci            .textAlign(TextAlign.Start)
45c41cb6d2Sopenharmony_ci            .maxLines(ComponentConfig.MAX_LINES_1)
46c41cb6d2Sopenharmony_ci            .textOverflow({ overflow: TextOverflow.Ellipsis })
47c41cb6d2Sopenharmony_ci            .margin({ top: $r('sys.float.ohos_id_text_margin_vertical') })
48c41cb6d2Sopenharmony_ci        }
49c41cb6d2Sopenharmony_ci      }
50c41cb6d2Sopenharmony_ci      .alignItems(HorizontalAlign.Start)
51c41cb6d2Sopenharmony_ci
52c41cb6d2Sopenharmony_ci      Blank()
53c41cb6d2Sopenharmony_ci
54c41cb6d2Sopenharmony_ci      Stack({ alignContent: Alignment.Start }) {
55c41cb6d2Sopenharmony_ci        Toggle({ type: ToggleType.Switch, isOn: this.isOn })
56c41cb6d2Sopenharmony_ci          .width('36vp')
57c41cb6d2Sopenharmony_ci          .height('20vp')
58c41cb6d2Sopenharmony_ci          .margin({ left: $r('app.float.wh_value_6') })
59c41cb6d2Sopenharmony_ci          .selectedColor('#007DFF')
60c41cb6d2Sopenharmony_ci          .onChange((isOn: boolean) => {
61c41cb6d2Sopenharmony_ci            if (!this.isEnabled) return;
62c41cb6d2Sopenharmony_ci            this.isOn = new Boolean(isOn).valueOf();
63c41cb6d2Sopenharmony_ci            this.toggleValue(this.isOn);
64c41cb6d2Sopenharmony_ci          });
65c41cb6d2Sopenharmony_ci      }
66c41cb6d2Sopenharmony_ci    }
67c41cb6d2Sopenharmony_ci    .padding({ left: $r('app.float.distance_12'), right: $r('app.float.distance_8') })
68c41cb6d2Sopenharmony_ci    .width(ComponentConfig.WH_100_100)
69c41cb6d2Sopenharmony_ci    .height(this.switchHeight)
70c41cb6d2Sopenharmony_ci    .backgroundColor($r("app.color.white_bg_color"))
71c41cb6d2Sopenharmony_ci    .alignItems(VerticalAlign.Center)
72c41cb6d2Sopenharmony_ci    .borderRadius($r("app.float.radius_16"))
73c41cb6d2Sopenharmony_ci  }
74c41cb6d2Sopenharmony_ci
75c41cb6d2Sopenharmony_ci  aboutToAppear() {
76c41cb6d2Sopenharmony_ci    if (this.controller) {
77c41cb6d2Sopenharmony_ci      this.toggleValue = this.controller.onSelfToggleValueBindThis;
78c41cb6d2Sopenharmony_ci
79c41cb6d2Sopenharmony_ci      // bind component and initialize
80c41cb6d2Sopenharmony_ci      this.controller.bindComponent(this)
81c41cb6d2Sopenharmony_ci        .bindProperties(["isOn", "isEnabled"])
82c41cb6d2Sopenharmony_ci        .initData()
83c41cb6d2Sopenharmony_ci        .subscribe();
84c41cb6d2Sopenharmony_ci    }
85c41cb6d2Sopenharmony_ci  }
86c41cb6d2Sopenharmony_ci
87c41cb6d2Sopenharmony_ci  aboutToDisappear() {
88c41cb6d2Sopenharmony_ci    this.controller?.unsubscribe();
89c41cb6d2Sopenharmony_ci  }
90c41cb6d2Sopenharmony_ci
91c41cb6d2Sopenharmony_ci  private toggleValue: (isOn: boolean) => void = () => {
92c41cb6d2Sopenharmony_ci  };
93c41cb6d2Sopenharmony_ci}