• 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 { Log } from '../../../utils/Log';
17import { SettingManager } from '../../../setting/SettingManager';
18import { SettingData, SettingGroupItem } from '../model/SettingData';
19import { Dispatch, getStore, OhCombinedState } from '../../../redux/store';
20
21class StateStruct {
22  mode: string = '';
23}
24
25class SetToggleDispatcher {
26  public setDispatch(dispatch: Dispatch) {
27    this.mDispatch = dispatch;
28  }
29
30  private mDispatch: Dispatch = (data) => data;
31}
32
33@Component
34export struct SetToggle {
35  @Link settingsList: SettingGroupItem[];
36  @State isOn: boolean = false;
37  @State generalStatusValue: boolean = false;
38  @State state: StateStruct = new StateStruct();
39  private TAG: string = '[SetToggle]:';
40  private itemValue: SettingData = {};
41  private WH_100_100: string = "100%";
42  private settingManager = SettingManager.getInstance();
43  private mAction: SetToggleDispatcher = new SetToggleDispatcher();
44
45  aboutToAppear() {
46    Log.info(`${this.TAG} aboutToAppear start`)
47    getStore().subscribe((state: OhCombinedState) => {
48      this.state = {
49        mode: state.modeReducer.mode
50      };
51    }, (dispatch: Dispatch) => {
52      this.mAction.setDispatch(dispatch);
53    });
54
55    try {
56      if (this.settingManager.getSettingValue(this.itemValue.settingAlias) === "1") {
57        this.generalStatusValue = true
58      } else {
59        this.generalStatusValue = false
60      }
61    } catch {
62      Log.info(`${this.TAG} aboutToAppear settingsList ${JSON.stringify(this.settingsList)}`)
63      this.generalStatusValue = false
64    }
65    Log.info(`${this.TAG} aboutToAppear end`)
66  }
67
68  build() {
69    Row() {
70      Flex({
71        direction: FlexDirection.Row,
72        alignItems: ItemAlign.Center,
73        justifyContent: FlexAlign.SpaceBetween
74      }) {
75        Column() {
76          Row() {
77            Image(this.itemValue.imagePath)
78              .width(24)
79              .height(24)
80              .fillColor('#FFFFFF')
81            Text(this.itemValue.settingName)
82              .fontColor('#FFFFFF')
83              .fontSize($r('sys.float.ohos_id_text_size_sub_title2'))
84              .fontWeight(FontWeight.Regular)
85              .margin({ left: $r('sys.float.ohos_id_elements_margin_horizontal_l') })
86          }
87
88          if (this.itemValue?.description) {
89            Row() {
90              Text(this.itemValue.description)
91                .fontColor('#FFFFFF')
92                .fontSize($r('sys.float.ohos_id_text_size_body2'))
93                .fontWeight(FontWeight.Regular)
94                .opacity($r('sys.float.ohos_id_alpha_content_secondary'))
95            }.margin({
96              top: $r('sys.float.ohos_id_text_margin_vertical'),
97              left: 40
98            })
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}