You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
fluffychat/lib/views/login.dart

185 lines
6.1 KiB
Dart

6 years ago
import 'dart:math';
import 'package:famedlysdk/famedlysdk.dart';
import 'package:fluffychat/components/matrix.dart';
import 'package:fluffychat/i18n/i18n.dart';
import 'package:fluffychat/utils/app_route.dart';
6 years ago
import 'package:flutter/material.dart';
import 'chat_list.dart';
class Login extends StatefulWidget {
6 years ago
@override
_LoginState createState() => _LoginState();
6 years ago
}
class _LoginState extends State<Login> {
6 years ago
final TextEditingController usernameController = TextEditingController();
final TextEditingController passwordController = TextEditingController();
final TextEditingController serverController =
TextEditingController(text: "matrix-client.matrix.org");
6 years ago
String usernameError;
String passwordError;
String serverError;
bool loading = false;
bool showPassword = false;
6 years ago
void login(BuildContext context) async {
MatrixState matrix = Matrix.of(context);
if (usernameController.text.isEmpty) {
setState(() => usernameError = I18n.of(context).pleaseEnterYourUsername);
6 years ago
} else {
setState(() => usernameError = null);
}
if (passwordController.text.isEmpty) {
setState(() => passwordError = I18n.of(context).pleaseEnterYourPassword);
6 years ago
} else {
setState(() => passwordError = null);
}
serverError = null;
6 years ago
if (usernameController.text.isEmpty || passwordController.text.isEmpty) {
6 years ago
return;
6 years ago
}
6 years ago
String homeserver = serverController.text;
if (homeserver.isEmpty) homeserver = "matrix-client.matrix.org";
6 years ago
if (!homeserver.startsWith("https://")) {
6 years ago
homeserver = "https://" + homeserver;
6 years ago
}
6 years ago
try {
print("[Login] Check server...");
setState(() => loading = true);
6 years ago
if (!await matrix.client.checkServer(homeserver)) {
setState(
() => serverError = I18n.of(context).homeserverIsNotCompatible);
6 years ago
return setState(() => loading = false);
6 years ago
}
} catch (exception) {
setState(() => serverError = I18n.of(context).connectionAttemptFailed);
return setState(() => loading = false);
6 years ago
}
try {
print("[Login] Try to login...");
await matrix.client.login(
usernameController.text, passwordController.text,
initialDeviceDisplayName: matrix.widget.clientName);
6 years ago
} on MatrixException catch (exception) {
setState(() => passwordError = exception.errorMessage);
return setState(() => loading = false);
6 years ago
} catch (exception) {
setState(() => passwordError = exception.toString());
return setState(() => loading = false);
6 years ago
}
try {
print("[Login] Setup Firebase...");
await matrix.setupFirebase();
} catch (exception) {
print("[Login] Failed to setup Firebase. Logout now...");
await matrix.client.logout();
matrix.clean();
setState(() => passwordError = exception.toString());
return setState(() => loading = false);
}
print("[Login] Store account and go to ChatListView");
6 years ago
await Matrix.of(context).saveAccount();
setState(() => loading = false);
await Navigator.of(context).pushAndRemoveUntil(
AppRoute.defaultRoute(context, ChatListView()), (r) => false);
6 years ago
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: TextField(
autocorrect: false,
6 years ago
controller: serverController,
decoration: InputDecoration(
icon: Icon(Icons.domain),
hintText: "matrix-client.matrix.org",
6 years ago
errorText: serverError,
errorMaxLines: 1,
prefixText: "https://",
labelText: serverError == null ? "Homeserver" : serverError),
),
),
body: ListView(
padding: EdgeInsets.symmetric(
vertical: 16,
horizontal: max((MediaQuery.of(context).size.width - 600) / 2, 16)),
children: <Widget>[
Container(
height: 150,
color: Theme.of(context).secondaryHeaderColor,
child: Center(
child: Icon(
Icons.vpn_key,
color: Theme.of(context).primaryColor,
size: 40,
),
),
6 years ago
),
ListTile(
leading: CircleAvatar(
backgroundColor: Colors.blue,
child: Icon(Icons.account_box),
),
title: TextField(
autocorrect: false,
controller: usernameController,
decoration: InputDecoration(
hintText:
"@${I18n.of(context).username.toLowerCase()}:domain",
errorText: usernameError,
labelText: I18n.of(context).username),
),
6 years ago
),
ListTile(
leading: CircleAvatar(
backgroundColor: Colors.yellow,
child: Icon(Icons.lock),
6 years ago
),
title: TextField(
autocorrect: false,
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),
),
labelText: I18n.of(context).password),
),
),
SizedBox(height: 20),
Container(
height: 50,
child: RaisedButton(
elevation: 7,
color: Theme.of(context).primaryColor,
shape: RoundedRectangleBorder(
6 years ago
borderRadius: BorderRadius.circular(50),
),
child: loading
? CircularProgressIndicator()
: Text(
I18n.of(context).login,
style: TextStyle(color: Colors.white, fontSize: 16),
),
onPressed: () => loading ? null : login(context),
6 years ago
),
),
],
),
);
}
}