freezer/lib/ui/login_screen.dart

431 lines
14 KiB
Dart
Raw Normal View History

2020-06-23 19:23:12 +00:00
import 'package:flutter/material.dart';
2020-10-31 20:52:23 +00:00
import 'package:flutter/services.dart';
2021-03-21 20:57:18 +00:00
import 'package:fluttertoast/fluttertoast.dart';
2020-06-23 19:23:12 +00:00
import 'package:freezer/api/deezer.dart';
import 'package:freezer/api/player.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
import 'package:freezer/translations.i18n.dart';
2020-06-23 19:23:12 +00:00
import '../settings.dart';
import '../api/definitions.dart';
import 'home_screen.dart';
class LoginWidget extends StatefulWidget {
2021-04-04 22:59:07 +00:00
final Function callback;
LoginWidget({this.callback, Key key}) : super(key: key);
2020-06-23 19:23:12 +00:00
@override
_LoginWidgetState createState() => _LoginWidgetState();
}
class _LoginWidgetState extends State<LoginWidget> {
String _arl;
String _error;
2020-06-23 19:23:12 +00:00
//Initialize deezer etc
Future _init() async {
deezerAPI.arl = settings.arl;
await playerHelper.start();
//Pre-cache homepage
if (!await HomePage().exists()) {
await deezerAPI.authorize();
settings.offlineMode = false;
HomePage hp = await deezerAPI.homePage();
await hp.save();
}
}
2021-04-04 22:59:07 +00:00
2020-06-23 19:23:12 +00:00
//Call _init()
void _start() async {
if (settings.arl != null) {
_init().then((_) {
if (widget.callback != null) widget.callback();
});
}
}
2021-03-16 19:35:50 +00:00
//Check if deezer available in current country
void _checkAvailability() async {
bool available = await DeezerAPI.chceckAvailability();
2021-04-04 22:59:07 +00:00
if (!(available ?? true)) {
2021-03-16 19:35:50 +00:00
showDialog(
2021-04-04 22:59:07 +00:00
context: context,
builder: (context) => AlertDialog(
title: Text("Deezer is unavailable".i18n),
content: Text(
"Deezer is unavailable in your country, Freezer might not work properly. Please use a VPN"
.i18n),
actions: [
TextButton(
child: Text('Continue'.i18n),
onPressed: () => Navigator.of(context).pop(),
)
],
));
2021-03-16 19:35:50 +00:00
}
}
2020-06-23 19:23:12 +00:00
@override
void didUpdateWidget(LoginWidget oldWidget) {
_start();
super.didUpdateWidget(oldWidget);
}
@override
void initState() {
_start();
2021-03-16 19:35:50 +00:00
_checkAvailability();
2020-06-23 19:23:12 +00:00
super.initState();
}
void errorDialog() {
showDialog(
2021-04-04 22:59:07 +00:00
context: context,
builder: (context) {
return AlertDialog(
title: Text('Error'.i18n),
content: Column(
mainAxisSize: MainAxisSize.min,
children: [
Text(
'Error logging in! Please check your token and internet connection and try again.'
.i18n),
if (_error != null) Text('\n\n$_error')
],
),
actions: <Widget>[
TextButton(
child: Text('Dismiss'.i18n),
onPressed: () {
Navigator.of(context).pop();
},
)
],
2021-04-04 22:59:07 +00:00
);
});
2020-06-23 19:23:12 +00:00
}
void _update() async {
setState(() => {});
//Try logging in
try {
deezerAPI.arl = settings.arl;
2021-04-04 22:59:07 +00:00
bool resp = await deezerAPI.rawAuthorize(
onError: (e) => setState(() => _error = e.toString()));
if (resp == false) {
//false, not null
if (settings.arl.length != 192) {
if (_error == null) _error = '';
2021-04-04 22:59:07 +00:00
_error += 'Invalid ARL length!';
}
2020-06-23 19:23:12 +00:00
setState(() => settings.arl = null);
errorDialog();
}
//On error show dialog and reset to null
} catch (e) {
_error = e.toString();
print('Login error: ' + e.toString());
2020-06-23 19:23:12 +00:00
setState(() => settings.arl = null);
errorDialog();
}
await settings.save();
_start();
}
2020-10-31 20:52:23 +00:00
// ARL auth: called on "Save" click, Enter and DPAD_Center press
void goARL(FocusNode node, TextEditingController _controller) {
if (node != null) {
node.unfocus();
}
_controller.clear();
settings.arl = _arl.trim();
Navigator.of(context).pop();
_update();
}
2020-06-23 19:23:12 +00:00
@override
Widget build(BuildContext context) {
//If arl non null, show loading
if (settings.arl != null)
return Scaffold(
body: Center(
child: CircularProgressIndicator(),
),
);
2020-10-31 20:52:23 +00:00
TextEditingController _controller = new TextEditingController();
// For "DPAD center" key handling on remote controls
2021-04-04 22:59:07 +00:00
FocusNode focusNode = FocusNode(
skipTraversal: true,
descendantsAreFocusable: false,
onKey: (node, event) {
if (event.logicalKey == LogicalKeyboardKey.select) {
goARL(node, _controller);
}
2021-08-29 22:25:18 +00:00
return KeyEventResult.handled;
2021-04-04 22:59:07 +00:00
});
2020-06-23 19:23:12 +00:00
if (settings.arl == null)
return Scaffold(
2021-04-04 22:59:07 +00:00
body: Padding(
padding: EdgeInsets.symmetric(vertical: 16.0, horizontal: 8.0),
child: Theme(
data: Theme.of(context).copyWith(
outlinedButtonTheme: OutlinedButtonThemeData(
style: ButtonStyle(
foregroundColor:
MaterialStateProperty.all(Colors.white)))),
//data: ThemeData(
// outlinedButtonTheme: OutlinedButtonThemeData(
// style: ButtonStyle(
// foregroundColor:
// MaterialStateProperty.all(Colors.white)))),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
2020-06-23 19:23:12 +00:00
children: <Widget>[
2021-04-04 22:59:07 +00:00
Expanded(
child: Padding(
padding: EdgeInsets.symmetric(horizontal: 32.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
FreezerTitle(),
Container(height: 16.0),
Text(
"Please login using your Deezer account.".i18n,
textAlign: TextAlign.center,
style: TextStyle(fontSize: 16.0),
),
Container(
height: 16.0,
),
//Email login dialog
OutlinedButton(
child: Text(
'Login using email'.i18n,
2020-06-23 19:23:12 +00:00
),
2021-04-04 22:59:07 +00:00
onPressed: () {
showDialog(
context: context,
builder: (context) => EmailLogin(_update));
},
2020-06-23 19:23:12 +00:00
),
2021-04-04 22:59:07 +00:00
OutlinedButton(
child: Text('Login using browser'.i18n),
onPressed: () {
Navigator.of(context).push(MaterialPageRoute(
builder: (context) =>
LoginBrowser(_update)));
},
),
OutlinedButton(
child: Text('Login using token'.i18n),
onPressed: () {
showDialog(
context: context,
builder: (context) {
Future.delayed(
Duration(seconds: 1),
() => {
focusNode.requestFocus()
}); // autofocus doesn't work - it's replacement
return AlertDialog(
title: Text('Enter ARL'.i18n),
content: Container(
child: TextField(
onChanged: (String s) => _arl = s,
decoration: InputDecoration(
labelText: 'Token (ARL)'.i18n),
focusNode: focusNode,
controller: _controller,
onSubmitted: (String s) {
goARL(focusNode, _controller);
},
),
),
actions: <Widget>[
TextButton(
child: Text('Save'.i18n),
onPressed: () =>
goARL(null, _controller),
)
],
);
});
},
),
]))),
Container(height: 16.0),
2020-06-23 19:23:12 +00:00
Text(
2021-04-04 22:59:07 +00:00
"If you don't have account, you can register on deezer.com for free."
.i18n,
2020-06-23 19:23:12 +00:00
textAlign: TextAlign.center,
2021-04-04 22:59:07 +00:00
style: TextStyle(fontSize: 16.0),
2020-06-23 19:23:12 +00:00
),
2021-04-04 22:59:07 +00:00
Container(height: 8.0),
2020-06-23 19:23:12 +00:00
Padding(
padding: EdgeInsets.symmetric(horizontal: 32.0),
2021-04-04 22:59:07 +00:00
child: OutlinedButton(
child: Text('Open in browser'.i18n),
2020-06-23 19:23:12 +00:00
onPressed: () {
2021-04-04 22:59:07 +00:00
InAppBrowser.openWithSystemBrowser(
2021-08-29 22:25:18 +00:00
url: Uri.parse('https://deezer.com/register'));
2020-06-23 19:23:12 +00:00
},
),
),
2021-04-04 22:59:07 +00:00
Container(
height: 8.0,
),
2020-06-23 19:23:12 +00:00
Divider(),
2021-04-04 22:59:07 +00:00
Container(
height: 8.0,
),
2020-06-23 19:23:12 +00:00
Text(
"By using this app, you don't agree with the Deezer ToS".i18n,
2020-06-23 19:23:12 +00:00
textAlign: TextAlign.center,
2021-04-04 22:59:07 +00:00
style: TextStyle(fontSize: 14.0),
2020-06-23 19:23:12 +00:00
)
],
),
),
2021-04-04 22:59:07 +00:00
));
2020-06-23 19:23:12 +00:00
return null;
}
}
class LoginBrowser extends StatelessWidget {
2021-04-04 22:59:07 +00:00
final Function updateParent;
2020-06-23 19:23:12 +00:00
LoginBrowser(this.updateParent);
@override
Widget build(BuildContext context) {
2021-08-29 22:25:18 +00:00
return SafeArea(
child: InAppWebView(
initialUrlRequest:
URLRequest(url: Uri.parse('https://deezer.com/login')),
onLoadStart: (InAppWebViewController controller, Uri uri) async {
//Offers URL
if (!uri.path.contains('/login') && !uri.path.contains('/register')) {
controller.evaluateJavascript(
source: 'window.location.href = "/open_app"');
}
print('scheme ${uri.scheme}, host: ${uri.host}');
//Parse arl from url
if (uri.scheme == 'intent' && uri.host == 'deezer.page.link') {
try {
//Actual url is in `link` query parameter
Uri linkUri = Uri.parse(uri.queryParameters['link']);
String arl = linkUri.queryParameters['arl'];
if (arl != null) {
settings.arl = arl;
Navigator.of(context).pop();
updateParent();
}
} catch (e) {
print(e);
}
}
},
),
2020-06-23 19:23:12 +00:00
);
}
}
2021-03-21 20:57:18 +00:00
class EmailLogin extends StatefulWidget {
2021-04-04 22:59:07 +00:00
final Function callback;
EmailLogin(this.callback, {Key key}) : super(key: key);
2021-03-21 20:57:18 +00:00
@override
_EmailLoginState createState() => _EmailLoginState();
}
class _EmailLoginState extends State<EmailLogin> {
String _email;
String _password;
bool _loading = false;
2021-03-21 21:46:44 +00:00
Future _login() async {
2021-03-21 20:57:18 +00:00
setState(() => _loading = true);
//Try logging in
String arl;
String exception;
try {
arl = await DeezerAPI.getArlByEmail(_email, _password);
} catch (e, st) {
exception = e.toString();
print(e);
print(st);
}
setState(() => _loading = false);
//Success
if (arl != null) {
settings.arl = arl;
Navigator.of(context).pop();
widget.callback();
return;
}
//Error
showDialog(
2021-04-04 22:59:07 +00:00
context: context,
builder: (context) => AlertDialog(
title: Text("Error logging in!".i18n),
content: Text(
"Error logging in using email, please check your credentials.\nError: " +
exception),
actions: [
TextButton(
child: Text('Dismiss'.i18n),
onPressed: () {
Navigator.of(context).pop();
},
)
],
));
2021-03-21 20:57:18 +00:00
}
@override
Widget build(BuildContext context) {
return AlertDialog(
title: Text('Email Login'.i18n),
content: Column(
mainAxisSize: MainAxisSize.min,
2021-04-04 22:59:07 +00:00
children: _loading
? [CircularProgressIndicator()]
: [
TextField(
decoration: InputDecoration(labelText: 'Email'.i18n),
onChanged: (s) => _email = s,
),
Container(
height: 8.0,
),
TextField(
obscureText: true,
decoration: InputDecoration(labelText: "Password".i18n),
onChanged: (s) => _password = s,
)
],
2021-03-21 20:57:18 +00:00
),
actions: [
if (!_loading)
TextButton(
child: Text('Login'),
onPressed: () async {
if (_email != null && _password != null)
await _login();
else
Fluttertoast.showToast(
2021-04-04 22:59:07 +00:00
msg: "Missing email or password!".i18n,
gravity: ToastGravity.BOTTOM,
toastLength: Toast.LENGTH_SHORT);
2021-03-21 20:57:18 +00:00
},
)
],
);
}
}