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