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