Improve PDF download on web

This commit is contained in:
Hillel Coren 2020-08-21 09:16:45 +03:00
parent 8dd8449ad5
commit d1caa6efa6
5 changed files with 27 additions and 11 deletions

View File

@ -30,14 +30,14 @@ class _InvoiceViewHistoryState extends State<InvoiceViewHistory> {
final viewModel = widget.viewModel; final viewModel = widget.viewModel;
final invoice = viewModel.invoice; final invoice = viewModel.invoice;
final historyList = invoice.history.toList();
historyList.sort((a, b) => b.createdAt.compareTo(a.createdAt));
// TODO remove this null check, it shouldn't be needed // TODO remove this null check, it shouldn't be needed
if (invoice.isStale || invoice.history == null) { if (invoice.isStale || invoice.history == null) {
return LoadingIndicator(); return LoadingIndicator();
} }
final historyList = invoice.history.toList();
historyList.sort((a, b) => b.createdAt.compareTo(a.createdAt));
return ListView.separated( return ListView.separated(
shrinkWrap: true, shrinkWrap: true,
padding: const EdgeInsets.symmetric(vertical: 16), padding: const EdgeInsets.symmetric(vertical: 16),

View File

@ -359,7 +359,7 @@ class ReportsScreenVM {
'${state.uiState.reportsUIState.report}_report_$date.csv'; '${state.uiState.reportsUIState.report}_report_$date.csv';
if (kIsWeb) { if (kIsWeb) {
WebUtils.downloadFile(filename, csvData); WebUtils.downloadTextFile(filename, csvData);
} else { } else {
final directory = await getExternalStorageDirectory(); final directory = await getExternalStorageDirectory();
final filePath = '${directory.path}/$filename'; final filePath = '${directory.path}/$filename';

View File

@ -137,9 +137,10 @@ class _PDFScaffoldState extends State<PDFScaffold> {
onPressed: _response == null onPressed: _response == null
? null ? null
: () async { : () async {
final fileName = '${invoice.number}.pdf';
if (kIsWeb) { if (kIsWeb) {
launch(invoice.invitationDownloadLink, WebUtils.downloadBinaryFile(
forceSafariVC: false, forceWebView: false); fileName, _response.bodyBytes);
} else { } else {
final directory = await getExternalStorageDirectory(); final directory = await getExternalStorageDirectory();
final filePath = final filePath =
@ -147,7 +148,7 @@ class _PDFScaffoldState extends State<PDFScaffold> {
final pdfData = file.File(filePath); final pdfData = file.File(filePath);
pdfData.writeAsBytes(_response.bodyBytes); pdfData.writeAsBytes(_response.bodyBytes);
await FlutterShare.shareFile( await FlutterShare.shareFile(
title: 'test.pdf', filePath: filePath); title: fileName, filePath: filePath);
} }
}, },
), ),

View File

@ -1,5 +1,7 @@
import 'dart:async'; import 'dart:async';
import 'dart:convert';
import 'dart:html'; import 'dart:html';
import 'dart:typed_data';
import 'dart:ui' as ui; import 'dart:ui' as ui;
import 'package:invoiceninja_flutter/redux/app/app_state.dart'; import 'package:invoiceninja_flutter/redux/app/app_state.dart';
import 'package:invoiceninja_flutter/utils/formatting.dart'; import 'package:invoiceninja_flutter/utils/formatting.dart';
@ -27,13 +29,22 @@ class WebUtils {
return completer.future; return completer.future;
} }
static void downloadFile(String filename, String data) { static void downloadTextFile(String filename, String data) {
final encodedFileContents = Uri.encodeComponent(data); final encodedFileContents = Uri.encodeComponent(data);
AnchorElement(href: 'data:text/plain;charset=utf-8,$encodedFileContents') AnchorElement(href: 'data:text/plain;charset=utf-8,$encodedFileContents')
..setAttribute('download', filename) ..setAttribute('download', filename)
..click(); ..click();
} }
static void downloadBinaryFile(String filename, Uint8List data) {
final encodedFileContents = base64Encode(data);
AnchorElement(
href:
'data:application/octet-stream;charset=utf-16le;base64,$encodedFileContents')
..setAttribute('download', filename)
..click();
}
static void reloadBrowser() => window.location.reload(); static void reloadBrowser() => window.location.reload();
static void registerWebView(String html) { static void registerWebView(String html) {
@ -54,7 +65,7 @@ class WebUtils {
}); });
} }
/* /*
String loadToken() { String loadToken() {
final cookies = document.cookie; final cookies = document.cookie;
final List<String> listValues = final List<String> listValues =

View File

@ -1,3 +1,5 @@
import 'dart:typed_data';
import 'package:invoiceninja_flutter/redux/app/app_state.dart'; import 'package:invoiceninja_flutter/redux/app/app_state.dart';
import 'package:redux/redux.dart'; import 'package:redux/redux.dart';
@ -6,7 +8,9 @@ class WebUtils {
static Future<String> filePicker() => null; static Future<String> filePicker() => null;
static void downloadFile(String filename, String data) {} static void downloadTextFile(String filename, String data) {}
static void downloadBinaryFile(String filename, Uint8List data) {}
static void reloadBrowser() {} static void reloadBrowser() {}
@ -14,7 +18,7 @@ class WebUtils {
static void warnChanges(Store<AppState> store) {} static void warnChanges(Store<AppState> store) {}
/* /*
static String loadToken() => null; static String loadToken() => null;
static void saveToken(String token) {} static void saveToken(String token) {}