diff --git a/lib/pages/new_private_chat.dart b/lib/pages/new_private_chat.dart index 154d1106..264d110b 100644 --- a/lib/pages/new_private_chat.dart +++ b/lib/pages/new_private_chat.dart @@ -1,4 +1,5 @@ import 'package:fluffychat/pages/qr_scanner_modal.dart'; +import 'package:fluffychat/utils/url_launcher.dart'; import 'package:matrix/matrix.dart'; import 'package:fluffychat/utils/fluffy_share.dart'; import 'package:fluffychat/pages/views/new_private_chat_view.dart'; @@ -21,40 +22,12 @@ class NewPrivateChatController extends State { static const Set supportedSigils = {'@', '!', '#'}; + static const String prefix = 'https://matrix.to/#/'; + void submitAction([_]) async { controller.text = controller.text.trim(); if (!formKey.currentState.validate()) return; - final client = Matrix.of(context).client; - - LoadingDialogResult roomIdResult; - - switch (controller.text.sigil) { - case '@': - roomIdResult = await showFutureLoadingDialog( - context: context, - future: () => client.startDirectChat(controller.text), - ); - break; - case '#': - case '!': - roomIdResult = await showFutureLoadingDialog( - context: context, - future: () async { - final roomId = await client.joinRoom(controller.text); - if (client.getRoomById(roomId) == null) { - await client.onSync.stream - .where((s) => s.rooms.join.containsKey(roomId)) - .first; - } - return roomId; - }, - ); - break; - } - - if (roomIdResult.error == null) { - VRouter.of(context).toSegments(['rooms', roomIdResult.result]); - } + UrlLauncher(context, '$prefix${controller.text}').openMatrixToUrl(); } String validateForm(String value) { diff --git a/lib/utils/url_launcher.dart b/lib/utils/url_launcher.dart index 98639545..66be0fef 100644 --- a/lib/utils/url_launcher.dart +++ b/lib/utils/url_launcher.dart @@ -150,33 +150,16 @@ class UrlLauncher { }); } } else if (identityParts.primaryIdentifier.sigil == '@') { - final user = User( - identityParts.primaryIdentifier, - room: Room(id: '', client: matrix.client), + final result = await showFutureLoadingDialog( + context: context, + future: () => matrix.client.startDirectChat( + identityParts.primaryIdentifier, + ), ); - var roomId = matrix.client.getDirectChatFromUserId(user.id); - if (roomId != null) { - VRouter.of(context).toSegments(['rooms', roomId]); - + if (result.error == null) { + VRouter.of(context).toSegments(['rooms', result.result]); return; } - - if (await showOkCancelAlertDialog( - useRootNavigator: false, - context: context, - title: 'Message user ${user.id}', - ) == - OkCancelResult.ok) { - roomId = (await showFutureLoadingDialog( - context: context, - future: () => user.startDirectChat(), - )) - .result; - - if (roomId != null) { - VRouter.of(context).toSegments(['rooms', roomId]); - } - } } } }