• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2021-2023 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 { Constants } from '../common/Constants';
17import { FormController } from './FormController';
18import { BigDataConstants, DataStoreUtil, Log, ReportToBigDataUtil } from '@ohos/common';
19
20const TAG: string = 'formA_FormControllerManager';
21
22export class FormControllerManager {
23  private formControllerMap = new Map();
24  private dataStore = DataStoreUtil.getInstance();
25
26  private constructor() {
27    Log.info(TAG, 'new FormControllerManager');
28  }
29
30  public static getInstance(): FormControllerManager {
31    if (AppStorage.Get(Constants.FROM_CONTROLLER_MANAGER) == null) {
32      AppStorage.setOrCreate(Constants.FROM_CONTROLLER_MANAGER, new FormControllerManager());
33    }
34    return AppStorage.Get(Constants.FROM_CONTROLLER_MANAGER);
35  }
36
37  public createFormController(formId: string, operationMode: number, callback?: Function): FormController {
38    Log.debug(TAG, 'createFormController start!');
39    if (formId == '0') {
40      Log.info(TAG, 'formId is 0 or formName is null!');
41      return null;
42    }
43    let controller = new FormController(formId, operationMode, callback);
44
45    if (controller == null || controller == undefined) {
46      Log.error(TAG, 'It is failed to create FormController!');
47      return null;
48    }
49    this.formControllerMap.set(formId, controller);
50    Log.debug(TAG, 'createFormController end!');
51    return controller;
52  }
53
54  async initData(formId: string, operationMode: number, callback?: Function): Promise<void> {
55    Log.debug(TAG, `initData start! operationMode: ${operationMode} formId: ${formId}`);
56    try {
57      await this.dataStore.init();
58      let formIdKey: string = 'formId_' + formId;
59      let hasFormId = await this.dataStore.hasData(formIdKey);
60      Log.debug(TAG, `FormControllerManager initData of hasFormId ${formId} is ${hasFormId}`);
61      if (hasFormId) {
62        this.createFormController(formId, operationMode, callback);
63      }
64    } catch (err) {
65      Log.error(TAG, `init err ${err}`);
66      let msg = {
67        'CatchError': JSON.stringify(err)
68      }
69      ReportToBigDataUtil.errEventReport(BigDataConstants.FA_CARD_ERROR, msg);
70    }
71    Log.debug(TAG, 'initData end!');
72  }
73
74  public destroyController(formId: string): void {
75    this.initData(formId, Constants.PHOTOS_FORM_OPERATION_MODE_DESTROY);
76  }
77
78  public updateController(formId: string, callback?: Function): void {
79    this.initData(formId, Constants.PHOTOS_FORM_OPERATION_MODE_UPDATE, callback);
80  }
81
82  public onEvent(formId: string, message: string): void {
83    this.initData(formId, Constants.PHOTOS_FORM_OPERATION_MODE_EVENT, function (): string {
84      return message;
85    })
86  }
87
88  public onCallback(formId: string, callback: Function): void {
89    this.initData(formId, Constants.PHOTOS_FORM_OPERATION_MODE_CALLBACK, callback);
90  }
91
92  public getController(formId: string): FormController {
93    Log.debug(TAG, 'getController start!');
94    let controller: FormController = this.formControllerMap.get(formId);
95    if (controller == null || controller == undefined) {
96      Log.info(TAG, `has no controller with formid ${formId}`);
97      return null;
98    }
99    Log.debug(TAG, 'getController end!');
100    return controller;
101  }
102
103  public deleteFormController(formId: string): void {
104    Log.debug(TAG, 'deleteFormController start!')
105    if (this.formControllerMap.has(formId)) {
106      let ret = this.formControllerMap.delete(formId);
107      if (ret) {
108        Log.info(TAG, 'It is successful to delete FormController');
109      } else {
110        Log.error(TAG, 'It is failed to delete FormController');
111      }
112    } else {
113      Log.info(TAG, `deleteFormController, It has no controller with formid ${formId}`);
114    }
115
116    Log.debug(TAG, 'deleteFormController end!');
117  }
118}