Receive sharing intent
This commit is contained in:
parent
a0828535d5
commit
701c4d2757
11 changed files with 167 additions and 77 deletions
67
lib/components/image_bubble.dart
Normal file
67
lib/components/image_bubble.dart
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
import 'package:bubble/bubble.dart';
|
||||
import 'package:famedlysdk/famedlysdk.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:fluffychat/utils/matrix_file_extension.dart';
|
||||
|
||||
class ImageBubble extends StatefulWidget {
|
||||
final Event event;
|
||||
|
||||
const ImageBubble(this.event, {Key key}) : super(key: key);
|
||||
|
||||
@override
|
||||
_ImageBubbleState createState() => _ImageBubbleState();
|
||||
}
|
||||
|
||||
class _ImageBubbleState extends State<ImageBubble> {
|
||||
MatrixFile _file;
|
||||
dynamic _error;
|
||||
|
||||
Future<MatrixFile> _getFile() async {
|
||||
if (_file != null) return _file;
|
||||
return widget.event.downloadAndDecryptAttachment();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final int size = 400;
|
||||
return Bubble(
|
||||
padding: BubbleEdges.all(0),
|
||||
radius: Radius.circular(10),
|
||||
elevation: 0,
|
||||
child: Container(
|
||||
height: size.toDouble(),
|
||||
width: size.toDouble(),
|
||||
child: Builder(
|
||||
builder: (BuildContext context) {
|
||||
if (_error != null) {
|
||||
return Center(
|
||||
child: Text(
|
||||
_error.toString(),
|
||||
),
|
||||
);
|
||||
}
|
||||
if (_file != null) {
|
||||
return InkWell(
|
||||
onTap: () => _file.open(),
|
||||
child: Image.memory(
|
||||
_file.bytes,
|
||||
width: size.toDouble(),
|
||||
height: size.toDouble(),
|
||||
fit: BoxFit.fill,
|
||||
),
|
||||
);
|
||||
}
|
||||
_getFile().then((MatrixFile file) {
|
||||
setState(() => _file = file);
|
||||
}, onError: (error) {
|
||||
setState(() => _error = error);
|
||||
});
|
||||
return Center(
|
||||
child: CircularProgressIndicator(),
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
@ -77,7 +77,16 @@ class ChatListItem extends StatelessWidget {
|
|||
|
||||
if (room.membership == Membership.join) {
|
||||
if (Matrix.of(context).shareContent != null) {
|
||||
unawaited(room.sendEvent(Matrix.of(context).shareContent));
|
||||
if (Matrix.of(context).shareContent["msgtype"] ==
|
||||
"chat.fluffy.shared_file") {
|
||||
await Matrix.of(context).tryRequestWithErrorToast(
|
||||
room.sendFileEvent(
|
||||
Matrix.of(context).shareContent["file"],
|
||||
),
|
||||
);
|
||||
} else {
|
||||
unawaited(room.sendEvent(Matrix.of(context).shareContent));
|
||||
}
|
||||
Matrix.of(context).shareContent = null;
|
||||
}
|
||||
await Navigator.pushAndRemoveUntil(
|
||||
|
|
|
|||
|
|
@ -394,7 +394,6 @@ class _InheritedMatrix extends InheritedWidget {
|
|||
bool update = old.data.client.accessToken != this.data.client.accessToken ||
|
||||
old.data.client.userID != this.data.client.userID ||
|
||||
old.data.client.matrixVersions != this.data.client.matrixVersions ||
|
||||
old.data.client.lazyLoadMembers != this.data.client.lazyLoadMembers ||
|
||||
old.data.client.deviceID != this.data.client.deviceID ||
|
||||
old.data.client.deviceName != this.data.client.deviceName ||
|
||||
old.data.client.homeserver != this.data.client.homeserver;
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
import 'package:bubble/bubble.dart';
|
||||
import 'package:famedlysdk/famedlysdk.dart';
|
||||
import 'package:fluffychat/components/audio_player.dart';
|
||||
import 'package:fluffychat/components/image_bubble.dart';
|
||||
import 'package:fluffychat/i18n/i18n.dart';
|
||||
import 'package:fluffychat/utils/event_extension.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
|
|
@ -26,37 +27,7 @@ class MessageContent extends StatelessWidget {
|
|||
switch (event.messageType) {
|
||||
case MessageTypes.Image:
|
||||
case MessageTypes.Sticker:
|
||||
final int size = 400;
|
||||
return Bubble(
|
||||
padding: BubbleEdges.all(0),
|
||||
radius: Radius.circular(10),
|
||||
elevation: 0,
|
||||
child: Container(
|
||||
height: size.toDouble(),
|
||||
width: size.toDouble(),
|
||||
child: FutureBuilder<MatrixFile>(
|
||||
future: event.downloadAndDecryptAttachment(),
|
||||
builder: (BuildContext context, snapshot) {
|
||||
if (snapshot.hasError) {
|
||||
return Center(
|
||||
child: Text(
|
||||
snapshot.error.toString(),
|
||||
),
|
||||
);
|
||||
}
|
||||
if (snapshot.hasData) {
|
||||
return InkWell(
|
||||
onTap: () => snapshot.data.open(),
|
||||
child: Image.memory(snapshot.data.bytes),
|
||||
);
|
||||
}
|
||||
return Center(
|
||||
child: CircularProgressIndicator(),
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
);
|
||||
return ImageBubble(event);
|
||||
case MessageTypes.Audio:
|
||||
return AudioPlayer(
|
||||
event,
|
||||
|
|
|
|||
|
|
@ -86,7 +86,6 @@ class Store extends StoreAPI {
|
|||
newDeviceID: credentials["deviceID"],
|
||||
newDeviceName: credentials["deviceName"],
|
||||
newHomeserver: credentials["homeserver"],
|
||||
newLazyLoadMembers: credentials["lazyLoadMembers"],
|
||||
newMatrixVersions: List<String>.from(credentials["matrixVersions"] ?? []),
|
||||
newToken: credentials["token"],
|
||||
newUserID: credentials["userID"],
|
||||
|
|
@ -104,7 +103,6 @@ class Store extends StoreAPI {
|
|||
"deviceID": client.deviceID,
|
||||
"deviceName": client.deviceName,
|
||||
"homeserver": client.homeserver,
|
||||
"lazyLoadMembers": client.lazyLoadMembers,
|
||||
"matrixVersions": client.matrixVersions,
|
||||
"token": client.accessToken,
|
||||
"userID": client.userID,
|
||||
|
|
@ -175,7 +173,6 @@ class ExtendedStore extends Store implements ExtendedStoreAPI {
|
|||
newUserID: clientList["matrix_id"],
|
||||
newDeviceID: clientList["device_id"],
|
||||
newDeviceName: clientList["device_name"],
|
||||
newLazyLoadMembers: clientList["lazy_load_members"] == 1,
|
||||
newMatrixVersions:
|
||||
clientList["matrix_versions"].toString().split(","),
|
||||
newPrevBatch: null,
|
||||
|
|
|
|||
|
|
@ -40,10 +40,6 @@ class AppInfo extends StatelessWidget {
|
|||
title: Text("Supported versions:"),
|
||||
subtitle: Text(client.matrixVersions.toString()),
|
||||
),
|
||||
ListTile(
|
||||
title: Text("Lazy Loading members enabled:"),
|
||||
subtitle: Text(client.lazyLoadMembers.toString()),
|
||||
),
|
||||
ListTile(
|
||||
title: Text("Device name:"),
|
||||
subtitle: Text(client.deviceName),
|
||||
|
|
|
|||
|
|
@ -1,12 +1,12 @@
|
|||
import 'dart:async';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:famedlysdk/famedlysdk.dart';
|
||||
import 'package:fluffychat/components/list_items/public_room_list_item.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_speed_dial/flutter_speed_dial.dart';
|
||||
import 'package:uni_links/uni_links.dart';
|
||||
import 'package:receive_sharing_intent/receive_sharing_intent.dart';
|
||||
|
||||
import '../components/dialogs/simple_dialogs.dart';
|
||||
import '../components/theme_switcher.dart';
|
||||
|
|
@ -103,35 +103,63 @@ class _ChatListState extends State<ChatList> {
|
|||
});
|
||||
setState(() => null);
|
||||
});
|
||||
initUniLinks();
|
||||
_initReceiveSharingINtent();
|
||||
super.initState();
|
||||
}
|
||||
|
||||
StreamSubscription _intentDataStreamSubscription;
|
||||
|
||||
StreamSubscription _onUniLinksub;
|
||||
StreamSubscription _intentFileStreamSubscription;
|
||||
|
||||
Future<void> initUniLinks() async {
|
||||
if (kIsWeb) return;
|
||||
_onUniLinksub ??= getLinksStream().listen(
|
||||
(String initialLink) {
|
||||
try {
|
||||
if (initialLink?.isEmpty ?? true) return;
|
||||
if (initialLink.startsWith("https://matrix.to/#/")) {
|
||||
UrlLauncher(context, initialLink).openMatrixToUrl();
|
||||
}
|
||||
} on PlatformException {
|
||||
debugPrint("initUniLinks failed during platform exception");
|
||||
}
|
||||
},
|
||||
onError: (error) => Scaffold.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text(
|
||||
I18n.of(context).oopsSomethingWentWrong + " " + error.toString(),
|
||||
),
|
||||
),
|
||||
void _processIncomingSharedFiles(List<SharedMediaFile> files) {
|
||||
if (files?.isEmpty ?? true) return;
|
||||
if (Navigator.of(context).canPop()) {
|
||||
Navigator.of(context).popUntil((r) => r.isFirst);
|
||||
}
|
||||
final File file = File(files.first.path);
|
||||
|
||||
Matrix.of(context).shareContent = {
|
||||
"msgtype": "chat.fluffy.shared_file",
|
||||
"file": MatrixFile(
|
||||
bytes: file.readAsBytesSync(),
|
||||
path: file.path,
|
||||
),
|
||||
);
|
||||
};
|
||||
setState(() => null);
|
||||
}
|
||||
|
||||
void _processIncomingSharedText(String text) {
|
||||
if (text == null) return;
|
||||
if (Navigator.of(context).canPop()) {
|
||||
Navigator.of(context).popUntil((r) => r.isFirst);
|
||||
}
|
||||
if (text.startsWith("https://matrix.to/#/")) {
|
||||
UrlLauncher(context, text).openMatrixToUrl();
|
||||
return;
|
||||
}
|
||||
Matrix.of(context).shareContent = {
|
||||
"msgtype": "m.text",
|
||||
"body": text,
|
||||
};
|
||||
setState(() => null);
|
||||
}
|
||||
|
||||
void _initReceiveSharingINtent() {
|
||||
if (kIsWeb) return;
|
||||
|
||||
// For sharing images coming from outside the app while the app is in the memory
|
||||
_intentFileStreamSubscription = ReceiveSharingIntent.getMediaStream()
|
||||
.listen(_processIncomingSharedFiles, onError: print);
|
||||
|
||||
// For sharing images coming from outside the app while the app is closed
|
||||
ReceiveSharingIntent.getInitialMedia().then(_processIncomingSharedFiles);
|
||||
|
||||
// For sharing or opening urls/text coming from outside the app while the app is in the memory
|
||||
_intentDataStreamSubscription = ReceiveSharingIntent.getTextStream()
|
||||
.listen(_processIncomingSharedText, onError: print);
|
||||
|
||||
// For sharing or opening urls/text coming from outside the app while the app is closed
|
||||
ReceiveSharingIntent.getInitialText().then(_processIncomingSharedText);
|
||||
}
|
||||
|
||||
@override
|
||||
|
|
@ -141,7 +169,7 @@ class _ChatListState extends State<ChatList> {
|
|||
() => setState(() => null),
|
||||
);
|
||||
_intentDataStreamSubscription?.cancel();
|
||||
_onUniLinksub?.cancel();
|
||||
_intentFileStreamSubscription?.cancel();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue