Browse Source

Fix send email for iOS clients

Neeraj Gupta 3 years ago
parent
commit
9e87dadae1
2 changed files with 93 additions and 13 deletions
  1. 1 13
      lib/ui/settings/support_section_widget.dart
  2. 92 0
      lib/utils/email_util.dart

+ 1 - 13
lib/ui/settings/support_section_widget.dart

@@ -2,16 +2,13 @@ import 'dart:io';
 
 import 'package:expandable/expandable.dart';
 import 'package:flutter/material.dart';
-import 'package:logging/logging.dart';
 import 'package:photos/core/configuration.dart';
 import 'package:photos/core/constants.dart';
 import 'package:photos/ui/common/web_page.dart';
 import 'package:photos/ui/settings/common_settings.dart';
 import 'package:photos/ui/settings/settings_section_title.dart';
 import 'package:photos/ui/settings/settings_text_item.dart';
-import 'package:photos/utils/dialog_util.dart';
 import 'package:photos/utils/email_util.dart';
-import 'package:url_launcher/url_launcher.dart';
 
 class SupportSectionWidget extends StatelessWidget {
   const SupportSectionWidget({Key key}) : super(key: key);
@@ -34,16 +31,7 @@ class SupportSectionWidget extends StatelessWidget {
         GestureDetector(
           behavior: HitTestBehavior.translucent,
           onTap: () async {
-            try {
-              final Uri emailLaunchUri = Uri(
-                scheme: 'mailto',
-                path: kSupportEmail,
-              );
-              launchUrl(emailLaunchUri);
-            } catch (e) {
-              Logger("SupportSection").severe(e);
-              showErrorDialog(context, "", "Please email us at $kSupportEmail");
-            }
+            await sendEmail(context, to: kSupportEmail);
           },
           child:
               const SettingsTextItem(text: "Email", icon: Icons.navigate_next),

+ 92 - 0
lib/utils/email_util.dart

@@ -6,6 +6,8 @@ import 'package:flutter/material.dart';
 import 'package:flutter/services.dart';
 import 'package:flutter_email_sender/flutter_email_sender.dart';
 import 'package:logging/logging.dart';
+import 'package:open_mail_app/open_mail_app.dart';
+import 'package:package_info_plus/package_info_plus.dart';
 import 'package:path_provider/path_provider.dart';
 import 'package:photos/core/configuration.dart';
 import 'package:photos/core/error-reporting/super_logging.dart';
@@ -13,7 +15,9 @@ import 'package:photos/ente_theme_data.dart';
 import 'package:photos/ui/common/dialogs.dart';
 import 'package:photos/ui/tools/debug/log_file_viewer.dart';
 import 'package:photos/utils/dialog_util.dart';
+import 'package:photos/utils/toast_util.dart';
 import 'package:share_plus/share_plus.dart';
+import 'package:url_launcher/url_launcher.dart';
 
 final Logger _logger = Logger('email_util');
 
@@ -174,3 +178,91 @@ Future<void> shareLogs(
     sharePositionOrigin: Rect.fromLTWH(0, 0, size.width, size.height / 2),
   );
 }
+
+Future<void> sendEmail(
+  BuildContext context, {
+  @required String to,
+  String subject,
+  String body,
+}) async {
+  try {
+    String clientDebugInfo = await clientInfo();
+    EmailContent email = EmailContent(
+      to: [
+        to,
+      ],
+      subject: subject ?? '[Support]',
+      body: (body ?? '') + clientDebugInfo,
+    );
+    if (Platform.isAndroid) {
+      // Special handling due to issue in proton mail android client
+      // https://github.com/ente-io/frame/pull/253
+      final Uri params = Uri(
+        scheme: 'mailto',
+        path: to,
+        query: 'subject=${email.subject}&body=${email.body}',
+      );
+      if (await canLaunchUrl(params)) {
+        await launchUrl(params);
+      } else {
+        // this will trigger _showNoMailAppsDialog
+        throw Exception('Could not launch ${params.toString()}');
+      }
+    } else {
+      OpenMailAppResult result = await OpenMailApp.composeNewEmailInMailApp(
+        nativePickerTitle: 'Select email app',
+        emailContent: email,
+      );
+      if (!result.didOpen && !result.canOpen) {
+        _showNoMailAppsDialog(context, to);
+      } else if (!result.didOpen && result.canOpen) {
+        showDialog(
+          context: context,
+          builder: (_) => MailAppPickerDialog(
+            mailApps: result.options,
+            emailContent: email,
+          ),
+        );
+      }
+    }
+  } catch (e) {
+    _logger.severe("Failed to send email to $to", e);
+    _showNoMailAppsDialog(context, to);
+  }
+}
+
+Future<String> clientInfo() async {
+  final packageInfo = await PackageInfo.fromPlatform();
+  String debugInfo = '\n\n\n\n ------------------- \nFollowing information can '
+      'help us in debugging if you are facing any issue '
+      '\nRegistered email: ${Configuration.instance.getEmail()}'
+      '\nClient: ${packageInfo.packageName}'
+      '\nVersion : ${packageInfo.version}';
+  return debugInfo;
+}
+
+void _showNoMailAppsDialog(BuildContext context, String toEmail) {
+  showDialog(
+    context: context,
+    builder: (context) {
+      return AlertDialog(
+        title: Text('Please email us at $toEmail'),
+        actions: <Widget>[
+          TextButton(
+            child: const Text("Copy email"),
+            onPressed: () async {
+              await Clipboard.setData(ClipboardData(text: toEmail));
+              showShortToast(context, 'Copied');
+            },
+          ),
+          TextButton(
+            child: const Text("OK"),
+            onPressed: () {
+              Navigator.pop(context);
+            },
+          )
+        ],
+      );
+    },
+  );
+}