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