1/*
2 * Copyright (c) 2022-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 */
15import { DialogCallback } from '../../model/common/DialogUtil';
16import { Log } from '../../utils/Log';
17import { ColumnSize, ScreenManager } from '../../model/common/ScreenManager';
18import { Constants } from '../../model/common/Constants';
19import data_preferences from '@ohos.data.preferences';
20import { BusinessError } from '@ohos.base';
21
22const TAG: string = 'common_DeleteDialog';
23
24@CustomDialog
25export struct DeleteDialog {
26  @StorageLink('isHorizontal') isHorizontal: boolean = ScreenManager.getInstance().isHorizontal();
27  @StorageLink('isSidebar') isSidebar: boolean = ScreenManager.getInstance().isSidebar();
28  @StorageLink('leftBlank') leftBlank: number[] =
29    [0, ScreenManager.getInstance().getStatusBarHeight(), 0, ScreenManager.getInstance().getNaviBarHeight()];
30  @Consume dialogCallback: DialogCallback;
31  @Consume dialogMessage: Resource;
32  controller?: CustomDialogController;
33  @StorageLink('isFirstTimeDelete') isFirstTimeDelete: boolean = false;
34  @StorageLink('confirmText') confirmText: Resource = $r('app.string.dialog_delete');
35  private isPcDevice: boolean = AppStorage.get<string>('deviceType') === Constants.PC_DEVICE_TYPE;
36
37  aboutToDisappear() {
38    if (this.isFirstTimeDelete) {
39      AppStorage.setOrCreate(Constants.IS_FIRST_TIME_DELETE, false);
40      let pref: data_preferences.Preferences =
41        AppStorage.get<data_preferences.Preferences>(Constants.PHOTOS_STORE_KEY) as data_preferences.Preferences;
42      pref.put(Constants.IS_FIRST_TIME_DELETE, false).then(() => {
43        Log.debug(TAG, `Succeeded in putting the value of '${Constants.IS_FIRST_TIME_DELETE}'.`);
44        pref.flush();
45      }).catch((err: BusinessError) => {
46        Log.error(TAG, `Failed to put the value of '${Constants.IS_FIRST_TIME_DELETE}'. Cause: ${err}`);
47      });
48    }
49  }
50
51  build() {
52    Column() {
53      Column() {
54        if (this.isFirstTimeDelete) {
55          Image($r('app.media.first_delete_dialog_ico'))
56            .objectFit(ImageFit.Cover)
57            .height($r('app.float.first_delete_dialog_ico_height'))
58            .width($r('app.float.first_delete_dialog_ico_width'))
59            .margin({
60              bottom: $r('app.float.first_delete_dialog_ico_margin_bottom')
61            })
62        }
63
64        Text(this.dialogMessage)
65          .fontSize($r('sys.float.ohos_id_text_size_sub_title2'))
66          .fontWeight(FontWeight.Medium)
67          .fontColor($r('sys.color.ohos_id_color_text_primary'))
68          .textAlign(TextAlign.Center)
69
70        if (this.isFirstTimeDelete) {
71          Text($r('app.plural.first_delete_message', Constants.RECYCLE_DAYS_MAX, Constants.RECYCLE_DAYS_MAX))
72            .textAlign(TextAlign.Center)
73            .fontSize($r('sys.float.ohos_id_text_size_body1'))
74            .fontWeight(FontWeight.Regular)
75            .fontColor($r('sys.color.ohos_id_color_text_primary'))
76            .margin({ top: $r('app.float.first_delete_message_margin_top') })
77        }
78      }
79      .alignItems(HorizontalAlign.Center)
80      .width('100%')
81      .margin({
82        top: $r('app.float.dialog_content_margin'),
83        bottom: $r('app.float.dialog_button_and_text_margin')
84      })
85
86
87      Stack({ alignContent: Alignment.Top }) {
88        Row() {
89          Column() {
90            Button() {
91              Text($r('app.string.dialog_cancel'))
92                .fontSize($r('sys.float.ohos_id_text_size_button1'))
93                .fontColor($r('app.color.color_control_highlight'))
94                .fontWeight(FontWeight.Medium)
95                .width('100%')
96                .textAlign(TextAlign.Center)
97            }
98            .key('DeleteDialogCancelButton')
99            .margin({
100              right: $r('app.float.dialog_double_buttons_margin_right')
101            })
102            .backgroundColor(this.isPcDevice ? $r('sys.color.ohos_id_color_button_normal') : $r('app.color.transparent'))
103            .borderRadius($r('sys.float.ohos_id_corner_radius_button'))
104            .height($r('app.float.details_dialog_button_height'))
105            .onClick(() => {
106              Log.debug(TAG, `cancelCallback`);
107              this.controller?.close();
108              this.dialogCallback && this.dialogCallback.cancelCallback();
109            })
110          }.width('50%')
111
112          if (!this.isPcDevice) {
113            Divider()
114              .vertical(true)
115              .color(Constants.DEFAULT_DIVIDER_COLOR)
116              .height(Constants.DEFAULT_DIVIDER_HEIGHT)
117          }
118
119          Column() {
120            Button() {
121              Text(this.confirmText)
122                .fontSize($r('sys.float.ohos_id_text_size_button1'))
123                .fontColor($r('sys.color.ohos_id_color_warning'))
124                .fontWeight(FontWeight.Medium)
125                .width('100%')
126                .textAlign(TextAlign.Center)
127            }
128            .key('DeleteDialogConfirmButton')
129            .margin({
130              left: $r('app.float.dialog_double_buttons_margin_left')
131            })
132            .backgroundColor(this.isPcDevice ? $r('sys.color.ohos_id_color_button_normal') : $r('app.color.transparent'))
133            .borderRadius($r('sys.float.ohos_id_corner_radius_button'))
134            .height($r('app.float.details_dialog_button_height'))
135            .onClick(() => {
136              Log.debug(TAG, `confirmCallback`);
137              this.controller?.close();
138              this.dialogCallback && this.dialogCallback.confirmCallback();
139            })
140          }.width('50%')
141        }
142      }
143      .width('100%')
144      .height($r('app.float.details_dialog_button_area_height'))
145    }
146    .borderRadius($r('sys.float.ohos_id_corner_radius_default_l'))
147    .width(this.isPcDevice ? $r('app.float.pc_dialog_width') : ScreenManager.getInstance()
148                                                                 .getColumnsWidth(ColumnSize.COLUMN_FOUR))
149    .backgroundColor($r('app.color.white'))
150    .margin({
151      right: $r('app.float.dialog_content_margin'),
152      left: $r('app.float.dialog_content_margin'),
153      bottom: this.isHorizontal || this.isSidebar ? 0 : Constants.DIALOG_BOTTOM_OFFSET + this.leftBlank[3]
154    })
155    .padding({
156      left: $r('app.float.dialog_double_buttons_padding'),
157      right: $r('app.float.dialog_double_buttons_padding')
158    })
159    .alignItems(HorizontalAlign.Center)
160    .shadow({
161      radius: $r('app.float.dialog_defult_shadow_m_radio'),
162      color: $r('app.color.dialog_defult_shadow_m_color'),
163      offsetX: $r('app.float.dialog_defult_shadow_m_offsetX'),
164      offsetY: $r('app.float.dialog_defult_shadow_m_offsetY')
165    })
166  }
167}
168