• 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@Component
33export struct SetToggle {
34  private TAG: string = '[SetToggle]:'
35  @Link settingsList: any[]
36  @State isOn: boolean = false
37  private itemValue: SettingData
38  private getToggleStatus: Promise<string>
39  private WH_100_100: string = "100%"
40  private settingManager = SettingManager.getInstance()
41  @State generalStatusValue: boolean = false
42  @State state: any = {}
43
44  aboutToAppear() {
45    Log.info(`${this.TAG} aboutToAppear start`)
46    getStore().connect(localState, localDispatcher)(this.state)
47    try {
48      if (this.settingManager.getSettingValue(this.itemValue.settingAlias) === "1") {
49        this.generalStatusValue = true
50      } else {
51        this.generalStatusValue = false
52      }
53    } catch {
54      Log.info(`${this.TAG} aboutToAppear settingsList ${JSON.stringify(this.settingsList)}`)
55      this.generalStatusValue = false
56    }
57    Log.info(`${this.TAG} aboutToAppear end`)
58  }
59
60  build() {
61    Row() {
62      Flex({
63        direction: FlexDirection.Row,
64        alignItems: ItemAlign.Center,
65        justifyContent: FlexAlign.SpaceBetween
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            .selectedColor('#1095E8')
100            .onChange((isOn: boolean) => {
101              Log.info(this.TAG + ' SettingItemToggle onChange for Wlan Enable:' + isOn)
102              this.isOn = new Boolean(isOn).valueOf()
103              let setToggle: string = "0"
104              if (this.isOn) {
105                setToggle = "1"
106              } else {
107                setToggle = "0"
108              }
109              this.settingManager.setSettingValue(this.itemValue.settingAlias, setToggle, this.state.mode)
110            })
111        }.margin({ left: $r("sys.float.ohos_id_card_margin_end") })
112      }
113      .height("100%")
114    }
115    .padding({ left: 12, right: 12 })
116    .height(this.itemValue?.description ? 72 : 56)
117    .width(this.WH_100_100)
118  }
119}