2020-05-16 06:43:27 +00:00
|
|
|
import 'dart:async';
|
2020-01-01 18:10:13 +00:00
|
|
|
import 'dart:math';
|
|
|
|
|
|
|
|
import 'package:famedlysdk/famedlysdk.dart';
|
2020-10-06 19:59:36 +00:00
|
|
|
import 'package:furrychat/components/dialogs/simple_dialogs.dart';
|
|
|
|
import 'package:furrychat/components/matrix.dart';
|
|
|
|
import 'package:furrychat/utils/app_route.dart';
|
|
|
|
import 'package:furrychat/utils/firebase_controller.dart';
|
2020-01-23 11:11:57 +00:00
|
|
|
import 'package:flutter/foundation.dart';
|
2020-01-01 18:10:13 +00:00
|
|
|
import 'package:flutter/material.dart';
|
2020-10-03 11:11:07 +00:00
|
|
|
import 'package:flutter_gen/gen_l10n/l10n.dart';
|
2020-01-01 18:10:13 +00:00
|
|
|
|
2020-01-08 13:19:15 +00:00
|
|
|
import 'chat_list.dart';
|
|
|
|
|
2020-01-09 21:52:27 +00:00
|
|
|
class Login extends StatefulWidget {
|
2020-10-06 19:59:36 +00:00
|
|
|
Login({Key key, this.username, this.wellknown}) : super(key: key);
|
2020-06-13 19:38:12 +00:00
|
|
|
|
|
|
|
final String username;
|
2020-06-14 12:12:47 +00:00
|
|
|
final WellKnownInformations wellknown;
|
2020-06-13 19:38:12 +00:00
|
|
|
|
2020-01-01 18:10:13 +00:00
|
|
|
@override
|
2020-01-09 21:52:27 +00:00
|
|
|
_LoginState createState() => _LoginState();
|
2020-01-01 18:10:13 +00:00
|
|
|
}
|
|
|
|
|
2020-01-09 21:52:27 +00:00
|
|
|
class _LoginState extends State<Login> {
|
2020-01-01 18:10:13 +00:00
|
|
|
final TextEditingController usernameController = TextEditingController();
|
|
|
|
final TextEditingController passwordController = TextEditingController();
|
|
|
|
String usernameError;
|
|
|
|
String passwordError;
|
2020-01-04 08:16:29 +00:00
|
|
|
bool loading = false;
|
2020-01-09 21:52:27 +00:00
|
|
|
bool showPassword = false;
|
2020-06-14 12:12:47 +00:00
|
|
|
WellKnownInformations newWellknown;
|
2020-01-01 18:10:13 +00:00
|
|
|
|
|
|
|
void login(BuildContext context) async {
|
2020-05-13 13:58:59 +00:00
|
|
|
var matrix = Matrix.of(context);
|
2020-01-01 18:10:13 +00:00
|
|
|
if (usernameController.text.isEmpty) {
|
2020-05-07 05:52:40 +00:00
|
|
|
setState(() => usernameError = L10n.of(context).pleaseEnterYourUsername);
|
2020-01-01 18:10:13 +00:00
|
|
|
} else {
|
|
|
|
setState(() => usernameError = null);
|
|
|
|
}
|
|
|
|
if (passwordController.text.isEmpty) {
|
2020-05-07 05:52:40 +00:00
|
|
|
setState(() => passwordError = L10n.of(context).pleaseEnterYourPassword);
|
2020-01-01 18:10:13 +00:00
|
|
|
} else {
|
|
|
|
setState(() => passwordError = null);
|
|
|
|
}
|
|
|
|
|
2020-01-02 21:31:39 +00:00
|
|
|
if (usernameController.text.isEmpty || passwordController.text.isEmpty) {
|
2020-01-01 18:10:13 +00:00
|
|
|
return;
|
2020-01-02 21:31:39 +00:00
|
|
|
}
|
2020-01-01 18:10:13 +00:00
|
|
|
|
2020-04-12 08:35:45 +00:00
|
|
|
setState(() => loading = true);
|
2020-01-01 18:10:13 +00:00
|
|
|
try {
|
2020-01-09 21:52:27 +00:00
|
|
|
await matrix.client.login(
|
2020-08-16 10:54:43 +00:00
|
|
|
user: usernameController.text,
|
|
|
|
password: passwordController.text,
|
2020-01-09 21:52:27 +00:00
|
|
|
initialDeviceDisplayName: matrix.widget.clientName);
|
2020-01-01 18:10:13 +00:00
|
|
|
} on MatrixException catch (exception) {
|
|
|
|
setState(() => passwordError = exception.errorMessage);
|
2020-01-04 08:16:29 +00:00
|
|
|
return setState(() => loading = false);
|
2020-01-01 18:10:13 +00:00
|
|
|
} catch (exception) {
|
|
|
|
setState(() => passwordError = exception.toString());
|
2020-01-04 08:16:29 +00:00
|
|
|
return setState(() => loading = false);
|
2020-01-01 18:10:13 +00:00
|
|
|
}
|
2020-01-23 11:11:57 +00:00
|
|
|
if (!kIsWeb) {
|
|
|
|
try {
|
2020-05-05 08:30:24 +00:00
|
|
|
await FirebaseController.setupFirebase(
|
2020-07-21 08:56:26 +00:00
|
|
|
matrix,
|
2020-05-05 08:30:24 +00:00
|
|
|
matrix.widget.clientName,
|
|
|
|
);
|
2020-01-23 11:11:57 +00:00
|
|
|
} catch (exception) {
|
|
|
|
await matrix.client.logout();
|
|
|
|
matrix.clean();
|
|
|
|
setState(() => passwordError = exception.toString());
|
|
|
|
return setState(() => loading = false);
|
|
|
|
}
|
2020-01-08 13:19:15 +00:00
|
|
|
}
|
2020-01-04 08:16:29 +00:00
|
|
|
setState(() => loading = false);
|
2020-06-14 12:12:47 +00:00
|
|
|
if (newWellknown != null) {
|
|
|
|
if (newWellknown.jitsiHomeserver?.baseUrl != null) {
|
|
|
|
if (!newWellknown.jitsiHomeserver.baseUrl.startsWith('https://')) {
|
|
|
|
newWellknown.jitsiHomeserver.baseUrl =
|
|
|
|
'https://${newWellknown.jitsiHomeserver.baseUrl}';
|
|
|
|
}
|
2020-10-06 19:59:36 +00:00
|
|
|
await Matrix.of(context).store.setItem('chat.fluffy.jitsi_instance',
|
2020-06-14 12:12:47 +00:00
|
|
|
'https://${Uri.parse(newWellknown.jitsiHomeserver.baseUrl).host}/');
|
|
|
|
Matrix.of(context).jitsiInstance =
|
|
|
|
'https://${Uri.parse(newWellknown.jitsiHomeserver.baseUrl).host}/';
|
|
|
|
}
|
|
|
|
} else if (widget.wellknown != null) {
|
|
|
|
if (widget.wellknown.jitsiHomeserver?.baseUrl != null) {
|
|
|
|
if (!widget.wellknown.jitsiHomeserver.baseUrl.startsWith('https://')) {
|
|
|
|
widget.wellknown.jitsiHomeserver.baseUrl =
|
|
|
|
'https://${widget.wellknown.jitsiHomeserver.baseUrl}';
|
|
|
|
}
|
2020-10-06 19:59:36 +00:00
|
|
|
await Matrix.of(context).store.setItem('chat.fluffy.jitsi_instance',
|
2020-06-14 12:12:47 +00:00
|
|
|
'https://${Uri.parse(widget.wellknown.jitsiHomeserver.baseUrl).host}/');
|
|
|
|
Matrix.of(context).jitsiInstance =
|
|
|
|
'https://${Uri.parse(widget.wellknown.jitsiHomeserver.baseUrl).host}/';
|
|
|
|
}
|
|
|
|
}
|
2020-01-08 13:19:15 +00:00
|
|
|
await Navigator.of(context).pushAndRemoveUntil(
|
|
|
|
AppRoute.defaultRoute(context, ChatListView()), (r) => false);
|
2020-01-01 18:10:13 +00:00
|
|
|
}
|
|
|
|
|
2020-05-16 06:43:27 +00:00
|
|
|
Timer _coolDown;
|
|
|
|
|
|
|
|
void _checkWellKnownWithCoolDown(String userId, BuildContext context) async {
|
|
|
|
_coolDown?.cancel();
|
|
|
|
_coolDown = Timer(
|
|
|
|
Duration(seconds: 1),
|
|
|
|
() => _checkWellKnown(userId, context),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
void _checkWellKnown(String userId, BuildContext context) async {
|
|
|
|
setState(() => usernameError = null);
|
|
|
|
if (!userId.isValidMatrixId) return;
|
|
|
|
try {
|
|
|
|
final wellKnownInformations = await Matrix.of(context)
|
|
|
|
.client
|
|
|
|
.getWellKnownInformationsByUserId(userId);
|
|
|
|
final newDomain = wellKnownInformations.mHomeserver?.baseUrl;
|
|
|
|
if ((newDomain?.isNotEmpty ?? false) &&
|
2020-08-16 10:54:43 +00:00
|
|
|
newDomain != Matrix.of(context).client.homeserver.toString()) {
|
2020-05-16 06:43:27 +00:00
|
|
|
await SimpleDialogs(context).tryRequestWithErrorToast(
|
|
|
|
Matrix.of(context).client.checkServer(newDomain));
|
|
|
|
setState(() => usernameError = null);
|
|
|
|
}
|
2020-06-14 12:12:47 +00:00
|
|
|
newWellknown = wellKnownInformations;
|
2020-05-16 06:43:27 +00:00
|
|
|
} catch (e) {
|
|
|
|
setState(() => usernameError = e.toString());
|
2020-06-14 12:12:47 +00:00
|
|
|
newWellknown = null;
|
2020-05-16 06:43:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-14 11:14:10 +00:00
|
|
|
@override
|
2020-06-13 19:38:12 +00:00
|
|
|
void initState() {
|
|
|
|
super.initState();
|
|
|
|
usernameController.text = widget?.username;
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void dispose() {
|
|
|
|
usernameController.dispose();
|
|
|
|
passwordController.dispose();
|
|
|
|
super.dispose();
|
|
|
|
}
|
|
|
|
|
2020-01-01 18:10:13 +00:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Scaffold(
|
|
|
|
appBar: AppBar(
|
2020-02-16 07:44:56 +00:00
|
|
|
leading: loading ? Container() : null,
|
2020-04-12 08:35:45 +00:00
|
|
|
elevation: 0,
|
|
|
|
title: Text(
|
2020-05-07 05:52:40 +00:00
|
|
|
L10n.of(context).logInTo(Matrix.of(context)
|
2020-04-12 08:35:45 +00:00
|
|
|
.client
|
|
|
|
.homeserver
|
2020-06-10 08:07:01 +00:00
|
|
|
.toString()
|
2020-04-12 08:35:45 +00:00
|
|
|
.replaceFirst('https://', '')),
|
2020-01-01 18:10:13 +00:00
|
|
|
),
|
|
|
|
),
|
2020-04-12 07:19:22 +00:00
|
|
|
body: Builder(builder: (context) {
|
|
|
|
return ListView(
|
|
|
|
padding: EdgeInsets.symmetric(
|
2020-06-14 11:14:10 +00:00
|
|
|
horizontal: max((MediaQuery.of(context).size.width - 600) / 2, 0),
|
|
|
|
vertical: 8.0),
|
2020-04-12 07:19:22 +00:00
|
|
|
children: <Widget>[
|
|
|
|
ListTile(
|
|
|
|
title: TextField(
|
|
|
|
readOnly: loading,
|
|
|
|
autocorrect: false,
|
2020-04-12 08:35:45 +00:00
|
|
|
autofocus: true,
|
2020-07-02 09:10:03 +00:00
|
|
|
keyboardType: TextInputType.emailAddress,
|
2020-05-16 06:43:27 +00:00
|
|
|
onChanged: (t) => _checkWellKnownWithCoolDown(t, context),
|
2020-04-12 07:19:22 +00:00
|
|
|
controller: usernameController,
|
|
|
|
decoration: InputDecoration(
|
2020-06-14 11:14:10 +00:00
|
|
|
icon: Icon(Icons.person_outline),
|
|
|
|
hintText:
|
|
|
|
'@${L10n.of(context).username.toLowerCase()}:domain',
|
|
|
|
errorText: usernameError,
|
|
|
|
labelText: L10n.of(context).username,
|
|
|
|
border: OutlineInputBorder(),
|
|
|
|
),
|
2020-04-12 07:19:22 +00:00
|
|
|
),
|
2020-01-09 21:52:27 +00:00
|
|
|
),
|
2020-06-14 11:14:10 +00:00
|
|
|
SizedBox(
|
|
|
|
height: 10,
|
|
|
|
),
|
2020-04-12 07:19:22 +00:00
|
|
|
ListTile(
|
|
|
|
title: TextField(
|
|
|
|
readOnly: loading,
|
|
|
|
autocorrect: false,
|
2020-06-13 19:38:12 +00:00
|
|
|
autofocus: widget.username != null ? true : false,
|
2020-04-12 07:19:22 +00:00
|
|
|
controller: passwordController,
|
|
|
|
obscureText: !showPassword,
|
|
|
|
onSubmitted: (t) => login(context),
|
|
|
|
decoration: InputDecoration(
|
2020-06-14 11:14:10 +00:00
|
|
|
icon: Icon(Icons.lock_outline),
|
|
|
|
hintText: '****',
|
|
|
|
errorText: passwordError,
|
|
|
|
suffixIcon: IconButton(
|
|
|
|
icon: Icon(
|
|
|
|
showPassword ? Icons.visibility_off : Icons.visibility),
|
|
|
|
onPressed: () =>
|
|
|
|
setState(() => showPassword = !showPassword),
|
|
|
|
),
|
|
|
|
labelText: L10n.of(context).password,
|
|
|
|
border: OutlineInputBorder(),
|
|
|
|
),
|
2020-04-12 07:19:22 +00:00
|
|
|
),
|
2020-01-01 18:10:13 +00:00
|
|
|
),
|
2020-04-12 07:19:22 +00:00
|
|
|
SizedBox(height: 20),
|
2020-04-12 08:35:45 +00:00
|
|
|
Hero(
|
|
|
|
tag: 'loginButton',
|
|
|
|
child: Container(
|
|
|
|
height: 50,
|
|
|
|
padding: EdgeInsets.symmetric(horizontal: 12),
|
|
|
|
child: RaisedButton(
|
|
|
|
elevation: 7,
|
|
|
|
color: Theme.of(context).primaryColor,
|
|
|
|
shape: RoundedRectangleBorder(
|
|
|
|
borderRadius: BorderRadius.circular(6),
|
|
|
|
),
|
|
|
|
child: loading
|
|
|
|
? CircularProgressIndicator()
|
|
|
|
: Text(
|
2020-05-07 05:52:40 +00:00
|
|
|
L10n.of(context).login.toUpperCase(),
|
2020-04-12 08:35:45 +00:00
|
|
|
style: TextStyle(color: Colors.white, fontSize: 16),
|
|
|
|
),
|
2020-06-20 09:35:54 +00:00
|
|
|
onPressed: loading ? null : () => login(context),
|
2020-04-12 07:19:22 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
}),
|
2020-01-01 18:10:13 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|