1/**
2 * Copyright (c) 2022-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 { BaseStartAppHandler } from '@ohos/common';
17import { AppItemInfo } from '@ohos/common';
18import { layoutConfigManager } from '@ohos/common';
19import { Log } from '@ohos/common';
20import { CheckEmptyUtils } from '@ohos/common';
21import { BigFolderConstants } from '../common/constants/BigFolderConstants';
22import { BigFolderStyleConfig } from './BigFolderStyleConfig';
23
24const TAG = 'BigFolderStartAppHandler';
25
26/**
27 * open big folder start app processing class
28 */
29export default class BigFolderStartAppHandler extends BaseStartAppHandler {
30    private mBigFolderStyleConfig;
31
32    private constructor() {
33        super();
34        this.mBigFolderStyleConfig = layoutConfigManager.getStyleConfig(BigFolderStyleConfig.APP_LIST_STYLE_CONFIG,
35            BigFolderConstants.FEATURE_NAME);
36    }
37
38    static getInstance(): BigFolderStartAppHandler {
39        Log.showInfo(TAG, 'BigFolderStartAppHandler getInstance called!');
40        if (globalThis.BigFolderStartAppHandler == null) {
41            Log.showInfo(TAG, 'BigFolderStartAppHandler constructor');
42            globalThis.BigFolderStartAppHandler = new BigFolderStartAppHandler();
43        }
44        return globalThis.BigFolderStartAppHandler;
45    }
46
47    protected calculateAppIconPosition(): void {
48        Log.showInfo(TAG, 'calculateAppIconPosition called');
49        if (CheckEmptyUtils.isEmpty(this.mBigFolderStyleConfig)) {
50            Log.showError(TAG, 'calculateAppIconPosition with invalid config');
51            return;
52        }
53
54        const appItemInfo = AppStorage.get('startAppItemInfo');
55        const screenWidth: number = AppStorage.get('screenWidth');
56        const screenHeight: number = AppStorage.get('screenHeight');
57        const appGridWidth: number = this.mBigFolderStyleConfig.mOpenFolderGridWidth;
58        const appGridHeight: number = this.mBigFolderStyleConfig.mOpenFolderGridHeight;
59        const swiperHeight: number = this.mBigFolderStyleConfig.mOpenFolderSwiperHeight;
60        const appGridPadding: number = this.mBigFolderStyleConfig.mOpenFolderGridPadding;
61        const titleHeight: number = this.mBigFolderStyleConfig.mFolderOpenMargin;
62        const titleTopMargin = this.mBigFolderStyleConfig.mFolderOpenTitle;
63        const appItemSize: number = this.mBigFolderStyleConfig.mOpenFolderAppSize;
64        const appIconSize: number = this.mBigFolderStyleConfig.mOpenFolderIconSize;
65        const bigFolderColumns: number = this.mBigFolderStyleConfig.mOpenFolderGridColumn;
66        const bigFolderRows: number = this.mBigFolderStyleConfig.mOpenFolderGridRow
67
68        let startPositionX: number = (screenWidth - appGridWidth) / 2;
69        let startPositionY: number = titleTopMargin + titleHeight + appGridPadding;
70        const index: number = this.getIndexInAppList(appItemInfo);
71        let row: number = Math.floor(index / bigFolderColumns);
72        let column: number = index % bigFolderColumns;
73        if (column != 0) {
74            row += 1;
75        } else {
76            column = bigFolderColumns;
77        }
78        Log.showInfo(TAG, `calculateAppIconPosition index ${index} row ${row} column ${column}`);
79        let columnsGap = this.mBigFolderStyleConfig.mOpenFolderGridGap;
80        let rowsGap = this.mBigFolderStyleConfig.mOpenFolderGridGap - this.mBigFolderStyleConfig.mOpenFolderGridIconTopPadding;
81        let appItemWidth = (appGridWidth - columnsGap * (bigFolderColumns - 1)) / bigFolderColumns;
82        let appItemHeight = (appGridHeight - rowsGap * (bigFolderRows - 1)) / bigFolderRows;
83        let itemLeftMargin = (appItemWidth - appIconSize) / 2;
84        this.mAppIconPositionX = startPositionX + (column - 1) * (appItemWidth + columnsGap) + itemLeftMargin;
85        this.mAppIconPositionY = startPositionY + (row - 1) * (appItemHeight + rowsGap)
86        + this.mBigFolderStyleConfig.mOpenFolderGridIconTopPadding;
87    }
88
89    private getIndexInAppList(appItemInfo): number {
90        let index: number = 0;
91        let folderInfo: {
92            layoutInfo: AppItemInfo[][],
93            enterEditing: boolean,
94            folderName: string,
95            folderId: string
96        } = AppStorage.get('openFolderData');
97        for (var i = 0; i < folderInfo.layoutInfo.length; i++) {
98            for (var j = 0; j < folderInfo.layoutInfo[i].length; j++) {
99                if (appItemInfo.bundleName === folderInfo.layoutInfo[i][j]?.bundleName) {
100                    index = j;
101                    break;
102                }
103            }
104        }
105
106        return index + 1;
107    }
108}
109