refactor: Homeserver picker

This commit is contained in:
Krille Fear 2021-11-15 12:14:00 +01:00
parent 44da6c9d33
commit fa9d62ce60
3 changed files with 96 additions and 127 deletions

View File

@ -137,6 +137,17 @@ class HomeserverPickerController extends State<HomeserverPicker> {
.setItem(SettingKeys.jitsiInstance, jitsi); .setItem(SettingKeys.jitsiInstance, jitsi);
AppConfig.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) { } catch (e) {
setState(() => error = (e as Object).toLocalizedString(context)); setState(() => error = (e as Object).toLocalizedString(context));
} finally { } finally {
@ -181,22 +192,6 @@ class HomeserverPickerController extends State<HomeserverPicker> {
.tryGetList('flows') .tryGetList('flows')
.any((flow) => flow['type'] == AuthenticationTypes.sso); .any((flow) => flow['type'] == AuthenticationTypes.sso);
Future<Map<String, dynamic>> 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; ChromeSafariBrowser browser;
static const String ssoHomeserverKey = 'sso-homeserver'; static const String ssoHomeserverKey = 'sso-homeserver';

View File

@ -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/default_app_bar_search_field.dart';
import 'package:fluffychat/widgets/layouts/one_page_card.dart'; import 'package:fluffychat/widgets/layouts/one_page_card.dart';
import 'package:fluffychat/widgets/matrix.dart'; import 'package:fluffychat/widgets/matrix.dart';
import '../../utils/localized_exception_extension.dart';
import 'homeserver_picker.dart'; import 'homeserver_picker.dart';
class HomeserverPickerView extends StatelessWidget { class HomeserverPickerView extends StatelessWidget {
@ -48,117 +47,92 @@ class HomeserverPickerView extends StatelessWidget {
? 'assets/banner_dark.png' ? 'assets/banner_dark.png'
: 'assets/banner.png', : 'assets/banner.png',
), ),
controller.isLoading if (controller.isLoading)
? const Center( const Center(
child: CircularProgressIndicator.adaptive(strokeWidth: 2)) child: CircularProgressIndicator.adaptive(strokeWidth: 2))
: controller.error != null else if (controller.error != null)
? Center( Center(
child: Padding( 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), padding: const EdgeInsets.all(12.0),
child: Text( child: Text(L10n.of(context).loginWithOneClick),
controller.error,
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 18,
color: Colors.red[900],
),
),
), ),
) const Expanded(child: Divider()),
: FutureBuilder( ]),
future: controller.getLoginTypes(), Wrap(
builder: (context, snapshot) { children: [
if (snapshot.hasError) { if (controller.ssoLoginSupported) ...{
return Center( for (final identityProvider
child: Text( in controller.identityProviders)
snapshot.error.toLocalizedString(context), _SsoButton(
textAlign: TextAlign.center, onPressed: () =>
), controller.ssoLoginAction(identityProvider.id),
); identityProvider: identityProvider,
}
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, ].toList(),
children: [ ),
if (controller.ssoLoginSupported) if (controller.ssoLoginSupported &&
Row(children: [ (controller.registrationSupported ||
const Expanded(child: Divider()), controller.passwordLoginSupported))
Padding( Row(children: [
padding: const EdgeInsets.all(12.0), const Expanded(child: Divider()),
child: Text( Padding(
L10n.of(context).loginWithOneClick), padding: const EdgeInsets.all(12.0),
), child: Text(L10n.of(context).or),
const Expanded(child: Divider()), ),
]), const Expanded(child: Divider()),
Wrap( ]),
children: [ if (controller.passwordLoginSupported) ...[
if (controller.ssoLoginSupported) ...{ Center(
for (final identityProvider child: _LoginButton(
in controller.identityProviders) onPressed: () => VRouter.of(context).to('login'),
_SsoButton( icon: Icon(
onPressed: () => CupertinoIcons.lock_open_fill,
controller.ssoLoginAction( color: Theme.of(context).textTheme.bodyText1.color,
identityProvider.id), ),
identityProvider: identityProvider, labelText: L10n.of(context).login,
), ),
}, ),
].toList(), const SizedBox(height: 12),
), ],
if (controller.ssoLoginSupported && if (controller.registrationSupported)
(controller.registrationSupported || Center(
controller.passwordLoginSupported)) child: _LoginButton(
Row(children: [ onPressed: controller.signUpAction,
const Expanded(child: Divider()), icon: Icon(
Padding( CupertinoIcons.person_add,
padding: const EdgeInsets.all(12.0), color: Theme.of(context).textTheme.bodyText1.color,
child: Text(L10n.of(context).or), ),
), labelText: L10n.of(context).register,
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,
),
),
],
),
);
}),
]), ]),
bottomNavigationBar: Material( bottomNavigationBar: Material(
elevation: 6, elevation: 6,

View File

@ -80,8 +80,8 @@ abstract class ClientManager {
legacyDatabaseBuilder: FlutterMatrixHiveStore.hiveDatabaseBuilder, legacyDatabaseBuilder: FlutterMatrixHiveStore.hiveDatabaseBuilder,
supportedLoginTypes: { supportedLoginTypes: {
AuthenticationTypes.password, AuthenticationTypes.password,
//if (PlatformInfos.isMobile || PlatformInfos.isWeb) if (PlatformInfos.isMobile || PlatformInfos.isWeb)
AuthenticationTypes.sso AuthenticationTypes.sso
}, },
compute: compute, compute: compute,
); );