FurryChat/lib/utils/url_launcher.dart

55 lines
1.6 KiB
Dart
Raw Normal View History

2020-01-19 18:28:12 +00:00
import 'package:famedlysdk/famedlysdk.dart';
2020-04-27 11:36:39 +00:00
import 'package:fluffychat/components/dialogs/simple_dialogs.dart';
2020-01-19 18:28:12 +00:00
import 'package:fluffychat/components/matrix.dart';
import 'package:fluffychat/utils/app_route.dart';
import 'package:fluffychat/views/chat.dart';
import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';
class UrlLauncher {
final String url;
final BuildContext context;
const UrlLauncher(this.context, this.url);
void launchUrl() {
2020-05-13 13:58:59 +00:00
if (url.startsWith('https://matrix.to/#/')) {
2020-01-19 18:28:12 +00:00
return openMatrixToUrl();
}
launch(url);
}
void openMatrixToUrl() async {
final matrix = Matrix.of(context);
2020-05-13 13:58:59 +00:00
final identifier = url.replaceAll('https://matrix.to/#/', '');
if (identifier.substring(0, 1) == '#') {
2020-04-27 11:36:39 +00:00
final response = await SimpleDialogs(context).tryRequestWithLoadingDialog(
2020-01-19 18:28:12 +00:00
matrix.client.joinRoomById(
Uri.encodeComponent(identifier),
),
);
if (response == false) return;
await Navigator.pushAndRemoveUntil(
context,
2020-05-13 13:58:59 +00:00
AppRoute.defaultRoute(context, ChatView(response['room_id'])),
2020-01-19 18:28:12 +00:00
(r) => r.isFirst,
);
2020-05-13 13:58:59 +00:00
} else if (identifier.substring(0, 1) == '@') {
final user = User(
2020-01-19 18:28:12 +00:00
identifier,
2020-05-13 13:58:59 +00:00
room: Room(id: '', client: matrix.client),
2020-01-19 18:28:12 +00:00
);
2020-04-27 11:36:39 +00:00
final String roomID = await SimpleDialogs(context)
.tryRequestWithLoadingDialog(user.startDirectChat());
2020-01-19 18:28:12 +00:00
Navigator.of(context).pop();
if (roomID != null) {
2020-05-09 11:06:18 +00:00
await Navigator.pushAndRemoveUntil(
2020-01-19 18:28:12 +00:00
context,
2020-05-09 11:06:18 +00:00
AppRoute.defaultRoute(context, ChatView(roomID)),
(r) => r.isFirst,
2020-01-19 18:28:12 +00:00
);
}
}
}
}