2021-02-01 12:12:28 +01:00
|
|
|
import 'dart:async';
|
2020-04-12 10:35:45 +02:00
|
|
|
import 'dart:math';
|
|
|
|
|
2021-01-16 12:46:38 +01:00
|
|
|
import 'package:adaptive_page_layout/adaptive_page_layout.dart';
|
2021-01-18 22:59:02 +01:00
|
|
|
import 'package:famedlysdk/famedlysdk.dart';
|
2021-01-22 21:39:37 +01:00
|
|
|
import 'package:fluffychat/components/default_app_bar_search_field.dart';
|
2020-04-12 10:35:45 +02:00
|
|
|
import 'package:fluffychat/components/matrix.dart';
|
2020-12-11 14:14:33 +01:00
|
|
|
import 'package:fluffychat/app_config.dart';
|
2021-01-17 15:43:38 +01:00
|
|
|
import 'package:fluffychat/utils/platform_infos.dart';
|
2021-01-17 15:33:31 +01:00
|
|
|
import 'package:flushbar/flushbar_helper.dart';
|
2021-02-01 12:12:28 +01:00
|
|
|
import 'package:flutter/foundation.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-02-01 12:12:28 +01:00
|
|
|
import 'package:future_loading_dialog/future_loading_dialog.dart';
|
|
|
|
import 'package:receive_sharing_intent/receive_sharing_intent.dart';
|
|
|
|
import 'package:url_launcher/url_launcher.dart';
|
2021-01-17 15:33:31 +01:00
|
|
|
import '../utils/localized_exception_extension.dart';
|
2021-02-01 12:12:28 +01:00
|
|
|
import 'package:universal_html/prefer_universal/html.dart' as html;
|
2020-04-12 10:35:45 +02:00
|
|
|
|
2021-01-17 15:33:31 +01:00
|
|
|
class HomeserverPicker extends StatefulWidget {
|
|
|
|
@override
|
|
|
|
_HomeserverPickerState createState() => _HomeserverPickerState();
|
|
|
|
}
|
2020-04-12 10:35:45 +02:00
|
|
|
|
2021-01-17 15:33:31 +01:00
|
|
|
class _HomeserverPickerState extends State<HomeserverPicker> {
|
|
|
|
bool _isLoading = false;
|
2021-01-19 21:40:46 +01:00
|
|
|
String _domain = AppConfig.defaultHomeserver;
|
2021-01-22 21:39:37 +01:00
|
|
|
final TextEditingController _controller =
|
|
|
|
TextEditingController(text: AppConfig.defaultHomeserver);
|
2021-02-01 12:12:28 +01:00
|
|
|
StreamSubscription _intentDataStreamSubscription;
|
|
|
|
|
|
|
|
void _loginWithToken(String token) {
|
|
|
|
if (token?.isEmpty ?? true) return;
|
|
|
|
showFutureLoadingDialog(
|
|
|
|
context: context,
|
|
|
|
future: () => Matrix.of(context).client.login(
|
|
|
|
type: AuthenticationTypes.token,
|
|
|
|
userIdentifierType: null,
|
|
|
|
token: token,
|
|
|
|
initialDeviceDisplayName: Matrix.of(context).clientName,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
void _processIncomingSharedText(String text) async {
|
|
|
|
if (text == null || !text.startsWith(AppConfig.appOpenUrlScheme)) return;
|
|
|
|
AdaptivePageLayout.of(context).popUntilIsFirst();
|
|
|
|
final token = Uri.parse(text).queryParameters['loginToken'];
|
|
|
|
_loginWithToken(token);
|
|
|
|
}
|
|
|
|
|
|
|
|
void _initReceiveSharingContent() {
|
|
|
|
if (!PlatformInfos.isMobile) return;
|
|
|
|
_intentDataStreamSubscription =
|
|
|
|
ReceiveSharingIntent.getTextStream().listen(_processIncomingSharedText);
|
|
|
|
ReceiveSharingIntent.getInitialText().then(_processIncomingSharedText);
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
|
|
|
_initReceiveSharingContent();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void dispose() {
|
|
|
|
super.dispose();
|
|
|
|
_intentDataStreamSubscription?.cancel();
|
|
|
|
}
|
2021-01-17 15:33:31 +01:00
|
|
|
|
|
|
|
void _checkHomeserverAction(BuildContext context) async {
|
2021-01-22 21:39:37 +01:00
|
|
|
try {
|
|
|
|
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-01-22 21:39:37 +01:00
|
|
|
setState(() => _isLoading = true);
|
2021-01-21 08:44:17 +01:00
|
|
|
await Matrix.of(context).client.checkHomeserver(homeserver);
|
2021-01-18 22:59:02 +01: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)) {
|
2021-02-01 12:12:28 +01:00
|
|
|
final redirectUrl = kIsWeb
|
|
|
|
? html.window.location.href
|
|
|
|
: '${Uri.encodeQueryComponent(AppConfig.appOpenUrlScheme.toLowerCase() + '://sso')}';
|
|
|
|
await launch(
|
|
|
|
'${Matrix.of(context).client.homeserver?.toString()}/_matrix/client/r0/login/sso/redirect?redirectUrl=$redirectUrl');
|
2021-01-18 22:59:02 +01:00
|
|
|
}
|
2021-01-22 21:39:37 +01:00
|
|
|
} on String catch (e) {
|
|
|
|
// ignore: unawaited_futures
|
|
|
|
FlushbarHelper.createError(message: e).show(context);
|
2021-01-17 15:33:31 +01:00
|
|
|
} catch (e) {
|
|
|
|
// ignore: unawaited_futures
|
|
|
|
FlushbarHelper.createError(
|
|
|
|
message: (e as Object).toLocalizedString(context))
|
|
|
|
.show(context);
|
|
|
|
} finally {
|
|
|
|
if (mounted) {
|
|
|
|
setState(() => _isLoading = false);
|
|
|
|
}
|
2020-04-12 10:35:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2021-01-18 07:59:01 +01:00
|
|
|
final padding = EdgeInsets.symmetric(
|
|
|
|
horizontal: max((MediaQuery.of(context).size.width - 600) / 2, 0),
|
|
|
|
);
|
2021-02-01 12:12:28 +01:00
|
|
|
if (kIsWeb) {
|
|
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
|
|
final token =
|
|
|
|
Uri.parse(html.window.location.href).queryParameters['loginToken'];
|
|
|
|
_loginWithToken(token);
|
|
|
|
});
|
|
|
|
}
|
2020-04-12 10:35:45 +02:00
|
|
|
return Scaffold(
|
2021-01-22 21:39:37 +01:00
|
|
|
appBar: AppBar(
|
|
|
|
title: DefaultAppBarSearchField(
|
|
|
|
prefixText: 'https://',
|
|
|
|
hintText: L10n.of(context).enterYourHomeserver,
|
|
|
|
searchController: _controller,
|
|
|
|
suffix: Icon(Icons.edit_outlined),
|
|
|
|
padding: padding,
|
|
|
|
onChanged: (s) => _domain = s,
|
2021-01-23 12:11:02 +01:00
|
|
|
readOnly: !AppConfig.allowOtherHomeservers,
|
2021-02-06 20:35:52 +01:00
|
|
|
onSubmit: (_) => _checkHomeserverAction(context),
|
2021-01-22 21:39:37 +01:00
|
|
|
),
|
|
|
|
elevation: 0,
|
|
|
|
),
|
2020-04-12 10:35:45 +02:00
|
|
|
body: SafeArea(
|
|
|
|
child: Padding(
|
2021-01-18 07:59:01 +01:00
|
|
|
padding: padding,
|
2021-01-17 15:33:31 +01: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 10:35:45 +02:00
|
|
|
),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
2021-01-19 21:40:46 +01:00
|
|
|
bottomNavigationBar: Padding(
|
|
|
|
padding: padding,
|
|
|
|
child: Column(
|
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
children: [
|
|
|
|
Hero(
|
|
|
|
tag: 'loginButton',
|
|
|
|
child: Container(
|
|
|
|
width: double.infinity,
|
2021-01-24 17:26:59 +01:00
|
|
|
height: 56,
|
2021-01-19 21:40:46 +01:00
|
|
|
padding: EdgeInsets.symmetric(horizontal: 12),
|
|
|
|
child: RaisedButton(
|
|
|
|
elevation: 7,
|
|
|
|
color: Theme.of(context).primaryColor,
|
|
|
|
shape: RoundedRectangleBorder(
|
2021-01-24 17:26:59 +01:00
|
|
|
borderRadius: BorderRadius.circular(12),
|
2021-01-19 21:40:46 +01:00
|
|
|
),
|
|
|
|
child: _isLoading
|
|
|
|
? LinearProgressIndicator()
|
|
|
|
: Text(
|
|
|
|
L10n.of(context).connect.toUpperCase(),
|
|
|
|
style: TextStyle(color: Colors.white, fontSize: 16),
|
|
|
|
),
|
|
|
|
onPressed:
|
|
|
|
_isLoading ? null : () => _checkHomeserverAction(context),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
2021-01-22 21:39:37 +01:00
|
|
|
Wrap(
|
|
|
|
alignment: WrapAlignment.center,
|
|
|
|
children: [
|
|
|
|
FlatButton(
|
|
|
|
child: Text(
|
|
|
|
L10n.of(context).privacy,
|
|
|
|
style: TextStyle(
|
|
|
|
decoration: TextDecoration.underline,
|
|
|
|
color: Colors.blueGrey,
|
|
|
|
),
|
|
|
|
),
|
2021-02-02 17:42:19 +01:00
|
|
|
onPressed: () => launch(AppConfig.privacyUrl),
|
2021-01-22 21:39:37 +01:00
|
|
|
),
|
|
|
|
FlatButton(
|
|
|
|
child: Text(
|
|
|
|
L10n.of(context).about,
|
|
|
|
style: TextStyle(
|
|
|
|
decoration: TextDecoration.underline,
|
|
|
|
color: Colors.blueGrey,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
onPressed: () => PlatformInfos.showDialog(context),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
2021-01-19 21:40:46 +01:00
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
2020-04-12 10:35:45 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|