2020-04-12 08:35:45 +00:00
|
|
|
import 'dart:math';
|
|
|
|
|
2020-06-13 19:38:12 +00:00
|
|
|
import 'package:famedlysdk/matrix_api/model/well_known_informations.dart';
|
2020-04-12 08:35:45 +00:00
|
|
|
import 'package:flutter/material.dart';
|
2020-10-15 13:51:24 +00:00
|
|
|
import 'package:flutter_gen/gen_l10n/l10n.dart';
|
|
|
|
|
|
|
|
import '../components/dialogs/simple_dialogs.dart';
|
|
|
|
import '../components/matrix.dart';
|
|
|
|
import '../utils/app_route.dart';
|
|
|
|
import 'login.dart';
|
|
|
|
import 'sign_up.dart';
|
2020-04-12 08:35:45 +00:00
|
|
|
|
2020-06-13 19:38:12 +00:00
|
|
|
class HomeserverPicker extends StatefulWidget {
|
|
|
|
@override
|
|
|
|
_HomeserverPickerState createState() => _HomeserverPickerState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _HomeserverPickerState extends State<HomeserverPicker> {
|
2020-05-13 13:58:59 +00:00
|
|
|
void _checkHomeserverAction(String homeserver, BuildContext context) async {
|
2020-06-13 19:38:12 +00:00
|
|
|
if (!_isMXID && !homeserver.startsWith('https://')) {
|
2020-04-12 08:35:45 +00:00
|
|
|
homeserver = 'https://$homeserver';
|
|
|
|
}
|
2020-06-13 19:38:12 +00:00
|
|
|
WellKnownInformations wellknown;
|
|
|
|
if (_isMXID) {
|
|
|
|
if (!homeserver.startsWith('@')) {
|
|
|
|
homeserver = '@$homeserver';
|
|
|
|
}
|
|
|
|
wellknown = await SimpleDialogs(context).tryRequestWithLoadingDialog(
|
|
|
|
Matrix.of(context)
|
|
|
|
.client
|
|
|
|
.getWellKnownInformationsByUserId(homeserver));
|
|
|
|
final success = await SimpleDialogs(context).tryRequestWithLoadingDialog(
|
|
|
|
Matrix.of(context).client.checkServer(wellknown.mHomeserver != null
|
|
|
|
? 'https://${Uri.parse(wellknown.mHomeserver.baseUrl).host}'
|
|
|
|
: homeserver));
|
|
|
|
if (success != false) {
|
|
|
|
await Navigator.of(context).push(AppRoute(Login(
|
|
|
|
username: homeserver,
|
2020-06-14 12:12:47 +00:00
|
|
|
wellknown: wellknown,
|
2020-06-13 19:38:12 +00:00
|
|
|
)));
|
|
|
|
}
|
|
|
|
} else {
|
2020-07-22 00:51:57 +00:00
|
|
|
homeserver = homeserver.trim();
|
|
|
|
if (homeserver.endsWith('/')) {
|
|
|
|
homeserver = homeserver.substring(0, homeserver.length - 1);
|
|
|
|
}
|
2020-06-13 19:38:12 +00:00
|
|
|
wellknown = await SimpleDialogs(context).tryRequestWithLoadingDialog(
|
|
|
|
Matrix.of(context)
|
|
|
|
.client
|
|
|
|
.getWellKnownInformationsByDomain(homeserver));
|
|
|
|
|
|
|
|
final success = await SimpleDialogs(context).tryRequestWithLoadingDialog(
|
|
|
|
Matrix.of(context).client.checkServer(wellknown.mHomeserver != null
|
|
|
|
? 'https://${Uri.parse(wellknown.mHomeserver.baseUrl).host}'
|
|
|
|
: homeserver));
|
|
|
|
if (success != false) {
|
2020-06-14 12:12:47 +00:00
|
|
|
await Navigator.of(context).push(AppRoute(SignUp(
|
|
|
|
wellknown: wellknown,
|
|
|
|
)));
|
2020-06-13 19:38:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
final textController = TextEditingController();
|
|
|
|
bool _isMXID = false;
|
|
|
|
|
|
|
|
void _checkInputType() {
|
|
|
|
if (textController.text.contains(':')) {
|
|
|
|
setState(() {
|
|
|
|
_isMXID = true;
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
setState(() {
|
|
|
|
_isMXID = false;
|
|
|
|
});
|
2020-04-12 08:35:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-13 19:38:12 +00:00
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
|
|
|
textController.addListener(_checkInputType);
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void dispose() {
|
|
|
|
textController.dispose();
|
|
|
|
super.dispose();
|
|
|
|
}
|
|
|
|
|
2020-04-12 08:35:45 +00:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Scaffold(
|
|
|
|
body: SafeArea(
|
|
|
|
child: Padding(
|
|
|
|
padding: EdgeInsets.symmetric(
|
|
|
|
horizontal:
|
|
|
|
max((MediaQuery.of(context).size.width - 600) / 2, 0)),
|
|
|
|
child: Column(
|
|
|
|
children: <Widget>[
|
|
|
|
Hero(
|
|
|
|
tag: 'loginBanner',
|
2020-06-13 19:38:12 +00:00
|
|
|
child: Center(
|
|
|
|
child: Padding(
|
|
|
|
padding: const EdgeInsets.fromLTRB(0.0, 32.0, 0.0, 18.0),
|
|
|
|
child: Text(
|
|
|
|
L10n.of(context).fluffychat,
|
|
|
|
style: TextStyle(fontSize: 28.0),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
2020-04-12 08:35:45 +00:00
|
|
|
),
|
|
|
|
Padding(
|
|
|
|
padding: const EdgeInsets.all(16.0),
|
|
|
|
child: Text(
|
2020-05-07 05:52:40 +00:00
|
|
|
L10n.of(context).welcomeText,
|
2020-04-12 08:35:45 +00:00
|
|
|
textAlign: TextAlign.center,
|
|
|
|
style: TextStyle(
|
|
|
|
fontSize: 22,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
Spacer(),
|
2020-06-13 19:38:12 +00:00
|
|
|
Padding(
|
|
|
|
padding: const EdgeInsets.all(16.0),
|
|
|
|
child: TextField(
|
|
|
|
controller: textController,
|
|
|
|
onSubmitted: (s) {
|
|
|
|
_checkHomeserverAction(s, context);
|
|
|
|
},
|
|
|
|
decoration: InputDecoration(
|
|
|
|
icon: (_isMXID
|
|
|
|
? Icon(Icons.person_outline)
|
|
|
|
: Icon(Icons.business)),
|
|
|
|
labelText: L10n.of(context).homeserverOrMXID,
|
|
|
|
border: OutlineInputBorder(),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
Spacer(),
|
2020-04-12 08:35:45 +00:00
|
|
|
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: Text(
|
2020-06-14 11:14:22 +00:00
|
|
|
(_isMXID
|
|
|
|
? L10n.of(context).login.toUpperCase()
|
|
|
|
: L10n.of(context).connect.toUpperCase()),
|
2020-04-12 08:35:45 +00:00
|
|
|
style: TextStyle(color: Colors.white, fontSize: 16),
|
|
|
|
),
|
2020-06-13 19:38:12 +00:00
|
|
|
onPressed: () =>
|
|
|
|
_checkHomeserverAction(textController.text, context),
|
2020-04-12 08:35:45 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
SizedBox(height: 16),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|