Showing
7 changed files
with
25 additions
and
15 deletions
| @@ -43,7 +43,7 @@ public class PrintingHandler implements MethodChannel.MethodCallHandler { | @@ -43,7 +43,7 @@ public class PrintingHandler implements MethodChannel.MethodCallHandler { | ||
| 43 | case "cancelJob": { | 43 | case "cancelJob": { |
| 44 | final PrintingJob printJob = | 44 | final PrintingJob printJob = |
| 45 | new PrintingJob(activity, this, (int) call.argument("job")); | 45 | new PrintingJob(activity, this, (int) call.argument("job")); |
| 46 | - printJob.cancelJob(); | 46 | + printJob.cancelJob(null); |
| 47 | result.success(1); | 47 | result.success(1); |
| 48 | break; | 48 | break; |
| 49 | } | 49 | } |
| @@ -125,18 +125,18 @@ public class PrintingHandler implements MethodChannel.MethodCallHandler { | @@ -125,18 +125,18 @@ public class PrintingHandler implements MethodChannel.MethodCallHandler { | ||
| 125 | if (result instanceof byte[]) { | 125 | if (result instanceof byte[]) { |
| 126 | printJob.setDocument((byte[]) result); | 126 | printJob.setDocument((byte[]) result); |
| 127 | } else { | 127 | } else { |
| 128 | - printJob.cancelJob(); | 128 | + printJob.cancelJob("Unknown data received"); |
| 129 | } | 129 | } |
| 130 | } | 130 | } |
| 131 | 131 | ||
| 132 | @Override | 132 | @Override |
| 133 | public void error(String errorCode, String errorMessage, Object errorDetails) { | 133 | public void error(String errorCode, String errorMessage, Object errorDetails) { |
| 134 | - printJob.cancelJob(); | 134 | + printJob.cancelJob(errorMessage); |
| 135 | } | 135 | } |
| 136 | 136 | ||
| 137 | @Override | 137 | @Override |
| 138 | public void notImplemented() { | 138 | public void notImplemented() { |
| 139 | - printJob.cancelJob(); | 139 | + printJob.cancelJob("notImplemented"); |
| 140 | } | 140 | } |
| 141 | }); | 141 | }); |
| 142 | } | 142 | } |
| @@ -187,9 +187,10 @@ public class PrintingJob extends PrintDocumentAdapter { | @@ -187,9 +187,10 @@ public class PrintingJob extends PrintDocumentAdapter { | ||
| 187 | printJob = printManager.print(name, this, null); | 187 | printJob = printManager.print(name, this, null); |
| 188 | } | 188 | } |
| 189 | 189 | ||
| 190 | - void cancelJob() { | 190 | + void cancelJob(String message) { |
| 191 | if (callback != null) callback.onLayoutCancelled(); | 191 | if (callback != null) callback.onLayoutCancelled(); |
| 192 | if (printJob != null) printJob.cancel(); | 192 | if (printJob != null) printJob.cancel(); |
| 193 | + printing.onCompleted(PrintingJob.this, false, message); | ||
| 193 | } | 194 | } |
| 194 | 195 | ||
| 195 | static void sharePdf(final Context context, final byte[] data, final String name) { | 196 | static void sharePdf(final Context context, final byte[] data, final String name) { |
| @@ -45,8 +45,9 @@ public class PrintJob: UIPrintPageRenderer, UIPrintInteractionControllerDelegate | @@ -45,8 +45,9 @@ public class PrintJob: UIPrintPageRenderer, UIPrintInteractionControllerDelegate | ||
| 45 | } | 45 | } |
| 46 | } | 46 | } |
| 47 | 47 | ||
| 48 | - func cancelJob() { | 48 | + func cancelJob(_ error: String?) { |
| 49 | pdfDocument = nil | 49 | pdfDocument = nil |
| 50 | + printing.onCompleted(printJob: self, completed: false, error: error as NSString?) | ||
| 50 | } | 51 | } |
| 51 | 52 | ||
| 52 | func setDocument(_ data: Data?) { | 53 | func setDocument(_ data: Data?) { |
| @@ -139,10 +139,15 @@ public class PrintingPlugin: NSObject, FlutterPlugin { | @@ -139,10 +139,15 @@ public class PrintingPlugin: NSObject, FlutterPlugin { | ||
| 139 | 139 | ||
| 140 | channel.invokeMethod("onLayout", arguments: arg, result: { (result: Any?) -> Void in | 140 | channel.invokeMethod("onLayout", arguments: arg, result: { (result: Any?) -> Void in |
| 141 | if result as? Bool == false { | 141 | if result as? Bool == false { |
| 142 | - printJob.cancelJob() | ||
| 143 | - } else { | 142 | + printJob.cancelJob(nil) |
| 143 | + } else if result is FlutterError { | ||
| 144 | + let error = result as! FlutterError | ||
| 145 | + printJob.cancelJob(error.message) | ||
| 146 | + } else if result is FlutterStandardTypedData { | ||
| 144 | let object = result as! FlutterStandardTypedData | 147 | let object = result as! FlutterStandardTypedData |
| 145 | printJob.setDocument(object.data) | 148 | printJob.setDocument(object.data) |
| 149 | + } else { | ||
| 150 | + printJob.cancelJob("Unknown data type") | ||
| 146 | } | 151 | } |
| 147 | }) | 152 | }) |
| 148 | } | 153 | } |
| @@ -44,7 +44,6 @@ class MethodChannelPrinting extends PrintingPlatform { | @@ -44,7 +44,6 @@ class MethodChannelPrinting extends PrintingPlatform { | ||
| 44 | switch (call.method) { | 44 | switch (call.method) { |
| 45 | case 'onLayout': | 45 | case 'onLayout': |
| 46 | final PrintJob job = _printJobs[call.arguments['job']]; | 46 | final PrintJob job = _printJobs[call.arguments['job']]; |
| 47 | - try { | ||
| 48 | final PdfPageFormat format = PdfPageFormat( | 47 | final PdfPageFormat format = PdfPageFormat( |
| 49 | call.arguments['width'], | 48 | call.arguments['width'], |
| 50 | call.arguments['height'], | 49 | call.arguments['height'], |
| @@ -61,9 +60,6 @@ class MethodChannelPrinting extends PrintingPlatform { | @@ -61,9 +60,6 @@ class MethodChannelPrinting extends PrintingPlatform { | ||
| 61 | } | 60 | } |
| 62 | 61 | ||
| 63 | return Uint8List.fromList(bytes); | 62 | return Uint8List.fromList(bytes); |
| 64 | - } catch (e) { | ||
| 65 | - return e.toString(); | ||
| 66 | - } | ||
| 67 | break; | 63 | break; |
| 68 | case 'onCompleted': | 64 | case 'onCompleted': |
| 69 | final bool completed = call.arguments['completed']; | 65 | final bool completed = call.arguments['completed']; |
| @@ -101,7 +101,9 @@ public class PrintJob: NSView, NSSharingServicePickerDelegate { | @@ -101,7 +101,9 @@ public class PrintJob: NSView, NSSharingServicePickerDelegate { | ||
| 101 | ) | 101 | ) |
| 102 | } | 102 | } |
| 103 | 103 | ||
| 104 | - public func cancelJob() {} | 104 | + func cancelJob(_ error: String?) { |
| 105 | + printing.onCompleted(printJob: self, completed: false, error: error as NSString?) | ||
| 106 | + } | ||
| 105 | 107 | ||
| 106 | public static func sharePdf(data: Data, withSourceRect rect: CGRect, andName name: String) { | 108 | public static func sharePdf(data: Data, withSourceRect rect: CGRect, andName name: String) { |
| 107 | let tempFile = NSTemporaryDirectory() + name | 109 | let tempFile = NSTemporaryDirectory() + name |
| @@ -139,10 +139,15 @@ public class PrintingPlugin: NSObject, FlutterPlugin { | @@ -139,10 +139,15 @@ public class PrintingPlugin: NSObject, FlutterPlugin { | ||
| 139 | 139 | ||
| 140 | channel.invokeMethod("onLayout", arguments: arg, result: { (result: Any?) -> Void in | 140 | channel.invokeMethod("onLayout", arguments: arg, result: { (result: Any?) -> Void in |
| 141 | if result as? Bool == false { | 141 | if result as? Bool == false { |
| 142 | - printJob.cancelJob() | ||
| 143 | - } else { | 142 | + printJob.cancelJob(nil) |
| 143 | + } else if result is FlutterError { | ||
| 144 | + let error = result as! FlutterError | ||
| 145 | + printJob.cancelJob(error.message) | ||
| 146 | + } else if result is FlutterStandardTypedData { | ||
| 144 | let object = result as! FlutterStandardTypedData | 147 | let object = result as! FlutterStandardTypedData |
| 145 | printJob.setDocument(object.data) | 148 | printJob.setDocument(object.data) |
| 149 | + } else { | ||
| 150 | + printJob.cancelJob("Unknown data type") | ||
| 146 | } | 151 | } |
| 147 | }) | 152 | }) |
| 148 | } | 153 | } |
-
Please register or login to post a comment