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