From b06d7dd0dc88e11a096821ddd63f07fd4290ac1a Mon Sep 17 00:00:00 2001 From: Fabian Freund Date: Tue, 25 Jun 2024 14:13:45 +0200 Subject: [PATCH] improve handling of downloads and documents --- .../presentation/widgets/web_view.dart | 83 ++++++++++++------- 1 file changed, 55 insertions(+), 28 deletions(-) diff --git a/app/lib/features/web_view/presentation/widgets/web_view.dart b/app/lib/features/web_view/presentation/widgets/web_view.dart index 9e569727..3815d6ab 100644 --- a/app/lib/features/web_view/presentation/widgets/web_view.dart +++ b/app/lib/features/web_view/presentation/widgets/web_view.dart @@ -55,6 +55,50 @@ class _WebViewState extends ConsumerState { Timer? _onLoadStopDebounce; Timer? _periodicScreenshotUpdate; + Future _downloadChat( + DownloadStartRequest downloadStartRequest, + BuildContext context, + ) async { + final fileName = getDispositionFileName( + downloadStartRequest.contentDisposition!, + ); + + if (fileName != null) { + final entity = ChatEntity.fromFileName(fileName); + if (entity.name != null) { + final fileWrite = await ref + .read(chatArchiveRepositoryProvider.notifier) + .archiveChat(fileName, downloadStartRequest.url); + + return fileWrite.fold( + (_) { + ScaffoldMessenger.of(context).showSnackBar( + SnackBar( + backgroundColor: Colors.green, + content: Text( + 'Conversation "$entity" saved successfully!', + style: const TextStyle(color: Colors.white), + ), + ), + ); + + return true; + }, + onFailure: (errorMessage) { + ui_helper.showErrorMessage( + context, + errorMessage.toString(), + ); + + return false; + }, + ); + } + } + + return false; + } + Future _updateScreenshot() async { final screenshot = await widget.page.value.controller ?.takeScreenshot( @@ -367,35 +411,18 @@ class _WebViewState extends ConsumerState { widget.updatePage((page) => page.copyWith.title(title)); }, onDownloadStartRequest: (controller, downloadStartRequest) async { - final fileName = getDispositionFileName( - downloadStartRequest.contentDisposition!, - ); + final handled = switch (downloadStartRequest.mimeType) { + 'text/markdown' => + await _downloadChat(downloadStartRequest, context), + _ => false + }; - if (fileName != null) { - final entity = ChatEntity.fromFileName(fileName); - if (entity.name != null) { - final fileWrite = await ref - .read(chatArchiveRepositoryProvider.notifier) - .archiveChat(fileName, downloadStartRequest.url); - - fileWrite.map( - onSuccess: (_) { - ScaffoldMessenger.of(context).showSnackBar( - SnackBar( - backgroundColor: Colors.green, - content: Text( - 'Conversation "$entity" saved successfully!', - style: const TextStyle(color: Colors.white), - ), - ), - ); - }, - onFailure: (errorMessage) { - ui_helper.showErrorMessage( - context, - errorMessage.toString(), - ); - }, + if (!handled) { + if (context.mounted) { + await ui_helper.launchUrlFeedback( + context, + downloadStartRequest.url, + mode: LaunchMode.platformDefault, ); } }