1/*
2 * Copyright (c) Huawei Technologies Co., Ltd. 2024. All rights reserved.
3 */
4import { DialogButton, DialogButtonDivider } from './DialogComponent';
5import { display } from '@kit.ArkUI';
6import Logger from '../../../base/log/Logger';
7
8const TAG = 'DownloadDialog';
9let storage = LocalStorage.getShared();
10
11@CustomDialog
12export struct DownloadDialog {
13  controller?: CustomDialogController;
14  cancel: Function = () => {
15  };
16  confirm: Function = () => {
17  };
18  appName: string | undefined = storage.get<string>('appName');
19  appIcon: string | undefined = storage.get<string>('appIcon');
20  downloadTips: Resource = $r('app.string.download_tips', this.appName, this.appName);
21  cancelButtonText: Resource = $r('app.string.cancel');
22  confirmButtonText: Resource = $r('app.string.agree');
23  @State contentAlign: ItemAlign = ItemAlign.Start;
24  @State dialogWidth: number = 328;
25  closeDialogBind: Function = () => this.closeDialog();
26
27  aboutToAppear(): void {
28    let px2VpScale: number = px2vp(1);
29    let screenWidth: number = display.getDefaultDisplaySync()?.width ?
30    px2vp(display.getDefaultDisplaySync()?.width) : px2vp(display.getDefaultDisplaySync()?.width * px2VpScale);
31    this.dialogWidth = screenWidth > 432 ? 400 : screenWidth - 32;
32  }
33
34  closeDialog(): void {
35    if (this.controller) {
36      Logger.i(TAG, 'DownloadDialog close.');
37      this.controller.close();
38    }
39  }
40
41  build() {
42    Column() {
43      Column() {
44        this.downloadDialogContent();
45      }
46      .padding({
47        left: $r('app.float.common_padding24'),
48        right: $r('app.float.common_padding24')
49      })
50    }
51    .borderRadius($r('sys.float.ohos_id_corner_radius_dialog'))
52    .backgroundColor($r('sys.color.ohos_id_color_dialog_bg'))
53    .alignItems(HorizontalAlign.End)
54    .margin({ left: $r('app.float.common_margin16'), right: $r('app.float.common_margin16') })
55    .width(this.dialogWidth)
56  }
57
58  @Builder
59  downloadDialogContent() {
60    Row() {
61      Image(this.appIcon)
62        .fillColor($r('sys.color.ohos_id_color_primary'))
63        .objectFit(ImageFit.Contain)
64        .width($r('app.float.common_size64'))
65        .height($r('app.float.common_size64'))
66        .interpolation(ImageInterpolation.High)
67        .draggable(false)
68        .focusable(true)
69        .margin({ top: 24 })
70        .autoResize(false)
71    }
72
73    Row() {
74      Text(this.downloadTips)
75        .lineHeight(21)
76        .fontColor($r('sys.color.ohos_id_color_text_primary'))
77        .margin({ top: $r('app.float.common_margin16') })
78        .alignSelf(ItemAlign.Center)
79    }
80
81    Row() {
82      DialogButton({
83        text: this.cancelButtonText,
84        isDisabled: false,
85        color: '#0A56F7',
86        bgColor: '#F1F3F5',
87        click: () => {
88          this.cancel()
89          this.closeDialog()
90        }
91      })
92      DialogButtonDivider()
93      DialogButton({
94        text: this.confirmButtonText,
95        isDisabled: false,
96        color: '#0A56F7',
97        bgColor: '#F1F3F5',
98        click: () => {
99          this.confirm();
100          this.closeDialog();
101        }
102      })
103    }.width('100%')
104    .padding({ bottom: $r('app.float.common_margin10'), top: $r('app.float.common_margin10') })
105    .margin({
106      top: 8,
107    })
108  }
109}