From c1c4740095fa68eb6ad67ca20249a49a170d18f9 Mon Sep 17 00:00:00 2001 From: Quinn Casey Date: Tue, 15 Dec 2020 09:24:48 -0800 Subject: [PATCH] Android Share Sheet intent (#26) * initial share sheet addition * add scanAmount to card * use unique shareAmount to bypass scan listener * commit new lock as per dart docs * revert .plural * use pubspec.lock from 0.0.7 release --- android/app/src/main/AndroidManifest.xml | 20 ++++++++++ lib/routes/documents_route.dart | 50 +++++++++++++++++++++++- pubspec.yaml | 1 + 3 files changed, 69 insertions(+), 2 deletions(-) diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml index ad0b8ae..33bf1ec 100644 --- a/android/app/src/main/AndroidManifest.xml +++ b/android/app/src/main/AndroidManifest.xml @@ -26,6 +26,26 @@ + + + + + + + + + + + + + + + + + + + + diff --git a/lib/routes/documents_route.dart b/lib/routes/documents_route.dart index 965fc0b..97b685b 100644 --- a/lib/routes/documents_route.dart +++ b/lib/routes/documents_route.dart @@ -9,6 +9,8 @@ import 'package:shared_preferences/shared_preferences.dart'; import 'package:cached_network_image/cached_network_image.dart'; import 'package:flutter_secure_storage/flutter_secure_storage.dart'; import 'package:get_it/get_it.dart'; +import 'package:receive_sharing_intent/receive_sharing_intent.dart'; +import 'dart:async'; import 'package:paperless_app/routes/server_details_route.dart'; import 'package:paperless_app/routes/settings_route.dart'; @@ -40,7 +42,10 @@ class _DocumentsRouteState extends State { String searchString; bool invertDocumentPreview = true; int scanAmount = 0; + int shareAmount = 0; ScanHandler scanHandler = ScanHandler(); + StreamSubscription intentDataStreamSubscription; + List sharedFiles; final List invertMatrix = [ -1, 0, 0, 0, 255, // @@ -288,7 +293,7 @@ class _DocumentsRouteState extends State { preferredSize: Size.fromHeight(5), ), Padding( - child: scanAmount > 0 + child: scanAmount > 0 || shareAmount > 0 ? Card( child: Row( mainAxisAlignment: MainAxisAlignment.center, @@ -298,7 +303,8 @@ class _DocumentsRouteState extends State { SizedBox(width: 10), Flexible( child: Text( - "Uploading 1 scanned document".plural(scanAmount), + "Uploading 1 scanned document" + .plural(scanAmount + shareAmount), textAlign: TextAlign.center, )), SizedBox(width: 10), @@ -351,6 +357,44 @@ class _DocumentsRouteState extends State { }); } + void uploadSharedDocuments() async { + if (sharedFiles != null && sharedFiles.isNotEmpty) { + for (var f in sharedFiles) { + await API.instance.uploadFile(f.path); + setState(() { + shareAmount--; + }); + } + } + } + + void loadShareSheet() { + // For sharing images coming from outside the app while the app is in the memory + intentDataStreamSubscription = ReceiveSharingIntent.getMediaStream().listen( + (List value) { + setState(() { + sharedFiles = value; + if (sharedFiles != null) { + shareAmount += sharedFiles.length; + } + }); + uploadSharedDocuments(); + }, onError: (err) { + print("getIntentDataStream error: $err"); + }); + + // For sharing images coming from outside the app while the app is closed + ReceiveSharingIntent.getInitialMedia().then((List value) { + setState(() { + sharedFiles = value; + if (sharedFiles != null) { + shareAmount += sharedFiles.length; + } + }); + uploadSharedDocuments(); + }); + } + @override void initState() { reloadDocuments(); @@ -361,12 +405,14 @@ class _DocumentsRouteState extends State { dateFormat = new DateFormat.yMMMMd(); scrollController = new ScrollController()..addListener(_scrollListener); scanHandler.attachListener(onScanAmountChange); + loadShareSheet(); super.initState(); } @override void dispose() { scrollController.removeListener(_scrollListener); + intentDataStreamSubscription.cancel(); super.dispose(); } diff --git a/pubspec.yaml b/pubspec.yaml index c5a18c9..d367d59 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -33,6 +33,7 @@ dependencies: open_file: ^3.0.1 settings_ui: ^0.2.0 shared_preferences: '>=0.5.7+2 <2.0.0' + receive_sharing_intent: ^1.4.2 sprintf: "^4.0.0" edge_detection: git: