fluffychat/lib/utils/url_launcher.dart

54 lines
1.6 KiB
Dart
Raw Normal View History

2020-01-19 19:28:12 +01:00
import 'package:famedlysdk/famedlysdk.dart';
2020-04-27 13:36:39 +02:00
import 'package:fluffychat/components/dialogs/simple_dialogs.dart';
2020-01-19 19:28:12 +01: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';
2020-04-26 08:24:41 +02:00
import 'package:fluffychat/utils/cross_url_launcher.dart';
2020-01-19 19:28:12 +01:00
class UrlLauncher {
final String url;
final BuildContext context;
const UrlLauncher(this.context, this.url);
void launchUrl() {
if (url.startsWith("https://matrix.to/#/")) {
return openMatrixToUrl();
}
launch(url);
}
void openMatrixToUrl() async {
final matrix = Matrix.of(context);
final String identifier = url.replaceAll("https://matrix.to/#/", "");
if (identifier.substring(0, 1) == "#") {
2020-04-27 13:36:39 +02:00
final response = await SimpleDialogs(context).tryRequestWithLoadingDialog(
2020-01-19 19:28:12 +01:00
matrix.client.joinRoomById(
Uri.encodeComponent(identifier),
),
);
if (response == false) return;
await Navigator.pushAndRemoveUntil(
context,
2020-01-27 10:14:38 +01:00
AppRoute.defaultRoute(context, ChatView(response["room_id"])),
2020-01-19 19:28:12 +01:00
(r) => r.isFirst,
);
} else if (identifier.substring(0, 1) == "@") {
final User user = User(
identifier,
room: Room(id: "", client: matrix.client),
);
2020-04-27 13:36:39 +02:00
final String roomID = await SimpleDialogs(context)
.tryRequestWithLoadingDialog(user.startDirectChat());
2020-01-19 19:28:12 +01:00
Navigator.of(context).pop();
if (roomID != null) {
await Navigator.push(
context,
2020-01-27 10:14:38 +01:00
MaterialPageRoute(builder: (context) => ChatView(roomID)),
2020-01-19 19:28:12 +01:00
);
}
}
}
}