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