From 9d7ab7279f510fef644976c41d137942a81db6cc Mon Sep 17 00:00:00 2001 From: Hillel Coren Date: Thu, 26 Dec 2019 21:40:59 +0200 Subject: [PATCH] PDF support --- lib/utils/i18n.dart | 3 +++ lib/utils/pdf.dart | 15 +++++++++------ lib/utils/platforms.dart | 11 +++++++++++ pubspec.lock | 30 +++++++++++++++++++++++++++++- pubspec.yaml | 3 ++- 5 files changed, 54 insertions(+), 8 deletions(-) diff --git a/lib/utils/i18n.dart b/lib/utils/i18n.dart index 276d79b40..356218168 100644 --- a/lib/utils/i18n.dart +++ b/lib/utils/i18n.dart @@ -14,6 +14,7 @@ abstract class LocaleCodeAware { mixin LocalizationsProvider on LocaleCodeAware { static final Map> _localizedValues = { 'en': { + 'pdf_min_requirements': 'The PDF renderer requires :version', 'adjust_fee_percent': 'Adjust Fee Percent', 'adjust_fee_percent_help': 'Ensure client fee matches the gateway fee', 'configure_settings': 'Configure Settings', @@ -15866,6 +15867,8 @@ mixin LocalizationsProvider on LocaleCodeAware { String get adjustFeePercentHelp => _localizedValues[localeCode]['adjust_fee_percent_help']; + String get pdfMinRequirements => _localizedValues[localeCode]['pdf_min_requirements']; + String lookup(String key) { final lookupKey = toSnakeCase(key); return _localizedValues[localeCode][lookupKey] ?? diff --git a/lib/utils/pdf.dart b/lib/utils/pdf.dart index 9d9731833..fd55be67a 100644 --- a/lib/utils/pdf.dart +++ b/lib/utils/pdf.dart @@ -5,10 +5,11 @@ import 'package:invoiceninja_flutter/data/models/invoice_model.dart'; import 'package:flutter/foundation.dart'; import 'package:invoiceninja_flutter/ui/app/loading_indicator.dart'; import 'package:invoiceninja_flutter/utils/localization.dart'; +import 'package:invoiceninja_flutter/utils/platforms.dart'; +import 'package:native_pdf_view/native_pdf_view.dart'; import 'package:url_launcher/url_launcher.dart'; import 'package:native_pdf_renderer/native_pdf_renderer.dart'; - Future viewPdf(InvoiceEntity invoice, BuildContext context) async { final localization = AppLocalization.of(context); if (Platform.isIOS) { @@ -56,7 +57,8 @@ Future viewPdf(InvoiceEntity invoice, BuildContext context) async { return LoadingIndicator(); case ConnectionState.done: if (snapshot.hasError) - return Text('Error: ${snapshot.error}'); + return Text( + '${getPdfRequirements(context)} - Error: ${snapshot.error}'); else return snapshot.data.length == 1 ? Center( @@ -80,10 +82,11 @@ Future viewPdf(InvoiceEntity invoice, BuildContext context) async { ), Container( color: Colors.white, - child: Image( - image: - MemoryImage(page.bytes), - height: double.infinity), + child: ExtendedImage.memory( + page.bytes, + fit: BoxFit.fitHeight, + mode: ExtendedImageMode.gesture, + ), ), ], )) diff --git a/lib/utils/platforms.dart b/lib/utils/platforms.dart index 93c2379ee..2cd7f8744 100644 --- a/lib/utils/platforms.dart +++ b/lib/utils/platforms.dart @@ -3,6 +3,7 @@ import 'package:flutter_redux/flutter_redux.dart'; import 'package:invoiceninja_flutter/constants.dart'; import 'package:invoiceninja_flutter/redux/app/app_state.dart'; import 'package:invoiceninja_flutter/redux/ui/pref_state.dart'; +import 'package:invoiceninja_flutter/utils/localization.dart'; bool isAndroid(BuildContext context) => Theme.of(context).platform == TargetPlatform.android; @@ -15,6 +16,16 @@ String getLegacyAppURL(BuildContext context) => isAndroid(context) ? 'https://play.google.com/store/apps/details?id=com.invoiceninja.invoiceninja' : 'https://itunes.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=1220337560&mt=8'; +String getPdfRequirements(BuildContext context) { + final localization = AppLocalization.of(context); + if (isMobile(context)) { + final version = isAndroid(context) ? 'Android 5.0+' : 'iOS 11.0+'; + return localization.pdfMinRequirements.replaceFirst(':version', version); + } else { + return ''; + } +} + String getPlatform(BuildContext context) => Theme.of(context).platform == TargetPlatform.iOS ? 'ios' : 'android'; diff --git a/pubspec.lock b/pubspec.lock index 84d6ece20..0b3f09d3a 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -190,6 +190,20 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.3.3" + extended_image: + dependency: transitive + description: + name: extended_image + url: "https://pub.dartlang.org" + source: hosted + version: "0.6.8" + extended_image_library: + dependency: transitive + description: + name: extended_image_library + url: "https://pub.dartlang.org" + source: hosted + version: "0.1.9" extension: dependency: transitive description: @@ -395,6 +409,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "0.12.0+2" + http_client_helper: + dependency: transitive + description: + name: http_client_helper + url: "https://pub.dartlang.org" + source: hosted + version: "0.2.1" http_multi_server: dependency: transitive description: @@ -522,12 +543,19 @@ packages: source: hosted version: "1.0.2" native_pdf_renderer: - dependency: "direct main" + dependency: transitive description: name: native_pdf_renderer url: "https://pub.dartlang.org" source: hosted version: "1.6.1" + native_pdf_view: + dependency: "direct main" + description: + name: native_pdf_view + url: "https://pub.dartlang.org" + source: hosted + version: "2.1.0" node_interop: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index d97124886..a2d8dccdc 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -40,7 +40,8 @@ dependencies: flutter_json_widget: ^1.0.2 webview_flutter: ^0.3.15+1 timeago: ^2.0.22 - native_pdf_renderer: any + #native_pdf_renderer: any + native_pdf_view: any flutter_typeahead: ^1.7.0 #quick_actions: ^0.2.1