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 delete this.mDialogController; 62 this.mDialogController = null; 63 this.showAppCenter = null; 64 } 65 66 private mDialogController: CustomDialogController = new CustomDialogController({ 67 builder: UninstallDialog({ 68 cancel: () => { 69 this.mDialogController.close() 70 }, 71 confirm: () => { 72 this.onConfirm() 73 }, 74 dialogName: this.dialogName, 75 dialogContent: this.mSelectedItem.appName + ' ?', 76 }), 77 cancel: () => { 78 }, 79 autoCancel: false, 80 customStyle: true 81 }); 82 83 onConfirm() { 84 if (this.mSelectedItem == null || this.mSelectedDockType == null) { 85 Log.showDebug(TAG, 'onConfirm click menu item null!'); 86 return; 87 } 88 if (this.deviceType === CommonConstants.PAD_DEVICE_TYPE) { 89 this.mSmartDockViewModel.deleteDockItem({ 90 bundleName: undefined, 91 keyName: this.mSelectedItem.keyName 92 }, this.mSelectedDockType); 93 } else { 94 this.mSmartDockViewModel.uninstallApp(this.mSelectedItem.bundleName, this.mSelectedItem.isUninstallAble); 95 } 96 this.mDialogController.close(); 97 } 98 99 showDialog() { 100 this.mSelectedItem = this.mSmartDockViewModel.getSelectedItem(); 101 this.mSelectedDockType = this.mSmartDockViewModel.getSelectedDockType(); 102 this.mDialogController.open(); 103 } 104 105 async onHoverEvent(onHover, bundleName) { 106 if (!onHover) { 107 this.popup = { show: false, showItem: '', popup: null }; 108 return; 109 } 110 111 let list = this.missionInfoList.filter(it => it.bundleName == bundleName); 112 if (list.length <= 0) { 113 AppStorage.SetOrCreate('snapshotList', []); 114 AppStorage.SetOrCreate('snapShotWidth', 0); 115 this.popup = { show: false, showItem: '', popup: null }; 116 return; 117 } 118 this.popup = { 119 show: true, 120 showItem: bundleName, 121 popup: { 122 builder: null, 123 placement: Placement.Top, 124 enableArrow: true 125 } 126 } 127 await this.mSmartDockViewModel.getSnapshot(list[0].missionInfoList, list[0].appName); 128 } 129 130 private buildLog(): boolean { 131 Log.showDebug(TAG, 'SmartDock buildLog'); 132 return true; 133 } 134 135 build() { 136 List({ 137 space: this.deviceType == CommonConstants.DEFAULT_DEVICE_TYPE || this.recentList.length == 0 ? CommonConstants.DOCK_SPACE : 138 this.mSmartDockViewModel.getStyleConfig().mDockGap 139 }) { 140 if (this.buildLog()) { 141 } 142 ListItem() { 143 ResidentLayout({ 144 mSmartDockStyleConfig: this.mSmartDockViewModel.getStyleConfig(), 145 onItemClick: (event, item) => this.mSmartDockViewModel.residentOnClick(event, item, this.showAppCenter), 146 buildMenu: ((item) => this.mSmartDockViewModel.buildMenuInfoList(item, SmartDockConstants.RESIDENT_DOCK_TYPE, this.showAppCenter, this.showDialog.bind(this))).bind(this.mSmartDockViewModel), 147 onDockListChangeFunc: () => this.mSmartDockViewModel.onDockListChange(), 148 }) 149 } 150 151 ListItem() { 152 RecentLayout({ 153 appList: $recentList, 154 mSmartDockStyleConfig: this.mSmartDockViewModel.getStyleConfig(), 155 onItemClick: (event, item) => this.mSmartDockViewModel.recentOnClick(event, item, () => this.recentOnClickWithPopup(item)), 156 buildMenu: ((item) => this.mSmartDockViewModel.buildMenuInfoList(item, SmartDockConstants.RECENT_DOCK_TYPE, this.showAppCenter, this.showDialog.bind(this))).bind(this.mSmartDockViewModel), 157 onDockListChangeFunc: () => this.mSmartDockViewModel.onDockListChange(), 158 popup: this.popup, 159 onHoverEvent: (onHover, bundleName) => this.onHoverEvent(onHover, bundleName) 160 }) 161 } 162 } 163 .alignListItem(ListItemAlign.Center) 164 .height(SmartDockConstants.PERCENTAGE_100) 165 .listDirection(SmartDockConstants.LIST_DIRECTION) 166 } 167 168 recentOnClickWithPopup(item) { 169 this.onHoverEvent(true, item.bundleName); 170 AppStorage.SetOrCreate('recentShowPopup', true); 171 } 172}