David PHAM-VAN

Simplify platform interface

... ... @@ -59,9 +59,11 @@ class PrintingPlugin extends PrintingPlatform {
@override
Future<bool> layoutPdf(
Printer? printer,
LayoutCallback onLayout,
String name,
PdfPageFormat format,
bool dynamicLayout,
) async {
final result = await onLayout(format);
... ... @@ -158,16 +160,6 @@ class PrintingPlugin extends PrintingPlatform {
}
@override
Future<bool> directPrintPdf(
Printer printer,
LayoutCallback onLayout,
String name,
PdfPageFormat format,
) {
return layoutPdf(onLayout, name, format);
}
@override
Future<List<Printer>> listPrinters() {
throw UnimplementedError();
}
... ...
... ... @@ -60,6 +60,7 @@ abstract class PrintingPlatform extends PlatformInterface {
/// and false if it is canceled.
/// throws an exception in case of error
Future<bool> layoutPdf(
Printer? printer,
LayoutCallback onLayout,
String name,
PdfPageFormat format,
... ... @@ -71,18 +72,6 @@ abstract class PrintingPlatform extends PlatformInterface {
/// Opens the native printer picker interface, and returns the URL of the selected printer.
Future<Printer?> pickPrinter(Rect bounds);
/// Prints a Pdf document to a specific local printer with no UI
///
/// returns a future with a `bool` set to true if the document is printed
/// and false if it is canceled.
/// throws an exception in case of error
Future<bool> directPrintPdf(
Printer printer,
LayoutCallback onLayout,
String name,
PdfPageFormat format,
);
/// Displays a platform popup to share the Pdf document to another application
Future<bool> sharePdf(
Uint8List bytes,
... ...
... ... @@ -15,7 +15,6 @@
*/
import 'dart:async';
import 'dart:io';
import 'dart:typed_data';
import 'package:flutter/rendering.dart' show Rect;
... ... @@ -62,14 +61,14 @@ class MethodChannelPrinting extends PrintingPlatform {
try {
bytes = await job.onLayout!(format);
} catch (e) {
if (Platform.isMacOS || Platform.isIOS) {
if (job.useFFI) {
return setErrorFfi(job, e.toString());
}
rethrow;
}
if (Platform.isMacOS || Platform.isIOS) {
if (job.useFFI) {
return setDocumentFfi(job, bytes);
}
... ... @@ -139,6 +138,7 @@ class MethodChannelPrinting extends PrintingPlatform {
@override
Future<bool> layoutPdf(
Printer? printer,
LayoutCallback onLayout,
String name,
PdfPageFormat format,
... ... @@ -149,6 +149,7 @@ class MethodChannelPrinting extends PrintingPlatform {
);
final params = <String, dynamic>{
if (printer != null) 'printer': printer.url,
'name': name,
'job': job.index,
'width': format.width,
... ... @@ -199,37 +200,6 @@ class MethodChannelPrinting extends PrintingPlatform {
}
@override
Future<bool> directPrintPdf(
Printer printer,
LayoutCallback onLayout,
String name,
PdfPageFormat format,
) async {
final job = _printJobs.add(
onCompleted: Completer<bool>(),
onLayout: onLayout,
);
final params = <String, dynamic>{
'name': name,
'printer': printer.url,
'width': format.width,
'height': format.height,
'marginLeft': format.marginLeft,
'marginTop': format.marginTop,
'marginRight': format.marginRight,
'marginBottom': format.marginBottom,
'job': job.index,
};
await _channel.invokeMethod<int>('printPdf', params);
try {
return await job.onCompleted!.future;
} finally {
_printJobs.remove(job.index);
}
}
@override
Future<bool> sharePdf(
Uint8List bytes,
String filename,
... ...
... ... @@ -15,6 +15,7 @@
*/
import 'dart:async';
import 'dart:io';
import 'dart:typed_data';
import 'callback.dart';
... ... @@ -29,6 +30,7 @@ class PrintJob {
this.onHtmlRendered,
this.onCompleted,
this.onPageRasterized,
required this.useFFI,
});
/// Callback used when calling Printing.layoutPdf()
... ... @@ -45,6 +47,9 @@ class PrintJob {
/// The Job number
final int index;
/// Use the FFI side-channel to send the PDF data
final bool useFFI;
}
/// Represents a list of print jobs
... ... @@ -69,6 +74,7 @@ class PrintJobs {
onHtmlRendered: onHtmlRendered,
onCompleted: onCompleted,
onPageRasterized: onPageRasterized,
useFFI: Platform.isMacOS || Platform.isIOS,
);
_printJobs[job.index] = job;
return job;
... ...
... ... @@ -41,7 +41,12 @@ mixin Printing {
String name = 'Document',
PdfPageFormat format = PdfPageFormat.standard,
}) {
return PrintingPlatform.instance.layoutPdf(onLayout, name, format);
return PrintingPlatform.instance.layoutPdf(
null,
onLayout,
name,
format,
);
}
/// Enumerate the available printers on the system.
... ... @@ -118,7 +123,7 @@ mixin Printing {
String name = 'Document',
PdfPageFormat format = PdfPageFormat.standard,
}) {
return PrintingPlatform.instance.directPrintPdf(
return PrintingPlatform.instance.layoutPdf(
printer,
onLayout,
name,
... ...
... ... @@ -108,7 +108,12 @@ class MockPrinting extends Mock
Future<PrintingInfo> info() async => const PrintingInfo();
@override
Future<bool> layoutPdf(onLayout, String name, PdfPageFormat format) async =>
Future<bool> layoutPdf(
Printer? printer,
LayoutCallback onLayout,
String name,
PdfPageFormat format,
) async =>
true;
@override
... ... @@ -119,11 +124,6 @@ class MockPrinting extends Mock
Future<Printer?> pickPrinter(Rect bounds) async => null;
@override
Future<bool> directPrintPdf(
Printer printer, onLayout, String name, PdfPageFormat format) async =>
true;
@override
Stream<PdfRaster> raster(
Uint8List document, List<int>? pages, double dpi) async* {}
... ...