FurryChat/lib/views/login.dart

182 lines
6 KiB
Dart
Raw Normal View History

2020-01-01 18:10:13 +00:00
import 'dart:math';
import 'package:famedlysdk/famedlysdk.dart';
import 'package:fluffychat/components/matrix.dart';
2020-01-20 12:46:39 +00:00
import 'package:fluffychat/i18n/i18n.dart';
2020-01-08 13:19:15 +00:00
import 'package:fluffychat/utils/app_route.dart';
import 'package:flutter/foundation.dart';
2020-01-01 18:10:13 +00:00
import 'package:flutter/material.dart';
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-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();
final TextEditingController serverController =
2020-01-29 15:19:55 +00:00
TextEditingController(text: "tchncs.de");
2020-01-01 18:10:13 +00:00
String usernameError;
String passwordError;
String serverError;
2020-01-04 08:16:29 +00:00
bool loading = false;
2020-01-09 21:52:27 +00:00
bool showPassword = false;
2020-01-01 18:10:13 +00:00
void login(BuildContext context) async {
MatrixState matrix = Matrix.of(context);
if (usernameController.text.isEmpty) {
2020-01-20 12:46:39 +00:00
setState(() => usernameError = I18n.of(context).pleaseEnterYourUsername);
2020-01-01 18:10:13 +00:00
} else {
setState(() => usernameError = null);
}
if (passwordController.text.isEmpty) {
2020-01-20 12:46:39 +00:00
setState(() => passwordError = I18n.of(context).pleaseEnterYourPassword);
2020-01-01 18:10:13 +00:00
} else {
setState(() => passwordError = null);
}
serverError = 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
String homeserver = serverController.text;
2020-01-14 14:55:38 +00:00
if (homeserver.isEmpty) homeserver = "matrix-client.matrix.org";
2020-01-02 21:31:39 +00:00
if (!homeserver.startsWith("https://")) {
2020-01-01 18:10:13 +00:00
homeserver = "https://" + homeserver;
2020-01-02 21:31:39 +00:00
}
2020-01-01 18:10:13 +00:00
try {
2020-01-04 08:16:29 +00:00
setState(() => loading = true);
2020-01-01 18:10:13 +00:00
if (!await matrix.client.checkServer(homeserver)) {
2020-01-20 12:46:39 +00:00
setState(
() => serverError = I18n.of(context).homeserverIsNotCompatible);
2020-01-01 18:10:13 +00:00
2020-01-04 08:16:29 +00:00
return setState(() => loading = false);
2020-01-01 18:10:13 +00:00
}
} catch (exception) {
2020-01-20 12:46:39 +00:00
setState(() => serverError = I18n.of(context).connectionAttemptFailed);
2020-01-04 08:16:29 +00:00
return setState(() => loading = false);
2020-01-01 18:10:13 +00:00
}
try {
2020-01-09 21:52:27 +00:00
await matrix.client.login(
usernameController.text, passwordController.text,
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
}
if (!kIsWeb) {
try {
await matrix.setupFirebase();
} 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-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
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: TextField(
2020-01-20 12:46:39 +00:00
autocorrect: false,
2020-01-01 18:10:13 +00:00
controller: serverController,
decoration: InputDecoration(
icon: Icon(Icons.domain),
2020-01-14 14:53:35 +00:00
hintText: "matrix-client.matrix.org",
2020-01-01 18:10:13 +00:00
errorText: serverError,
errorMaxLines: 1,
prefixText: "https://",
labelText: serverError == null ? "Homeserver" : serverError),
),
),
body: ListView(
padding: EdgeInsets.symmetric(
2020-01-27 09:59:03 +00:00
horizontal: max((MediaQuery.of(context).size.width - 600) / 2, 0)),
2020-01-01 18:10:13 +00:00
children: <Widget>[
2020-01-09 21:52:27 +00:00
Container(
height: 150,
color: Theme.of(context).secondaryHeaderColor,
child: Center(
child: Icon(
Icons.vpn_key,
2020-01-27 09:59:03 +00:00
size: 60,
2020-01-09 21:52:27 +00:00
),
),
2020-01-01 18:10:13 +00:00
),
2020-01-09 21:52:27 +00:00
ListTile(
leading: CircleAvatar(
2020-01-27 09:59:03 +00:00
backgroundColor: Colors.white,
child: Icon(Icons.account_box,
color: Theme.of(context).primaryColor),
2020-01-09 21:52:27 +00:00
),
title: TextField(
2020-01-20 12:46:39 +00:00
autocorrect: false,
2020-01-09 21:52:27 +00:00
controller: usernameController,
decoration: InputDecoration(
2020-01-20 12:46:39 +00:00
hintText:
"@${I18n.of(context).username.toLowerCase()}:domain",
2020-01-09 21:52:27 +00:00
errorText: usernameError,
2020-01-20 12:46:39 +00:00
labelText: I18n.of(context).username),
2020-01-09 21:52:27 +00:00
),
2020-01-01 18:10:13 +00:00
),
2020-01-09 21:52:27 +00:00
ListTile(
leading: CircleAvatar(
2020-01-27 09:59:03 +00:00
backgroundColor: Colors.white,
child: Icon(Icons.lock, color: Theme.of(context).primaryColor),
2020-01-01 18:10:13 +00:00
),
2020-01-09 21:52:27 +00:00
title: TextField(
2020-01-20 12:46:39 +00:00
autocorrect: false,
2020-01-09 21:52:27 +00:00
controller: passwordController,
obscureText: !showPassword,
onSubmitted: (t) => login(context),
decoration: InputDecoration(
hintText: "****",
errorText: passwordError,
suffixIcon: IconButton(
icon: Icon(
showPassword ? Icons.visibility_off : Icons.visibility),
onPressed: () =>
setState(() => showPassword = !showPassword),
),
2020-01-20 12:46:39 +00:00
labelText: I18n.of(context).password),
2020-01-09 21:52:27 +00:00
),
),
SizedBox(height: 20),
Container(
height: 50,
2020-01-27 09:59:03 +00:00
padding: EdgeInsets.symmetric(horizontal: 12),
2020-01-09 21:52:27 +00:00
child: RaisedButton(
elevation: 7,
color: Theme.of(context).primaryColor,
shape: RoundedRectangleBorder(
2020-01-27 09:59:03 +00:00
borderRadius: BorderRadius.circular(6),
2020-01-01 18:10:13 +00:00
),
2020-01-09 21:52:27 +00:00
child: loading
? CircularProgressIndicator()
: Text(
2020-01-27 09:59:03 +00:00
I18n.of(context).login.toUpperCase(),
2020-01-09 21:52:27 +00:00
style: TextStyle(color: Colors.white, fontSize: 16),
),
onPressed: () => loading ? null : login(context),
2020-01-01 18:10:13 +00:00
),
),
],
),
);
}
}