fluffychat/lib/pages/homeserver_picker/homeserver_picker.dart

77 lines
2.5 KiB
Dart
Raw Normal View History

2021-06-06 15:07:19 +00:00
import 'dart:async';
2020-04-12 08:35:45 +00:00
import 'package:flutter/material.dart';
2021-10-26 16:50:34 +00:00
import 'package:matrix/matrix.dart';
import 'package:vrouter/vrouter.dart';
2021-06-06 15:07:19 +00:00
2021-10-26 16:50:34 +00:00
import 'package:fluffychat/config/app_config.dart';
2021-11-09 20:32:16 +00:00
import 'package:fluffychat/pages/homeserver_picker/homeserver_picker_view.dart';
2021-10-26 16:50:34 +00:00
import 'package:fluffychat/widgets/matrix.dart';
2021-11-09 20:32:16 +00:00
import '../../utils/localized_exception_extension.dart';
2020-04-12 08:35:45 +00:00
2021-01-17 14:33:31 +00:00
class HomeserverPicker extends StatefulWidget {
2022-01-29 11:35:03 +00:00
const HomeserverPicker({Key? key}) : super(key: key);
2021-10-14 16:09:30 +00:00
2021-01-17 14:33:31 +00:00
@override
2021-04-09 14:28:26 +00:00
HomeserverPickerController createState() => HomeserverPickerController();
2021-01-17 14:33:31 +00:00
}
2020-04-12 08:35:45 +00:00
2021-04-09 14:28:26 +00:00
class HomeserverPickerController extends State<HomeserverPicker> {
bool isLoading = false;
final TextEditingController homeserverController =
2021-01-22 20:39:37 +00:00
TextEditingController(text: AppConfig.defaultHomeserver);
2022-01-29 11:35:03 +00:00
String? error;
2021-11-16 09:23:29 +00:00
2021-04-09 14:28:26 +00:00
/// Starts an analysis of the given homeserver. It uses the current domain and
/// makes sure that it is prefixed with https. Then it searches for the
/// well-known information and forwards to the login page depending on the
2021-06-11 08:08:04 +00:00
/// login type.
2021-10-16 07:59:38 +00:00
Future<void> checkHomeserverAction() async {
2021-11-16 09:23:29 +00:00
setState(() {
2022-04-15 09:42:59 +00:00
error = null;
2021-11-16 09:23:29 +00:00
isLoading = true;
});
try {
2022-04-15 09:42:59 +00:00
homeserverController.text =
homeserverController.text.trim().toLowerCase().replaceAll(' ', '-');
var homeserver = Uri.parse(homeserverController.text);
if (homeserver.scheme.isEmpty) {
homeserver = Uri.https(homeserverController.text, '');
}
final matrix = Matrix.of(context);
matrix.loginHomeserverSummary =
await matrix.getLoginClient().checkHomeserver(homeserver);
final ssoSupported = matrix.loginHomeserverSummary!.loginFlows
.any((flow) => flow.type == 'm.login.sso');
2021-11-15 11:14:00 +00:00
try {
await Matrix.of(context).getLoginClient().register();
2022-04-15 09:42:59 +00:00
matrix.loginRegistrationSupported = true;
2021-11-15 11:14:00 +00:00
} on MatrixException catch (e) {
2022-04-15 09:42:59 +00:00
matrix.loginRegistrationSupported = e.requireAdditionalAuthentication;
}
if (!ssoSupported && matrix.loginRegistrationSupported == false) {
// Server does not support SSO or registration. We can skip to login page:
VRouter.of(context).to('login');
} else {
VRouter.of(context).to('connect');
2021-11-15 11:14:00 +00:00
}
2021-01-17 14:33:31 +00:00
} catch (e) {
2022-01-29 11:35:03 +00:00
setState(() => error = (e).toLocalizedString(context));
2021-01-17 14:33:31 +00:00
} finally {
if (mounted) {
2021-04-09 14:28:26 +00:00
setState(() => isLoading = false);
2021-01-17 14:33:31 +00:00
}
2020-04-12 08:35:45 +00:00
}
}
@override
Widget build(BuildContext context) {
Matrix.of(context).navigatorContext = context;
return HomeserverPickerView(this);
}
2020-04-12 08:35:45 +00:00
}