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 '@ohos/common'; 17import { PinyinSort } from '@ohos/common'; 18import { BaseViewModel } from '@ohos/common'; 19import { layoutConfigManager, localEventManager, EventConstants } from '@ohos/common'; 20import AppcenterConstants from '../common/constants/AppcenterConstants'; 21import AppCenterGridStyleConfig from '../common/AppCenterGridStyleConfig'; 22 23const TAG = 'AppListViewModel'; 24 25interface AnimationInfo { 26 appScaleX: number; 27 appScaleY: number; 28} 29 30const KEY_NAME = "name"; 31 32export class AppListViewModel extends BaseViewModel { 33 private mPinyinSort: PinyinSort; 34 private mAppGridStyleConfig: AppCenterGridStyleConfig; 35 36 protected constructor() { 37 super(); 38 this.mPinyinSort = new PinyinSort(); 39 this.mAppGridStyleConfig = layoutConfigManager.getStyleConfig(AppCenterGridStyleConfig.APP_GRID_STYLE_CONFIG, AppcenterConstants.FEATURE_NAME); 40 } 41 42 public static getInstance(): AppListViewModel { 43 if (globalThis.AppListViewModel == null) { 44 globalThis.AppListViewModel = new AppListViewModel(); 45 } 46 return globalThis.AppListViewModel; 47 } 48 49 private readonly mLocalEventListener = { 50 onReceiveEvent: (event, params) => { 51 Log.showInfo(TAG, `localEventListener receive event: ${event}, params: ${JSON.stringify(params)}`); 52 if (event === EventConstants.EVENT_BADGE_UPDATE) { 53 this.updateBadgeNum(params); 54 } else if (event === EventConstants.EVENT_ANIMATION_START_APPLICATION) { 55 this.startAppAnimation(); 56 } else if (event === EventConstants.EVENT_ANIMATION_CLOSE_APPLICATION) { 57 this.closeAppAnimation(); 58 } 59 } 60 } 61 62 private startAppAnimation() { 63 animateTo({ 64 duration: 500, 65 curve: Curve.Friction, 66 onFinish: () => { 67 } 68 }, () => { 69 let animationInfo: AnimationInfo = { 70 appScaleX: 0.97, 71 appScaleY: 0.97 72 } 73 AppStorage.setOrCreate('animationInfo_scale', animationInfo); 74 }) 75 } 76 77 private closeAppAnimation() { 78 AppStorage.setOrCreate('animationInfo_alpha', 0.0); 79 animateTo({ 80 duration: 140, 81 delay: 210, 82 curve: Curve.Linear, 83 }, () => { 84 AppStorage.setOrCreate('animationInfo_alpha', 1.0); 85 }) 86 87 let scale = { 88 appScaleX: 0.9, 89 appScaleY: 0.9 90 } 91 AppStorage.setOrCreate('animationInfo_scale', scale); 92 animateTo({ 93 duration: 490, 94 delay: 210, 95 curve: Curve.Friction, 96 }, () => { 97 let scale_finish = { 98 appScaleX: 1.0, 99 appScaleY: 1.0 100 } 101 AppStorage.setOrCreate('animationInfo_scale', scale_finish); 102 }) 103 } 104 105 /** 106 * Registering Listening Events. 107 */ 108 onAppListViewCreate(): void { 109 localEventManager.registerEventListener(this.mLocalEventListener, [ 110 EventConstants.EVENT_BADGE_UPDATE, 111 EventConstants.EVENT_ANIMATION_START_APPLICATION, 112 EventConstants.EVENT_ANIMATION_CLOSE_APPLICATION 113 ]); 114 } 115 116 /** 117 * Unregistering Listening Events. 118 */ 119 onAppListViewDestroy(): void { 120 localEventManager.unregisterEventListener(this.mLocalEventListener); 121 } 122 123 private async updateBadgeNum(badgeInfo) { 124 let appList = await this.mAppModel.getAppList(); 125 126 let appItem = appList.find(item => { 127 Log.showDebug(TAG, `updateBadgeNum appItem is ${JSON.stringify(item)}`); 128 return item.bundleName == badgeInfo.bundleName; 129 }); 130 131 appItem.badgeNumber = badgeInfo.badgeNumber; 132 appList.sort(this.mPinyinSort.sortByAppName.bind(this.mPinyinSort)); 133 AppStorage.setOrCreate('listInfo', appList); 134 } 135 136 public async getAppList(): Promise<void> { 137 let appList = await this.mAppModel.getAppList(); 138 appList.sort(this.mPinyinSort.sortByAppName.bind(this.mPinyinSort)); 139 AppStorage.setOrCreate('listInfo', appList); 140 } 141 142 public async regroupDataAppListChange(callbackList) { 143 for (let item of callbackList) { 144 let cacheKey = item.appLabelId + item.bundleName + item.moduleName; 145 let appName = this.mResourceManager.getAppResourceCache(cacheKey, KEY_NAME); 146 Log.showDebug(TAG, `regroupDataAppListChange appName: ${appName}`); 147 if (appName != null) { 148 item.appName = appName; 149 } else { 150 let loadAppName = await this.mResourceManager.getAppNameSync(item.appLabelId, item.bundleName, 151 item.moduleName, item.appName); 152 Log.showDebug(TAG, `regroupDataAppListChange loadAppName: ${loadAppName}`); 153 item.appName = loadAppName; 154 } 155 } 156 callbackList.sort(this.mPinyinSort.sortByAppName.bind(this.mPinyinSort)); 157 animateTo({ 158 duration: 200, 159 curve: Curve.EaseInOut, 160 delay: 100, 161 playMode: PlayMode.Normal, 162 tempo: 0.5, 163 iterations: 1, 164 onFinish: () => { 165 } 166 }, () => { 167 AppStorage.setOrCreate('listInfo', callbackList); 168 }) 169 } 170 171 public intoSetting() { 172 Log.showDebug(TAG, 'intoSetting'); 173 this.jumpToSetting(); 174 } 175 176 /** 177 * Open application function. 178 * 179 * @param {string} abilityName - ability name of the application to be jump to. 180 * @param {string} bundleName - bundle name of the application to be jump to. 181 */ 182 public openApplication(abilityName: string, bundleName: string, moduleName: string) { 183 Log.showDebug(TAG, `openApplication abilityName: ${abilityName}`); 184 this.jumpTo(abilityName, bundleName, moduleName); 185 } 186 187 public getAppGridStyleConfig(): AppCenterGridStyleConfig { 188 return this.mAppGridStyleConfig; 189 } 190}