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