fluffychat/lib/pages/homeserver_picker/homeserver_picker.dart

151 lines
4.6 KiB
Dart
Raw Permalink Normal View History

2021-06-06 17:07:19 +02:00
import 'dart:async';
2020-04-12 10:35:45 +02:00
import 'package:flutter/material.dart';
2021-10-26 18:50:34 +02:00
import 'package:matrix/matrix.dart';
import 'package:matrix_homeserver_recommendations/matrix_homeserver_recommendations.dart';
2021-10-26 18:50:34 +02:00
import 'package:vrouter/vrouter.dart';
2021-06-06 17:07:19 +02:00
2021-10-26 18:50:34 +02:00
import 'package:fluffychat/config/app_config.dart';
import 'package:fluffychat/pages/homeserver_picker/homeserver_bottom_sheet.dart';
2021-11-09 21:32:16 +01:00
import 'package:fluffychat/pages/homeserver_picker/homeserver_picker_view.dart';
2021-10-26 18:50:34 +02:00
import 'package:fluffychat/widgets/matrix.dart';
2021-11-09 21:32:16 +01:00
import '../../utils/localized_exception_extension.dart';
2020-04-12 10:35:45 +02:00
2021-01-17 15:33:31 +01:00
class HomeserverPicker extends StatefulWidget {
2022-01-29 12:35:03 +01:00
const HomeserverPicker({Key? key}) : super(key: key);
2021-10-14 18:09:30 +02:00
2021-01-17 15:33:31 +01:00
@override
2021-04-09 16:28:26 +02:00
HomeserverPickerController createState() => HomeserverPickerController();
2021-01-17 15:33:31 +01:00
}
2020-04-12 10:35:45 +02:00
2021-04-09 16:28:26 +02:00
class HomeserverPickerController extends State<HomeserverPicker> {
bool isLoading = false;
final TextEditingController homeserverController = TextEditingController(
text: AppConfig.defaultHomeserver,
);
final FocusNode homeserverFocusNode = FocusNode();
2022-01-29 12:35:03 +01:00
String? error;
List<HomeserverBenchmarkResult>? benchmarkResults;
bool displayServerList = false;
bool get loadingHomeservers =>
AppConfig.allowOtherHomeservers && benchmarkResults == null;
String searchTerm = '';
void _updateFocus() {
if (benchmarkResults == null) _loadHomeserverList();
if (homeserverFocusNode.hasFocus) {
setState(() {
displayServerList = true;
});
}
}
void showServerInfo(HomeserverBenchmarkResult server) => showModalBottomSheet(
context: context,
builder: (_) => HomeserverBottomSheet(
homeserver: server,
),
);
void onChanged(String text) => setState(() {
searchTerm = text;
});
List<HomeserverBenchmarkResult> get filteredHomeservers => benchmarkResults!
.where((element) =>
element.homeserver.baseUrl.host.contains(searchTerm) ||
(element.homeserver.description?.contains(searchTerm) ?? false))
.toList();
void _loadHomeserverList() async {
try {
final homeserverList = await JoinmatrixOrgParser().fetchHomeservers();
final benchmark = await HomeserverListProvider.benchmarkHomeserver(
homeserverList,
timeout: const Duration(seconds: 10),
);
setState(() {
benchmarkResults = benchmark;
});
} catch (e, s) {
Logs().e('Homeserver benchmark failed', e, s);
benchmarkResults = [];
}
}
void setServer(String server) => setState(() {
homeserverController.text = server;
searchTerm = '';
homeserverFocusNode.unfocus();
displayServerList = false;
});
2021-11-16 10:23:29 +01:00
2021-04-09 16:28:26 +02: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 10:08:04 +02:00
/// login type.
2021-10-16 09:59:38 +02:00
Future<void> checkHomeserverAction() async {
2021-11-16 10:23:29 +01:00
setState(() {
homeserverFocusNode.unfocus();
2022-04-15 11:42:59 +02:00
error = null;
2021-11-16 10:23:29 +01:00
isLoading = true;
searchTerm = '';
displayServerList = false;
2021-11-16 10:23:29 +01:00
});
try {
2022-04-15 11:42:59 +02: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 12:14:00 +01:00
try {
await Matrix.of(context).getLoginClient().register();
2022-04-15 11:42:59 +02:00
matrix.loginRegistrationSupported = true;
2021-11-15 12:14:00 +01:00
} on MatrixException catch (e) {
2022-04-15 11:42:59 +02: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 12:14:00 +01:00
}
2021-01-17 15:33:31 +01:00
} catch (e) {
2022-01-29 12:35:03 +01:00
setState(() => error = (e).toLocalizedString(context));
2021-01-17 15:33:31 +01:00
} finally {
if (mounted) {
2021-04-09 16:28:26 +02:00
setState(() => isLoading = false);
2021-01-17 15:33:31 +01:00
}
2020-04-12 10:35:45 +02:00
}
}
@override
void dispose() {
homeserverFocusNode.removeListener(_updateFocus);
super.dispose();
}
@override
void initState() {
homeserverFocusNode.addListener(_updateFocus);
super.initState();
}
2020-04-12 10:35:45 +02:00
@override
Widget build(BuildContext context) {
Matrix.of(context).navigatorContext = context;
return HomeserverPickerView(this);
}
2020-04-12 10:35:45 +02:00
}