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 { CountDownDialogType } from '@ohos/common/src/main/ets/const/update_const'; 17import { FormatUtils } from '@ohos/common/src/main/ets/util/FormatUtils' 18 19/** 20 * 倒计时弹框建造者 21 * 22 * @since 2022-06-05 23 */ 24@CustomDialog 25export struct CountDownInstallDialogBuilder { 26 @StorageProp('configLanguage') 27 @Watch('onLanguageChange') private configLanguage: string = AppStorage.Get('configLanguage'); 28 29 /** 30 * 控制器 31 */ 32 controller: CustomDialogController; 33 34 /** 35 * 文本显示内容 36 */ 37 textString: Resource | string; 38 39 /** 40 * 取消按钮显示内容 41 */ 42 @State cancelBtnText: string = ''; 43 44 /** 45 * 确定按钮显示内容 46 */ 47 @State confirmBtnText: string = ''; 48 49 /** 50 * 弹窗类型 51 */ 52 dialogType: CountDownDialogType; 53 54 /** 55 * 倒计时 56 */ 57 @State count: number = 20; 58 59 /** 60 * 取消回调 61 */ 62 cancel: () => void; 63 64 /** 65 * 确认回调 66 */ 67 confirm: () => void; 68 69 private intervalID: number = null; 70 71 aboutToAppear() { 72 this.getCount(); 73 this.initButtonText(); 74 } 75 76 aboutToDisappear() { 77 78 } 79 80 private getCount() { 81 this.intervalID = setInterval(() => { 82 this.count--; 83 this.initButtonText(); 84 if (this.count <= 0) { 85 clearInterval(this.intervalID); 86 this.controller.close(); 87 this.confirm(); 88 } 89 }, 1000); 90 } 91 92 private onLanguageChange(): void { 93 this.initButtonText(); 94 } 95 96 private initButtonText(): void { 97 this.cancelBtnText = FormatUtils.toUpperCase(globalThis.abilityContext, $r('app.string.later')); 98 let confirmBtnRes = null; 99 if (this.dialogType == CountDownDialogType.OTA) { 100 confirmBtnRes = $r('app.string.install_now'); 101 } else if (this.dialogType == CountDownDialogType.OTA_AB) { 102 confirmBtnRes = $r('app.string.reboot_now'); 103 } 104 this.confirmBtnText = FormatUtils.toUpperCase(globalThis.abilityContext, confirmBtnRes, this.count); 105 } 106 107 @Styles buttonStyles() { 108 .onClick(() => { 109 this.controller.close(); 110 if (this.intervalID != null) { 111 clearInterval(this.intervalID); 112 } 113 this.confirm(); 114 }) 115 .backgroundColor($r('sys.color.ohos_id_color_dialog_bg_transparent')) 116 .height($r('app.float.dialog_button_height')) 117 .layoutWeight(1) 118 .padding({ 119 left: '0vp', 120 right: '0vp' 121 }) 122 } 123 124 build() { 125 Column() { 126 Column() { 127 Text($r('app.string.software_update')).width('100%').fontSize($r('app.float.text_size_dialog_title')) 128 Text(this.textString) 129 .width('100%') 130 .fontSize($r('app.float.text_size_dialog_body')) 131 .margin({ 132 top: $r('app.float.dialog_margin_top'), 133 bottom: $r('app.float.dialog_content_margin_vertical') 134 }) 135 } 136 .margin({ 137 left: $r('app.float.dialog_margin_horizontal'), 138 right: $r('app.float.dialog_margin_horizontal') 139 }) 140 141 Row() { 142 Button(this.cancelBtnText) 143 .onClick(() => { 144 this.controller.close(); 145 if (this.intervalID != null) { 146 clearInterval(this.intervalID); 147 } 148 this.cancel(); 149 }) 150 .backgroundColor($r('sys.color.ohos_id_color_dialog_bg_transparent')) 151 .fontColor($r('app.color.blue')) 152 .height($r('app.float.dialog_button_height')) 153 .fontSize($r('app.float.text_size_btn')) 154 .layoutWeight(1) 155 .padding({ 156 right: '0vp', 157 left: '0vp' 158 }) 159 Divider() 160 .vertical(true) 161 .height($r('app.float.dialog_divider_height')) 162 .strokeWidth('0.8vp') 163 .color($r('app.color.dialog_divider_color')) 164 .margin({ 165 left: $r('app.float.divider_margin'), 166 right: $r('app.float.divider_margin') 167 }) 168 if (this.confirmBtnText) { 169 Button() { 170 Text(this.confirmBtnText) 171 .fontColor($r('app.color.blue')) 172 .fontSize($r('app.float.text_size_btn')) 173 .textAlign(TextAlign.Center) 174 } 175 .buttonStyles() 176 } 177 } 178 .margin({ 179 left: $r('app.float.dialog_row_margin_horizontal'), 180 right: $r('app.float.dialog_row_margin_horizontal') 181 }) 182 } 183 .margin({ 184 top: $r('app.float.dialog_margin_top'), 185 bottom: $r('app.float.dialog_margin_bottom'), 186 }) 187 } 188}