feat: open links better

This commit is contained in:
Sorunome 2020-09-05 13:45:03 +02:00
commit 955210a8e5
7 changed files with 127 additions and 47 deletions

View file

@ -1,7 +1,7 @@
import 'package:famedlysdk/famedlysdk.dart';
import 'package:flutter_matrix_html/flutter_html.dart';
import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';
import '../utils/url_launcher.dart';
import 'matrix.dart';
@ -42,12 +42,8 @@ class HtmlMessage extends StatelessWidget {
),
shrinkToFit: true,
maxLines: maxLines,
onLinkTap: (String url) {
if (url == null || url.isEmpty) {
return;
}
launch(url);
},
onLinkTap: (url) => UrlLauncher(context, url).launchUrl(),
onPillTap: (url) => UrlLauncher(context, url).launchUrl(),
getMxcUrl: (String mxc, double width, double height) {
final ratio = MediaQuery.of(context).devicePixelRatio;
return Uri.parse(mxc)?.getThumbnail(

View file

@ -4,11 +4,12 @@ import 'package:fluffychat/components/image_bubble.dart';
import 'package:fluffychat/l10n/l10n.dart';
import 'package:fluffychat/utils/event_extension.dart';
import 'package:flutter/material.dart';
import 'package:link_text/link_text.dart';
import 'package:matrix_link_text/link_text.dart';
import 'package:url_launcher/url_launcher.dart';
import 'matrix.dart';
import 'message_download_content.dart';
import 'html_message.dart';
import '../utils/url_launcher.dart';
class MessageContent extends StatelessWidget {
final Event event;
@ -84,6 +85,7 @@ class MessageContent extends StatelessWidget {
fontSize: DefaultTextStyle.of(context).style.fontSize,
decoration: event.redacted ? TextDecoration.lineThrough : null,
),
onLinkTap: (url) => UrlLauncher(context, url).launchUrl(),
);
}
break;

View file

@ -12,7 +12,8 @@ class UrlLauncher {
const UrlLauncher(this.context, this.url);
void launchUrl() {
if (url.startsWith('https://matrix.to/#/')) {
if (url.startsWith('https://matrix.to/#/') ||
{'#', '@', '!', '+', '\$'}.contains(url[0])) {
return openMatrixToUrl();
}
launch(url);
@ -21,33 +22,83 @@ class UrlLauncher {
void openMatrixToUrl() async {
final matrix = Matrix.of(context);
final identifier = url.replaceAll('https://matrix.to/#/', '');
if (identifier.substring(0, 1) == '#') {
final response = await SimpleDialogs(context).tryRequestWithLoadingDialog(
matrix.client.joinRoom(
Uri.encodeComponent(identifier),
),
);
if (response == false) return;
await Navigator.pushAndRemoveUntil(
context,
AppRoute.defaultRoute(context, ChatView(response['room_id'])),
(r) => r.isFirst,
);
} else if (identifier.substring(0, 1) == '@') {
if (identifier[0] == '#' || identifier[0] == '!') {
var room = matrix.client.getRoomByAlias(identifier);
room ??= matrix.client.getRoomById(identifier);
var roomId = room?.id;
var servers = <String>[];
if (room == null && identifier == '#') {
// we were unable to find the room locally...so resolve it
final response =
await SimpleDialogs(context).tryRequestWithLoadingDialog(
matrix.client.requestRoomAliasInformations(identifier),
);
if (response != false) {
roomId = response.roomId;
servers = response.servers;
room = matrix.client.getRoomById(roomId);
}
}
if (room != null) {
// we have the room, so....just open it!
await Navigator.pushAndRemoveUntil(
context,
AppRoute.defaultRoute(context, ChatView(room.id)),
(r) => r.isFirst,
);
return;
}
if (identifier == '!') {
roomId = identifier;
}
if (roomId == null) {
// we haven't found this room....so let's ignore it
return;
}
if (await SimpleDialogs(context)
.askConfirmation(titleText: 'Join room $identifier')) {
final response =
await SimpleDialogs(context).tryRequestWithLoadingDialog(
matrix.client.joinRoomOrAlias(
Uri.encodeComponent(roomId),
servers: servers,
),
);
if (response == false) return;
await Navigator.pushAndRemoveUntil(
context,
AppRoute.defaultRoute(context, ChatView(response['room_id'])),
(r) => r.isFirst,
);
}
} else if (identifier[0] == '@') {
final user = User(
identifier,
room: Room(id: '', client: matrix.client),
);
final String roomID = await SimpleDialogs(context)
.tryRequestWithLoadingDialog(user.startDirectChat());
Navigator.of(context).pop();
if (roomID != null) {
var roomId = matrix.client.getDirectChatFromUserId(identifier);
if (roomId != null) {
await Navigator.pushAndRemoveUntil(
context,
AppRoute.defaultRoute(context, ChatView(roomID)),
AppRoute.defaultRoute(context, ChatView(roomId)),
(r) => r.isFirst,
);
return;
}
if (await SimpleDialogs(context)
.askConfirmation(titleText: 'Message user $identifier')) {
roomId = await SimpleDialogs(context)
.tryRequestWithLoadingDialog(user.startDirectChat());
Navigator.of(context).pop();
if (roomId != null) {
await Navigator.pushAndRemoveUntil(
context,
AppRoute.defaultRoute(context, ChatView(roomId)),
(r) => r.isFirst,
);
}
}
}
}

View file

@ -14,9 +14,10 @@ import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:bot_toast/bot_toast.dart';
import 'package:image_picker/image_picker.dart';
import 'package:link_text/link_text.dart';
import 'package:matrix_link_text/link_text.dart';
import 'package:memoryfilepicker/memoryfilepicker.dart';
import './settings_emotes.dart';
import '../utils/url_launcher.dart';
class ChatDetails extends StatefulWidget {
final Room room;
@ -222,6 +223,8 @@ class _ChatDetailsState extends State<ChatDetails> {
.bodyText2
.color,
),
onLinkTap: (url) =>
UrlLauncher(context, url).launchUrl(),
),
onTap: widget.room.canSendEvent('m.room.topic')
? () => setTopicAction(context)