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 parameter from '@ohos.systemparameter'
17import Log from '../../../../../../../../common/src/main/ets/default/Log';
18import { FASlotName } from '../../../../../../../../common/src/main/ets/default/Constants';
19import { TintContentInfo, getOrCreateTintContentInfo
20} from '../../../../../../../../common/src/main/ets/default/TintStateManager';
21import createOrGet from '../../../../../../../../common/src/main/ets/default/SingleInstanceHelper';
22import NFCModeService from '../model/NFCModeService';
23import NfcController  from '@ohos.nfc.controller';
24
25export const NFC_MODE_COMPONENT_MODE_KEY = 'NFCModeComponentMode';
26
27const TAG = 'NFCModeVM';
28
29export class NFCModeVM {
30  mIsStart = false;
31  mNFCModeComponentMode: SubscribedAbstractProperty<NfcController.NfcState>;
32  mTintContentInfo: TintContentInfo = getOrCreateTintContentInfo(FASlotName.NFC);
33
34  constructor() {
35    Log.showInfo(TAG, 'constructor');
36  }
37
38  initViewModel(): void {
39    if (this.mIsStart) {
40      return;
41    }
42    Log.showInfo(TAG, 'initViewModel ');
43    this.mIsStart = true;
44
45    this.mNFCModeComponentMode = AppStorage.SetAndLink(NFC_MODE_COMPONENT_MODE_KEY, NfcController.NfcState.STATE_OFF);
46
47    NFCModeService.registerListener(this);
48    NFCModeService.startService();
49  }
50
51  updateNFCMode(mode: NfcController.NfcState): void {
52    Log.showInfo(TAG, `updateNFCMode, mode: ${JSON.stringify(mode)} `);
53    this.mNFCModeComponentMode.set(mode);
54  }
55
56  setNFCMode(mode: NfcController.NfcState): void {
57    Log.showInfo(TAG, `setNFCMode, mode: ${JSON.stringify(mode)} `);
58    NFCModeService.setNFCMode(mode);
59  }
60
61  getTintContentInfo(): TintContentInfo {
62    return this.mTintContentInfo;
63  }
64
65  isSupported(): string {
66    return parameter.getSync("const.SystemCapability.Communication.NFC.Core", "false")
67  }
68}
69
70let sNFCModeVM = createOrGet(NFCModeVM, TAG);
71
72export default sNFCModeVM;