• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 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 */
15
16import FormExtension from '@ohos.app.form.FormExtensionAbility';
17import { Log } from '@ohos/base/src/main/ets/utils/Log';
18import { FormControllerManager } from './controller/FormControllerManager';
19import { FormController } from './controller/FormController';
20import { Constants } from './common/Constants';
21import mediaModel from '@ohos/base/src/main/ets/model/MediaModel';
22import  DataStoreUtil  from '@ohos/base/src/main/ets/utils/DataStoreUtil';
23
24export default class FormAbility extends FormExtension {
25    private TAG: string = 'FormAbility';
26    onAddForm(want) {
27        Log.info(this.TAG, `form onAddForm. want ${JSON.stringify(want)}`);
28        this.init();
29        let param = want.parameters;
30        let formId = param['ohos.extra.param.key.form_identity'];
31        Log.info(this.TAG, `form onAddForm formId: ${formId}`);
32        let formControllerManager: FormControllerManager = FormControllerManager.getInstance();
33        formControllerManager.initData(formId, Constants.PHOTOS_FORM_OPERATION_MODE_NONE).then(() => {
34            let formController: FormController = formControllerManager.getController(formId);
35            Log.info(this.TAG, `form onAddForm. formController ${formController}`);
36            formController = (formController == null) ? formControllerManager.createFormController(formId,
37                Constants.PHOTOS_FORM_OPERATION_MODE_NONE) : formController;
38            if (formController == null) {
39                 Log.error(this.TAG, `Get null controller. formId: ${formId}`);
40                return null;
41            }
42            return formController.bindFormData(formId);
43        }).catch((err) => {
44             Log.error(this.TAG, `init err ${err}`);
45        })
46        return null;
47    }
48
49    onCastToNormalForm(formId) {
50        Log.info(this.TAG, `onCastToNormalForm, formId: ${formId}`);
51    }
52
53    onUpdateForm(formId) {
54        Log.info(this.TAG, `onUpdateForm, formId: ${formId} context ${JSON.stringify(this.context)}`);
55        // 经常起来后可能直接走onUpdate, 所以要初始化一下
56        this.init();
57        let formControllerManager: FormControllerManager = FormControllerManager.getInstance();
58        formControllerManager.updateController(formId);
59    }
60
61    onChangeFormVisibility(newStatus) {
62        Log.info(this.TAG, `onChangeFormVisibility, newStatus: ${JSON.stringify(newStatus)}`);
63        // 经常起来后可能直接走onChangeFormVisibility, 所以要初始化一下
64        this.init();
65        this.clearCache(newStatus);
66    }
67
68    private async clearCache(newStatus) {
69        try {
70            let dataStore = DataStoreUtil.getInstance();
71            await dataStore.removeCache();
72            let formControllerManager: FormControllerManager = FormControllerManager.getInstance();
73            for (let key in newStatus) {
74                Log.info(this.TAG, `onVisibilityChange, key:${key}  value ${newStatus[key]}`);
75                let formId = key;
76                formControllerManager.initData(formId, Constants.PHOTOS_FORM_OPERATION_MODE_NONE);
77            }
78        } catch (err) {
79            Log.error(this.TAG, `clearCache err:` + JSON.stringify(err));
80        }
81    }
82
83    onFormEvent(formId, message) {
84        Log.info(this.TAG, `onFormEvent, formId: ${formId}, message: ${message}`);
85        // 经常起来后可能直接走onEvent, 所以要初始化一下
86        this.init();
87        let formControllerManager: FormControllerManager = FormControllerManager.getInstance();
88        formControllerManager.onEvent(formId, message);
89    }
90
91    onRemoveForm(formId) {
92        Log.info(this.TAG, `onRemoveForm, formId: ${formId}`);
93        // 经常起来后可能直接走onDestroy, 所以要初始化一下
94        this.init();
95        let formControllerManager: FormControllerManager = FormControllerManager.getInstance();
96        formControllerManager.destroyController(formId);
97    }
98
99    private init() {
100        mediaModel.onCreate(this.context);
101        globalThis.appContext = this.context;
102    }
103};