• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 { BaseViewModel, ProcessResult } from '../common/base/BaseViewModel';
17import { BaseIntent } from '../common/base/BaseIntent';
18import { GrantDialogModel } from './GrantDialogModel';
19import { GrantDialogIntent } from './GrantDialogIntent';
20import { GrantDialogViewState } from './GrantDialogViewState';
21import { Permission } from '../common/model/definition';
22import { CallerAppInfo } from '../common/model/typedef';
23import { Log } from '../common/utils/utils';
24import window from '@ohos.window';
25
26export class GrantDialogViewModel extends BaseViewModel<GrantDialogModel, GrantDialogViewState> {
27  private callerAppInfo: CallerAppInfo;
28  private win: window.Window;
29
30  constructor(callerAppInfo: CallerAppInfo, win: window.Window) {
31    super();
32    this.callerAppInfo = callerAppInfo;
33    this.win = win;
34  }
35
36  private async processInitIntent(
37    viewState: GrantDialogViewState, model: GrantDialogModel, intention: GrantDialogIntent.InitIntent
38  ): Promise<ProcessResult> {
39    if (this.callerAppInfo.reqPerms.includes(Permission.READ_PASTEBOARD)) {
40      viewState.pasteBoardName = model.getPasteBoardInfo();
41    }
42    viewState.curIndex = 0;
43    viewState.appName = model.getAppName(this.callerAppInfo.bundleName);
44    viewState.locationFlag = model.initLocationFlag(this.callerAppInfo);
45    viewState.grantGroups = model.getGrantGroups(
46      this.callerAppInfo, intention.context, viewState.appName, viewState.locationFlag, viewState.pasteBoardName
47    );
48    if (viewState.grantGroups.length === 0) {
49      Log.error(`grantGroups length is 0, terminate.`);
50      model.terminateWithResult(intention.context, this.win, this.callerAppInfo);
51    }
52    viewState.grantGroups.forEach((groupConfig) => {
53      Log.info(`group: ${groupConfig.groupName}.`);
54      if (groupConfig.title === '') {
55        Log.error(`get resource faild, terminate.`);
56        model.terminateWithResult(intention.context, this.win, this.callerAppInfo);
57      }
58    })
59    return ProcessResult.SUCCESS;
60  }
61
62  private async processRefreshIntent(
63    viewState: GrantDialogViewState, model: GrantDialogModel, intention: GrantDialogIntent.RefreshIntent
64  ): Promise<ProcessResult> {
65    if (this.callerAppInfo.reqPerms.includes(Permission.READ_PASTEBOARD)) {
66      viewState.pasteBoardName = model.getPasteBoardInfo();
67    }
68    viewState.appName = model.getAppName(intention.callerAppInfo.bundleName);
69    viewState.grantGroups = model.getGrantGroups(
70      intention.callerAppInfo, intention.context, viewState.appName, viewState.locationFlag, viewState.pasteBoardName
71    );
72    return ProcessResult.SUCCESS;
73  }
74
75  private async processClickIntent(
76    viewState: GrantDialogViewState, model: GrantDialogModel, intention: GrantDialogIntent.ClickIntent
77  ): Promise<ProcessResult> {
78    await model.clickHandle(
79      intention.groupConfig, intention.callerAppInfo, viewState.locationFlag, intention.buttonStatus
80    );
81    if (viewState.curIndex === viewState.grantGroups.length - 1) {
82      let timer: number = setTimeout(() => {
83        model.terminateWithResult(intention.context, this.win, intention.callerAppInfo);
84        clearTimeout(timer);
85      }, 200);
86      return ProcessResult.SUCCESS;
87    } else {
88      let nextIndex = viewState.curIndex + 1;
89      viewState.curIndex = nextIndex >= viewState.grantGroups.length ? viewState.grantGroups.length - 1 : nextIndex;
90    }
91    return ProcessResult.SUCCESS;
92  }
93
94  protected initModel(): GrantDialogModel {
95    return new GrantDialogModel();
96  }
97
98  protected initViewState(): GrantDialogViewState {
99    return new GrantDialogViewState();
100  }
101
102  protected async processIntentWithModel(
103    intention: BaseIntent, model: GrantDialogModel, viewState: GrantDialogViewState
104  ): Promise<ProcessResult> {
105    if (intention instanceof GrantDialogIntent.InitIntent) {
106      return await this.processInitIntent(viewState, model, intention);
107    }
108    if (intention instanceof GrantDialogIntent.RefreshIntent) {
109      return await this.processRefreshIntent(viewState, model, intention);
110    }
111    if (intention instanceof GrantDialogIntent.ClickIntent) {
112      return await this.processClickIntent(viewState, model, intention);
113    }
114    return ProcessResult.FAIL;
115  }
116}