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 */ 15import { 16 Log, 17 FormModel, 18 SettingsModel, 19 PageDesktopModel, 20 CommonConstants, 21 layoutConfigManager, 22 FormListInfoCacheManager, 23 CardItemInfo, 24 LauncherDragItemInfo, 25 GridLayoutInfo 26} from '@ohos/common'; 27import { FormStyleConfig } from '../common/FormStyleConfig'; 28import FeatureConstants from '../common/constants/FeatureConstants'; 29 30const TAG = 'FormViewModel'; 31 32/** 33 * Class FormViewModel. 34 */ 35export class FormViewModel { 36 private readonly mFormModel: FormModel; 37 private readonly mSettingsModel: SettingsModel; 38 private readonly mPageDesktopModel: PageDesktopModel; 39 private readonly mFormStyleConfig: FormStyleConfig; 40 private readonly mFormListInfoCacheManager: FormListInfoCacheManager; 41 private mAllFormsInfo?: CardItemInfo[]; 42 private KEY_FORM_LIST:string = 'formListInfo'; 43 44 private constructor() { 45 Log.showInfo(TAG, 'constructor start'); 46 this.mFormModel = FormModel.getInstance(); 47 this.mSettingsModel = SettingsModel.getInstance(); 48 this.mPageDesktopModel = PageDesktopModel.getInstance(); 49 this.mFormStyleConfig = layoutConfigManager.getStyleConfig(FormStyleConfig.APP_LIST_STYLE_CONFIG, 50 FeatureConstants.FEATURE_NAME); 51 this.mFormListInfoCacheManager = FormListInfoCacheManager.getInstance(); 52 } 53 54 /** 55 * Get the form view model object. 56 * 57 * @return {object} form view model singleton 58 */ 59 static getInstance(): FormViewModel { 60 if (globalThis.FormViewModelInstance == null) { 61 globalThis.FormViewModelInstance = new FormViewModel(); 62 } 63 return globalThis.FormViewModelInstance; 64 } 65 66 /** 67 * Get the form style config info. 68 * 69 * @return {object} mFormStyleConfig - get the style from layout config manager. 70 */ 71 getFormStyleConfig(): FormStyleConfig { 72 return this.mFormStyleConfig; 73 } 74 75 /** 76 * Judge whether the current application supports form 77 * 78 * @param {any} appInfo 79 */ 80 async isSupportForm(appInfo: LauncherDragItemInfo) { 81 const formInfoList = await this.mFormModel.getAllFormsInfo(); 82 const formInfo: CardItemInfo = formInfoList.find(item => { 83 if (item.bundleName === appInfo.bundleName) { 84 return true; 85 } 86 }); 87 let isSupportForm = false; 88 if (formInfo) { 89 isSupportForm = true; 90 } 91 return isSupportForm; 92 } 93 94 /** 95 * Obtains the FormInfo objects provided by all ohos applications on the device. 96 */ 97 async getForms() { 98 Log.showDebug(TAG, 'getForms start'); 99 this.mAllFormsInfo = await this.mFormModel.getAllFormsInfo(); 100 AppStorage.setOrCreate('allFormsInfo', this.mAllFormsInfo); 101 } 102 103 /** 104 * Delete form by cardId. 105 * 106 * @param {number} cardId. 107 */ 108 async deleteForm(cardId) { 109 Log.showDebug(TAG, 'deleteForm start'); 110 let gridLayoutInfo: GridLayoutInfo = { 111 layoutDescription: undefined, 112 layoutInfo: [] 113 }; 114 gridLayoutInfo = this.mSettingsModel.getLayoutInfo(); 115 const cardIndex = gridLayoutInfo.layoutInfo.findIndex(item => { 116 return item.typeId === CommonConstants.TYPE_CARD && item.cardId === cardId; 117 }); 118 if (cardIndex != CommonConstants.INVALID_VALUE) { 119 this.mFormModel.deleteFormById(cardId); 120 const page = gridLayoutInfo.layoutInfo[cardIndex].page; 121 gridLayoutInfo.layoutInfo.splice(cardIndex, 1); 122 let ret: boolean = this.mPageDesktopModel.deleteBlankPageFromLayoutInfo(gridLayoutInfo, page); 123 this.mSettingsModel.setLayoutInfo(gridLayoutInfo); 124 if(ret){ 125 const curPageIndex = this.mPageDesktopModel.getPageIndex(); 126 Log.showInfo(TAG, 'deleteForm' + curPageIndex); 127 this.mPageDesktopModel.setPageIndex(curPageIndex - 1); 128 } 129 } 130 const formInfoList: any = this.mFormListInfoCacheManager.getCache(this.KEY_FORM_LIST); 131 if (formInfoList === CommonConstants.INVALID_VALUE) { 132 return; 133 } 134 for(let i = 0; i < formInfoList.length; i++) { 135 if (formInfoList[i].cardId === cardId){ 136 formInfoList.splice(i, 1); 137 break; 138 } 139 } 140 if (formInfoList.length === 0) { 141 this.mFormListInfoCacheManager.setCache(this.KEY_FORM_LIST, null); 142 } else { 143 this.mFormListInfoCacheManager.setCache(this.KEY_FORM_LIST, formInfoList); 144 } 145 } 146}