[Android] Implement sharing text to GitJournal

Related to #38
This commit is contained in:
Vishesh Handa
2020-05-13 17:40:23 +02:00
parent b3facb16de
commit 1d68d3a4a5
5 changed files with 102 additions and 5 deletions

View File

@ -32,6 +32,14 @@
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
<!-- Sharing Related -->
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/*" />
</intent-filter>
</activity>
<activity

View File

@ -1,3 +1,4 @@
import 'dart:async';
import 'dart:io';
import 'package:device_info/device_info.dart';
@ -19,6 +20,7 @@ import 'package:easy_localization_loader/easy_localization_loader.dart';
import 'package:git_bindings/git_bindings.dart';
import 'package:quick_actions/quick_actions.dart';
import 'package:receive_sharing_intent/receive_sharing_intent.dart';
import 'package:gitjournal/apis/git.dart';
import 'package:gitjournal/settings.dart';
@ -142,6 +144,9 @@ class _JournalAppState extends State<JournalApp> {
final _navigatorKey = GlobalKey<NavigatorState>();
String _pendingShortcut;
StreamSubscription _intentDataStreamSubscription;
String _sharedText;
@override
void initState() {
super.initState();
@ -176,6 +181,8 @@ class _JournalAppState extends State<JournalApp> {
]);
print("Nav key $_navigatorKey");
_initShareSubscriptions();
}
void _afterBuild(BuildContext context) {
@ -186,6 +193,65 @@ class _JournalAppState extends State<JournalApp> {
}
}
void _initShareSubscriptions() {
// For sharing images coming from outside the app while the app is in the memory
/*
_intentDataStreamSubscription =
ReceiveSharingIntent.getMediaStream().listen((List<SharedMediaFile> 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<SharedMediaFile> 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<JournalApp> {
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");

View File

@ -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<NoteEditor> {
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) {

View File

@ -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:

View File

@ -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"