• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2023 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 { DeviceUtils } from '@ohos/common/src/main/ets/util/DeviceUtils';
17import { LogUtils } from '@ohos/common/src/main/ets/util/LogUtils';
18import { FormatUtils } from '@ohos/common/src/main/ets/util/FormatUtils';
19
20/**
21 * 弹框辅助者
22 *
23 * @since 2022-06-05
24 */
25export namespace DialogHelper {
26  /**
27   * 弹框操作接口
28   *
29   * @since 2022-06-05
30   */
31  export interface DialogOperator {
32    /**
33     * 取消
34     */
35    onCancel?: () => void;
36
37    /**
38     * 确认
39     */
40    onConfirm?: () => void;
41  }
42
43  /**
44   * 网络弹框
45   *
46   * @param operator 回调
47   */
48  export function displayNetworkDialog(operator: DialogOperator): void {
49    AlertDialog.show(
50      {
51        title: $r('app.string.software_update'),
52        message: $r('app.string.network_request'),
53        primaryButton: {
54          value: FormatUtils.toUpperCase(globalThis.abilityContext, $r('app.string.cancel')),
55          action: () => {
56            logInfo('Callback when the first button is clicked');
57            operator.onCancel?.();
58          },
59          backgroundColor: $r('sys.float.ohos_id_corner_radius_button')
60        },
61        secondaryButton: {
62          value: $r('app.string.ok'),
63          action: () => {
64            logInfo('Callback when the second button is clicked');
65            operator.onConfirm?.();
66          },
67          backgroundColor: $r('sys.float.ohos_id_corner_radius_button')
68        },
69        cancel: () => {
70          logInfo('Closed callbacks');
71        },
72        alignment: DeviceUtils.getDialogLocation(),
73        offset: ({
74          dx: "0vp",
75          dy: DeviceUtils.getDialogOffsetY()
76        }),
77        autoCancel: false
78      }
79    )
80  }
81
82  /**
83   * 升级失败弹框
84   *
85   * @param operator 回调
86   */
87  export function displayUpgradeFailDialog(operator ?: DialogOperator): void {
88    defaultNoTitleDialog($r('app.string.update_fail'), operator);
89  }
90
91  /**
92   * 下载失败弹框
93   *
94   * @param operator 回调
95   */
96  export function displayDownloadFailDialog(operator ?: DialogOperator): void {
97    defaultNoTitleDialog($r('app.string.download_fail'), operator);
98  }
99
100  /**
101   * 无网弹框
102   *
103   * @param operator 回调
104   */
105  export function displayNoNetworkDialog(operator ?: DialogOperator): void {
106    defaultKnowDialog($r('app.string.net_error_title'), $r('app.string.net_error_content'), operator);
107  }
108
109  /**
110   * 电量不足弹框
111   *
112   * @param operator 回调
113   */
114  export function displayNotEnoughBatteryDialog(operator ?: DialogOperator): void {
115    defaultKnowDialog($r('app.string.battery_not_enough_title'), $r('app.string.battery_not_enough_content',
116      FormatUtils.getNumberFormat(0.3)), operator);
117  }
118
119  /**
120   * 空间不足弹框
121   *
122   * @param operator 回调
123   */
124  export function displayNotEnoughSpaceDialog(operator ?: DialogOperator): void {
125    defaultKnowDialog($r('app.string.space_not_enough_title'), $r('app.string.space_not_enough_content'), operator);
126  }
127
128  /**
129   * 文件校验失败弹框
130   *
131   * @param operator 回调
132   */
133  export function displayVerifyFailDialog(operator ?: DialogOperator): void {
134    defaultNoTitleDialog($r('app.string.package_verify_fail'), operator);
135  }
136
137  /**
138   * 默认提示弹框
139   *
140   * @param title 标题
141   * @param message 内容
142   * @param operator 回调
143   */
144  function defaultKnowDialog(title: string | Resource, message: string | Resource, operator ?: DialogOperator): void {
145    showDialog(title, message, $r('app.string.button_know'), operator);
146  }
147
148  /**
149   * 默认无标题弹框
150   *
151   * @param message 内容
152   * @param operator 回调
153   */
154  function defaultNoTitleDialog(message: string | Resource, operator ?: DialogOperator): void {
155    showDialog(null, message, $r('app.string.button_know'), operator);
156  }
157
158  /**
159   * 弹框
160   *
161   * @param title 标题
162   * @param message 内容
163   * @param confirmText 确认按钮显示内容
164   * @param operator 回调
165   */
166  function showDialog(title: string | Resource, message: string | Resource, confirmText?: string | Resource,
167    operator ?: DialogOperator): void {
168    AlertDialog.show(
169      {
170        title: title,
171        message: message,
172        confirm: {
173          value: confirmText,
174          action: () => {
175            logInfo('defaultKnowDialog button is clicked');
176            if (operator) {
177              operator.onConfirm();
178            }
179          },
180          backgroundColor: $r('sys.float.ohos_id_corner_radius_button')
181        },
182        cancel: () => {
183          logInfo('Closed callbacks');
184          if(operator) {
185            operator.onCancel();
186          }
187        },
188        alignment: DeviceUtils.getDialogLocation(),
189        offset: ({
190          dx: '0vp',
191          dy: DeviceUtils.getDialogOffsetY()
192        }),
193        autoCancel: false,
194      }
195    )
196  }
197
198  /**
199   * info级别日志打印
200   *
201   * @param message 日志内容
202   */
203   function logInfo(message: string): void {
204     LogUtils.info('DialogHelper', message);
205  }
206}