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