1/* 2 * Copyright (c) 2021-2022 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 '../../../../../../../../common/src/main/ets/default/Log'; 17import EventManager from "../../../../../../../../common/src/main/ets/default/event/EventManager"; 18import { obtainStartAbility } from "../../../../../../../../common/src/main/ets/default/event/EventUtil"; 19import NfcController from '@ohos.nfc.controller'; 20import ViewModel from '../viewmodel/NFCModeVM'; 21import SimpleToggleBase from '../../../../../../../../common/src/main/ets/template/SimpleToggleBase'; 22 23const TAG = 'nfcmode-ControlCenterSimpleToggleNFComponent'; 24 25@Component 26export default struct ControlCenterSimpleToggleNFComponent { 27 private keyId: string; 28 private mEditMode: boolean = false; 29 private mDragMode: boolean = false; 30 @State mIcon: Resource = $r("app.media.ic_notification_nfc_filled"); 31 @State mLabel: Resource = $r("app.string.control_center_complex_toggle_nfc_mode_title"); 32 @State mDefaultChangeSwitch: boolean = false; 33 @StorageLink('NFCModeComponentMode') @Watch('onNFCModeUpdated') NFCModeComponentMode: NfcController.NfcState = NfcController.NfcState.STATE_OFF; 34 aboutToAppear() { 35 Log.showInfo(TAG, 'aboutToAppear'); 36 ViewModel.initViewModel(); 37 this.onNFCModeUpdated('NFCModeComponentMode'); 38 } 39 40 aboutToDisappear() { 41 Log.showInfo(TAG, 'aboutToDisappear'); 42 } 43 44 onNFCModeUpdated(propName: string): void { 45 Log.showInfo(TAG, `onNFCModeUpdated, propName: ${propName} NFCModeComponentMode: ${JSON.stringify(this.NFCModeComponentMode)}`); 46 if (this.NFCModeComponentMode == NfcController.NfcState.STATE_OFF) { 47 this.mDefaultChangeSwitch = false; 48 } else if (this.NFCModeComponentMode == NfcController.NfcState.STATE_ON) { 49 this.mDefaultChangeSwitch = true; 50 } 51 } 52 53 build() { 54 SimpleToggleBase({ 55 mToggleId: this.keyId, 56 mIcon: $mIcon, 57 mChangeSwitch: $mDefaultChangeSwitch, 58 mLabel: $mLabel, 59 mEditMode: this.mEditMode, 60 mDragMode: this.mDragMode, 61 mClickEvent: () => this.mClickEvent() 62 }) 63 } 64 65 mClickEvent() { 66 Log.showInfo(TAG, `mClickEvent---${this.NFCModeComponentMode}`); 67 if (this.NFCModeComponentMode == NfcController.NfcState.STATE_OFF) { 68 ViewModel.setNFCMode(NfcController.NfcState.STATE_OFF) 69 } else if (this.NFCModeComponentMode == NfcController.NfcState.STATE_ON) { 70 ViewModel.setNFCMode(NfcController.NfcState.STATE_ON) 71 } 72 } 73}