• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/**
2 * Copyright (c) 2024-2024 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 { ProcessResult } from '../../common/base/BaseViewModel';
17import { BaseViewModel } from '../../common/base/BaseViewModel';
18import { BaseIntent } from '../../common/base/BaseIntent';
19import MenuInfo from '../../common/bean/MenuInfo';
20import { AutoMenuClickIntent, AutoMenuInitIntent, AutoMenuRefreshIntent } from './AutoMenuIntent';
21import { AutoMenuModel } from './AutoMenuModel';
22import { AutoMenuViewState } from './AutoMenuViewState';
23import Logger from '../../common/utils/Logger';
24
25const TAG = 'AutoMenu';
26const DISPLAY_MODEL_LIST = 'list';
27const DISPLAY_MODEL_CARD = 'card';
28
29export class AutoMenuViewModel extends BaseViewModel<AutoMenuModel, AutoMenuViewState> {
30  protected initModel(): AutoMenuModel {
31    return new AutoMenuModel();
32  }
33
34  protected initViewState(): AutoMenuViewState {
35    return new AutoMenuViewState();
36  }
37
38  protected async processIntentWithModel(intents: BaseIntent, model: AutoMenuModel,
39    viewStat: AutoMenuViewState): Promise<ProcessResult> {
40    Logger.info(TAG, 'start processIntentWithModel: ' + intents.getIntentTag());
41    if (intents instanceof AutoMenuInitIntent) {
42      let menuInfoList: MenuInfo[] = await model.getMenuInfoListFromRdb(intents.context);
43      viewStat.listMenuList = menuInfoList.filter((item) => {
44        return (DISPLAY_MODEL_LIST === item.displayedMode || DISPLAY_MODEL_CARD === item.displayedMode)
45      })
46      return ProcessResult.SUCCESS;
47    }
48    if (intents instanceof AutoMenuRefreshIntent) {
49      let menuInfoList: MenuInfo[] = await model.getMenuInfoListFromBms(intents.context);
50      viewStat.listMenuList = menuInfoList.filter((item) => {
51        return (DISPLAY_MODEL_LIST === item.displayedMode || DISPLAY_MODEL_CARD === item.displayedMode)
52      })
53      Logger.info(TAG, 'AutoMenuRefreshIntent: ' + JSON.stringify(menuInfoList));
54      Logger.info(TAG, 'viewState: ' + JSON.stringify(viewStat));
55      return ProcessResult.SUCCESS;
56    }
57    if (intents instanceof AutoMenuClickIntent) {
58      model.handleMenuClick(intents.menuInfo);
59      return ProcessResult.SUCCESS;
60    }
61    Logger.error(TAG, 'undefined intents: ' + intents.getIntentTag());
62    return ProcessResult.FAIL;
63  }
64}