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 TabletSetToggle { 35 @Link settingsList: SettingGroupItem[] 36 @State isOn: boolean = false 37 @State generalStatusValue: boolean = false 38 @State state: StateStruct = new StateStruct() 39 private TAG: string = '[TabletSetToggle]:' 40 private itemValue: SettingData = new SettingData(); 41 // private getToggleStatus: Promise<string> 42 private WH_100_100: string = "100%" 43 private settingManager = SettingManager.getInstance() 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 }) { 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 .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({ 123 left: 12, 124 right: 12, 125 top: 4, 126 bottom: 4 127 }) 128 } 129 .height(this.itemValue?.description ? 72 : 56) 130 .width(this.WH_100_100) 131 } 132}