2020-05-11 19:22:50 +02:00
|
|
|
import 'dart:async';
|
|
|
|
|
|
|
|
|
|
import 'package:flutter/material.dart';
|
2020-05-13 16:52:39 +02:00
|
|
|
import 'package:musicus_client/musicus_client.dart';
|
2020-05-11 19:22:50 +02:00
|
|
|
import 'package:musicus_common/musicus_common.dart';
|
|
|
|
|
|
2020-05-13 16:52:39 +02:00
|
|
|
import 'email.dart';
|
|
|
|
|
import 'password.dart';
|
2020-05-11 19:22:50 +02:00
|
|
|
import 'register.dart';
|
|
|
|
|
|
|
|
|
|
class AccountSettingsScreen extends StatefulWidget {
|
|
|
|
|
@override
|
|
|
|
|
_AccountSettingsScreenState createState() => _AccountSettingsScreenState();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class _AccountSettingsScreenState extends State<AccountSettingsScreen> {
|
2020-05-13 16:52:39 +02:00
|
|
|
final _usernameController = TextEditingController();
|
|
|
|
|
final _passwordController = TextEditingController();
|
2020-05-11 19:22:50 +02:00
|
|
|
|
2020-05-13 16:52:39 +02:00
|
|
|
MusicusBackendState _backend;
|
|
|
|
|
StreamSubscription<MusicusAccountCredentials> _accountSubscription;
|
|
|
|
|
bool _loading = false;
|
|
|
|
|
bool _loggedIn = false;
|
|
|
|
|
String _username;
|
|
|
|
|
String _email;
|
2020-05-11 19:22:50 +02:00
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
void didChangeDependencies() {
|
|
|
|
|
super.didChangeDependencies();
|
|
|
|
|
|
2020-05-13 16:52:39 +02:00
|
|
|
_backend = MusicusBackend.of(context);
|
2020-05-11 19:22:50 +02:00
|
|
|
|
2020-05-13 16:52:39 +02:00
|
|
|
final credentials = _backend.settings.account.value;
|
|
|
|
|
if (credentials != null) {
|
|
|
|
|
_setCredentials(credentials);
|
|
|
|
|
_getDetails();
|
2020-05-11 19:22:50 +02:00
|
|
|
}
|
|
|
|
|
|
2020-05-13 16:52:39 +02:00
|
|
|
_accountSubscription = _backend.settings.account.listen((credentials) {
|
|
|
|
|
_setCredentials(credentials);
|
2020-05-11 19:22:50 +02:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-13 16:52:39 +02:00
|
|
|
Future<void> _setCredentials(MusicusAccountCredentials credentials) async {
|
2020-05-13 17:42:04 +02:00
|
|
|
if (mounted) {
|
|
|
|
|
if (credentials != null) {
|
2020-05-13 16:52:39 +02:00
|
|
|
setState(() {
|
|
|
|
|
_loggedIn = true;
|
|
|
|
|
_username = credentials.username;
|
|
|
|
|
});
|
2020-05-13 17:42:04 +02:00
|
|
|
} else {
|
|
|
|
|
setState(() {
|
|
|
|
|
_loggedIn = false;
|
|
|
|
|
});
|
2020-05-13 16:52:39 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<void> _getDetails() async {
|
|
|
|
|
setState(() {
|
|
|
|
|
_email = null;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
final email = (await _backend.client.getAccountDetails()).email;
|
|
|
|
|
|
|
|
|
|
if (mounted) {
|
|
|
|
|
setState(() {
|
|
|
|
|
_email = email;
|
|
|
|
|
});
|
|
|
|
|
}
|
2020-05-11 19:22:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
Widget build(BuildContext context) {
|
2020-05-13 16:52:39 +02:00
|
|
|
List<Widget> children;
|
|
|
|
|
|
|
|
|
|
if (_loggedIn) {
|
|
|
|
|
children = [
|
|
|
|
|
Material(
|
|
|
|
|
elevation: 2.0,
|
|
|
|
|
child: ListTile(
|
|
|
|
|
title: Text('Logged in as: $_username'),
|
2020-05-11 19:22:50 +02:00
|
|
|
),
|
2020-05-13 16:52:39 +02:00
|
|
|
),
|
|
|
|
|
ListTile(
|
|
|
|
|
title: Text('E-mail address'),
|
|
|
|
|
subtitle: Text(
|
|
|
|
|
_email != null ? _email.isNotEmpty ? _email : 'Not set' : '...'),
|
|
|
|
|
trailing: const Icon(Icons.chevron_right),
|
|
|
|
|
onTap: () async {
|
|
|
|
|
await Navigator.push(
|
|
|
|
|
context,
|
|
|
|
|
MaterialPageRoute(
|
|
|
|
|
builder: (context) => EmailScreen(
|
|
|
|
|
email: _email,
|
|
|
|
|
),
|
2020-05-11 19:22:50 +02:00
|
|
|
),
|
2020-05-13 16:52:39 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
_getDetails();
|
|
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
ListTile(
|
|
|
|
|
title: Text('Change password'),
|
|
|
|
|
trailing: const Icon(Icons.chevron_right),
|
|
|
|
|
onTap: () {
|
|
|
|
|
Navigator.push(
|
|
|
|
|
context,
|
|
|
|
|
MaterialPageRoute(
|
|
|
|
|
builder: (context) => PasswordScreen(),
|
2020-05-11 19:22:50 +02:00
|
|
|
),
|
2020-05-13 16:52:39 +02:00
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
),
|
2020-05-13 17:42:04 +02:00
|
|
|
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: <Widget>[
|
|
|
|
|
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);
|
|
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
),
|
2020-05-13 16:52:39 +02:00
|
|
|
ListTile(
|
|
|
|
|
title: Text('Logout'),
|
|
|
|
|
onTap: () async {
|
|
|
|
|
await _backend.settings.clearAccount();
|
|
|
|
|
Navigator.pop(context);
|
|
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
];
|
|
|
|
|
} else {
|
|
|
|
|
children = [
|
|
|
|
|
Padding(
|
|
|
|
|
padding: const EdgeInsets.only(
|
|
|
|
|
left: 16.0,
|
|
|
|
|
right: 16.0,
|
|
|
|
|
top: 16.0,
|
|
|
|
|
bottom: 8.0,
|
|
|
|
|
),
|
|
|
|
|
child: Text(
|
|
|
|
|
'Enter your Musicus account credentials:',
|
|
|
|
|
style: Theme.of(context).textTheme.subtitle1,
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
Padding(
|
|
|
|
|
padding: const EdgeInsets.symmetric(
|
|
|
|
|
horizontal: 16.0,
|
|
|
|
|
),
|
|
|
|
|
child: TextField(
|
|
|
|
|
controller: _usernameController,
|
|
|
|
|
decoration: InputDecoration(
|
|
|
|
|
labelText: 'User name',
|
2020-05-11 19:22:50 +02:00
|
|
|
),
|
|
|
|
|
),
|
2020-05-13 16:52:39 +02:00
|
|
|
),
|
|
|
|
|
SizedBox(
|
|
|
|
|
height: 16.0,
|
|
|
|
|
),
|
|
|
|
|
Padding(
|
|
|
|
|
padding: const EdgeInsets.symmetric(
|
|
|
|
|
horizontal: 16.0,
|
2020-05-11 19:22:50 +02:00
|
|
|
),
|
2020-05-13 16:52:39 +02:00
|
|
|
child: TextField(
|
|
|
|
|
controller: _passwordController,
|
|
|
|
|
obscureText: true,
|
|
|
|
|
decoration: InputDecoration(
|
|
|
|
|
labelText: 'Password',
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
SizedBox(
|
|
|
|
|
height: 32.0,
|
|
|
|
|
),
|
|
|
|
|
ListTile(
|
|
|
|
|
title: Text('Create a new account'),
|
|
|
|
|
onTap: () {
|
|
|
|
|
Navigator.pushReplacement(
|
|
|
|
|
context,
|
|
|
|
|
MaterialPageRoute(
|
|
|
|
|
builder: (context) => RegisterScreen(),
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Scaffold(
|
|
|
|
|
appBar: AppBar(
|
|
|
|
|
title: Text('Musicus account'),
|
|
|
|
|
actions: <Widget>[
|
|
|
|
|
Builder(
|
|
|
|
|
builder: (context) {
|
|
|
|
|
if (_loggedIn) {
|
|
|
|
|
return Container();
|
|
|
|
|
} else 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 {
|
|
|
|
|
setState(() {
|
|
|
|
|
_loading = true;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
final credentials = MusicusAccountCredentials(
|
|
|
|
|
username: _usernameController.text,
|
|
|
|
|
password: _passwordController.text,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
_backend.client.credentials = credentials;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
await _backend.client.login();
|
|
|
|
|
await _backend.settings.setAccount(credentials);
|
|
|
|
|
Navigator.pop(context);
|
|
|
|
|
} on MusicusLoginFailedException {
|
|
|
|
|
Scaffold.of(context).showSnackBar(
|
|
|
|
|
SnackBar(
|
|
|
|
|
content: Text('Login failed'),
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setState(() {
|
|
|
|
|
_loading = false;
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
child: Text('LOGIN'),
|
|
|
|
|
);
|
|
|
|
|
}
|
2020-05-11 19:22:50 +02:00
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
),
|
2020-05-13 16:52:39 +02:00
|
|
|
body: ListView(
|
|
|
|
|
children: children,
|
|
|
|
|
),
|
2020-05-11 19:22:50 +02:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
void dispose() {
|
|
|
|
|
super.dispose();
|
2020-05-13 16:52:39 +02:00
|
|
|
_accountSubscription.cancel();
|
2020-05-11 19:22:50 +02:00
|
|
|
}
|
|
|
|
|
}
|