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 { 17 Log, 18 CommonConstants, 19 ResourceManager, 20 UninstallDialog } from '@ohos/common'; 21import RecentLayout from './RecentLayout'; 22import ResidentLayout from './ResidentLayout'; 23import SmartDockViewModel from '../viewmodel/SmartDockViewModel'; 24import SmartDockConstants from '../common/constants/SmartDockConstants'; 25 26const TAG = 'SmartDock'; 27 28@Component 29export struct SmartDock { 30 popup: { 31 show: boolean, 32 showItem: string, 33 popup 34 } = { show: false, showItem: '', popup: null }; 35 showAppCenter: Function = () => {}; 36 @StorageLink('showDock') showDock: boolean = false; 37 @StorageLink('recentList') recentList: any = []; 38 @StorageLink('missionInfoList') missionInfoList: any = []; 39 private deviceType: string = CommonConstants.DEFAULT_DEVICE_TYPE; 40 private mSmartDockViewModel: SmartDockViewModel; 41 private mSelectedItem = null; 42 private mSelectedDockType = 0; 43 private dialogName = ""; 44 45 aboutToAppear(): void { 46 Log.showInfo(TAG, 'aboutToAppear start!'); 47 this.deviceType = AppStorage.Get('deviceType'); 48 try { 49 this.mSmartDockViewModel = SmartDockViewModel.getInstance(); 50 } catch (error) { 51 Log.showError(TAG, `catch error ${JSON.stringify(error)}`); 52 } 53 ResourceManager.getInstance().getStringByResource(this.deviceType === CommonConstants.PAD_DEVICE_TYPE 54 ? $r('app.string.is_delete_form') : $r('app.string.isUninstall')).then((resName) => { 55 this.dialogName = resName; 56 }); 57 } 58 59 aboutToDisappear(): void { 60 Log.showInfo(TAG, 'aboutToDisappear!'); 61 } 62 63 private mDialogController: CustomDialogController = new CustomDialogController({ 64 builder: UninstallDialog({ 65 cancel: () => { 66 this.mDialogController.close() 67 }, 68 confirm: () => { 69 this.onConfirm() 70 }, 71 dialogName: this.dialogName, 72 dialogContent: this.mSelectedItem.appName + ' ?', 73 }), 74 cancel: () => { 75 }, 76 autoCancel: false, 77 customStyle: true 78 }); 79 80 onConfirm() { 81 if (this.mSelectedItem == null || this.mSelectedDockType == null) { 82 Log.showDebug(TAG, 'onConfirm click menu item null!'); 83 return; 84 } 85 if (this.deviceType === CommonConstants.PAD_DEVICE_TYPE) { 86 this.mSmartDockViewModel.deleteDockItem({ 87 bundleName: undefined, 88 keyName: this.mSelectedItem.keyName 89 }, this.mSelectedDockType); 90 } else { 91 this.mSmartDockViewModel.uninstallApp(this.mSelectedItem.bundleName, this.mSelectedItem.isUninstallAble); 92 } 93 this.mDialogController.close(); 94 } 95 96 showDialog() { 97 this.mSelectedItem = this.mSmartDockViewModel.getSelectedItem(); 98 this.mSelectedDockType = this.mSmartDockViewModel.getSelectedDockType(); 99 this.mDialogController.open(); 100 } 101 102 async onHoverEvent(onHover, bundleName) { 103 if (!onHover) { 104 this.popup = { show: false, showItem: '', popup: null }; 105 return; 106 } 107 108 let list = this.missionInfoList.filter(it => it.bundleName == bundleName); 109 if (list.length <= 0) { 110 AppStorage.SetOrCreate('snapshotList', []); 111 AppStorage.SetOrCreate('snapShotWidth', 0); 112 this.popup = { show: false, showItem: '', popup: null }; 113 return; 114 } 115 this.popup = { 116 show: true, 117 showItem: bundleName, 118 popup: { 119 builder: null, 120 placement: Placement.Top, 121 enableArrow: true 122 } 123 } 124 await this.mSmartDockViewModel.getSnapshot(list[0].missionInfoList, list[0].appName); 125 } 126 127 private buildLog(): boolean { 128 Log.showDebug(TAG, 'SmartDock buildLog'); 129 return true; 130 } 131 132 build() { 133 List({ 134 space: this.deviceType == CommonConstants.DEFAULT_DEVICE_TYPE || this.recentList.length == 0 ? CommonConstants.DOCK_SPACE : 135 this.mSmartDockViewModel.getStyleConfig().mDockGap 136 }) { 137 if (this.buildLog()) { 138 } 139 ListItem() { 140 ResidentLayout({ 141 mSmartDockStyleConfig: this.mSmartDockViewModel.getStyleConfig(), 142 onItemClick: (event, item) => this.mSmartDockViewModel.residentOnClick(event, item, this.showAppCenter), 143 buildMenu: ((item) => this.mSmartDockViewModel.buildMenuInfoList(item, SmartDockConstants.RESIDENT_DOCK_TYPE, this.showAppCenter, this.showDialog.bind(this))).bind(this.mSmartDockViewModel), 144 onDockListChangeFunc: () => this.mSmartDockViewModel.onDockListChange(), 145 }) 146 } 147 148 ListItem() { 149 RecentLayout({ 150 appList: $recentList, 151 mSmartDockStyleConfig: this.mSmartDockViewModel.getStyleConfig(), 152 onItemClick: (event, item) => this.mSmartDockViewModel.recentOnClick(event, item, () => this.recentOnClickWithPopup(item)), 153 buildMenu: ((item) => this.mSmartDockViewModel.buildMenuInfoList(item, SmartDockConstants.RECENT_DOCK_TYPE, this.showAppCenter, this.showDialog.bind(this))).bind(this.mSmartDockViewModel), 154 onDockListChangeFunc: () => this.mSmartDockViewModel.onDockListChange(), 155 popup: this.popup, 156 onHoverEvent: (onHover, bundleName) => this.onHoverEvent(onHover, bundleName) 157 }) 158 } 159 } 160 .alignListItem(ListItemAlign.Center) 161 .height(SmartDockConstants.PERCENTAGE_100) 162 .listDirection(SmartDockConstants.LIST_DIRECTION) 163 } 164 165 recentOnClickWithPopup(item) { 166 this.onHoverEvent(true, item.bundleName); 167 AppStorage.SetOrCreate('recentShowPopup', true); 168 } 169}