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