fluffychat/lib/pages/homeserver_picker.dart

69 lines
2.5 KiB
Dart
Raw Normal View History

2021-01-16 12:46:38 +01:00
import 'package:adaptive_page_layout/adaptive_page_layout.dart';
2021-01-18 22:59:02 +01:00
import 'package:famedlysdk/famedlysdk.dart';
import 'package:fluffychat/pages/views/homeserver_picker_view.dart';
2021-05-22 08:53:52 +02:00
import 'package:fluffychat/widgets/matrix.dart';
2021-04-09 16:29:48 +02:00
import 'package:fluffychat/config/app_config.dart';
import 'package:fluffychat/config/setting_keys.dart';
2020-10-03 11:11:28 +02:00
import 'package:flutter_gen/gen_l10n/l10n.dart';
2020-04-12 10:35:45 +02:00
import 'package:flutter/material.dart';
2021-01-17 15:33:31 +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 {
@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;
String domain = AppConfig.defaultHomeserver;
final TextEditingController homeserverController =
2021-01-22 21:39:37 +01:00
TextEditingController(text: AppConfig.defaultHomeserver);
2021-01-17 15:33:31 +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
/// login type. For SSO login only the app opens the page and otherwise it
/// forwards to the route `/signup`.
void checkHomeserverAction() async {
2021-01-22 21:39:37 +01:00
try {
2021-04-09 16:28:26 +02:00
if (domain.isEmpty) throw L10n.of(context).changeTheHomeserver;
var homeserver = domain;
2021-01-22 21:39:37 +01:00
if (!homeserver.startsWith('https://')) {
homeserver = 'https://$homeserver';
}
2021-01-18 22:59:02 +01:00
2021-04-09 16:28:26 +02:00
setState(() => isLoading = true);
final wellKnown =
await Matrix.of(context).client.checkHomeserver(homeserver);
var jitsi = wellKnown?.content
?.tryGet<Map<String, dynamic>>('im.vector.riot.jitsi')
?.tryGet<String>('preferredDomain');
if (jitsi != null) {
if (!jitsi.endsWith('/')) {
jitsi += '/';
}
Logs().v('Found custom jitsi instance $jitsi');
await Matrix.of(context)
.store
.setItem(SettingKeys.jitsiInstance, jitsi);
AppConfig.jitsiInstance = jitsi;
}
2021-05-22 10:25:37 +02:00
await AdaptivePageLayout.of(context)
.pushNamed(AppConfig.enableRegistration ? '/signup' : '/login');
2021-01-17 15:33:31 +01:00
} catch (e) {
2021-04-03 13:09:20 +02:00
AdaptivePageLayout.of(context).showSnackBar(
SnackBar(content: Text((e as Object).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
2021-05-22 09:13:47 +02:00
Widget build(BuildContext context) => HomeserverPickerView(this);
2020-04-12 10:35:45 +02:00
}