1/* 2 * Copyright (c) 2023-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 { PrinterInfo, PrintJob, queryAllPrintJobs} from '@ohos/common'; 17import PrintAdapter from '../../Common/Adapter/PrintAdapter' 18import { GlobalThisHelper, GlobalThisStorageKey} from '@ohos/common'; 19import {Constants } from '@ohos/common' 20import { Log } from '@ohos/common'; 21import { CopyUtil }from '@ohos/common'; 22import promptAction from '@ohos.promptAction'; 23import {CancelButton} from '../component/BaseComponent'; 24 25const TAG = 'CusDialogComp' 26 27@CustomDialog 28export struct PrintingSelectDialog{ 29 @StorageLink('PrinterQueue') printerList: Array<PrinterInfo> = new Array(); 30 @Consume('PrintAdapter') adapter:PrintAdapter; 31 @Consume('PrinterSelectFlag') printerSelectFlag:boolean; 32 @Consume('WLANFlag') wlanFlag:boolean; 33 private printingSelectDialogCtl?: CustomDialogController; 34 35 build(){ 36 Column() { 37 Row(){ 38 Text($r('app.string.custom_dialog_choose_printer')).key('PrintingSelectDialog_Text_choosePrinter') 39 .fontWeight(FontWeight.Medium) 40 .fontSize($r('app.float.font_size_headline8')) 41 .textAlign(TextAlign.Start) 42 } 43 .justifyContent(FlexAlign.Start) 44 .width('100%') 45 .height($r('app.float.printer_select_title_height')) 46 .padding({left:$r('app.float.printer_select_padding_left'),right:$r('app.float.printer_select_padding_right')}) 47 //show printer list 48 Stack(){ 49 List({initialIndex: Constants.NUMBER_0}){ 50 ForEach(this.printerList,(printer: PrinterInfo)=>{ 51 ListItem(){ 52 printerItem({currentPrinter:printer,printingSelectDialogCtl:this.printingSelectDialogCtl}) 53 }.key(`PrintingSelectDialog_ListItem_${printer.printerName}`) 54 },(printer: PrinterInfo)=>JSON.stringify(printer)) 55 }.key('PrintingSelectDialog_List_printerList') 56 .width('100%') 57 .height(this.getAdjustedHeight()) 58 .edgeEffect(EdgeEffect.Spring) 59 60 Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 61 Column(){ 62 Image($r('app.media.ic_no_printer')).fillColor('#66000000').width(96).height(96).margin({bottom:8}) 63 Text($r('app.string.custom_dialog_no_printer')).fontColor('#66000000').fontSize($r('app.float.font_size_body2')) 64 } 65 }.width($r('app.float.no_printer_image_width')).height($r('app.float.no_printer_image_height')).backgroundColor($r('app.color.white')) 66 .visibility(this.printerList.length === Constants.NUMBER_0 ? Visibility.Visible : Visibility.None) 67 } 68 69 CancelButton({ 70 cancelLabel: $r('app.string.Cancel'), 71 cancelWidth: $r('app.float.dialog_button_width'), 72 cancelHeight: $r('app.float.dialog_button_height'), 73 cancelClick: () => { 74 this.printingSelectDialogCtl?.close() 75 GlobalThisHelper.createValue<boolean>(false, GlobalThisStorageKey.KEY_PRINTER_SELECT_DIALOG_OPEN, true) 76 } 77 }) 78 .margin({top:$r('app.float.printer_select_button_margin_top'), bottom:$r('app.float.printer_select_button_margin_bottom')}) 79 .key('PrintingSelectDialog_Button_cancel') 80 81 82 } 83 .width($r('app.float.dialog_width')) 84 .backgroundColor(Color.White) 85 .borderRadius($r('app.float.radius_l')) 86 .alignItems(HorizontalAlign.Center) 87 } 88 89 aboutToAppear(){ 90 Log.info(TAG,'PrintingSelectDialog aboutToAppear'); 91 GlobalThisHelper.createValue<boolean>(true, GlobalThisStorageKey.KEY_PRINTER_SELECT_DIALOG_OPEN, true); 92 Log.info(TAG,'createValue KEY_PRINTER_SELECT_DIALOG_OPEN: '+true) 93 } 94 95 getAdjustedHeight():number | Resource { 96 let height: number | Resource = Constants.NUMBER_48; 97 if(this.printerList.length < Constants.NUMBER_9){ 98 height = Constants.NUMBER_48 * this.printerList.length 99 }else{ 100 height = $r('app.float.printer_select_max_height') 101 } 102 Log.info(TAG,'getAdjustedHeight: '+height) 103 return height 104 } 105} 106 107@Component 108struct printerItem{ 109 private printingSelectDialogCtl?: CustomDialogController; 110 private currentPrinter ?: PrinterInfo; 111 @Consume('CanPrint') canPrint:boolean; 112 @Consume('Printer') printer:PrinterInfo; 113 @Consume('PrintAdapter') adapter:PrintAdapter; 114 @Consume('ConnectFlag') connectFlag:boolean; 115 @Consume('PrinterSelectFlag') printerSelectFlag:boolean; 116 @Consume('ConnectCountDownTimer') timer:number; 117 @Consume('ConnectingPrinterId') connectingPrinterId:string; 118 @Consume('UsedPrinterId') usedPrinterId: string; 119 @State bgColor : Resource = $r('app.color.white') 120 121 build(){ 122 Row() { 123 Image($r('app.media.ic_public_printer')) 124 .width($r('app.float.image_comp_width')) 125 .height($r('app.float.image_comp_height')) 126 .margin({left:$r('app.float.printer_select_item_image_margin_left'),right:$r('app.float.printer_select_item_image_margin_right')}) 127 Text(this.currentPrinter?.printerName) 128 .margin({right:$r('app.float.printer_select_item_text_margin_right')}) 129 .fontSize($r('app.float.font_size_body1')) 130 } 131 .backgroundColor(this.bgColor) 132 .borderRadius($r('app.float.radius_l')).height($r('app.float.printer_select_item_height')) 133 .width('100%').margin({left:$r('app.float.printer_select_item_padding_left'),right:$r('app.float.printer_select_item_padding_right')}) 134 .onClick(()=>{ 135 this.handleToConnect() 136 }) 137 .onHover((isHover: boolean) => { 138 if (isHover) { 139 this.bgColor = $r('app.color.effect_color_hover') 140 } else { 141 this.bgColor = $r('app.color.white') 142 } 143 }) 144 .onTouch((event: TouchEvent) => { 145 if (event.type === TouchType.Down) { 146 this.bgColor = $r('app.color.effect_color_press') 147 } 148 if (event.type === TouchType.Up) { 149 this.bgColor = $r('app.color.white') 150 } 151 }) 152 } 153 154 async handleToConnect(){ 155 let result:Array<PrintJob> = await queryAllPrintJobs(this.usedPrinterId) 156 Log.info(TAG,'handleToConnect after queryAllPrintJobs') 157 Log.info(TAG,'handleToConnect typeof result'+typeof result) 158 Log.info(TAG,'handleToConnect this.currentPrinter.printerId'+this.currentPrinter?.printerId) 159 if (this.usedPrinterId !== this.currentPrinter?.printerId 160 && result!==undefined && result.length!==0 161 && !this.checkPrintingPrinter(this.currentPrinter!.printerId,result)){ 162 this.printingSelectDialogCtl?.close() 163 GlobalThisHelper.createValue<boolean>(false, GlobalThisStorageKey.KEY_PRINTER_SELECT_DIALOG_OPEN, true) 164 this.showToast($r('app.string.toast_cannot_connect_while_printing')) 165 }else{ 166 Log.info(TAG,'handleToConnect result.length==0 ') 167 clearInterval(this.timer) 168 if (this.printer === undefined || 169 this.printer.printerId === Constants.STRING_NEGATIVE_ONE || 170 this.printer.printerId !== this.currentPrinter!.printerId) { 171 this.checkConnectingPrinter() 172 if (this.canPrint) { 173 this.canPrint = false 174 } 175 } 176 this.printer = CopyUtil.deepClone(this.currentPrinter) 177 this.printingSelectDialogCtl?.close() 178 GlobalThisHelper.createValue<boolean>(false, GlobalThisStorageKey.KEY_PRINTER_SELECT_DIALOG_OPEN, true) 179 } 180 181 } 182 183 184 checkConnectingPrinter(){ 185 if (this.connectingPrinterId !== Constants.DEFAULT_CONNECTING_PRINTER_ID) { 186 this.adapter?.getPrinterDiscCtl()?.disconnectPrinter(this.connectingPrinterId).then(() => { 187 this.startConnect(); 188 }); 189 } else { 190 this.startConnect() 191 } 192 } 193 194 checkPrintingPrinter(printerId: string,jobs: Array<PrintJob>): boolean{ 195 let job = jobs.find((printJob)=>{ 196 return printJob.printerId === printerId 197 }); 198 if (job === undefined || job === null) { 199 Log.info(TAG,'checkPrintingPrinter false') 200 return false; 201 }else{ 202 Log.info(TAG,'checkPrintingPrinter true') 203 return true; 204 } 205 } 206 207 startConnect(){ 208 Log.info(TAG, 'startConnect printConnect: ' + JSON.stringify(this.currentPrinter)); 209 this.adapter?.getPrinterDiscCtl()?.connectPrinter(this.currentPrinter) 210 this.connectingPrinterId = this.currentPrinter!.printerId 211 } 212 213 showToast(message: Resource) { 214 try { 215 promptAction.showToast({ 216 message: message, 217 duration: Constants.TOAST_INTERVAL, 218 bottom: Constants.TOAST_BOTTOM 219 }); 220 } catch (error) { 221 Log.error(TAG, `showToast args error code is ${error.code}, message is ${error.message}`); 222 }; 223 } 224 225 226 227} 228 229@CustomDialog 230export struct alarmDialog{ 231 private alarmDialogCtl?: CustomDialogController 232 private alarmText: string = Constants.STRING_NONE; 233 build(){ 234 Column(){ 235 Image($r('app.media.ic_tip')).key('AlarmDialog_Image_tips') 236 .height($r('app.float.alarm_dialog_image_height')) 237 .width($r('app.float.alarm_dialog_image_width')) 238 .margin({top:$r('app.float.alarm_dialog_image_margin_top')}) 239 Text(this.alarmText).key('AlarmDialog_Text_alarmText') 240 .fontWeight(FontWeight.Medium) 241 .fontSize($r('app.float.font_size_headline8')) 242 .margin({top:$r('app.float.alarm_dialog_text_margin_top'),left:$r('app.float.alarm_dialog_text_margin_left') 243 ,right:$r('app.float.alarm_dialog_text_margin_right'),bottom:$r('app.float.alarm_dialog_text_margin_bottom')}) 244 Button($r('app.string.confirm')).key('AlarmDialog_Button_confirm') 245 .width($r('app.float.dialog_button_width')) 246 .height($r('app.float.dialog_button_height')) 247 .margin({bottom:$r('app.float.dialog_button_margin_bottom')}) 248 .onClick(()=>{ 249 this.alarmDialogCtl?.close() 250 }) 251 } 252 .width($r('app.float.dialog_width')) 253 .height($r('app.float.alarm_dialog_height')) 254 .backgroundColor($r('app.color.white')) 255 .borderRadius($r('app.float.radius_l')) 256 .alignItems(HorizontalAlign.Center) 257 } 258} 259 260@CustomDialog 261export struct WLANConfirmDialog{ 262 private WLANConfirmDialogCtl: CustomDialogController 263 @Consume('PrintAdapter') adapter:PrintAdapter; 264 @Consume('PrinterSelectFlag') printerSelectFlag:boolean 265 @Consume('IsNeedListenWlan') isNeedListenWlan:boolean 266 build(){ 267 Column(){ 268 Text($r('app.string.wlan_confirm_dialog_open_wlan')).key('WLANConfirmDialog_Text_openWIFI') 269 .fontWeight(FontWeight.Regular) 270 .fontSize($r('app.float.font_size_body1')) 271 .margin({top:$r('app.float.wlan_dialog_text_margin_top'),left:$r('app.float.wlan_dialog_text_margin_left') 272 ,right:$r('app.float.wlan_dialog_text_margin_right'),bottom:$r('app.float.wlan_dialog_text_margin_bottom')}) 273 Row(){ 274 CancelButton({ 275 cancelLabel: $r('app.string.JobManagerPage_ok'), 276 cancelWidth: $r('app.float.wlan_dialog_button_width'), 277 cancelHeight: $r('app.float.dialog_button_height'), 278 cancelClick: () => { 279 this.WLANConfirmDialogCtl.close() 280 } 281 }) 282 .margin({right:$r('app.float.wlan_dialog_button_cancel_margin_right')}) 283 .key('WLANConfirmDialog_Button_cancel') 284 }.margin({left:$r('app.float.wlan_dialog_buttons_margin_left'),right:$r('app.float.wlan_dialog_buttons_margin_right') 285 ,bottom:$r('app.float.wlan_dialog_buttons_margin_bottom')}) 286 } 287 .width($r('app.float.dialog_width')) 288 .backgroundColor($r('app.color.white')) 289 .borderRadius($r('app.float.radius_l')) 290 .alignItems(HorizontalAlign.Center) 291 } 292 293 aboutToAppear(){ 294// this.isNeedListenWlan = true 295 } 296 297 showToast(message: Resource) { 298 try { 299 promptAction.showToast({ 300 message: message, 301 duration: Constants.TOAST_INTERVAL, 302 bottom: Constants.TOAST_BOTTOM 303 }); 304 } catch (error) { 305 Log.error(TAG, `showToast args error code is ${error.code}, message is ${error.message}`); 306 }; 307 } 308} 309 310@CustomDialog 311export struct connectConfirmDialog{ 312 private connectConfirmDialogCtl: CustomDialogController 313 @Consume('ConnectCountDown') count:number 314 315 build(){ 316 Column(){ 317 Text($r('app.string.connect_confirm_dialog_confirm')).key('ConnectConfirmDialog_Text_confirm') 318 .fontWeight(FontWeight.Medium) 319 .maxFontSize($r('app.float.connect_dialog_text_max_font')) 320 .minFontSize($r('app.float.connect_dialog_text_min_font')) 321 .maxLines(Constants.NUMBER_2) 322 .textOverflow({overflow:TextOverflow.Ellipsis}) 323 .margin({top:$r('app.float.connect_dialog_text_margin_top'),left:$r('app.float.connect_dialog_text_margin_left') 324 ,right:$r('app.float.connect_dialog_text_margin_right'),bottom:$r('app.float.connect_dialog_text_margin_bottom')}) 325 Text($r('app.string.connect_confirm_dialog_tips')).key('ConnectConfirmDialog_Text_tips') 326 .fontWeight(FontWeight.Medium) 327 .fontSize($r('app.float.font_size_body1')) 328 .margin({left:$r('app.float.connect_dialog_confirm_text_margin_left'),right:$r('app.float.connect_dialog_confirm_text_margin_right') 329 ,bottom:$r('app.float.connect_dialog_confirm_text_margin_bottom')}) 330 Button($r('app.string.connectConfirmDialog_countDown',this.count-Constants.COUNTDOWN_TO_FAIL)) 331 .key('ConnectConfirmDialog_Button_confirmed') 332 .fontColor($r('sys.color.ohos_id_color_text_primary_activated')) 333 .backgroundColor($r('sys.color.ohos_id_color_button_normal')) 334 .width($r('app.float.dialog_button_width')) 335 .height($r('app.float.dialog_button_height')) 336 .margin({bottom:$r('app.float.connect_dialog_button_margin_bottom')}) 337 .onClick(()=>{ 338 this.connectConfirmDialogCtl.close() 339 }) 340 } 341 .width($r('app.float.dialog_width')) 342 .backgroundColor($r('app.color.white')) 343 .borderRadius($r('app.float.radius_l')) 344 .alignItems(HorizontalAlign.Center) 345 } 346 347}