Skip to content

Commit 9cd0884

Browse files
committed
support to specify own fetching method
Setting your own fetching method can be useful for downloading the message contents e.g. from disk. MimeMessageDownloader also support setting download timeouts now.
1 parent bc369fe commit 9cd0884

File tree

2 files changed

+30
-20
lines changed

2 files changed

+30
-20
lines changed

lib/src/mime_message_downloader.dart

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@ class MimeMessageDownloader extends StatefulWidget {
1515
Key? key,
1616
required this.mimeMessage,
1717
required this.mailClient,
18+
this.fetchMessageContents,
1819
this.maxDownloadSize = 128 * 1024,
1920
this.maxImageWidth,
2021
this.downloadErrorMessage = 'Unable to download message.',
2122
this.markAsSeen = false,
23+
this.downloadTimeout,
2224
this.includedInlineTypes,
2325
this.onDownloaded,
2426
this.adjustHeight = true,
@@ -41,6 +43,16 @@ class MimeMessageDownloader extends StatefulWidget {
4143
/// The high level mail client to download message contents
4244
final MailClient mailClient;
4345

46+
/// The optional alternative message loader,
47+
/// for example to load the message from disk
48+
final Future<MimeMessage> Function(
49+
MimeMessage message, {
50+
int? maxSize,
51+
bool markAsSeen,
52+
List<MediaToptype>? includedInlineTypes,
53+
Duration? responseTimeout,
54+
})? fetchMessageContents;
55+
4456
/// The maximum size in bytes of messages that are fully downloaded.
4557
/// The defaults to `128*1024` / `128kb`.
4658
///
@@ -61,6 +73,9 @@ class MimeMessageDownloader extends StatefulWidget {
6173
/// Optional list of media types to be shown inline
6274
final List<MediaToptype>? includedInlineTypes;
6375

76+
/// The optional timeout for the download operation
77+
final Duration? downloadTimeout;
78+
6479
/// Callback to get informed when the message has been downloaded
6580
final void Function(MimeMessage message)? onDownloaded;
6681

@@ -95,7 +110,7 @@ class MimeMessageDownloader extends StatefulWidget {
95110
/// Returns `true` when the given `url` was handled.
96111
final Future<bool> Function(String url)? urlLauncherDelegate;
97112

98-
/// Register this callback if you want a reference to the [WebViewController].
113+
/// Retrieve a reference to the [InAppWebViewController].
99114
final void Function(InAppWebViewController controller)? onWebViewCreated;
100115

101116
/// This callback will be called when the webview zooms out after loading.
@@ -181,29 +196,28 @@ class _MimeMessageDownloaderState extends State<MimeMessageDownloader> {
181196
Future<MimeMessage> _downloadMessageContents() async {
182197
try {
183198
// print('download message UID ${mimeMessage.uid} for state $this');
184-
mimeMessage = await widget.mailClient.fetchMessageContents(
199+
final fetchCall =
200+
widget.fetchMessageContents ?? widget.mailClient.fetchMessageContents;
201+
202+
mimeMessage = await fetchCall(
185203
widget.mimeMessage,
186204
maxSize: widget.maxDownloadSize,
187205
markAsSeen: widget.markAsSeen,
188206
includedInlineTypes: widget.includedInlineTypes,
207+
responseTimeout: widget.downloadTimeout,
189208
);
190-
191-
if (widget.onDownloaded != null) {
192-
widget.onDownloaded!(mimeMessage);
193-
}
209+
widget.onDownloaded?.call(mimeMessage);
194210
} on MailException catch (e, s) {
195-
if (widget.onError != null) {
196-
widget.onError!(e, s);
197-
} else {
211+
widget.onError?.call(e, s);
212+
if (widget.onError == null) {
198213
print('Unable to download message '
199214
'${widget.mimeMessage.decodeSubject()}: $e $s');
200215
}
201216
} catch (e, s) {
202-
print(
203-
'unexpected exception while downloading message with UID ${widget.mimeMessage.uid} / ID ${widget.mimeMessage.sequenceId}: $e $s');
204-
if (widget.onError != null) {
205-
widget.onError!(e, s);
206-
}
217+
print('unexpected exception while downloading message with '
218+
'UID ${widget.mimeMessage.uid} / '
219+
'ID ${widget.mimeMessage.sequenceId}: $e $s');
220+
widget.onError?.call(e, s);
207221
}
208222
return mimeMessage;
209223
}

lib/src/mime_message_viewer.dart

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,7 @@ class MimeMessageViewer extends StatelessWidget {
7474
/// Returns `true` when the given `url` was handled.
7575
final Future<bool> Function(String url)? urlLauncherDelegate;
7676

77-
/// Register this callback
78-
/// to get a reference to the [InAppWebViewController].
77+
/// Retrieve a reference to the [InAppWebViewController].
7978
final void Function(InAppWebViewController controller)? onWebViewCreated;
8079

8180
/// This callback will be called when the webview zooms out after loading.
@@ -181,10 +180,7 @@ class _HtmlViewerState extends State<_HtmlMimeMessageViewer> {
181180
final result = await compute(_generateHtmlImpl, args);
182181
_htmlData = result.html;
183182
if (_htmlData == null) {
184-
final onError = widget.config.onError;
185-
if (onError != null) {
186-
onError(result.errorDetails, null);
187-
}
183+
widget.config.onError?.call(result.errorDetails, null);
188184
}
189185
if (mounted) {
190186
setState(() {

0 commit comments

Comments
 (0)