From c08e07311a1894ecfefab73eadce1e4c4284644c Mon Sep 17 00:00:00 2001 From: shilangyu Date: Tue, 8 Sep 2020 01:27:02 +0200 Subject: [PATCH] you can now add accounts/instances --- lib/pages/settings.dart | 185 ++++++++++++++++++++++++++++++++-------- 1 file changed, 148 insertions(+), 37 deletions(-) diff --git a/lib/pages/settings.dart b/lib/pages/settings.dart index 6d0ae8c..655d99c 100644 --- a/lib/pages/settings.dart +++ b/lib/pages/settings.dart @@ -84,7 +84,6 @@ class _AccountsConfig extends HookWidget { @override Widget build(BuildContext context) { var theme = Theme.of(context); - var textFieldController = useTextEditingController(); return Scaffold( key: _scaffoldKey, @@ -96,44 +95,12 @@ class _AccountsConfig extends HookWidget { centerTitle: true, ), floatingActionButton: FloatingActionButton( - onPressed: () async { - await showDialog( + onPressed: () { + showDialog( context: context, - builder: (ctx) => AlertDialog( - title: Text('Add instance'), - content: TextField( - controller: textFieldController, - decoration: InputDecoration( - border: OutlineInputBorder(), - labelText: 'Instance url', - ), - ), - actions: [ - FlatButton( - child: Text('Add'), - onPressed: () async { - try { - await context - .read() - .addInstance(textFieldController.text); - } on Exception catch (err) { - _scaffoldKey.currentState.showSnackBar(SnackBar( - content: Text(err.toString()), - )); - } - Navigator.of(context).pop(); - }, - ), - FlatButton( - child: Text('Cancel'), - onPressed: () { - Navigator.of(context).pop(); - }, - ) - ], - ), + builder: (_) => + _AccountsConfigAddInstanceDialog(scaffoldKey: _scaffoldKey), ); - textFieldController.clear(); }, child: Icon(Icons.add), ), @@ -163,6 +130,19 @@ class _AccountsConfig extends HookWidget { onTap: () {}, // TODO: go to managing account ), ], + ListTile( + leading: Icon(Icons.add), + title: Text('Add account'), + onTap: () { + showDialog( + context: context, + builder: (_) => _AccountsConfigAddAccountDialog( + scaffoldKey: _scaffoldKey, + instanceUrl: entry.key, + ), + ); + }, + ), Divider(), ] ]..removeLast(), // removes trailing Divider @@ -173,6 +153,137 @@ class _AccountsConfig extends HookWidget { } } +class _AccountsConfigAddInstanceDialog extends HookWidget { + final GlobalKey scaffoldKey; + + const _AccountsConfigAddInstanceDialog({@required this.scaffoldKey}) + : assert(scaffoldKey != null); + + @override + Widget build(BuildContext context) { + var instanceController = useTextEditingController(); + useValueListenable(instanceController); + + var loading = useState(false); + + return AlertDialog( + title: Text('Add instance'), + content: TextField( + autofocus: true, + controller: instanceController, + decoration: InputDecoration( + border: OutlineInputBorder(), + labelText: 'Instance url', + ), + ), + actions: [ + FlatButton( + child: !loading.value ? Text('Add') : CircularProgressIndicator(), + onPressed: instanceController.text.isEmpty + ? null + : () async { + try { + loading.value = true; + await context + .read() + .addInstance(instanceController.text); + scaffoldKey.currentState.hideCurrentSnackBar(); + } on Exception catch (err) { + scaffoldKey.currentState.showSnackBar(SnackBar( + content: Text(err.toString()), + )); + } + loading.value = false; + Navigator.of(context).pop(); + }, + ), + FlatButton( + child: Text('Cancel'), + onPressed: () { + Navigator.of(context).pop(); + }, + ) + ], + ); + } +} + +class _AccountsConfigAddAccountDialog extends HookWidget { + final GlobalKey scaffoldKey; + final String instanceUrl; + + const _AccountsConfigAddAccountDialog( + {@required this.scaffoldKey, @required this.instanceUrl}) + : assert(scaffoldKey != null), + assert(instanceUrl != null); + + @override + Widget build(BuildContext context) { + var usernameController = useTextEditingController(); + var passwordController = useTextEditingController(); + useValueListenable(usernameController); + useValueListenable(passwordController); + + var loading = useState(false); + + return AlertDialog( + title: Text('Add account'), + content: Column( + mainAxisSize: MainAxisSize.min, + children: [ + TextField( + autofocus: true, + controller: usernameController, + decoration: InputDecoration( + border: OutlineInputBorder(), + labelText: 'Username or email', + ), + ), + const SizedBox(height: 5), + TextField( + controller: passwordController, + obscureText: true, + decoration: InputDecoration( + border: OutlineInputBorder(), + labelText: 'Password', + ), + ), + ], + ), + actions: [ + FlatButton( + child: !loading.value ? Text('Add') : CircularProgressIndicator(), + onPressed: + usernameController.text.isEmpty || passwordController.text.isEmpty + ? null + : () async { + try { + loading.value = true; + await context.read().addAccount( + instanceUrl, + usernameController.text, + passwordController.text, + ); + } on Exception catch (err) { + scaffoldKey.currentState.showSnackBar(SnackBar( + content: Text(err.toString()), + )); + } + loading.value = false; + Navigator.of(context).pop(); + }, + ), + FlatButton( + child: Text('Cancel'), + onPressed: () { + Navigator.of(context).pop(); + }, + ) + ], + ); + } +} + class _SectionHeading extends StatelessWidget { final String text;