diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml index a196f863..3bbb5db3 100644 --- a/android/app/src/main/AndroidManifest.xml +++ b/android/app/src/main/AndroidManifest.xml @@ -32,6 +32,14 @@ + + + + + + + + { final _navigatorKey = GlobalKey(); String _pendingShortcut; + StreamSubscription _intentDataStreamSubscription; + String _sharedText; + @override void initState() { super.initState(); @@ -176,6 +181,8 @@ class _JournalAppState extends State { ]); print("Nav key $_navigatorKey"); + + _initShareSubscriptions(); } void _afterBuild(BuildContext context) { @@ -186,6 +193,65 @@ class _JournalAppState extends State { } } + void _initShareSubscriptions() { + // For sharing images coming from outside the app while the app is in the memory + /* + _intentDataStreamSubscription = + ReceiveSharingIntent.getMediaStream().listen((List value) { + setState(() { + print("Shared:" + (_sharedFiles?.map((f)=> f.path)?.join(",") ?? "")); + _sharedFiles = value; + }); + }, 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; + }); + }); + */ + + // For sharing or opening text coming from outside the app while the app is in the memory + var handleShare = () { + print("handleShare $_sharedText"); + if (_sharedText == null) { + return; + } + + var editor = Settings.instance.defaultEditor.toInternalString(); + _navigatorKey.currentState.pushNamed("/newNote/$editor"); + }; + + _intentDataStreamSubscription = + ReceiveSharingIntent.getTextStream().listen((String value) { + Log.i("Received Share $value"); + setState(() { + _sharedText = value; + }); + WidgetsBinding.instance.addPostFrameCallback((_) => handleShare()); + }, onError: (err) { + Log.e("getLinkStream error: $err"); + }); + + // For sharing or opening text coming from outside the app while the app is closed + ReceiveSharingIntent.getInitialText().then((String value) { + Log.i("Received Share with App running $value"); + setState(() { + _sharedText = value; + }); + WidgetsBinding.instance.addPostFrameCallback((_) => handleShare()); + }); + } + + @override + void dispose() { + _intentDataStreamSubscription.cancel(); + super.dispose(); + } + @override Widget build(BuildContext context) { return DynamicTheme( @@ -269,7 +335,11 @@ class _JournalAppState extends State { Log.i("EditorType: $et"); var rootFolder = widget.appState.notesFolder; - return NoteEditor.newNote(getFolderForEditor(rootFolder, et), et); + return NoteEditor.newNote( + getFolderForEditor(rootFolder, et), + et, + existingText: _sharedText, + ); } assert(false, "Not found named route in _screenForRoute"); diff --git a/lib/screens/note_editor.dart b/lib/screens/note_editor.dart index d6c94b81..2b081704 100644 --- a/lib/screens/note_editor.dart +++ b/lib/screens/note_editor.dart @@ -24,15 +24,23 @@ class NoteEditor extends StatefulWidget { final NotesFolderFS notesFolder; final EditorType defaultEditorType; + final String existingText; + NoteEditor.fromNote(this.note) : notesFolder = note.parent, - defaultEditorType = null; - NoteEditor.newNote(this.notesFolder, this.defaultEditorType) : note = null; + defaultEditorType = null, + existingText = null; + + NoteEditor.newNote( + this.notesFolder, + this.defaultEditorType, { + this.existingText, + }) : note = null; @override NoteEditorState createState() { if (note == null) { - return NoteEditorState.newNote(notesFolder); + return NoteEditorState.newNote(notesFolder, existingText); } else { return NoteEditorState.fromNote(note); } @@ -55,8 +63,11 @@ class NoteEditorState extends State { return widget.note == null; } - NoteEditorState.newNote(NotesFolderFS folder) { + NoteEditorState.newNote(NotesFolderFS folder, String existingText) { note = Note.newNote(folder); + if (existingText != null) { + note.body = existingText; + } } NoteEditorState.fromNote(this.note) { diff --git a/pubspec.lock b/pubspec.lock index f93d03db..06e4adde 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -672,6 +672,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "2.1.3" + receive_sharing_intent: + dependency: "direct main" + description: + name: receive_sharing_intent + url: "https://pub.dartlang.org" + source: hosted + version: "1.4.0+2" resource: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index 912c9d90..8c1bc8c3 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -56,6 +56,7 @@ dependencies: easy_localization: ^2.2.1 easy_localization_loader: ^0.0.2 quick_actions: ^0.4.0+5 + receive_sharing_intent: ^1.4.0+2 dev_dependencies: flutter_launcher_icons: "^0.7.2"