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 { FASlotName } from '../../../../../../../../common/src/main/ets/default/Constants';
18import { TintContentInfo, getOrCreateTintContentInfo
19} from '../../../../../../../../common/src/main/ets/default/TintStateManager';
20import createOrGet from '../../../../../../../../common/src/main/ets/default/SingleInstanceHelper';
21import { AudioRingMode } from '../common/Constants';
22import RingModeService from '../model/RingModeService';
23
24export const RING_MODE_COMPONENT_MODE_KEY = 'RingModeComponentMode';
25
26const TAG = 'RingModeVM';
27
28export class RingModeVM {
29  mIsStart = false;
30  mRingModeComponentMode: SubscribedAbstractProperty<AudioRingMode>;
31  mTintContentInfo: TintContentInfo = getOrCreateTintContentInfo(FASlotName.RING_MODE);
32
33  constructor() {
34    Log.showInfo(TAG, 'constructor');
35  }
36
37  initViewModel(): void {
38    if (this.mIsStart) {
39      return;
40    }
41    Log.showInfo(TAG, 'initViewModel ');
42    this.mIsStart = true;
43
44    this.mRingModeComponentMode = AppStorage.SetAndLink(RING_MODE_COMPONENT_MODE_KEY, AudioRingMode.RINGER_MODE_NORMAL);
45
46    RingModeService.registerListener(this);
47    RingModeService.startService();
48  }
49
50  updateRingerMode(mode: AudioRingMode): void {
51    Log.showInfo(TAG, `updateRingerMode, mode: ${JSON.stringify(mode)} `);
52    this.mRingModeComponentMode.set(mode);
53  }
54
55  setRingerMode(mode: AudioRingMode): void {
56    Log.showInfo(TAG, `setRingerMode, mode: ${JSON.stringify(mode)} `);
57    RingModeService.setRingerMode(mode);
58  }
59
60  getTintContentInfo(): TintContentInfo {
61    return this.mTintContentInfo;
62  }
63}
64
65let sRingModeVM = createOrGet(RingModeVM, TAG);
66
67export default sRingModeVM;