fluffychat/lib/views/homeserver_picker.dart

196 lines
6.6 KiB
Dart
Raw Normal View History

2020-04-12 08:35:45 +00:00
import 'dart:math';
2021-01-19 20:40:46 +00:00
import 'package:adaptive_dialog/adaptive_dialog.dart';
2021-01-16 11:46:38 +00:00
import 'package:adaptive_page_layout/adaptive_page_layout.dart';
2021-01-18 21:59:02 +00:00
import 'package:famedlysdk/famedlysdk.dart';
2020-04-12 08:35:45 +00:00
import 'package:fluffychat/components/matrix.dart';
2020-12-11 13:14:33 +00:00
import 'package:fluffychat/app_config.dart';
2021-01-17 14:33:31 +00:00
import 'package:fluffychat/components/sentry_switch_list_tile.dart';
2021-01-17 14:43:38 +00:00
import 'package:fluffychat/utils/platform_infos.dart';
2021-01-17 14:33:31 +00:00
import 'package:flushbar/flushbar_helper.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';
2020-04-12 08:35:45 +00:00
2021-01-17 14:33:31 +00:00
class HomeserverPicker extends StatefulWidget {
@override
_HomeserverPickerState createState() => _HomeserverPickerState();
}
2020-04-12 08:35:45 +00:00
2021-01-17 14:33:31 +00:00
class _HomeserverPickerState extends State<HomeserverPicker> {
bool _isLoading = false;
2021-01-19 20:40:46 +00:00
String _domain = AppConfig.defaultHomeserver;
2021-01-17 14:33:31 +00:00
void _checkHomeserverAction(BuildContext context) async {
2021-01-19 20:40:46 +00:00
var homeserver = _domain;
2020-09-08 08:55:32 +00:00
2020-04-12 08:35:45 +00:00
if (!homeserver.startsWith('https://')) {
homeserver = 'https://$homeserver';
}
2021-01-17 14:33:31 +00:00
setState(() => _isLoading = true);
2021-01-18 21:59:02 +00:00
try {
2021-01-21 07:44:17 +00:00
await Matrix.of(context).client.checkHomeserver(homeserver);
2021-01-18 21:59:02 +00:00
final loginTypes = await Matrix.of(context).client.requestLoginTypes();
if (loginTypes.flows
.any((flow) => flow.type == AuthenticationTypes.password)) {
await AdaptivePageLayout.of(context)
.pushNamed(AppConfig.enableRegistration ? '/signup' : '/login');
} else if (loginTypes.flows
.any((flow) => flow.type == AuthenticationTypes.sso)) {
await AdaptivePageLayout.of(context).pushNamed('/sso');
}
2021-01-17 14:33:31 +00:00
} catch (e) {
// ignore: unawaited_futures
FlushbarHelper.createError(
title: L10n.of(context).noConnectionToTheServer,
message: (e as Object).toLocalizedString(context))
.show(context);
} finally {
if (mounted) {
setState(() => _isLoading = false);
}
2020-04-12 08:35:45 +00:00
}
}
2021-01-19 20:40:46 +00:00
void _changeHomeserverAction(BuildContext context) async {
final input = await showTextInputDialog(
context: context,
title: L10n.of(context).changeTheHomeserver,
textFields: [
DialogTextField(
keyboardType: TextInputType.url,
prefixText: 'https://',
hintText: AppConfig.defaultHomeserver,
),
],
);
if (input?.single?.isNotEmpty ?? false) {
setState(() => _domain = input.single);
}
}
2020-04-12 08:35:45 +00:00
@override
Widget build(BuildContext context) {
2021-01-18 06:59:01 +00:00
final padding = EdgeInsets.symmetric(
horizontal: max((MediaQuery.of(context).size.width - 600) / 2, 0),
);
2020-04-12 08:35:45 +00:00
return Scaffold(
body: SafeArea(
child: Padding(
2021-01-18 06:59:01 +00:00
padding: padding,
2021-01-17 14:33:31 +00:00
child: ListView(
children: [
Hero(
tag: 'loginBanner',
child: Image.asset('assets/banner.png'),
),
Padding(
padding: const EdgeInsets.all(16.0),
child: Text(
AppConfig.applicationWelcomeMessage ??
L10n.of(context).welcomeText,
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 22,
),
2020-04-12 08:35:45 +00:00
),
),
2020-12-11 13:14:33 +00:00
SizedBox(height: 16),
2021-01-19 20:40:46 +00:00
Padding(
padding: const EdgeInsets.symmetric(horizontal: 16.0),
child: Material(
borderRadius: BorderRadius.circular(16),
elevation: 2,
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
SizedBox(height: 8),
Text(
L10n.of(context).youWillBeConnectedTo(_domain),
style: TextStyle(fontSize: 16),
),
FlatButton(
padding: EdgeInsets.all(8),
child: Text(
L10n.of(context).changeTheHomeserver,
style: TextStyle(
decoration: TextDecoration.underline,
fontSize: 16,
color: Colors.blue,
2021-01-17 14:33:31 +00:00
),
2021-01-19 20:40:46 +00:00
),
onPressed: () => _changeHomeserverAction(context),
),
],
2020-04-12 08:35:45 +00:00
),
),
),
2020-12-11 13:14:33 +00:00
Wrap(
2021-01-17 14:33:31 +00:00
alignment: WrapAlignment.center,
2020-12-11 13:14:33 +00:00
children: [
2021-01-17 14:33:31 +00:00
FlatButton(
padding: EdgeInsets.all(8),
child: Text(
2021-01-19 20:40:46 +00:00
L10n.of(context).privacy,
2021-01-17 14:33:31 +00:00
style: TextStyle(
decoration: TextDecoration.underline,
2021-01-19 20:40:46 +00:00
color: Colors.blueGrey,
2020-12-11 13:14:33 +00:00
),
),
2021-01-17 14:43:38 +00:00
onPressed: () => PlatformInfos.showDialog(context),
2021-01-17 14:33:31 +00:00
),
2020-12-11 13:14:33 +00:00
FlatButton(
2021-01-16 14:13:29 +00:00
padding: EdgeInsets.all(8),
2020-12-11 13:14:33 +00:00
child: Text(
2021-01-19 20:40:46 +00:00
L10n.of(context).about,
2020-12-11 13:14:33 +00:00
style: TextStyle(
decoration: TextDecoration.underline,
2021-01-19 20:40:46 +00:00
color: Colors.blueGrey,
2020-12-11 13:14:33 +00:00
),
),
2021-01-19 20:40:46 +00:00
onPressed: () => PlatformInfos.showDialog(context),
2020-04-12 08:35:45 +00:00
),
2020-12-11 13:14:33 +00:00
],
2020-04-12 08:35:45 +00:00
),
],
),
),
),
2021-01-19 20:40:46 +00:00
bottomNavigationBar: Padding(
padding: padding,
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Hero(
tag: 'loginButton',
child: Container(
width: double.infinity,
height: 50,
padding: EdgeInsets.symmetric(horizontal: 12),
child: RaisedButton(
elevation: 7,
color: Theme.of(context).primaryColor,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(6),
),
child: _isLoading
? LinearProgressIndicator()
: Text(
L10n.of(context).connect.toUpperCase(),
style: TextStyle(color: Colors.white, fontSize: 16),
),
onPressed:
_isLoading ? null : () => _checkHomeserverAction(context),
),
),
),
SentrySwitchListTile(),
],
),
),
2020-04-12 08:35:45 +00:00
);
}
}