2020-06-23 21:23:12 +02:00
|
|
|
import 'package:flutter/cupertino.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:freezer/api/deezer.dart';
|
|
|
|
import 'package:freezer/api/player.dart';
|
|
|
|
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
|
2020-09-18 19:36:41 +02:00
|
|
|
import 'package:freezer/translations.i18n.dart';
|
2020-06-23 21:23:12 +02:00
|
|
|
|
|
|
|
import '../settings.dart';
|
|
|
|
import '../api/definitions.dart';
|
|
|
|
import 'home_screen.dart';
|
|
|
|
|
|
|
|
class LoginWidget extends StatefulWidget {
|
|
|
|
|
|
|
|
Function callback;
|
|
|
|
LoginWidget({this.callback, Key key}): super(key: key);
|
|
|
|
|
|
|
|
@override
|
|
|
|
_LoginWidgetState createState() => _LoginWidgetState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _LoginWidgetState extends State<LoginWidget> {
|
|
|
|
|
|
|
|
String _arl;
|
2020-10-09 20:52:45 +02:00
|
|
|
String _error;
|
2020-06-23 21:23:12 +02: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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//Call _init()
|
|
|
|
void _start() async {
|
|
|
|
if (settings.arl != null) {
|
|
|
|
_init().then((_) {
|
|
|
|
if (widget.callback != null) widget.callback();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void didUpdateWidget(LoginWidget oldWidget) {
|
|
|
|
_start();
|
|
|
|
super.didUpdateWidget(oldWidget);
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
_start();
|
|
|
|
super.initState();
|
|
|
|
}
|
|
|
|
|
|
|
|
void errorDialog() {
|
|
|
|
showDialog(
|
|
|
|
context: context,
|
|
|
|
builder: (context) {
|
|
|
|
return AlertDialog(
|
2020-09-18 19:36:41 +02:00
|
|
|
title: Text('Error'.i18n),
|
2020-10-09 20:52:45 +02:00
|
|
|
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')
|
|
|
|
],
|
|
|
|
),
|
2020-06-23 21:23:12 +02:00
|
|
|
actions: <Widget>[
|
|
|
|
FlatButton(
|
2020-09-18 19:36:41 +02:00
|
|
|
child: Text('Dismiss'.i18n),
|
2020-06-23 21:23:12 +02:00
|
|
|
onPressed: () {
|
|
|
|
Navigator.of(context).pop();
|
|
|
|
},
|
|
|
|
)
|
|
|
|
],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
void _update() async {
|
|
|
|
setState(() => {});
|
|
|
|
|
|
|
|
//Try logging in
|
|
|
|
try {
|
|
|
|
deezerAPI.arl = settings.arl;
|
2020-10-09 20:52:45 +02:00
|
|
|
bool resp = await deezerAPI.rawAuthorize(onError: (e) => _error = e.toString());
|
2020-06-23 21:23:12 +02:00
|
|
|
if (resp == false) { //false, not null
|
|
|
|
setState(() => settings.arl = null);
|
|
|
|
errorDialog();
|
|
|
|
}
|
|
|
|
//On error show dialog and reset to null
|
|
|
|
} catch (e) {
|
2020-10-09 20:52:45 +02:00
|
|
|
_error = e;
|
|
|
|
print('Login error: ' + e);
|
2020-06-23 21:23:12 +02:00
|
|
|
setState(() => settings.arl = null);
|
|
|
|
errorDialog();
|
|
|
|
}
|
|
|
|
|
|
|
|
await settings.save();
|
|
|
|
_start();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
|
|
|
|
//If arl non null, show loading
|
|
|
|
if (settings.arl != null)
|
|
|
|
return Scaffold(
|
|
|
|
body: Center(
|
|
|
|
child: CircularProgressIndicator(),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
if (settings.arl == null)
|
|
|
|
return Scaffold(
|
|
|
|
body: Padding(
|
|
|
|
padding: EdgeInsets.symmetric(horizontal: 8.0),
|
|
|
|
child: ListView(
|
|
|
|
children: <Widget>[
|
|
|
|
Container(height: 16.0,),
|
|
|
|
Text(
|
2020-09-18 19:36:41 +02:00
|
|
|
'Welcome to'.i18n,
|
2020-06-23 21:23:12 +02:00
|
|
|
textAlign: TextAlign.center,
|
|
|
|
style: TextStyle(
|
|
|
|
fontSize: 16.0
|
|
|
|
),
|
|
|
|
),
|
|
|
|
FreezerTitle(),
|
|
|
|
Container(height: 8.0,),
|
|
|
|
Text(
|
2020-09-18 19:36:41 +02:00
|
|
|
"Please login using your Deezer account.".i18n,
|
2020-06-23 21:23:12 +02:00
|
|
|
textAlign: TextAlign.center,
|
|
|
|
style: TextStyle(
|
2020-07-19 14:41:05 +02:00
|
|
|
fontSize: 16.0
|
2020-06-23 21:23:12 +02:00
|
|
|
),
|
|
|
|
),
|
|
|
|
Container(height: 16.0,),
|
|
|
|
Padding(
|
|
|
|
padding: EdgeInsets.symmetric(horizontal: 32.0),
|
|
|
|
child: OutlineButton(
|
2020-09-18 19:36:41 +02:00
|
|
|
child: Text('Login using browser'.i18n),
|
2020-06-23 21:23:12 +02:00
|
|
|
onPressed: () {
|
|
|
|
Navigator.of(context).push(
|
|
|
|
MaterialPageRoute(builder: (context) => LoginBrowser(_update))
|
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
),
|
|
|
|
Padding(
|
|
|
|
padding: EdgeInsets.symmetric(horizontal: 32.0),
|
|
|
|
child: OutlineButton(
|
2020-09-18 19:36:41 +02:00
|
|
|
child: Text('Login using token'.i18n),
|
2020-06-23 21:23:12 +02:00
|
|
|
onPressed: () {
|
|
|
|
showDialog(
|
|
|
|
context: context,
|
|
|
|
builder: (context) {
|
|
|
|
return AlertDialog(
|
2020-09-18 19:36:41 +02:00
|
|
|
title: Text('Enter ARL'.i18n),
|
2020-06-23 21:23:12 +02:00
|
|
|
content: Container(
|
|
|
|
child: TextField(
|
|
|
|
onChanged: (String s) => _arl = s,
|
|
|
|
decoration: InputDecoration(
|
2020-09-18 19:36:41 +02:00
|
|
|
labelText: 'Token (ARL)'.i18n
|
2020-06-23 21:23:12 +02:00
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
actions: <Widget>[
|
|
|
|
FlatButton(
|
2020-09-18 21:43:12 +02:00
|
|
|
child: Text('Save'.i18n),
|
2020-06-23 21:23:12 +02:00
|
|
|
onPressed: () {
|
2020-09-18 19:36:41 +02:00
|
|
|
settings.arl = _arl.trim();
|
2020-06-23 21:23:12 +02:00
|
|
|
Navigator.of(context).pop();
|
|
|
|
_update();
|
|
|
|
},
|
|
|
|
)
|
|
|
|
],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
),
|
|
|
|
Container(height: 16.0,),
|
|
|
|
Text(
|
2020-09-18 19:36:41 +02:00
|
|
|
"If you don't have account, you can register on deezer.com for free.".i18n,
|
2020-06-23 21:23:12 +02:00
|
|
|
textAlign: TextAlign.center,
|
|
|
|
style: TextStyle(
|
2020-07-19 14:41:05 +02:00
|
|
|
fontSize: 16.0
|
2020-06-23 21:23:12 +02:00
|
|
|
),
|
|
|
|
),
|
|
|
|
Padding(
|
|
|
|
padding: EdgeInsets.symmetric(horizontal: 32.0),
|
|
|
|
child: OutlineButton(
|
2020-09-18 21:43:12 +02:00
|
|
|
child: Text('Open in browser'.i18n),
|
2020-06-23 21:23:12 +02:00
|
|
|
onPressed: () {
|
|
|
|
InAppBrowser.openWithSystemBrowser(url: 'https://deezer.com/register');
|
|
|
|
},
|
|
|
|
),
|
|
|
|
),
|
|
|
|
Container(height: 8.0,),
|
|
|
|
Divider(),
|
|
|
|
Container(height: 8.0,),
|
|
|
|
Text(
|
2020-09-18 19:36:41 +02:00
|
|
|
"By using this app, you don't agree with the Deezer ToS".i18n,
|
2020-06-23 21:23:12 +02:00
|
|
|
textAlign: TextAlign.center,
|
|
|
|
style: TextStyle(
|
2020-07-19 14:41:05 +02:00
|
|
|
fontSize: 16.0
|
2020-06-23 21:23:12 +02:00
|
|
|
),
|
|
|
|
)
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class LoginBrowser extends StatelessWidget {
|
|
|
|
|
|
|
|
Function updateParent;
|
|
|
|
LoginBrowser(this.updateParent);
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Column(
|
|
|
|
children: <Widget>[
|
|
|
|
Expanded(
|
|
|
|
child: Container(
|
|
|
|
child: InAppWebView(
|
|
|
|
initialUrl: 'https://deezer.com/login',
|
|
|
|
onLoadStart: (InAppWebViewController controller, String url) async {
|
2020-09-22 22:22:55 +02:00
|
|
|
|
|
|
|
//Offers URL
|
|
|
|
if (url.contains('/offers')) {
|
|
|
|
controller.evaluateJavascript(source: 'window.location.href = "/open_app"');
|
|
|
|
}
|
|
|
|
|
2020-06-23 21:23:12 +02:00
|
|
|
//Parse arl from url
|
|
|
|
if (url.startsWith('intent://deezer.page.link')) {
|
|
|
|
try {
|
|
|
|
//Parse url
|
|
|
|
Uri uri = Uri.parse(url);
|
|
|
|
//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) {}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|