2021-01-18 22:59:02 +01:00
|
|
|
import 'package:famedlysdk/famedlysdk.dart';
|
2021-05-22 09:08:23 +02:00
|
|
|
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';
|
2021-02-13 09:56:16 +01:00
|
|
|
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';
|
2021-05-23 13:11:55 +02:00
|
|
|
import 'package:vrouter/vrouter.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;
|
2020-07-02 11:10:03 +02:00
|
|
|
|
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);
|
2021-02-13 09:56:16 +01:00
|
|
|
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 13:11:55 +02:00
|
|
|
VRouter.of(context).push(
|
|
|
|
AppConfig.enableRegistration ? '/signup' : '/login',
|
|
|
|
historyState: {'/home': '/signup'});
|
2021-01-17 15:33:31 +01:00
|
|
|
} catch (e) {
|
2021-05-23 13:11:55 +02:00
|
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
2021-04-03 13:09:20 +02:00
|
|
|
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-23 13:28:55 +02:00
|
|
|
Widget build(BuildContext context) {
|
|
|
|
Matrix.of(context).navigatorContext = context;
|
|
|
|
return HomeserverPickerView(this);
|
|
|
|
}
|
2020-04-12 10:35:45 +02:00
|
|
|
}
|