• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/**
2 * Copyright (c) 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 AssistiveGrid from '../../../setting/settingitem/AssistiveGrid'
17import { Log } from '../../../utils/Log'
18import { SettingManager } from '../../../setting/SettingManager'
19import { SettingData }  from '../model/SettingData'
20import getStore from '../../../redux/store';
21
22let localState = (state) => {
23  return {
24    mode: state.ModeReducer.mode,
25  }
26}
27
28let localDispatcher = (dispatch) => {
29  return {}
30}
31
32
33@Component
34export struct SetToggle {
35  private TAG: string = '[SetToggle]:'
36  @Link settingsList: any[]
37  @State isOn: boolean = false
38  private itemValue: SettingData
39  private getToggleStatus: Promise<string>
40  private WH_100_100: string = "100%"
41  private settingManager = SettingManager.getInstance()
42  @State generalStatusValue: boolean = false
43  @State state: any = {}
44
45  aboutToAppear() {
46    Log.info(`${this.TAG} aboutToAppear start`)
47    getStore().connect(localState, localDispatcher)(this.state)
48    try {
49      if (this.settingManager.getSettingValue(this.itemValue.settingAlias) === "1") {
50        this.generalStatusValue = true
51      } else {
52        this.generalStatusValue = false
53      }
54    } catch {
55      Log.info(`${this.TAG} aboutToAppear settingsList ${JSON.stringify(this.settingsList)}`)
56      this.generalStatusValue = false
57    }
58    Log.info(`${this.TAG} aboutToAppear end`)
59  }
60
61  build() {
62    Row() {
63      Flex({
64        direction: FlexDirection.Row,
65        alignItems: ItemAlign.Center
66      }) {
67        Column() {
68          Row() {
69            Image(this.itemValue.imagePath)
70              .width(24)
71              .height(24)
72              .fillColor("#FFFFFF")
73            Text(this.itemValue.settingName)
74              .fontColor("#FFFFFF")
75              .fontSize($r("sys.float.ohos_id_text_size_sub_title2"))
76              .fontWeight(FontWeight.Regular)
77              .margin({ left: $r("sys.float.ohos_id_elements_margin_horizontal_l") })
78          }
79
80          if (this.itemValue?.description) {
81            Row() {
82              Text(this.itemValue.description)
83                .fontColor("#FFFFFF")
84                .fontSize($r("sys.float.ohos_id_text_size_body2"))
85                .fontWeight(FontWeight.Regular)
86                .opacity($r("sys.float.ohos_id_alpha_content_secondary"))
87            }.margin({
88              top: $r("sys.float.ohos_id_text_margin_vertical"),
89              left: 40 })
90          }
91        }
92        .layoutWeight(1)
93        .alignItems(HorizontalAlign.Start)
94
95        Row() {
96          Toggle({ type: ToggleType.Switch, isOn: this.generalStatusValue })
97            .width(36)
98            .height(20)
99            .onChange((isOn: boolean) => {
100              Log.info(this.TAG + ' SettingItemToggle onChange for Wlan Enable:' + isOn)
101              this.isOn = new Boolean(isOn).valueOf()
102              let setToggle: string = "0"
103              if (this.isOn) {
104                setToggle = "1"
105              } else {
106                setToggle = "0"
107              }
108              this.settingManager.setSettingValue(this.itemValue.settingAlias, setToggle, this.state.mode)
109            })
110        }
111        .margin({ left: $r("sys.float.ohos_id_card_margin_end") })
112      }
113      .padding({ left: 12, right: 12, top: 4, bottom: 4 })
114    }
115    .height(this.itemValue?.description ? 72 : 56)
116    .width(this.WH_100_100)
117  }
118}