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 SetToggle {
36  private TAG: string = '[SetToggle]:';
37  @Link settingsList: SettingGroupItem[];
38  @State isOn: boolean = false;
39  private itemValue: SettingData = {};
40  private WH_100_100: string = "100%";
41  private settingManager = SettingManager.getInstance();
42  @State generalStatusValue: boolean = false;
43  @State state: StateStruct = new StateStruct();
44  private mAction: SetToggleDispatcher = new SetToggleDispatcher();
45
46  aboutToAppear() {
47    Log.info(`${this.TAG} aboutToAppear start`)
48    getStore().subscribe((state: OhCombinedState) => {
49      this.state = {
50        mode: state.modeReducer.mode
51      };
52    }, (dispatch: Dispatch) => {
53      this.mAction.setDispatch(dispatch);
54    });
55
56    try {
57      if (this.settingManager.getSettingValue(this.itemValue.settingAlias) === "1") {
58        this.generalStatusValue = true
59      } else {
60        this.generalStatusValue = false
61      }
62    } catch {
63      Log.info(`${this.TAG} aboutToAppear settingsList ${JSON.stringify(this.settingsList)}`)
64      this.generalStatusValue = false
65    }
66    Log.info(`${this.TAG} aboutToAppear end`)
67  }
68
69  build() {
70    Row() {
71      Flex({
72        direction: FlexDirection.Row,
73        alignItems: ItemAlign.Center,
74        justifyContent: FlexAlign.SpaceBetween
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            .selectedColor('#1095E8')
109            .onChange((isOn: boolean) => {
110              Log.info(this.TAG + ' SettingItemToggle onChange for Wlan Enable:' + isOn)
111              this.isOn = new Boolean(isOn).valueOf()
112              let setToggle: string = "0"
113              if (this.isOn) {
114                setToggle = "1"
115              } else {
116                setToggle = "0"
117              }
118              this.settingManager.setSettingValue(this.itemValue.settingAlias, setToggle, this.state.mode)
119            })
120        }.margin({ left: $r('sys.float.ohos_id_card_margin_end') })
121      }
122      .height('100%')
123    }
124    .padding({ left: 12, right: 12 })
125    .height(this.itemValue?.description ? 72 : 56)
126    .width(this.WH_100_100)
127  }
128}