From 4bc45a75c5fabd7d6677977f2a19a1eac8e01ec0 Mon Sep 17 00:00:00 2001 From: Krille Fear Date: Mon, 15 Nov 2021 12:14:00 +0100 Subject: [PATCH] refactor: Homeserver picker --- .../homeserver_picker/homeserver_picker.dart | 27 +-- .../homeserver_picker_view.dart | 190 ++++++++---------- lib/utils/client_manager.dart | 4 +- 3 files changed, 95 insertions(+), 126 deletions(-) diff --git a/lib/pages/homeserver_picker/homeserver_picker.dart b/lib/pages/homeserver_picker/homeserver_picker.dart index 0d17e6063..00a23ab7e 100644 --- a/lib/pages/homeserver_picker/homeserver_picker.dart +++ b/lib/pages/homeserver_picker/homeserver_picker.dart @@ -137,6 +137,17 @@ class HomeserverPickerController extends State { .setItem(SettingKeys.jitsiInstance, jitsi); AppConfig.jitsiInstance = jitsi; } + + _rawLoginTypes = await Matrix.of(context).getLoginClient().request( + RequestType.GET, + '/client/r0/login', + ); + try { + await Matrix.of(context).getLoginClient().register(); + registrationSupported = true; + } on MatrixException catch (e) { + registrationSupported = e.requireAdditionalAuthentication ?? false; + } } catch (e) { setState(() => error = (e as Object).toLocalizedString(context)); } finally { @@ -181,22 +192,6 @@ class HomeserverPickerController extends State { .tryGetList('flows') .any((flow) => flow['type'] == AuthenticationTypes.sso); - Future> getLoginTypes() async { - _rawLoginTypes ??= await Matrix.of(context).getLoginClient().request( - RequestType.GET, - '/client/r0/login', - ); - if (registrationSupported == null) { - try { - await Matrix.of(context).getLoginClient().register(); - registrationSupported = true; - } on MatrixException catch (e) { - registrationSupported = e.requireAdditionalAuthentication ?? false; - } - } - return _rawLoginTypes; - } - ChromeSafariBrowser browser; static const String ssoHomeserverKey = 'sso-homeserver'; diff --git a/lib/pages/homeserver_picker/homeserver_picker_view.dart b/lib/pages/homeserver_picker/homeserver_picker_view.dart index a667fd346..e84b2cfba 100644 --- a/lib/pages/homeserver_picker/homeserver_picker_view.dart +++ b/lib/pages/homeserver_picker/homeserver_picker_view.dart @@ -13,7 +13,6 @@ import 'package:fluffychat/utils/platform_infos.dart'; import 'package:fluffychat/widgets/default_app_bar_search_field.dart'; import 'package:fluffychat/widgets/layouts/one_page_card.dart'; import 'package:fluffychat/widgets/matrix.dart'; -import '../../utils/localized_exception_extension.dart'; import 'homeserver_picker.dart'; class HomeserverPickerView extends StatelessWidget { @@ -48,117 +47,92 @@ class HomeserverPickerView extends StatelessWidget { ? 'assets/banner_dark.png' : 'assets/banner.png', ), - controller.isLoading - ? const Center( - child: CircularProgressIndicator.adaptive(strokeWidth: 2)) - : controller.error != null - ? Center( - child: Padding( + if (controller.isLoading) + const Center( + child: CircularProgressIndicator.adaptive(strokeWidth: 2)) + else if (controller.error != null) + Center( + child: Padding( + padding: const EdgeInsets.all(12.0), + child: Text( + controller.error, + textAlign: TextAlign.center, + style: TextStyle( + fontSize: 18, + color: Colors.red[900], + ), + ), + ), + ) + else + Padding( + padding: const EdgeInsets.symmetric( + horizontal: 12.0, + vertical: 4.0, + ), + child: Column( + mainAxisSize: MainAxisSize.min, + children: [ + if (controller.ssoLoginSupported) + Row(children: [ + const Expanded(child: Divider()), + Padding( padding: const EdgeInsets.all(12.0), - child: Text( - controller.error, - textAlign: TextAlign.center, - style: TextStyle( - fontSize: 18, - color: Colors.red[900], + child: Text(L10n.of(context).loginWithOneClick), + ), + const Expanded(child: Divider()), + ]), + Wrap( + children: [ + if (controller.ssoLoginSupported) ...{ + for (final identityProvider + in controller.identityProviders) + _SsoButton( + onPressed: () => + controller.ssoLoginAction(identityProvider.id), + identityProvider: identityProvider, ), + }, + ].toList(), + ), + if (controller.ssoLoginSupported && + (controller.registrationSupported || + controller.passwordLoginSupported)) + Row(children: [ + const Expanded(child: Divider()), + Padding( + padding: const EdgeInsets.all(12.0), + child: Text(L10n.of(context).or), + ), + const Expanded(child: Divider()), + ]), + if (controller.passwordLoginSupported) ...[ + Center( + child: _LoginButton( + onPressed: () => VRouter.of(context).to('login'), + icon: Icon( + CupertinoIcons.lock_open_fill, + color: Theme.of(context).textTheme.bodyText1.color, ), + labelText: L10n.of(context).login, ), - ) - : FutureBuilder( - future: controller.getLoginTypes(), - builder: (context, snapshot) { - if (snapshot.hasError) { - return Center( - child: Text( - snapshot.error.toLocalizedString(context), - textAlign: TextAlign.center, - ), - ); - } - if (!snapshot.hasData) { - return const Center( - child: CircularProgressIndicator.adaptive( - strokeWidth: 2)); - } - return Padding( - padding: const EdgeInsets.symmetric( - horizontal: 12.0, - vertical: 4.0, - ), - child: Column( - mainAxisSize: MainAxisSize.min, - children: [ - if (controller.ssoLoginSupported) - Row(children: [ - const Expanded(child: Divider()), - Padding( - padding: const EdgeInsets.all(12.0), - child: Text( - L10n.of(context).loginWithOneClick), - ), - const Expanded(child: Divider()), - ]), - Wrap( - children: [ - if (controller.ssoLoginSupported) ...{ - for (final identityProvider - in controller.identityProviders) - _SsoButton( - onPressed: () => - controller.ssoLoginAction( - identityProvider.id), - identityProvider: identityProvider, - ), - }, - ].toList(), - ), - if (controller.ssoLoginSupported && - (controller.registrationSupported || - controller.passwordLoginSupported)) - Row(children: [ - const Expanded(child: Divider()), - Padding( - padding: const EdgeInsets.all(12.0), - child: Text(L10n.of(context).or), - ), - const Expanded(child: Divider()), - ]), - if (controller.passwordLoginSupported) ...[ - Center( - child: _LoginButton( - onPressed: () => - VRouter.of(context).to('login'), - icon: Icon( - CupertinoIcons.lock_open_fill, - color: Theme.of(context) - .textTheme - .bodyText1 - .color, - ), - labelText: L10n.of(context).login, - ), - ), - const SizedBox(height: 12), - ], - if (controller.registrationSupported) - Center( - child: _LoginButton( - onPressed: controller.signUpAction, - icon: Icon( - CupertinoIcons.person_add, - color: Theme.of(context) - .textTheme - .bodyText1 - .color, - ), - labelText: L10n.of(context).register, - ), - ), - ], - ), - ); - }), + ), + const SizedBox(height: 12), + ], + if (controller.registrationSupported) + Center( + child: _LoginButton( + onPressed: controller.signUpAction, + icon: Icon( + CupertinoIcons.person_add, + color: Theme.of(context).textTheme.bodyText1.color, + ), + labelText: L10n.of(context).register, + ), + ), + ], + ), + ), ]), bottomNavigationBar: Material( elevation: 6, diff --git a/lib/utils/client_manager.dart b/lib/utils/client_manager.dart index 8cb833d16..8201ce1d0 100644 --- a/lib/utils/client_manager.dart +++ b/lib/utils/client_manager.dart @@ -80,8 +80,8 @@ abstract class ClientManager { legacyDatabaseBuilder: FlutterMatrixHiveStore.hiveDatabaseBuilder, supportedLoginTypes: { AuthenticationTypes.password, - //if (PlatformInfos.isMobile || PlatformInfos.isWeb) - AuthenticationTypes.sso + if (PlatformInfos.isMobile || PlatformInfos.isWeb) + AuthenticationTypes.sso }, compute: compute, );