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 { AudioRingMode } from '../common/Constants'
20import ViewModel from '../viewmodel/RingModeVM'
21import SimpleToggleBase from '../../../../../../../../common/src/main/ets/template/SimpleToggleBase'
22import StyleConfiguration, { ControlCenterRingModeComponentStyle } from '../common/StyleConfiguration'
23
24const TAG = 'ringmode-ControlCenterSimpleToggleRingModeComponent'
25
26@Component
27export default struct ControlCenterSimpleToggleRingModeComponent {
28  private keyId: string
29  private mEditMode: boolean = false
30  private mDragMode: boolean = false
31  @State mIcon: Resource = $r("app.media.ic_controlcenter_ring_on_filled")
32  @State mLabel: Resource = $r("app.string.control_center_complex_toggle_ring_mode_title_on")
33  @State mDefaultChangeSwitch: boolean  = true
34  @StorageLink('RingModeComponentMode') @Watch('onRingModeUpdated') RingModeComponentMode: AudioRingMode = AudioRingMode.RINGER_MODE_NORMAL
35  @State style: ControlCenterRingModeComponentStyle = StyleConfiguration.getControlCenterRingModeComponentStyle()
36
37  aboutToAppear() {
38    Log.showInfo(TAG, 'aboutToAppear');
39    ViewModel.initViewModel()
40    this.onRingModeUpdated('RingModeComponentMode')
41  }
42
43  aboutToDisappear() {
44    Log.showInfo(TAG, 'aboutToDisappear');
45  }
46
47  onRingModeUpdated(propName: string): void {
48    Log.showInfo(TAG, `onRingModeUpdated, propName: ${propName} RingModeComponentMode: ${JSON.stringify(this.RingModeComponentMode)}`);
49    if (this.RingModeComponentMode == AudioRingMode.RINGER_MODE_SILENT) {
50      this.mIcon = $r("app.media.ic_controlcenter_ring_off_filled")
51      this.mLabel = $r("app.string.control_center_complex_toggle_ring_mode_title_off")
52    } else if (this.RingModeComponentMode == AudioRingMode.RINGER_MODE_VIBRATE) {
53      this.mIcon = $r("app.media.ic_controlcenter_vibration_filled")
54      this.mLabel = $r("app.string.control_center_complex_toggle_ring_mode_title_vibration")
55    } else if (this.RingModeComponentMode == AudioRingMode.RINGER_MODE_NORMAL) {
56      this.mIcon = $r("app.media.ic_controlcenter_ring_on_filled")
57      this.mLabel = $r("app.string.control_center_complex_toggle_ring_mode_title_on")
58    }
59  }
60
61  build() {
62    SimpleToggleBase({
63      mToggleId: this.keyId,
64      mIcon: $mIcon,
65      mChangeSwitch: $mDefaultChangeSwitch,
66      mLabel: $mLabel,
67      mIconOnBG: this.style.onBgColor,
68      mEditMode: this.mEditMode,
69      mDragMode: this.mDragMode,
70      mClickEvent: () => this.mClickEvent(),
71      mLongClickEvent: () => this.mLongClickEvent()
72    })
73  }
74
75  mClickEvent() {
76    Log.showInfo(TAG, `mClickEvent`);
77    if (this.RingModeComponentMode == AudioRingMode.RINGER_MODE_SILENT) {
78      ViewModel.setRingerMode(AudioRingMode.RINGER_MODE_NORMAL)
79    } else if (this.RingModeComponentMode == AudioRingMode.RINGER_MODE_NORMAL) {
80      ViewModel.setRingerMode(AudioRingMode.RINGER_MODE_SILENT)
81    }
82  }
83
84  mLongClickEvent() {
85    Log.showInfo(TAG, `mLongClickEvent`);
86    EventManager.publish(obtainStartAbility('com.ohos.settings', 'com.ohos.settings.MainAbility'))
87  }
88}