diff --git a/lib/utils/pdf.dart b/lib/utils/pdf.dart index 0751f1b3a..701545e88 100644 --- a/lib/utils/pdf.dart +++ b/lib/utils/pdf.dart @@ -1,73 +1,47 @@ import 'dart:async'; +import 'dart:io'; +import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:invoiceninja_flutter/data/models/invoice_model.dart'; -import 'package:invoiceninja_flutter/utils/localization.dart'; -import 'package:url_launcher/url_launcher.dart'; -//import 'package:flutter_plugin_pdf_viewer/flutter_plugin_pdf_viewer.dart'; +import 'package:invoiceninja_flutter/ui/app/loading_indicator.dart'; +import 'package:path_provider/path_provider.dart'; +import 'package:flutter_pdfview/flutter_pdfview.dart'; Future viewPdf(InvoiceEntity invoice, BuildContext context) async { - /* - final navigator = Navigator.of(context); - navigator.push( - MaterialPageRoute(builder: (context) => PDFScreen(invoice))); - */ - final localization = AppLocalization.of(context); - if (Theme.of(context).platform == TargetPlatform.iOS) { - if (await canLaunch(invoice.invitationBorderlessLink)) { - await launch(invoice.invitationBorderlessLink, - forceSafariVC: true, forceWebView: true); - } else { - throw localization.anErrorOccurred; - } - } else { - final String url = - 'https://docs.google.com/viewer?url=' + invoice.invitationDownloadLink; - if (await canLaunch(url)) { - await launch(url, forceSafariVC: false, forceWebView: false); - } else { - throw localization.anErrorOccurred; - } - } + showDialog( + context: context, + builder: (BuildContext context) { + return Scaffold( + appBar: AppBar(), + body: FutureBuilder( + future: createFileOfPdfUrl(invoice.invitationDownloadLink), + builder: (BuildContext context, AsyncSnapshot snapshot) { + switch (snapshot.connectionState) { + case ConnectionState.none: + case ConnectionState.active: + case ConnectionState.waiting: + return LoadingIndicator(); + case ConnectionState.done: + if (snapshot.hasError) + return Text('Error: ${snapshot.error}'); + else + return PDFView(filePath: snapshot.data.path); + } + return null; // unreachable + }), + ); + }); + } -/* -class PDFScreen extends StatefulWidget { - const PDFScreen(this.invoice); - - final InvoiceEntity invoice; - - @override - _PDFScreenState createState() => _PDFScreenState(); +Future createFileOfPdfUrl(String url) async { + final filename = url.substring(url.lastIndexOf('/') + 1); + final request = await HttpClient().getUrl(Uri.parse(url)); + final response = await request.close(); + final bytes = await consolidateHttpClientResponseBytes(response); + final dir = (await getApplicationDocumentsDirectory()).path; + final file = new File('$dir/$filename'); + await file.writeAsBytes(bytes); + return file; } - -class _PDFScreenState extends State { - PDFDocument document; - - @override - void initState() { - super.initState(); - - PDFDocument.fromURL(widget.invoice.invitationDownloadLink).then((doc) { - setState(() => document = doc); - }); - } - - @override - Widget build(BuildContext context) { - return Scaffold( - appBar: AppBar( - title: Text('${widget.invoice.invoiceNumber}'), - ), - body: Center( - child: document == null - ? CircularProgressIndicator() - : PDFViewer( - showPicker: false, - document: document, - ), - ), - ); - } -} -*/ diff --git a/pubspec.lock b/pubspec.lock index ca8d175e5..19a9a4f1d 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -226,6 +226,13 @@ packages: description: flutter source: sdk version: "0.0.0" + flutter_pdfview: + dependency: "direct main" + description: + name: flutter_pdfview + url: "https://pub.dartlang.org" + source: hosted + version: "1.0.0+7" flutter_redux: dependency: "direct main" description: diff --git a/pubspec.yaml b/pubspec.yaml index 68b60df6c..09ba43ede 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -30,11 +30,8 @@ dependencies: local_auth: ^0.3.0 sentry: ^2.2.0 image_picker: ^0.6.0+17 - #flutter_plugin_pdf_viewer: any + flutter_pdfview: ^1.0.0+7 #quick_actions: ^0.2.1 - #flutter_html_view: ^0.5.8 - #flutter_html: ^0.5.3 - #flutter_native_web: ^0.1.2 dev_dependencies: flutter_driver: