[Android] Implement sharing images to GitJournal

Fixes #38
This commit is contained in:
Vishesh Handa
2020-05-13 18:30:33 +02:00
parent 1d68d3a4a5
commit 867f6df14c
4 changed files with 83 additions and 29 deletions

View File

@ -40,6 +40,17 @@
<data android:mimeType="text/*" /> <data android:mimeType="text/*" />
</intent-filter> </intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="image/*" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND_MULTIPLE" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="image/*" />
</intent-filter>
</activity> </activity>
<activity <activity

View File

@ -146,6 +146,7 @@ class _JournalAppState extends State<JournalApp> {
StreamSubscription _intentDataStreamSubscription; StreamSubscription _intentDataStreamSubscription;
String _sharedText; String _sharedText;
List<String> _sharedImages;
@override @override
void initState() { void initState() {
@ -194,30 +195,8 @@ class _JournalAppState extends State<JournalApp> {
} }
void _initShareSubscriptions() { 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 = () { var handleShare = () {
print("handleShare $_sharedText"); if (_sharedText == null && _sharedImages == null) {
if (_sharedText == null) {
return; return;
} }
@ -225,9 +204,35 @@ class _JournalAppState extends State<JournalApp> {
_navigatorKey.currentState.pushNamed("/newNote/$editor"); _navigatorKey.currentState.pushNamed("/newNote/$editor");
}; };
// For sharing images coming from outside the app while the app is in the memory
_intentDataStreamSubscription = ReceiveSharingIntent.getMediaStream()
.listen((List<SharedMediaFile> value) {
Log.d("Received Share $value");
if (value == null) return;
setState(() {
_sharedImages = value.map((f) => f.path)?.toList();
});
WidgetsBinding.instance.addPostFrameCallback((_) => handleShare());
}, onError: (err) {
Log.e("getIntentDataStream error: $err");
});
// For sharing images coming from outside the app while the app is closed
ReceiveSharingIntent.getInitialMedia().then((List<SharedMediaFile> value) {
Log.d("Received Share with App running $value");
if (value == null) return;
setState(() {
_sharedImages = value.map((f) => f.path)?.toList();
});
WidgetsBinding.instance.addPostFrameCallback((_) => handleShare());
});
// For sharing or opening text coming from outside the app while the app is in the memory
_intentDataStreamSubscription = _intentDataStreamSubscription =
ReceiveSharingIntent.getTextStream().listen((String value) { ReceiveSharingIntent.getTextStream().listen((String value) {
Log.i("Received Share $value"); Log.d("Received Share $value");
setState(() { setState(() {
_sharedText = value; _sharedText = value;
}); });
@ -238,7 +243,7 @@ class _JournalAppState extends State<JournalApp> {
// For sharing or opening text coming from outside the app while the app is closed // For sharing or opening text coming from outside the app while the app is closed
ReceiveSharingIntent.getInitialText().then((String value) { ReceiveSharingIntent.getInitialText().then((String value) {
Log.i("Received Share with App running $value"); Log.d("Received Share with App running $value");
setState(() { setState(() {
_sharedText = value; _sharedText = value;
}); });
@ -335,10 +340,20 @@ class _JournalAppState extends State<JournalApp> {
Log.i("EditorType: $et"); Log.i("EditorType: $et");
var rootFolder = widget.appState.notesFolder; var rootFolder = widget.appState.notesFolder;
var sharedImages = _sharedImages;
var sharedText = _sharedText;
_sharedText = null;
_sharedImages = null;
Log.d("sharedText: $sharedText");
Log.d("sharedImages: $sharedImages");
return NoteEditor.newNote( return NoteEditor.newNote(
getFolderForEditor(rootFolder, et), getFolderForEditor(rootFolder, et),
et, et,
existingText: _sharedText, existingText: sharedText,
existingImages: sharedImages,
); );
} }

View File

@ -274,6 +274,14 @@ class Note with NotesNotifier {
body = "$body\n ![Image](./$imageFileName)\n"; body = "$body\n ![Image](./$imageFileName)\n";
} }
Future<void> addImageSync(File file) async {
var imageFileName = p.basename(file.path);
var imagePath = p.join(parent.folderPath, imageFileName);
file.copySync(imagePath);
body = "$body\n ![Image](./$imageFileName)\n";
}
@override @override
int get hashCode => _filePath.hashCode; int get hashCode => _filePath.hashCode;

View File

@ -1,3 +1,5 @@
import 'dart:io';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:collection/collection.dart'; import 'package:collection/collection.dart';
@ -25,22 +27,25 @@ class NoteEditor extends StatefulWidget {
final EditorType defaultEditorType; final EditorType defaultEditorType;
final String existingText; final String existingText;
final List<String> existingImages;
NoteEditor.fromNote(this.note) NoteEditor.fromNote(this.note)
: notesFolder = note.parent, : notesFolder = note.parent,
defaultEditorType = null, defaultEditorType = null,
existingText = null; existingText = null,
existingImages = null;
NoteEditor.newNote( NoteEditor.newNote(
this.notesFolder, this.notesFolder,
this.defaultEditorType, { this.defaultEditorType, {
this.existingText, this.existingText,
this.existingImages,
}) : note = null; }) : note = null;
@override @override
NoteEditorState createState() { NoteEditorState createState() {
if (note == null) { if (note == null) {
return NoteEditorState.newNote(notesFolder, existingText); return NoteEditorState.newNote(notesFolder, existingText, existingImages);
} else { } else {
return NoteEditorState.fromNote(note); return NoteEditorState.fromNote(note);
} }
@ -63,11 +68,26 @@ class NoteEditorState extends State<NoteEditor> {
return widget.note == null; return widget.note == null;
} }
NoteEditorState.newNote(NotesFolderFS folder, String existingText) { NoteEditorState.newNote(
NotesFolderFS folder,
String existingText,
List<String> existingImages,
) {
note = Note.newNote(folder); note = Note.newNote(folder);
if (existingText != null) { if (existingText != null) {
note.body = existingText; note.body = existingText;
} }
if (existingImages != null) {
for (var imagePath in existingImages) {
try {
var file = File(imagePath);
note.addImageSync(file);
} catch (e) {
Log.e(e);
}
}
}
} }
NoteEditorState.fromNote(this.note) { NoteEditorState.fromNote(this.note) {