• 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, { unsubscribe } from '../../../../../../../../common/src/main/ets/default/event/EventManager'
18import CheckEmptyUtils from '../../../../../../../../common/src/main/ets/default/CheckEmptyUtils'
19import { WINDOW_SHOW_HIDE_EVENT } from '../../../../../../../../common/src/main/ets/default/WindowManager'
20import { PluginType,itemData } from "../../../../../../../../common/src/main/ets/plugindatasource/common/Constants"
21import ViewModel from '../viewmodel/StatusBarVM'
22import StyleConfigurationCommon, { CommonStyle
23} from '../../../../../../../../common/src/main/ets/default/StyleConfiguration'
24import StyleConfiguration, { IconItemComponentStyle } from '../common/StyleConfiguration'
25import MetaIconItemComponent from './MetaIconItemComponent'
26import PluginIconItemComponent from './PluginIconItemComponent'
27
28const TAG = 'IconItemComponent'
29
30class itemStatusData{
31  selected: boolean
32}
33
34@Component
35export default struct IconItemComponent {
36  private keyId: string
37  @State mItemData: itemData = new itemData()
38  @State mItemStatus: itemStatusData = {
39    selected: false
40  }
41  mClearCallback: unsubscribe | undefined
42  @State style: IconItemComponentStyle = StyleConfiguration.getIconItemComponentStyle()
43  @State styleCommon: CommonStyle = StyleConfigurationCommon.getCommonStyle()
44  @State mHasMarginLR: boolean = false
45
46  aboutToAppear() {
47    Log.showInfo(TAG, `aboutToAppear Start, keyId: ${this.keyId} `);
48    this.linkItemData()
49    if (this.mItemData?.relationWindowId) {
50      this.mClearCallback = EventManager.subscribe(WINDOW_SHOW_HIDE_EVENT, (args) => this.onWindowChange(args))
51    }
52  }
53
54  aboutToDisappear() {
55    Log.showInfo(TAG, `aboutToDisappear`);
56    this.mClearCallback && this.mClearCallback()
57    this.mClearCallback = undefined
58  }
59
60  linkItemData() {
61    this.mItemData = AppStorage.Get('StatusBar_' + this.keyId)
62    Log.showInfo(TAG, `linkItemData, mItemData: ${this.keyId} ${this.mItemData.label} ${
63    this.mItemData
64    .iconUrl}`)
65
66    const statusBarStatusKey = 'StatusBar_Status_' + this.keyId
67    AppStorage.SetOrCreate(statusBarStatusKey, { selected: false })
68    this.mItemStatus = AppStorage.Get(statusBarStatusKey)
69//    this.mItemStatus = AppStorage.SetAndLink('StatusBar_Status_' + this.keyId, { selected: false }).get()
70  }
71
72  build() {
73    Row() {
74      if (this.mHasMarginLR) {
75        Row().width(this.style.marginLeft).height('100%')
76      }
77      Row() {
78        if (this.mItemData.pluginType == PluginType.META) {
79          MetaIconItemComponent({
80            keyId: this.keyId
81          })
82        } else if (this.mItemData.pluginType == PluginType.DATA_ABILITY) {
83          // TODO:
84        } else if (this.mItemData.pluginType == PluginType.PLUGIN_COMPONENT) {
85          PluginIconItemComponent({
86            keyId: this.keyId
87          })
88        }
89      }
90      .height(this.style.stackHeight)
91      .borderRadius(this.style.stackBorderRadius)
92      .backgroundColor(this.mItemStatus.selected ? this.style.stackBgColorSelected : this.style.stackBgColorUnSelected)
93      .onClick(this.onIconItemClick.bind(this))
94      .gesture(LongPressGesture({ repeat: false }).onAction(this.onIconItemLongPressGesture.bind(this)))
95      .onTouch(this.onIconItemTouch.bind(this))
96      .onAreaChange((e, e2) => {
97        this.mHasMarginLR = e2.width > 0
98      })
99
100      if (this.mHasMarginLR) {
101        Row().width(this.style.marginRight).height('100%')
102      }
103    }.height('100%')
104  }
105
106  onWindowChange(args) {
107    if (this.mItemData.canSelect && args?.windowName == this.mItemData.relationWindowId) {
108      this.mItemStatus.selected = args?.isShow
109    }
110  }
111
112  onIconItemClick(event: ClickEvent) {
113    Log.showInfo(TAG, `onIconItemClick`);
114    if (!this.mItemStatus.selected) {
115      this.execClickAction()
116    } else {
117      this.execSelectedClickAction()
118    }
119  }
120
121  execClickAction() {
122    Log.showInfo(TAG, `execClickAction`);
123    if (this.mItemData?.actionData?.clickAction) {
124      EventManager.publish(this.mItemData.actionData.clickAction)
125    }
126  }
127
128  execSelectedClickAction() {
129    Log.showInfo(TAG, `execSelectedClickAction ${this.mItemData?.actionData?.selectedClickAction}`);
130    if (this.mItemData?.actionData?.selectedClickAction) {
131      EventManager.publish(this.mItemData.actionData.selectedClickAction)
132    }
133  }
134
135  onIconItemLongPressGesture(event: GestureEvent) {
136    Log.showInfo(TAG, `onIconItemLongPressGesture, event: ${JSON.stringify(event)}`);
137    let longClickEvent = this.mItemData.actionData.longClickAction;
138    longClickEvent && EventManager.publish(longClickEvent);
139  }
140
141  onIconItemTouch(event: TouchEvent) {
142    Log.showInfo(TAG, `onIconItemTouch, event: ${JSON.stringify(event)}`);
143    if (this.mItemData?.actionData?.clickAction || this.mItemData?.actionData?.longClickAction) {
144      event.stopPropagation()
145    }
146  }
147}