1/* 2 * Copyright (c) 2025 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 { Log } from '../utils/utils'; 17import { BaseIntent } from './BaseIntent'; 18import { BaseModel } from './BaseModel'; 19import { BaseState } from './BaseState'; 20 21export enum ProcessResult { 22 FAIL = 0, 23 SUCCESS = 1 24} 25 26export abstract class BaseViewModel<M extends BaseModel, S extends BaseState> { 27 private _model?: M; 28 private _viewState?: S; 29 30 /** 31 * 获取界面状态 32 * return 界面状态 33 */ 34 public getViewState(): S { 35 if (this._viewState === null || this._viewState === undefined) { 36 this._viewState = this.initViewState(); 37 } 38 return this._viewState; 39 } 40 41 /** 42 * 异步的方式执行intention 43 * @param intention view的意图 44 * return 执行结果 45 */ 46 public async processIntent(intention: BaseIntent): Promise<ProcessResult> { 47 Log.info(`start processIntent: ${intention.getIntentTag()}.`); 48 if (this._model === null || this._model === undefined) { 49 this._model = this.initModel(); 50 } 51 if (this._viewState === null || this._viewState === undefined) { 52 this._viewState = this.initViewState(); 53 } 54 if (this._model !== null && this._viewState !== null) { 55 try { 56 return await this.processIntentWithModel(intention, this._model, this._viewState); 57 } catch (err) { 58 Log.error(`error when process intention: ${intention.getIntentTag()}.`); 59 } 60 } 61 return ProcessResult.FAIL; 62 } 63 64 /** 65 * 同步的方式执行intention 66 * @param intention view的意图 67 * return 执行结果 68 */ 69 public processIntentSync(intention: BaseIntent): ProcessResult { 70 Log.info(`start processIntent: ${intention.getIntentTag()}.`); 71 if (this._model === null || this._model === undefined) { 72 this._model = this.initModel(); 73 } 74 if (this._viewState === null || this._viewState === undefined) { 75 this._viewState = this.initViewState(); 76 } 77 if (this._model !== null && this._viewState !== null) { 78 try { 79 return this.processIntentWithModelSync(intention, this._model, this._viewState); 80 } catch (err) { 81 Log.error(`error when process intention: ${intention.getIntentTag()}.`); 82 } 83 } 84 return ProcessResult.FAIL; 85 } 86 87 /** 88 * 初始化逻辑处理类,子类需要实现这个方法 89 * return 逻辑处理类 90 */ 91 protected abstract initModel(): M; 92 93 /** 94 * 初始化界面状态,子类需要实现这个方法 95 * return 界面状态 96 */ 97 protected abstract initViewState(): S; 98 99 /** 100 * 异步的方式处理intention 101 * @param intention view的意图 102 * @param model 逻辑处理类 103 * @param viewState 界面状态 104 * return 界面状态 105 */ 106 protected abstract processIntentWithModel(intention: BaseIntent, model: M, viewState: S): Promise<ProcessResult>; 107 108 /** 109 * 同步的方式处理intention 110 * @param intention view的意图 111 * @param model 逻辑处理类 112 * @param viewState 界面状态 113 * return 界面状态 114 */ 115 protected processIntentWithModelSync(intention: BaseIntent, model: M, viewState: S): ProcessResult { 116 return ProcessResult.SUCCESS; 117 } 118 119}