moved out logic from UI

This commit is contained in:
shilangyu 2020-09-08 16:41:46 +02:00
parent 8eb31434e2
commit 94daf2acc6
1 changed files with 35 additions and 33 deletions

View File

@ -165,6 +165,22 @@ class _AccountsConfigAddInstanceDialog extends HookWidget {
var loading = useState(false); var loading = useState(false);
handleOnAdd() async {
try {
loading.value = true;
await context
.read<AccountsStore>()
.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();
}
return AlertDialog( return AlertDialog(
title: Text('Add instance'), title: Text('Add instance'),
content: TextField( content: TextField(
@ -182,23 +198,7 @@ class _AccountsConfigAddInstanceDialog extends HookWidget {
), ),
FlatButton( FlatButton(
child: !loading.value ? Text('Add') : CircularProgressIndicator(), child: !loading.value ? Text('Add') : CircularProgressIndicator(),
onPressed: instanceController.text.isEmpty onPressed: instanceController.text.isEmpty ? null : handleOnAdd,
? null
: () async {
try {
loading.value = true;
await context
.read<AccountsStore>()
.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();
},
), ),
], ],
); );
@ -223,6 +223,23 @@ class _AccountsConfigAddAccountDialog extends HookWidget {
var loading = useState(false); var loading = useState(false);
handleOnAdd() async {
try {
loading.value = true;
await context.read<AccountsStore>().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();
}
return AlertDialog( return AlertDialog(
title: Text('Add account'), title: Text('Add account'),
content: Column( content: Column(
@ -257,22 +274,7 @@ class _AccountsConfigAddAccountDialog extends HookWidget {
onPressed: onPressed:
usernameController.text.isEmpty || passwordController.text.isEmpty usernameController.text.isEmpty || passwordController.text.isEmpty
? null ? null
: () async { : handleOnAdd,
try {
loading.value = true;
await context.read<AccountsStore>().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();
},
), ),
], ],
); );