Deepak
Committed by David PHAM-VAN

added subject, body and email parameters in sharePdf

@@ -49,7 +49,10 @@ public class PrintingHandler implements MethodChannel.MethodCallHandler { @@ -49,7 +49,10 @@ public class PrintingHandler implements MethodChannel.MethodCallHandler {
49 case "sharePdf": { 49 case "sharePdf": {
50 final byte[] document = call.argument("doc"); 50 final byte[] document = call.argument("doc");
51 final String name = call.argument("name"); 51 final String name = call.argument("name");
52 - PrintingJob.sharePdf(activity, document, name); 52 + final String subject = call.argument("subject");
  53 + final String body = call.argument("body");
  54 + final ArrayList<String> emails = call.argument("emails");
  55 + PrintingJob.sharePdf(activity, document, name, subject, body, emails);
53 result.success(1); 56 result.success(1);
54 break; 57 break;
55 } 58 }
@@ -207,7 +207,8 @@ public class PrintingJob extends PrintDocumentAdapter { @@ -207,7 +207,8 @@ public class PrintingJob extends PrintDocumentAdapter {
207 printing.onCompleted(PrintingJob.this, false, message); 207 printing.onCompleted(PrintingJob.this, false, message);
208 } 208 }
209 209
210 - static void sharePdf(final Context context, final byte[] data, final String name) { 210 + static void sharePdf(final Context context, final byte[] data, final String name,
  211 + final String subject, final String body, final ArrayList<String> emails) {
211 assert name != null; 212 assert name != null;
212 213
213 try { 214 try {
@@ -233,6 +234,9 @@ public class PrintingJob extends PrintDocumentAdapter { @@ -233,6 +234,9 @@ public class PrintingJob extends PrintDocumentAdapter {
233 shareIntent.setType("application/pdf"); 234 shareIntent.setType("application/pdf");
234 shareIntent.putExtra(Intent.EXTRA_STREAM, apkURI); 235 shareIntent.putExtra(Intent.EXTRA_STREAM, apkURI);
235 shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); 236 shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
  237 + shareIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
  238 + shareIntent.putExtra(Intent.EXTRA_TEXT, body);
  239 + shareIntent.putExtra(Intent.EXTRA_EMAIL, emails != null ? emails.toArray(new String[0]) : null);
236 Intent chooserIntent = Intent.createChooser(shareIntent, null); 240 Intent chooserIntent = Intent.createChooser(shareIntent, null);
237 List<ResolveInfo> resInfoList = context.getPackageManager().queryIntentActivities( 241 List<ResolveInfo> resInfoList = context.getPackageManager().queryIntentActivities(
238 chooserIntent, PackageManager.MATCH_DEFAULT_ONLY); 242 chooserIntent, PackageManager.MATCH_DEFAULT_ONLY);
@@ -186,7 +186,7 @@ public class PrintJob: UIPrintPageRenderer, UIPrintInteractionControllerDelegate @@ -186,7 +186,7 @@ public class PrintJob: UIPrintPageRenderer, UIPrintInteractionControllerDelegate
186 ) 186 )
187 } 187 }
188 188
189 - static func sharePdf(data: Data, withSourceRect rect: CGRect, andName name: String) { 189 + static func sharePdf(data: Data, withSourceRect rect: CGRect, andName name: String, subject: String?, body: String?) {
190 let tmpDirURL = URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true) 190 let tmpDirURL = URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true)
191 let fileURL = tmpDirURL.appendingPathComponent(name) 191 let fileURL = tmpDirURL.appendingPathComponent(name)
192 192
@@ -197,7 +197,8 @@ public class PrintJob: UIPrintPageRenderer, UIPrintInteractionControllerDelegate @@ -197,7 +197,8 @@ public class PrintJob: UIPrintPageRenderer, UIPrintInteractionControllerDelegate
197 return 197 return
198 } 198 }
199 199
200 - let activityViewController = UIActivityViewController(activityItems: [fileURL], applicationActivities: nil) 200 + let activityViewController = UIActivityViewController(activityItems: [fileURL, body], applicationActivities: nil)
  201 + activityViewController.setValue(subject , forKey: "subject")
201 if UI_USER_INTERFACE_IDIOM() == .pad { 202 if UI_USER_INTERFACE_IDIOM() == .pad {
202 let controller: UIViewController? = UIApplication.shared.keyWindow?.rootViewController 203 let controller: UIViewController? = UIApplication.shared.keyWindow?.rootViewController
203 activityViewController.popoverPresentationController?.sourceView = controller?.view 204 activityViewController.popoverPresentationController?.sourceView = controller?.view
@@ -84,7 +84,9 @@ public class PrintingPlugin: NSObject, FlutterPlugin { @@ -84,7 +84,9 @@ public class PrintingPlugin: NSObject, FlutterPlugin {
84 width: CGFloat((args["w"] as? NSNumber)?.floatValue ?? 0.0), 84 width: CGFloat((args["w"] as? NSNumber)?.floatValue ?? 0.0),
85 height: CGFloat((args["h"] as? NSNumber)?.floatValue ?? 0.0) 85 height: CGFloat((args["h"] as? NSNumber)?.floatValue ?? 0.0)
86 ), 86 ),
87 - andName: args["name"] as! String 87 + andName: args["name"] as! String,
  88 + subject: args["subject"] as? String,
  89 + body: args["body"] as? String
88 ) 90 )
89 result(NSNumber(value: 1)) 91 result(NSNumber(value: 1))
90 } else if call.method == "convertHtml" { 92 } else if call.method == "convertHtml" {
@@ -159,6 +159,9 @@ class PrintingPlugin extends PrintingPlatform { @@ -159,6 +159,9 @@ class PrintingPlugin extends PrintingPlatform {
159 Uint8List bytes, 159 Uint8List bytes,
160 String filename, 160 String filename,
161 Rect bounds, 161 Rect bounds,
  162 + String? subject,
  163 + String? body,
  164 + List<String>? emails,
162 ) async { 165 ) async {
163 final pdfFile = html.Blob( 166 final pdfFile = html.Blob(
164 <Uint8List>[Uint8List.fromList(bytes)], 167 <Uint8List>[Uint8List.fromList(bytes)],
@@ -73,11 +73,26 @@ abstract class PrintingPlatform extends PlatformInterface { @@ -73,11 +73,26 @@ abstract class PrintingPlatform extends PlatformInterface {
73 /// Opens the native printer picker interface, and returns the URL of the selected printer. 73 /// Opens the native printer picker interface, and returns the URL of the selected printer.
74 Future<Printer?> pickPrinter(Rect bounds); 74 Future<Printer?> pickPrinter(Rect bounds);
75 75
76 - /// Displays a platform popup to share the Pdf document to another application 76 + /// Displays a platform popup to share the Pdf document to another application.
  77 + ///
  78 + /// [subject] will be the email subject if selected application is email.
  79 + ///
  80 + /// [body] will be the extra text that can be shared along with the Pdf document.
  81 + /// For email application [body] will be the email body text.
  82 + ///
  83 + /// [emails] will be the list of emails to which you want to share the Pdf document.
  84 + /// If the selected application is email application then the these [emails] will be
  85 + /// filled in the to address.
  86 + ///
  87 + /// [subject] and [body] will only work for Android and iOS platforms.
  88 + /// [emails] will only work for Android Platform.
77 Future<bool> sharePdf( 89 Future<bool> sharePdf(
78 Uint8List bytes, 90 Uint8List bytes,
79 String filename, 91 String filename,
80 Rect bounds, 92 Rect bounds,
  93 + String? subject,
  94 + String? body,
  95 + List<String>? emails,
81 ); 96 );
82 97
83 /// Convert an html document to a pdf data 98 /// Convert an html document to a pdf data
@@ -225,10 +225,16 @@ class MethodChannelPrinting extends PrintingPlatform { @@ -225,10 +225,16 @@ class MethodChannelPrinting extends PrintingPlatform {
225 Uint8List bytes, 225 Uint8List bytes,
226 String filename, 226 String filename,
227 Rect bounds, 227 Rect bounds,
  228 + String? subject,
  229 + String? body,
  230 + List<String>? emails,
228 ) async { 231 ) async {
229 final params = <String, dynamic>{ 232 final params = <String, dynamic>{
230 'doc': Uint8List.fromList(bytes), 233 'doc': Uint8List.fromList(bytes),
231 'name': filename, 234 'name': filename,
  235 + 'subject': subject,
  236 + 'body': body,
  237 + 'emails': emails,
232 'x': bounds.left, 238 'x': bounds.left,
233 'y': bounds.top, 239 'y': bounds.top,
234 'w': bounds.width, 240 'w': bounds.width,
@@ -135,11 +135,26 @@ mixin Printing { @@ -135,11 +135,26 @@ mixin Printing {
135 ); 135 );
136 } 136 }
137 137
138 - /// Displays a platform popup to share the Pdf document to another application 138 + /// Displays a platform popup to share the Pdf document to another application.
  139 + ///
  140 + /// [subject] will be the email subject if selected application is email.
  141 + ///
  142 + /// [body] will be the extra text that can be shared along with the Pdf document.
  143 + /// For email application [body] will be the email body text.
  144 + ///
  145 + /// [emails] will be the list of emails to which you want to share the Pdf document.
  146 + /// If the selected application is email application then the these [emails] will be
  147 + /// filled in the to address.
  148 + ///
  149 + /// [subject] and [body] will only work for Android and iOS platforms.
  150 + /// [emails] will only work for Android Platform.
139 static Future<bool> sharePdf({ 151 static Future<bool> sharePdf({
140 required Uint8List bytes, 152 required Uint8List bytes,
141 String filename = 'document.pdf', 153 String filename = 'document.pdf',
142 Rect? bounds, 154 Rect? bounds,
  155 + String? subject,
  156 + String? body,
  157 + List<String>? emails,
143 }) { 158 }) {
144 bounds ??= Rect.fromCircle(center: Offset.zero, radius: 10); 159 bounds ??= Rect.fromCircle(center: Offset.zero, radius: 10);
145 160
@@ -147,6 +162,9 @@ mixin Printing { @@ -147,6 +162,9 @@ mixin Printing {
147 bytes, 162 bytes,
148 filename, 163 filename,
149 bounds, 164 bounds,
  165 + subject,
  166 + body,
  167 + emails,
150 ); 168 );
151 } 169 }
152 170
@@ -118,7 +118,8 @@ class MockPrinting extends Mock @@ -118,7 +118,8 @@ class MockPrinting extends Mock
118 true; 118 true;
119 119
120 @override 120 @override
121 - Future<bool> sharePdf(Uint8List bytes, String filename, Rect bounds) async => 121 + Future<bool> sharePdf(Uint8List bytes, String filename, Rect bounds,
  122 + String? subject, String? body, List<String>? emails) async =>
122 true; 123 true;
123 124
124 @override 125 @override