email_util.dart 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import 'dart:io';
  2. import 'package:archive/archive_io.dart';
  3. import 'package:email_validator/email_validator.dart';
  4. import 'package:flutter/material.dart';
  5. import 'package:flutter/services.dart';
  6. import 'package:flutter/widgets.dart';
  7. import 'package:flutter_email_sender/flutter_email_sender.dart';
  8. import 'package:logging/logging.dart';
  9. import 'package:path_provider/path_provider.dart';
  10. import 'package:photos/ui/common/dialogs.dart';
  11. import 'package:photos/ui/log_file_viewer.dart';
  12. import 'package:photos/utils/dialog_util.dart';
  13. import 'package:share_plus/share_plus.dart';
  14. import 'package:super_logging/super_logging.dart';
  15. final Logger _logger = Logger('email_util');
  16. bool isValidEmail(String email) {
  17. return EmailValidator.validate(email);
  18. }
  19. Future<void> sendLogs(
  20. BuildContext context,
  21. String title,
  22. String toEmail, {
  23. Function postShare,
  24. String subject,
  25. String body,
  26. }) async {
  27. final List<Widget> actions = [
  28. TextButton(
  29. child: Row(
  30. mainAxisAlignment: MainAxisAlignment.start,
  31. children: [
  32. Icon(
  33. Icons.feed_outlined,
  34. color: Colors.white.withOpacity(0.85),
  35. ),
  36. Padding(padding: EdgeInsets.all(4)),
  37. Text(
  38. "view logs",
  39. style: TextStyle(
  40. color: Colors.white.withOpacity(0.85),
  41. ),
  42. ),
  43. ],
  44. ),
  45. onPressed: () async {
  46. // routeToPage(context, LogFileViewer(SuperLogging.logFile));
  47. showDialog(
  48. context: context,
  49. builder: (BuildContext context) {
  50. return LogFileViewer(SuperLogging.logFile);
  51. },
  52. barrierColor: Colors.black87,
  53. barrierDismissible: false,
  54. );
  55. },
  56. ),
  57. TextButton(
  58. child: Text(
  59. title,
  60. style: TextStyle(
  61. color: Theme.of(context).buttonColor,
  62. ),
  63. ),
  64. onPressed: () async {
  65. Navigator.of(context, rootNavigator: true).pop('dialog');
  66. await _sendLogs(context, toEmail, subject, body);
  67. if (postShare != null) {
  68. postShare();
  69. }
  70. },
  71. ),
  72. ];
  73. final List<Widget> content = [];
  74. content.addAll(
  75. [
  76. Text(
  77. "this will send across logs and metrics that will help us debug your issue better",
  78. style: TextStyle(
  79. height: 1.5,
  80. fontFamily: 'Ubuntu',
  81. fontSize: 16,
  82. ),
  83. ),
  84. Padding(padding: EdgeInsets.all(12)),
  85. Row(
  86. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  87. children: actions,
  88. ),
  89. ],
  90. );
  91. final confirmation = AlertDialog(
  92. title: Text(
  93. title,
  94. style: TextStyle(
  95. fontSize: 18,
  96. ),
  97. ),
  98. content: SingleChildScrollView(
  99. child: ListBody(
  100. children: content,
  101. ),
  102. ),
  103. );
  104. showDialog(
  105. context: context,
  106. builder: (_) {
  107. return confirmation;
  108. },
  109. );
  110. }
  111. Future<void> _sendLogs(
  112. BuildContext context, String toEmail, String subject, String body) async {
  113. final dialog = createProgressDialog(context, "preparing logs...");
  114. await dialog.show();
  115. final tempPath = (await getTemporaryDirectory()).path;
  116. final zipFilePath = tempPath + "/logs.zip";
  117. final logsDirectory = Directory(tempPath + "/logs");
  118. var encoder = ZipFileEncoder();
  119. encoder.create(zipFilePath);
  120. encoder.addDirectory(logsDirectory);
  121. encoder.close();
  122. await dialog.hide();
  123. final Email email = Email(
  124. recipients: [toEmail],
  125. subject: subject,
  126. body: body,
  127. attachmentPaths: [zipFilePath],
  128. isHTML: false,
  129. );
  130. try {
  131. await FlutterEmailSender.send(email);
  132. } catch (e, s) {
  133. _logger.severe('email sender failed', e, s);
  134. final result = await showChoiceDialog(
  135. context, "email logs", "please send the logs to $toEmail",
  136. firstAction: "copy email", secondAction: "ok");
  137. if (result != null && result == DialogUserChoice.firstChoice) {
  138. await Clipboard.setData(ClipboardData(text: toEmail));
  139. }
  140. await Share.shareFiles([zipFilePath]);
  141. }
  142. }