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