• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 SimpleToggleBase from '../../../../../../../../common/src/main/ets/template/SimpleToggleBase';
19import PluginIconItemComponent from './PluginIconItemComponent';
20import {PluginType} from "../../../../../../../../common/src/main/ets/plugindatasource/common/Constants"
21import { ControlComponentData } from '../common/Constants';
22
23const TAG = 'SimpleToggleComponent';
24
25@Component
26export default struct SimpleToggleComponent {
27  @Prop keyId: string;
28  @State mItemData: ControlComponentData = {};
29  private mEditMode: boolean = false;
30  private mDragMode: boolean = false;
31  @State mDefaultIcon: Resource = $r("app.media.icon");
32  @State mDefaultChangeSwitch: boolean  = false;
33  @State mDefaultLabel: string = '';
34
35  aboutToAppear() {
36    Log.showInfo(TAG, `aboutToAppear Start, keyId: ${this.keyId}, mEditMode: ${this.mEditMode} mDragMode: ${this.mDragMode}`);
37    this.linkItemData();
38  }
39
40  aboutToDisappear() {
41    Log.showInfo(TAG, `aboutToDisappear`);
42  }
43
44  linkItemData() {
45    Log.showDebug(TAG, `linkItemData, keyId: ${this.keyId}`);
46    this.mItemData = AppStorage.Get('ControlCenter_' + this.keyId) as ControlComponentData;
47    Log.showDebug(TAG, `linkItemData, mItemData: ${this.keyId} ${this.mItemData.label} ${this.mItemData.iconUrl}`);
48  }
49
50  getLabel() {
51    switch (this.keyId) {
52      case 'screenshot':
53        return $r('app.string.control_center_simple_toggle_screenshot_mode_title')
54    }
55    return this.mItemData.label;
56  }
57
58  build() {
59    Column() {
60      if (this.keyId != ""){
61        if (this.mItemData.pluginType == PluginType.META) {
62          SimpleToggleBase({
63            mToggleId: this.keyId,
64            mIcon: $mDefaultIcon,
65            mIconStr: this.mItemData.iconUrl,
66            mUseIconStr: true,
67            mChangeSwitch: $mDefaultChangeSwitch,
68            mLabel: $mDefaultLabel,
69            mLabelStr: this.getLabel(),
70            mUseLabelStr: true,
71            mEditMode: this.mEditMode,
72            mDragMode: this.mDragMode,
73            mClickEvent: (): void => this.onIconItemClick(),
74            mLongClickEvent: (): void => this.onIconItemLongPressGesture()
75          })
76        } else if (this.mItemData.pluginType == PluginType.DATA_ABILITY) {
77          // TODO:
78        } else if (this.mItemData.pluginType == PluginType.PLUGIN_COMPONENT) {
79          PluginIconItemComponent({
80            keyId: this.keyId,
81            mEditMode: this.mEditMode,
82            mDragMode: this.mDragMode
83          })
84        }
85      }
86    }
87    .width('100%')
88    .height('100%')
89  }
90
91  onIconItemClick() {
92    Log.showDebug(TAG, `onIconItemClick`);
93    if (this.mItemData?.actionData?.clickAction) {
94      if (this.mItemData.pluginType == PluginType.META) {
95        EventManager.publish(this.mItemData.actionData.clickAction, this.mItemData.pluginType);
96      } else {
97        EventManager.publish(this.mItemData.actionData.clickAction);
98      }
99    };
100  }
101
102  onIconItemLongPressGesture() {
103    Log.showDebug(TAG, `onIconItemLongPressGesture`);
104    if (this.mItemData?.actionData?.longClickAction) {
105      EventManager.publish(this.mItemData.actionData.longClickAction);
106    };
107  }
108}