fluffychat/lib/pages/homeserver_picker.dart

70 lines
2.5 KiB
Dart
Raw Normal View History

2021-01-18 21:59:02 +00:00
import 'package:famedlysdk/famedlysdk.dart';
import 'package:fluffychat/pages/views/homeserver_picker_view.dart';
2021-05-22 06:53:52 +00:00
import 'package:fluffychat/widgets/matrix.dart';
2021-04-09 14:29:48 +00:00
import 'package:fluffychat/config/app_config.dart';
import 'package:fluffychat/config/setting_keys.dart';
2020-10-03 09:11:28 +00:00
import 'package:flutter_gen/gen_l10n/l10n.dart';
2020-04-12 08:35:45 +00:00
import 'package:flutter/material.dart';
2021-01-17 14:33:31 +00:00
import '../utils/localized_exception_extension.dart';
2021-05-23 11:11:55 +00:00
import 'package:vrouter/vrouter.dart';
2020-04-12 08:35:45 +00:00
2021-01-17 14:33:31 +00:00
class HomeserverPicker extends StatefulWidget {
@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;
String domain = AppConfig.defaultHomeserver;
final TextEditingController homeserverController =
2021-01-22 20:39:37 +00:00
TextEditingController(text: AppConfig.defaultHomeserver);
2021-01-17 14:33:31 +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
/// 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 20:39:37 +00:00
try {
2021-04-09 14:28:26 +00:00
if (domain.isEmpty) throw L10n.of(context).changeTheHomeserver;
var homeserver = domain;
2021-01-22 20:39:37 +00:00
if (!homeserver.startsWith('https://')) {
homeserver = 'https://$homeserver';
}
2021-01-18 21:59:02 +00:00
2021-04-09 14:28:26 +00: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-23 11:11:55 +00:00
VRouter.of(context).push(
AppConfig.enableRegistration ? '/signup' : '/login',
historyState: {'/home': '/signup'});
2021-01-17 14:33:31 +00:00
} catch (e) {
2021-05-23 11:11:55 +00:00
ScaffoldMessenger.of(context).showSnackBar(
2021-04-03 11:09:20 +00:00
SnackBar(content: Text((e as Object).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
2021-05-22 07:13:47 +00:00
Widget build(BuildContext context) => HomeserverPickerView(this);
2020-04-12 08:35:45 +00:00
}