From 5ffecb97c68cbf724622b3f05f4ea1b8451887f0 Mon Sep 17 00:00:00 2001 From: Elias Projahn Date: Wed, 13 May 2020 17:54:16 +0200 Subject: [PATCH] mobile: Fine tune account settings If the user enters something into the login fields, the values will be copied to the registration screen. Also, the password is required to delete an account now. --- mobile/lib/screens/account_settings.dart | 35 +++------ mobile/lib/screens/delete_account.dart | 96 ++++++++++++++++++++++++ mobile/lib/screens/register.dart | 21 ++++++ 3 files changed, 127 insertions(+), 25 deletions(-) create mode 100644 mobile/lib/screens/delete_account.dart diff --git a/mobile/lib/screens/account_settings.dart b/mobile/lib/screens/account_settings.dart index 449a050..9afdb89 100644 --- a/mobile/lib/screens/account_settings.dart +++ b/mobile/lib/screens/account_settings.dart @@ -4,6 +4,7 @@ import 'package:flutter/material.dart'; import 'package:musicus_client/musicus_client.dart'; import 'package:musicus_common/musicus_common.dart'; +import 'delete_account.dart'; import 'email.dart'; import 'password.dart'; import 'register.dart'; @@ -114,30 +115,11 @@ class _AccountSettingsScreenState extends State { ), ListTile( title: Text('Delete this account'), - onTap: () async { - showDialog( - context: context, - builder: (context) => AlertDialog( - title: Text('Delete account'), - content: Text( - 'Do you really want to delete the account with the ' - 'username $_username?'), - actions: [ - FlatButton( - child: Text('DELETE'), - onPressed: () async { - await _backend.client.deleteAccount(); - await _backend.settings.clearAccount(); - Navigator.pop(context); - }, - ), - FlatButton( - child: Text('CANCEL'), - onPressed: () { - Navigator.pop(context); - }, - ), - ], + onTap: () { + Navigator.push( + context, + MaterialPageRoute( + builder: (context) => DeleteAccountScreen(), ), ); }, @@ -199,7 +181,10 @@ class _AccountSettingsScreenState extends State { Navigator.pushReplacement( context, MaterialPageRoute( - builder: (context) => RegisterScreen(), + builder: (context) => RegisterScreen( + username: _usernameController.text, + password: _passwordController.text, + ), ), ); }, diff --git a/mobile/lib/screens/delete_account.dart b/mobile/lib/screens/delete_account.dart new file mode 100644 index 0000000..0f84586 --- /dev/null +++ b/mobile/lib/screens/delete_account.dart @@ -0,0 +1,96 @@ +import 'package:flutter/material.dart'; +import 'package:musicus_common/musicus_common.dart'; + +class DeleteAccountScreen extends StatefulWidget { + @override + _DeleteAccountScreenState createState() => _DeleteAccountScreenState(); +} + +class _DeleteAccountScreenState extends State { + final _passwordController = TextEditingController(); + + bool _loading = false; + + @override + Widget build(BuildContext context) { + return Scaffold( + appBar: AppBar( + title: Text('Delete account'), + actions: [ + Builder( + builder: (context) { + if (_loading) { + return Padding( + padding: const EdgeInsets.all(16.0), + child: Center( + child: SizedBox( + width: 24.0, + height: 24.0, + child: CircularProgressIndicator( + strokeWidth: 2.0, + ), + ), + ), + ); + } else { + return FlatButton( + onPressed: () async { + final backend = MusicusBackend.of(context); + + if (_passwordController.text == + backend.settings.account.value.password) { + setState(() { + _loading = true; + }); + + await backend.client.deleteAccount(); + await backend.settings.clearAccount(); + + setState(() { + _loading = false; + }); + + Navigator.pop(context); + } else { + Scaffold.of(context).showSnackBar(SnackBar( + content: Text('Wrong password'), + )); + } + }, + child: Text('DELETE'), + ); + } + }, + ), + ], + ), + body: ListView( + children: [ + Padding( + padding: const EdgeInsets.only( + left: 16.0, + right: 16.0, + top: 16.0, + bottom: 8.0, + ), + child: Text( + 'If you really want to delete your account, enter your password ' + 'below.', + style: Theme.of(context).textTheme.subtitle1, + ), + ), + Padding( + padding: const EdgeInsets.all(16.0), + child: TextField( + controller: _passwordController, + obscureText: true, + decoration: InputDecoration( + labelText: 'Password', + ), + ), + ), + ], + ), + ); + } +} diff --git a/mobile/lib/screens/register.dart b/mobile/lib/screens/register.dart index 3944afe..c0eb881 100644 --- a/mobile/lib/screens/register.dart +++ b/mobile/lib/screens/register.dart @@ -4,6 +4,14 @@ import 'package:musicus_common/musicus_common.dart'; /// A screen for creating a new Musicus account. class RegisterScreen extends StatefulWidget { + final String username; + final String password; + + RegisterScreen({ + this.username, + this.password, + }); + @override _RegisterScreenState createState() => _RegisterScreenState(); } @@ -16,6 +24,19 @@ class _RegisterScreenState extends State { bool _loading = false; + @override + void initState() { + super.initState(); + + if (widget.username != null) { + nameController.text = widget.username; + } + + if (widget.password != null) { + passwordController.text = widget.password; + } + } + @override Widget build(BuildContext context) { final backend = MusicusBackend.of(context);