fluffychat/lib/views/login.dart

198 lines
6.7 KiB
Dart
Raw Normal View History

2020-05-16 08:43:27 +02:00
import 'dart:async';
2020-01-01 19:10:13 +01:00
import 'dart:math';
import 'package:famedlysdk/famedlysdk.dart';
2020-05-16 08:43:27 +02:00
import 'package:fluffychat/components/dialogs/simple_dialogs.dart';
2020-01-01 19:10:13 +01:00
import 'package:fluffychat/components/matrix.dart';
2020-05-07 07:52:40 +02:00
import 'package:fluffychat/l10n/l10n.dart';
2020-01-08 14:19:15 +01:00
import 'package:fluffychat/utils/app_route.dart';
2020-05-05 10:30:24 +02:00
import 'package:fluffychat/utils/firebase_controller.dart';
import 'package:flutter/foundation.dart';
2020-01-01 19:10:13 +01:00
import 'package:flutter/material.dart';
2020-01-08 14:19:15 +01:00
import 'chat_list.dart';
2020-01-09 22:52:27 +01:00
class Login extends StatefulWidget {
2020-01-01 19:10:13 +01:00
@override
2020-01-09 22:52:27 +01:00
_LoginState createState() => _LoginState();
2020-01-01 19:10:13 +01:00
}
2020-01-09 22:52:27 +01:00
class _LoginState extends State<Login> {
2020-01-01 19:10:13 +01:00
final TextEditingController usernameController = TextEditingController();
final TextEditingController passwordController = TextEditingController();
String usernameError;
String passwordError;
2020-01-04 09:16:29 +01:00
bool loading = false;
2020-01-09 22:52:27 +01:00
bool showPassword = false;
2020-01-01 19:10:13 +01:00
void login(BuildContext context) async {
2020-05-13 15:58:59 +02:00
var matrix = Matrix.of(context);
2020-01-01 19:10:13 +01:00
if (usernameController.text.isEmpty) {
2020-05-07 07:52:40 +02:00
setState(() => usernameError = L10n.of(context).pleaseEnterYourUsername);
2020-01-01 19:10:13 +01:00
} else {
setState(() => usernameError = null);
}
if (passwordController.text.isEmpty) {
2020-05-07 07:52:40 +02:00
setState(() => passwordError = L10n.of(context).pleaseEnterYourPassword);
2020-01-01 19:10:13 +01:00
} else {
setState(() => passwordError = null);
}
2020-01-02 22:31:39 +01:00
if (usernameController.text.isEmpty || passwordController.text.isEmpty) {
2020-01-01 19:10:13 +01:00
return;
2020-01-02 22:31:39 +01:00
}
2020-01-01 19:10:13 +01:00
2020-04-12 10:35:45 +02:00
setState(() => loading = true);
2020-01-01 19:10:13 +01:00
try {
2020-01-09 22:52:27 +01:00
await matrix.client.login(
2020-08-16 12:54:43 +02:00
user: usernameController.text,
password: passwordController.text,
2020-01-09 22:52:27 +01:00
initialDeviceDisplayName: matrix.widget.clientName);
2020-01-01 19:10:13 +01:00
} on MatrixException catch (exception) {
setState(() => passwordError = exception.errorMessage);
2020-01-04 09:16:29 +01:00
return setState(() => loading = false);
2020-01-01 19:10:13 +01:00
} catch (exception) {
setState(() => passwordError = exception.toString());
2020-01-04 09:16:29 +01:00
return setState(() => loading = false);
2020-01-01 19:10:13 +01:00
}
if (!kIsWeb) {
try {
2020-05-05 10:30:24 +02:00
await FirebaseController.setupFirebase(
matrix,
2020-05-05 10:30:24 +02:00
matrix.widget.clientName,
);
} catch (exception) {
await matrix.client.logout();
matrix.clean();
setState(() => passwordError = exception.toString());
return setState(() => loading = false);
}
2020-01-08 14:19:15 +01:00
}
2020-01-04 09:16:29 +01:00
setState(() => loading = false);
2020-01-08 14:19:15 +01:00
await Navigator.of(context).pushAndRemoveUntil(
AppRoute.defaultRoute(context, ChatListView()), (r) => false);
2020-01-01 19:10:13 +01:00
}
2020-05-16 08:43:27 +02: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 12:54:43 +02:00
newDomain != Matrix.of(context).client.homeserver.toString()) {
2020-05-16 08:43:27 +02:00
await SimpleDialogs(context).tryRequestWithErrorToast(
Matrix.of(context).client.checkServer(newDomain));
setState(() => usernameError = null);
}
} catch (e) {
setState(() => usernameError = e.toString());
}
}
2020-01-01 19:10:13 +01:00
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
2020-02-16 08:44:56 +01:00
leading: loading ? Container() : null,
2020-04-12 10:35:45 +02:00
elevation: 0,
title: Text(
2020-05-07 07:52:40 +02:00
L10n.of(context).logInTo(Matrix.of(context)
2020-04-12 10:35:45 +02:00
.client
.homeserver
2020-06-10 10:07:01 +02:00
.toString()
2020-04-12 10:35:45 +02:00
.replaceFirst('https://', '')),
2020-01-01 19:10:13 +01:00
),
),
2020-04-12 09:19:22 +02:00
body: Builder(builder: (context) {
return ListView(
padding: EdgeInsets.symmetric(
horizontal:
max((MediaQuery.of(context).size.width - 600) / 2, 0)),
children: <Widget>[
ListTile(
leading: CircleAvatar(
child: Icon(Icons.account_box,
color: Theme.of(context).primaryColor),
),
title: TextField(
readOnly: loading,
autocorrect: false,
2020-04-12 10:35:45 +02:00
autofocus: true,
keyboardType: TextInputType.emailAddress,
2020-05-16 08:43:27 +02:00
onChanged: (t) => _checkWellKnownWithCoolDown(t, context),
2020-04-12 09:19:22 +02:00
controller: usernameController,
decoration: InputDecoration(
hintText:
2020-05-13 15:58:59 +02:00
'@${L10n.of(context).username.toLowerCase()}:domain',
2020-04-12 09:19:22 +02:00
errorText: usernameError,
2020-05-07 07:52:40 +02:00
labelText: L10n.of(context).username),
2020-04-12 09:19:22 +02:00
),
2020-01-09 22:52:27 +01:00
),
2020-04-12 09:19:22 +02:00
ListTile(
leading: CircleAvatar(
backgroundColor: Theme.of(context).brightness == Brightness.dark
? Color(0xff121212)
: Colors.white,
child: Icon(Icons.lock, color: Theme.of(context).primaryColor),
2020-01-01 19:10:13 +01:00
),
2020-04-12 09:19:22 +02:00
title: TextField(
readOnly: loading,
autocorrect: false,
controller: passwordController,
obscureText: !showPassword,
onSubmitted: (t) => login(context),
decoration: InputDecoration(
2020-05-13 15:58:59 +02:00
hintText: '****',
2020-04-12 09:19:22 +02:00
errorText: passwordError,
suffixIcon: IconButton(
icon: Icon(showPassword
? Icons.visibility_off
: Icons.visibility),
onPressed: () =>
setState(() => showPassword = !showPassword),
2020-01-09 22:52:27 +01:00
),
2020-05-07 07:52:40 +02:00
labelText: L10n.of(context).password),
2020-04-12 09:19:22 +02:00
),
2020-01-01 19:10:13 +01:00
),
2020-04-12 09:19:22 +02:00
SizedBox(height: 20),
2020-04-12 10:35:45 +02: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 07:52:40 +02:00
L10n.of(context).login.toUpperCase(),
2020-04-12 10:35:45 +02:00
style: TextStyle(color: Colors.white, fontSize: 16),
),
2020-06-20 11:35:54 +02:00
onPressed: loading ? null : () => login(context),
2020-04-12 09:19:22 +02:00
),
),
),
],
);
}),
2020-01-01 19:10:13 +01:00
);
}
}