improve handling of downloads and documents
This commit is contained in:
@@ -55,6 +55,50 @@ class _WebViewState extends ConsumerState<WebView> {
|
|||||||
Timer? _onLoadStopDebounce;
|
Timer? _onLoadStopDebounce;
|
||||||
Timer? _periodicScreenshotUpdate;
|
Timer? _periodicScreenshotUpdate;
|
||||||
|
|
||||||
|
Future<bool> _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<void> _updateScreenshot() async {
|
Future<void> _updateScreenshot() async {
|
||||||
final screenshot = await widget.page.value.controller
|
final screenshot = await widget.page.value.controller
|
||||||
?.takeScreenshot(
|
?.takeScreenshot(
|
||||||
@@ -367,35 +411,18 @@ class _WebViewState extends ConsumerState<WebView> {
|
|||||||
widget.updatePage((page) => page.copyWith.title(title));
|
widget.updatePage((page) => page.copyWith.title(title));
|
||||||
},
|
},
|
||||||
onDownloadStartRequest: (controller, downloadStartRequest) async {
|
onDownloadStartRequest: (controller, downloadStartRequest) async {
|
||||||
final fileName = getDispositionFileName(
|
final handled = switch (downloadStartRequest.mimeType) {
|
||||||
downloadStartRequest.contentDisposition!,
|
'text/markdown' =>
|
||||||
);
|
await _downloadChat(downloadStartRequest, context),
|
||||||
|
_ => false
|
||||||
|
};
|
||||||
|
|
||||||
if (fileName != null) {
|
if (!handled) {
|
||||||
final entity = ChatEntity.fromFileName(fileName);
|
if (context.mounted) {
|
||||||
if (entity.name != null) {
|
await ui_helper.launchUrlFeedback(
|
||||||
final fileWrite = await ref
|
context,
|
||||||
.read(chatArchiveRepositoryProvider.notifier)
|
downloadStartRequest.url,
|
||||||
.archiveChat(fileName, downloadStartRequest.url);
|
mode: LaunchMode.platformDefault,
|
||||||
|
|
||||||
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(),
|
|
||||||
);
|
|
||||||
},
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user