2020-01-01 19:10:13 +01:00
|
|
|
|
import 'dart:math';
|
|
|
|
|
|
|
|
|
|
import 'package:famedlysdk/famedlysdk.dart';
|
|
|
|
|
import 'package:fluffychat/components/matrix.dart';
|
2020-01-08 14:19:15 +01:00
|
|
|
|
import 'package:fluffychat/utils/app_route.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();
|
|
|
|
|
final TextEditingController serverController =
|
2020-01-14 15:53:35 +01:00
|
|
|
|
TextEditingController(text: "matrix-client.matrix.org");
|
2020-01-01 19:10:13 +01:00
|
|
|
|
String usernameError;
|
|
|
|
|
String passwordError;
|
|
|
|
|
String serverError;
|
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 {
|
|
|
|
|
MatrixState matrix = Matrix.of(context);
|
|
|
|
|
if (usernameController.text.isEmpty) {
|
|
|
|
|
setState(() => usernameError = "Please enter your username.");
|
|
|
|
|
} else {
|
|
|
|
|
setState(() => usernameError = null);
|
|
|
|
|
}
|
|
|
|
|
if (passwordController.text.isEmpty) {
|
|
|
|
|
setState(() => passwordError = "Please enter your password.");
|
|
|
|
|
} else {
|
|
|
|
|
setState(() => passwordError = null);
|
|
|
|
|
}
|
|
|
|
|
serverError = 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
|
|
|
|
|
|
|
|
|
String homeserver = serverController.text;
|
2020-01-14 15:55:38 +01:00
|
|
|
|
if (homeserver.isEmpty) homeserver = "matrix-client.matrix.org";
|
2020-01-02 22:31:39 +01:00
|
|
|
|
if (!homeserver.startsWith("https://")) {
|
2020-01-01 19:10:13 +01:00
|
|
|
|
homeserver = "https://" + homeserver;
|
2020-01-02 22:31:39 +01:00
|
|
|
|
}
|
2020-01-01 19:10:13 +01:00
|
|
|
|
|
|
|
|
|
try {
|
2020-01-08 14:19:15 +01:00
|
|
|
|
print("[Login] Check server...");
|
2020-01-04 09:16:29 +01:00
|
|
|
|
setState(() => loading = true);
|
2020-01-01 19:10:13 +01:00
|
|
|
|
if (!await matrix.client.checkServer(homeserver)) {
|
|
|
|
|
setState(() => serverError = "Homeserver is not compatible.");
|
|
|
|
|
|
2020-01-04 09:16:29 +01:00
|
|
|
|
return setState(() => loading = false);
|
2020-01-01 19:10:13 +01:00
|
|
|
|
}
|
|
|
|
|
} catch (exception) {
|
|
|
|
|
setState(() => serverError = "Connection attempt failed!");
|
2020-01-04 09:16:29 +01:00
|
|
|
|
return setState(() => loading = false);
|
2020-01-01 19:10:13 +01:00
|
|
|
|
}
|
|
|
|
|
try {
|
2020-01-08 14:19:15 +01:00
|
|
|
|
print("[Login] Try to login...");
|
2020-01-09 22:52:27 +01:00
|
|
|
|
await matrix.client.login(
|
|
|
|
|
usernameController.text, passwordController.text,
|
|
|
|
|
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
|
|
|
|
}
|
2020-01-08 14:19:15 +01:00
|
|
|
|
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");
|
2020-01-02 22:31:39 +01:00
|
|
|
|
await Matrix.of(context).saveAccount();
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
|
return Scaffold(
|
|
|
|
|
appBar: AppBar(
|
|
|
|
|
title: TextField(
|
|
|
|
|
controller: serverController,
|
|
|
|
|
decoration: InputDecoration(
|
|
|
|
|
icon: Icon(Icons.domain),
|
2020-01-14 15:53:35 +01:00
|
|
|
|
hintText: "matrix-client.matrix.org",
|
2020-01-01 19:10:13 +01:00
|
|
|
|
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>[
|
2020-01-09 22:52:27 +01:00
|
|
|
|
Container(
|
|
|
|
|
height: 150,
|
|
|
|
|
color: Theme.of(context).secondaryHeaderColor,
|
|
|
|
|
child: Center(
|
|
|
|
|
child: Icon(
|
|
|
|
|
Icons.vpn_key,
|
|
|
|
|
color: Theme.of(context).primaryColor,
|
|
|
|
|
size: 40,
|
|
|
|
|
),
|
|
|
|
|
),
|
2020-01-01 19:10:13 +01:00
|
|
|
|
),
|
2020-01-09 22:52:27 +01:00
|
|
|
|
ListTile(
|
|
|
|
|
leading: CircleAvatar(
|
|
|
|
|
backgroundColor: Colors.blue,
|
|
|
|
|
child: Icon(Icons.account_box),
|
|
|
|
|
),
|
|
|
|
|
title: TextField(
|
|
|
|
|
controller: usernameController,
|
|
|
|
|
decoration: InputDecoration(
|
|
|
|
|
hintText: "@username:domain",
|
|
|
|
|
errorText: usernameError,
|
|
|
|
|
labelText: "Username"),
|
|
|
|
|
),
|
2020-01-01 19:10:13 +01:00
|
|
|
|
),
|
2020-01-09 22:52:27 +01:00
|
|
|
|
ListTile(
|
|
|
|
|
leading: CircleAvatar(
|
|
|
|
|
backgroundColor: Colors.yellow,
|
|
|
|
|
child: Icon(Icons.lock),
|
2020-01-01 19:10:13 +01:00
|
|
|
|
),
|
2020-01-09 22:52:27 +01:00
|
|
|
|
title: TextField(
|
|
|
|
|
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: "Password"),
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
SizedBox(height: 20),
|
|
|
|
|
Container(
|
|
|
|
|
height: 50,
|
|
|
|
|
child: RaisedButton(
|
|
|
|
|
elevation: 7,
|
|
|
|
|
color: Theme.of(context).primaryColor,
|
|
|
|
|
shape: RoundedRectangleBorder(
|
2020-01-01 19:10:13 +01:00
|
|
|
|
borderRadius: BorderRadius.circular(50),
|
|
|
|
|
),
|
2020-01-09 22:52:27 +01:00
|
|
|
|
child: loading
|
|
|
|
|
? CircularProgressIndicator()
|
|
|
|
|
: Text(
|
|
|
|
|
"Login",
|
|
|
|
|
style: TextStyle(color: Colors.white, fontSize: 16),
|
|
|
|
|
),
|
|
|
|
|
onPressed: () => loading ? null : login(context),
|
2020-01-01 19:10:13 +01:00
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|