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