From 5c4526f1fc1b239006842293135bde5bb0d1209a Mon Sep 17 00:00:00 2001 From: Brandon Presley Date: Fri, 29 Dec 2023 14:50:00 +1300 Subject: [PATCH] Add loader for logging in --- lib/login.dart | 102 ++++++++++++++++++++++++++++++++----------------- 1 file changed, 67 insertions(+), 35 deletions(-) diff --git a/lib/login.dart b/lib/login.dart index c974fc8..9e35f79 100644 --- a/lib/login.dart +++ b/lib/login.dart @@ -15,11 +15,36 @@ class _LoginPageState extends State { final serverController = TextEditingController(); final usernameController = TextEditingController(); final passwordController = TextEditingController(); + bool loggingIn = false; + String failedMessage = ''; void connectMatrix() async { - final provider = Provider.of(context, listen: false); - await provider.initialize(serverController.text, usernameController.text, - passwordController.text); + setState(() { + loggingIn = true; + }); + + try { + final provider = + Provider.of(context, listen: false); + await provider.initialize(serverController.text, usernameController.text, + passwordController.text); + } catch (error) { + print("Error signing in $error"); + setState(() { + failedMessage = error.toString(); + }); + + Future.delayed(const Duration(seconds: 10), () { + setState(() { + failedMessage = ''; + }); + }); + return; + } finally { + setState(() { + loggingIn = false; + }); + } final prefs = await SharedPreferences.getInstance(); prefs.setString("homeserver", serverController.text); @@ -40,40 +65,47 @@ class _LoginPageState extends State { @override Widget build(BuildContext context) { - return Scaffold( - appBar: AppBar( - backgroundColor: Theme.of(context).colorScheme.inversePrimary, - title: const Text("Login"), - ), - body: Padding( - padding: const EdgeInsets.all(16.0), - child: Center( - child: Column( - mainAxisAlignment: MainAxisAlignment.center, - children: [ - TextFormField( - controller: serverController, - decoration: const InputDecoration( - labelText: 'Server', hintText: 'https://matrix.org'), - ), - TextFormField( - controller: usernameController, - decoration: const InputDecoration( - labelText: 'Username', hintText: 'john'), - ), - TextFormField( - controller: passwordController, - decoration: const InputDecoration(labelText: 'Password'), - obscureText: true, - ), - ], + return Consumer( + builder: (context, provider, child) => Scaffold( + appBar: AppBar( + backgroundColor: Theme.of(context).colorScheme.inversePrimary, + title: const Text("Login"), + ), + body: Padding( + padding: const EdgeInsets.all(16.0), + child: Center( + child: Column( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + TextFormField( + controller: serverController, + decoration: const InputDecoration( + labelText: 'Server', hintText: 'https://matrix.org'), + ), + TextFormField( + controller: usernameController, + decoration: const InputDecoration( + labelText: 'Username', hintText: 'john'), + ), + TextFormField( + controller: passwordController, + decoration: const InputDecoration(labelText: 'Password'), + obscureText: true, + onFieldSubmitted: (value) => connectMatrix(), + ), + Text(failedMessage, + style: Theme.of(context).textTheme.headlineSmall) + ], + ), ), ), - ), - floatingActionButton: FloatingActionButton( - onPressed: connectMatrix, - tooltip: 'Log in', - child: const Icon(Icons.login), + floatingActionButton: loggingIn + ? const CircularProgressIndicator() + : FloatingActionButton( + onPressed: connectMatrix, + tooltip: 'Log in', + child: const Icon(Icons.login), + ), ), ); }