David PHAM-VAN

Simplify platform interface

@@ -59,9 +59,11 @@ class PrintingPlugin extends PrintingPlatform { @@ -59,9 +59,11 @@ class PrintingPlugin extends PrintingPlatform {
59 59
60 @override 60 @override
61 Future<bool> layoutPdf( 61 Future<bool> layoutPdf(
  62 + Printer? printer,
62 LayoutCallback onLayout, 63 LayoutCallback onLayout,
63 String name, 64 String name,
64 PdfPageFormat format, 65 PdfPageFormat format,
  66 + bool dynamicLayout,
65 ) async { 67 ) async {
66 final result = await onLayout(format); 68 final result = await onLayout(format);
67 69
@@ -158,16 +160,6 @@ class PrintingPlugin extends PrintingPlatform { @@ -158,16 +160,6 @@ class PrintingPlugin extends PrintingPlatform {
158 } 160 }
159 161
160 @override 162 @override
161 - Future<bool> directPrintPdf(  
162 - Printer printer,  
163 - LayoutCallback onLayout,  
164 - String name,  
165 - PdfPageFormat format,  
166 - ) {  
167 - return layoutPdf(onLayout, name, format);  
168 - }  
169 -  
170 - @override  
171 Future<List<Printer>> listPrinters() { 163 Future<List<Printer>> listPrinters() {
172 throw UnimplementedError(); 164 throw UnimplementedError();
173 } 165 }
@@ -60,6 +60,7 @@ abstract class PrintingPlatform extends PlatformInterface { @@ -60,6 +60,7 @@ abstract class PrintingPlatform extends PlatformInterface {
60 /// and false if it is canceled. 60 /// and false if it is canceled.
61 /// throws an exception in case of error 61 /// throws an exception in case of error
62 Future<bool> layoutPdf( 62 Future<bool> layoutPdf(
  63 + Printer? printer,
63 LayoutCallback onLayout, 64 LayoutCallback onLayout,
64 String name, 65 String name,
65 PdfPageFormat format, 66 PdfPageFormat format,
@@ -71,18 +72,6 @@ abstract class PrintingPlatform extends PlatformInterface { @@ -71,18 +72,6 @@ abstract class PrintingPlatform extends PlatformInterface {
71 /// Opens the native printer picker interface, and returns the URL of the selected printer. 72 /// Opens the native printer picker interface, and returns the URL of the selected printer.
72 Future<Printer?> pickPrinter(Rect bounds); 73 Future<Printer?> pickPrinter(Rect bounds);
73 74
74 - /// Prints a Pdf document to a specific local printer with no UI  
75 - ///  
76 - /// returns a future with a `bool` set to true if the document is printed  
77 - /// and false if it is canceled.  
78 - /// throws an exception in case of error  
79 - Future<bool> directPrintPdf(  
80 - Printer printer,  
81 - LayoutCallback onLayout,  
82 - String name,  
83 - PdfPageFormat format,  
84 - );  
85 -  
86 /// Displays a platform popup to share the Pdf document to another application 75 /// Displays a platform popup to share the Pdf document to another application
87 Future<bool> sharePdf( 76 Future<bool> sharePdf(
88 Uint8List bytes, 77 Uint8List bytes,
@@ -15,7 +15,6 @@ @@ -15,7 +15,6 @@
15 */ 15 */
16 16
17 import 'dart:async'; 17 import 'dart:async';
18 -import 'dart:io';  
19 import 'dart:typed_data'; 18 import 'dart:typed_data';
20 19
21 import 'package:flutter/rendering.dart' show Rect; 20 import 'package:flutter/rendering.dart' show Rect;
@@ -62,14 +61,14 @@ class MethodChannelPrinting extends PrintingPlatform { @@ -62,14 +61,14 @@ class MethodChannelPrinting extends PrintingPlatform {
62 try { 61 try {
63 bytes = await job.onLayout!(format); 62 bytes = await job.onLayout!(format);
64 } catch (e) { 63 } catch (e) {
65 - if (Platform.isMacOS || Platform.isIOS) { 64 + if (job.useFFI) {
66 return setErrorFfi(job, e.toString()); 65 return setErrorFfi(job, e.toString());
67 } 66 }
68 67
69 rethrow; 68 rethrow;
70 } 69 }
71 70
72 - if (Platform.isMacOS || Platform.isIOS) { 71 + if (job.useFFI) {
73 return setDocumentFfi(job, bytes); 72 return setDocumentFfi(job, bytes);
74 } 73 }
75 74
@@ -139,6 +138,7 @@ class MethodChannelPrinting extends PrintingPlatform { @@ -139,6 +138,7 @@ class MethodChannelPrinting extends PrintingPlatform {
139 138
140 @override 139 @override
141 Future<bool> layoutPdf( 140 Future<bool> layoutPdf(
  141 + Printer? printer,
142 LayoutCallback onLayout, 142 LayoutCallback onLayout,
143 String name, 143 String name,
144 PdfPageFormat format, 144 PdfPageFormat format,
@@ -149,6 +149,7 @@ class MethodChannelPrinting extends PrintingPlatform { @@ -149,6 +149,7 @@ class MethodChannelPrinting extends PrintingPlatform {
149 ); 149 );
150 150
151 final params = <String, dynamic>{ 151 final params = <String, dynamic>{
  152 + if (printer != null) 'printer': printer.url,
152 'name': name, 153 'name': name,
153 'job': job.index, 154 'job': job.index,
154 'width': format.width, 155 'width': format.width,
@@ -199,37 +200,6 @@ class MethodChannelPrinting extends PrintingPlatform { @@ -199,37 +200,6 @@ class MethodChannelPrinting extends PrintingPlatform {
199 } 200 }
200 201
201 @override 202 @override
202 - Future<bool> directPrintPdf(  
203 - Printer printer,  
204 - LayoutCallback onLayout,  
205 - String name,  
206 - PdfPageFormat format,  
207 - ) async {  
208 - final job = _printJobs.add(  
209 - onCompleted: Completer<bool>(),  
210 - onLayout: onLayout,  
211 - );  
212 -  
213 - final params = <String, dynamic>{  
214 - 'name': name,  
215 - 'printer': printer.url,  
216 - 'width': format.width,  
217 - 'height': format.height,  
218 - 'marginLeft': format.marginLeft,  
219 - 'marginTop': format.marginTop,  
220 - 'marginRight': format.marginRight,  
221 - 'marginBottom': format.marginBottom,  
222 - 'job': job.index,  
223 - };  
224 - await _channel.invokeMethod<int>('printPdf', params);  
225 - try {  
226 - return await job.onCompleted!.future;  
227 - } finally {  
228 - _printJobs.remove(job.index);  
229 - }  
230 - }  
231 -  
232 - @override  
233 Future<bool> sharePdf( 203 Future<bool> sharePdf(
234 Uint8List bytes, 204 Uint8List bytes,
235 String filename, 205 String filename,
@@ -15,6 +15,7 @@ @@ -15,6 +15,7 @@
15 */ 15 */
16 16
17 import 'dart:async'; 17 import 'dart:async';
  18 +import 'dart:io';
18 import 'dart:typed_data'; 19 import 'dart:typed_data';
19 20
20 import 'callback.dart'; 21 import 'callback.dart';
@@ -29,6 +30,7 @@ class PrintJob { @@ -29,6 +30,7 @@ class PrintJob {
29 this.onHtmlRendered, 30 this.onHtmlRendered,
30 this.onCompleted, 31 this.onCompleted,
31 this.onPageRasterized, 32 this.onPageRasterized,
  33 + required this.useFFI,
32 }); 34 });
33 35
34 /// Callback used when calling Printing.layoutPdf() 36 /// Callback used when calling Printing.layoutPdf()
@@ -45,6 +47,9 @@ class PrintJob { @@ -45,6 +47,9 @@ class PrintJob {
45 47
46 /// The Job number 48 /// The Job number
47 final int index; 49 final int index;
  50 +
  51 + /// Use the FFI side-channel to send the PDF data
  52 + final bool useFFI;
48 } 53 }
49 54
50 /// Represents a list of print jobs 55 /// Represents a list of print jobs
@@ -69,6 +74,7 @@ class PrintJobs { @@ -69,6 +74,7 @@ class PrintJobs {
69 onHtmlRendered: onHtmlRendered, 74 onHtmlRendered: onHtmlRendered,
70 onCompleted: onCompleted, 75 onCompleted: onCompleted,
71 onPageRasterized: onPageRasterized, 76 onPageRasterized: onPageRasterized,
  77 + useFFI: Platform.isMacOS || Platform.isIOS,
72 ); 78 );
73 _printJobs[job.index] = job; 79 _printJobs[job.index] = job;
74 return job; 80 return job;
@@ -41,7 +41,12 @@ mixin Printing { @@ -41,7 +41,12 @@ mixin Printing {
41 String name = 'Document', 41 String name = 'Document',
42 PdfPageFormat format = PdfPageFormat.standard, 42 PdfPageFormat format = PdfPageFormat.standard,
43 }) { 43 }) {
44 - return PrintingPlatform.instance.layoutPdf(onLayout, name, format); 44 + return PrintingPlatform.instance.layoutPdf(
  45 + null,
  46 + onLayout,
  47 + name,
  48 + format,
  49 + );
45 } 50 }
46 51
47 /// Enumerate the available printers on the system. 52 /// Enumerate the available printers on the system.
@@ -118,7 +123,7 @@ mixin Printing { @@ -118,7 +123,7 @@ mixin Printing {
118 String name = 'Document', 123 String name = 'Document',
119 PdfPageFormat format = PdfPageFormat.standard, 124 PdfPageFormat format = PdfPageFormat.standard,
120 }) { 125 }) {
121 - return PrintingPlatform.instance.directPrintPdf( 126 + return PrintingPlatform.instance.layoutPdf(
122 printer, 127 printer,
123 onLayout, 128 onLayout,
124 name, 129 name,
@@ -108,7 +108,12 @@ class MockPrinting extends Mock @@ -108,7 +108,12 @@ class MockPrinting extends Mock
108 Future<PrintingInfo> info() async => const PrintingInfo(); 108 Future<PrintingInfo> info() async => const PrintingInfo();
109 109
110 @override 110 @override
111 - Future<bool> layoutPdf(onLayout, String name, PdfPageFormat format) async => 111 + Future<bool> layoutPdf(
  112 + Printer? printer,
  113 + LayoutCallback onLayout,
  114 + String name,
  115 + PdfPageFormat format,
  116 + ) async =>
112 true; 117 true;
113 118
114 @override 119 @override
@@ -119,11 +124,6 @@ class MockPrinting extends Mock @@ -119,11 +124,6 @@ class MockPrinting extends Mock
119 Future<Printer?> pickPrinter(Rect bounds) async => null; 124 Future<Printer?> pickPrinter(Rect bounds) async => null;
120 125
121 @override 126 @override
122 - Future<bool> directPrintPdf(  
123 - Printer printer, onLayout, String name, PdfPageFormat format) async =>  
124 - true;  
125 -  
126 - @override  
127 Stream<PdfRaster> raster( 127 Stream<PdfRaster> raster(
128 Uint8List document, List<int>? pages, double dpi) async* {} 128 Uint8List document, List<int>? pages, double dpi) async* {}
129 129