[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/*" />
</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

View File

@ -146,6 +146,7 @@ class _JournalAppState extends State<JournalApp> {
StreamSubscription _intentDataStreamSubscription;
String _sharedText;
List<String> _sharedImages;
@override
void initState() {
@ -194,30 +195,8 @@ 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) {
if (_sharedText == null && _sharedImages == null) {
return;
}
@ -225,9 +204,35 @@ class _JournalAppState extends State<JournalApp> {
_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 =
ReceiveSharingIntent.getTextStream().listen((String value) {
Log.i("Received Share $value");
Log.d("Received Share $value");
setState(() {
_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
ReceiveSharingIntent.getInitialText().then((String value) {
Log.i("Received Share with App running $value");
Log.d("Received Share with App running $value");
setState(() {
_sharedText = value;
});
@ -335,10 +340,20 @@ class _JournalAppState extends State<JournalApp> {
Log.i("EditorType: $et");
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(
getFolderForEditor(rootFolder, et),
et,
existingText: _sharedText,
existingText: sharedText,
existingImages: sharedImages,
);
}

View File

@ -274,6 +274,14 @@ class Note with NotesNotifier {
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
int get hashCode => _filePath.hashCode;

View File

@ -1,3 +1,5 @@
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:collection/collection.dart';
@ -25,22 +27,25 @@ class NoteEditor extends StatefulWidget {
final EditorType defaultEditorType;
final String existingText;
final List<String> existingImages;
NoteEditor.fromNote(this.note)
: notesFolder = note.parent,
defaultEditorType = null,
existingText = null;
existingText = null,
existingImages = null;
NoteEditor.newNote(
this.notesFolder,
this.defaultEditorType, {
this.existingText,
this.existingImages,
}) : note = null;
@override
NoteEditorState createState() {
if (note == null) {
return NoteEditorState.newNote(notesFolder, existingText);
return NoteEditorState.newNote(notesFolder, existingText, existingImages);
} else {
return NoteEditorState.fromNote(note);
}
@ -63,11 +68,26 @@ class NoteEditorState extends State<NoteEditor> {
return widget.note == null;
}
NoteEditorState.newNote(NotesFolderFS folder, String existingText) {
NoteEditorState.newNote(
NotesFolderFS folder,
String existingText,
List<String> existingImages,
) {
note = Note.newNote(folder);
if (existingText != null) {
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) {