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