2020-06-25 12:28:56 +00:00
|
|
|
import 'package:flutter/material.dart';
|
2021-04-05 20:22:32 +00:00
|
|
|
import 'package:flutter/services.dart';
|
|
|
|
import 'package:fluttertoast/fluttertoast.dart';
|
2020-10-19 19:28:45 +00:00
|
|
|
import 'package:freezer/settings.dart';
|
2020-06-25 12:28:56 +00:00
|
|
|
import 'package:freezer/ui/menu.dart';
|
2021-04-05 20:22:32 +00:00
|
|
|
import 'package:freezer/api/importer.dart';
|
|
|
|
import 'package:freezer/api/spotify.dart';
|
|
|
|
import 'package:freezer/ui/elements.dart';
|
2020-09-18 17:36:41 +00:00
|
|
|
import 'package:freezer/translations.i18n.dart';
|
2021-04-05 20:22:32 +00:00
|
|
|
import 'package:spotify/spotify.dart' as spotify;
|
|
|
|
import 'package:url_launcher/url_launcher.dart';
|
|
|
|
|
|
|
|
import 'dart:async';
|
2020-06-25 12:28:56 +00:00
|
|
|
|
2021-04-05 20:22:32 +00:00
|
|
|
class SpotifyImporterV1 extends StatefulWidget {
|
2020-06-25 12:28:56 +00:00
|
|
|
@override
|
2021-04-05 20:22:32 +00:00
|
|
|
_SpotifyImporterV1State createState() => _SpotifyImporterV1State();
|
2020-06-25 12:28:56 +00:00
|
|
|
}
|
|
|
|
|
2021-04-05 20:22:32 +00:00
|
|
|
class _SpotifyImporterV1State extends State<SpotifyImporterV1> {
|
2021-09-01 12:38:32 +00:00
|
|
|
late String _url;
|
2020-06-25 12:28:56 +00:00
|
|
|
bool _error = false;
|
|
|
|
bool _loading = false;
|
2021-09-01 12:38:32 +00:00
|
|
|
SpotifyPlaylist? _data;
|
2020-06-25 12:28:56 +00:00
|
|
|
|
2021-04-05 20:22:32 +00:00
|
|
|
//Load URL
|
2020-06-25 12:28:56 +00:00
|
|
|
Future _load() async {
|
|
|
|
setState(() {
|
|
|
|
_error = false;
|
|
|
|
_loading = true;
|
|
|
|
});
|
|
|
|
try {
|
2021-09-01 12:38:32 +00:00
|
|
|
String? uri = await SpotifyScrapper.resolveUrl(_url);
|
2020-06-25 12:28:56 +00:00
|
|
|
|
|
|
|
//Error/NonPlaylist
|
|
|
|
if (uri == null || uri.split(':')[1] != 'playlist') {
|
|
|
|
throw Exception();
|
|
|
|
}
|
|
|
|
//Load
|
2021-04-05 20:22:32 +00:00
|
|
|
SpotifyPlaylist data = await SpotifyScrapper.playlist(uri);
|
2020-06-25 12:28:56 +00:00
|
|
|
setState(() => _data = data);
|
|
|
|
return;
|
2020-09-18 17:36:41 +00:00
|
|
|
} catch (e, st) {
|
|
|
|
print('$e, $st');
|
2020-06-25 12:28:56 +00:00
|
|
|
setState(() {
|
|
|
|
_error = true;
|
|
|
|
_loading = false;
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
2021-04-05 20:22:32 +00:00
|
|
|
}
|
2020-06-25 12:28:56 +00:00
|
|
|
|
2021-04-05 20:22:32 +00:00
|
|
|
//Start importing
|
|
|
|
Future _start() async {
|
2021-09-01 12:38:32 +00:00
|
|
|
List<ImporterTrack> tracks = _data!.toImporter();
|
|
|
|
await importer.start(context, _data!.name, _data!.description, tracks);
|
2020-06-25 12:28:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Scaffold(
|
2020-10-19 19:28:45 +00:00
|
|
|
appBar: FreezerAppBar('Importer'.i18n),
|
2020-06-25 12:28:56 +00:00
|
|
|
body: ListView(
|
|
|
|
children: <Widget>[
|
|
|
|
ListTile(
|
2021-09-01 12:38:32 +00:00
|
|
|
title: Text(
|
|
|
|
'Currently supporting only Spotify, with 100 tracks limit'
|
|
|
|
.i18n),
|
2020-09-18 17:36:41 +00:00
|
|
|
subtitle: Text('Due to API limitations'.i18n),
|
2020-06-25 12:28:56 +00:00
|
|
|
leading: Icon(
|
|
|
|
Icons.warning,
|
|
|
|
color: Colors.deepOrangeAccent,
|
|
|
|
),
|
|
|
|
),
|
2020-10-19 19:28:45 +00:00
|
|
|
FreezerDivider(),
|
2021-09-01 12:38:32 +00:00
|
|
|
Container(
|
|
|
|
height: 16.0,
|
|
|
|
),
|
2020-06-25 12:28:56 +00:00
|
|
|
Text(
|
2020-09-18 17:36:41 +00:00
|
|
|
'Enter your playlist link below'.i18n,
|
2020-06-25 12:28:56 +00:00
|
|
|
textAlign: TextAlign.center,
|
2021-09-01 12:38:32 +00:00
|
|
|
style: TextStyle(fontSize: 20.0),
|
2020-06-25 12:28:56 +00:00
|
|
|
),
|
|
|
|
Padding(
|
|
|
|
padding: EdgeInsets.symmetric(horizontal: 16.0, vertical: 8.0),
|
|
|
|
child: Row(
|
|
|
|
children: <Widget>[
|
|
|
|
Expanded(
|
|
|
|
child: TextField(
|
|
|
|
onChanged: (String s) => _url = s,
|
|
|
|
onSubmitted: (String s) {
|
|
|
|
_url = s;
|
|
|
|
_load();
|
|
|
|
},
|
2021-09-01 12:38:32 +00:00
|
|
|
decoration: InputDecoration(hintText: 'URL'),
|
2020-06-25 12:28:56 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
IconButton(
|
2021-09-01 12:38:32 +00:00
|
|
|
icon: Icon(
|
|
|
|
Icons.search,
|
|
|
|
semanticLabel: "Search".i18n,
|
|
|
|
),
|
2020-06-25 12:28:56 +00:00
|
|
|
onPressed: () => _load(),
|
|
|
|
)
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
2021-09-01 12:38:32 +00:00
|
|
|
Container(
|
|
|
|
height: 8.0,
|
|
|
|
),
|
2020-06-25 12:28:56 +00:00
|
|
|
|
|
|
|
if (_data == null && _loading)
|
|
|
|
Row(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
2021-09-01 12:38:32 +00:00
|
|
|
children: <Widget>[CircularProgressIndicator()],
|
2020-06-25 12:28:56 +00:00
|
|
|
),
|
|
|
|
if (_error)
|
|
|
|
ListTile(
|
2020-09-18 17:36:41 +00:00
|
|
|
title: Text('Error loading URL!'.i18n),
|
2021-09-01 12:38:32 +00:00
|
|
|
leading: Icon(
|
|
|
|
Icons.error,
|
|
|
|
color: Colors.red,
|
|
|
|
),
|
2020-06-25 12:28:56 +00:00
|
|
|
),
|
2021-04-05 20:22:32 +00:00
|
|
|
//Playlist
|
2021-09-01 12:38:32 +00:00
|
|
|
if (_data != null) ...[
|
|
|
|
FreezerDivider(),
|
|
|
|
ListTile(
|
|
|
|
title: Text(_data!.name!),
|
|
|
|
subtitle: Text((_data!.description ?? '') == ''
|
|
|
|
? '${_data!.tracks!.length} tracks'
|
|
|
|
: _data!.description!),
|
|
|
|
leading: Image.network(_data!.image ??
|
|
|
|
'http://cdn-images.deezer.com/images/cover//256x256-000000-80-0-0.jpg')),
|
|
|
|
ImporterSettings(),
|
|
|
|
Padding(
|
|
|
|
padding: EdgeInsets.symmetric(vertical: 4.0, horizontal: 16.0),
|
|
|
|
child: ElevatedButton(
|
|
|
|
child: Text('Start import'.i18n),
|
|
|
|
onPressed: () async {
|
|
|
|
await _start();
|
|
|
|
Navigator.of(context).pushReplacement(MaterialPageRoute(
|
|
|
|
builder: (context) => ImporterStatusScreen()));
|
|
|
|
},
|
2021-04-05 20:22:32 +00:00
|
|
|
),
|
2021-09-01 12:38:32 +00:00
|
|
|
),
|
|
|
|
]
|
2020-06-25 12:28:56 +00:00
|
|
|
],
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-05 20:22:32 +00:00
|
|
|
class ImporterSettings extends StatefulWidget {
|
2020-06-25 12:28:56 +00:00
|
|
|
@override
|
2021-04-05 20:22:32 +00:00
|
|
|
_ImporterSettingsState createState() => _ImporterSettingsState();
|
2020-06-25 12:28:56 +00:00
|
|
|
}
|
|
|
|
|
2021-04-05 20:22:32 +00:00
|
|
|
class _ImporterSettingsState extends State<ImporterSettings> {
|
2020-06-25 12:28:56 +00:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Column(
|
2021-04-05 20:22:32 +00:00
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
children: [
|
2020-06-25 12:28:56 +00:00
|
|
|
ListTile(
|
2021-04-05 20:22:32 +00:00
|
|
|
title: Text('Download imported tracks'.i18n),
|
|
|
|
leading: Switch(
|
|
|
|
value: importer.download,
|
|
|
|
onChanged: (v) => setState(() => importer.download = v),
|
|
|
|
),
|
2020-06-25 12:28:56 +00:00
|
|
|
),
|
2021-04-05 20:22:32 +00:00
|
|
|
],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class ImporterStatusScreen extends StatefulWidget {
|
|
|
|
@override
|
|
|
|
_ImporterStatusScreenState createState() => _ImporterStatusScreenState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _ImporterStatusScreenState extends State<ImporterStatusScreen> {
|
|
|
|
bool _done = false;
|
2021-09-01 12:38:32 +00:00
|
|
|
StreamSubscription? _subscription;
|
2021-04-05 20:22:32 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
//If import done mark as not done, to prevent double routing
|
|
|
|
if (importer.done) {
|
|
|
|
_done = true;
|
|
|
|
importer.done = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Update
|
|
|
|
_subscription = importer.updateStream.listen((event) {
|
|
|
|
setState(() {
|
|
|
|
//Unset done so this page doesn't reopen
|
|
|
|
if (importer.done) {
|
|
|
|
_done = true;
|
|
|
|
importer.done = false;
|
2021-09-01 12:38:32 +00:00
|
|
|
}
|
2021-04-05 20:22:32 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
super.initState();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void dispose() {
|
2021-09-01 12:38:32 +00:00
|
|
|
_subscription?.cancel();
|
2021-04-05 20:22:32 +00:00
|
|
|
super.dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Scaffold(
|
|
|
|
appBar: FreezerAppBar('Importing...'.i18n),
|
|
|
|
body: ListView(
|
|
|
|
children: [
|
|
|
|
// Spinner
|
|
|
|
if (!_done)
|
|
|
|
Padding(
|
|
|
|
padding: EdgeInsets.symmetric(vertical: 8.0),
|
|
|
|
child: Row(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
2021-09-01 12:38:32 +00:00
|
|
|
children: <Widget>[CircularProgressIndicator()],
|
2021-04-05 20:22:32 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
|
2021-09-01 12:38:32 +00:00
|
|
|
// Progress indicator
|
|
|
|
Container(
|
|
|
|
child: Row(
|
|
|
|
mainAxisSize: MainAxisSize.max,
|
|
|
|
mainAxisAlignment: MainAxisAlignment.spaceAround,
|
|
|
|
children: <Widget>[
|
|
|
|
Row(
|
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
children: <Widget>[
|
|
|
|
Icon(
|
|
|
|
Icons.import_export,
|
|
|
|
size: 24.0,
|
|
|
|
),
|
|
|
|
Container(
|
|
|
|
width: 4.0,
|
|
|
|
),
|
|
|
|
Text(
|
|
|
|
'${importer.ok + importer.error}/${importer.tracks.length}',
|
|
|
|
style: TextStyle(fontSize: 24.0),
|
2021-04-05 20:22:32 +00:00
|
|
|
)
|
2021-09-01 12:38:32 +00:00
|
|
|
],
|
|
|
|
),
|
|
|
|
Row(
|
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
children: <Widget>[
|
|
|
|
Icon(
|
|
|
|
Icons.done,
|
|
|
|
size: 24.0,
|
|
|
|
),
|
|
|
|
Container(
|
|
|
|
width: 4.0,
|
|
|
|
),
|
|
|
|
Text(
|
|
|
|
'${importer.ok}',
|
|
|
|
style: TextStyle(fontSize: 24.0),
|
|
|
|
)
|
|
|
|
],
|
|
|
|
),
|
|
|
|
Row(
|
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
children: <Widget>[
|
|
|
|
Icon(
|
|
|
|
Icons.error,
|
|
|
|
size: 24.0,
|
|
|
|
),
|
|
|
|
Container(
|
|
|
|
width: 4.0,
|
|
|
|
),
|
|
|
|
Text(
|
|
|
|
'${importer.error}',
|
|
|
|
style: TextStyle(fontSize: 24.0),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
|
|
|
|
//When Done
|
|
|
|
if (_done)
|
|
|
|
TextButton(
|
|
|
|
child: Text('Playlist menu'.i18n),
|
|
|
|
onPressed: () {
|
|
|
|
MenuSheet m = MenuSheet(context);
|
|
|
|
m.defaultPlaylistMenu(importer.playlist!);
|
|
|
|
},
|
|
|
|
)
|
2021-04-05 20:22:32 +00:00
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
Container(height: 8.0),
|
|
|
|
FreezerDivider(),
|
|
|
|
|
|
|
|
//Tracks
|
|
|
|
...List.generate(importer.tracks.length, (i) {
|
|
|
|
ImporterTrack t = importer.tracks[i];
|
|
|
|
return ListTile(
|
|
|
|
leading: t.state.icon,
|
2021-09-01 12:38:32 +00:00
|
|
|
title: Text(t.title!),
|
2021-04-05 20:22:32 +00:00
|
|
|
subtitle: Text(
|
2021-09-01 12:38:32 +00:00
|
|
|
t.artists!.join(", "),
|
2021-04-05 20:22:32 +00:00
|
|
|
maxLines: 1,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
})
|
|
|
|
],
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class SpotifyImporterV2 extends StatefulWidget {
|
|
|
|
@override
|
|
|
|
_SpotifyImporterV2State createState() => _SpotifyImporterV2State();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _SpotifyImporterV2State extends State<SpotifyImporterV2> {
|
|
|
|
bool _authorizing = false;
|
2021-09-01 12:38:32 +00:00
|
|
|
String? _clientId;
|
|
|
|
String? _clientSecret;
|
|
|
|
final SpotifyAPIWrapper spotify = SpotifyAPIWrapper();
|
2021-04-05 20:22:32 +00:00
|
|
|
|
|
|
|
//Spotify authorization flow
|
|
|
|
Future _authorize() async {
|
|
|
|
setState(() => _authorizing = true);
|
|
|
|
await spotify.authorize(_clientId, _clientSecret);
|
|
|
|
//Save credentials
|
|
|
|
settings.spotifyClientId = _clientId;
|
|
|
|
settings.spotifyClientSecret = _clientSecret;
|
|
|
|
await settings.save();
|
|
|
|
setState(() => _authorizing = false);
|
|
|
|
//Redirect
|
|
|
|
Navigator.of(context).pushReplacement(MaterialPageRoute(
|
2021-09-01 12:38:32 +00:00
|
|
|
builder: (context) => SpotifyImporterV2Main(spotify)));
|
2021-04-05 20:22:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
_clientId = settings.spotifyClientId;
|
|
|
|
_clientSecret = settings.spotifyClientSecret;
|
2021-04-06 07:57:38 +00:00
|
|
|
|
|
|
|
//Try saved
|
|
|
|
spotify.trySaved().then((r) {
|
|
|
|
if (r) {
|
|
|
|
Navigator.of(context).pushReplacement(MaterialPageRoute(
|
2021-09-01 12:38:32 +00:00
|
|
|
builder: (context) => SpotifyImporterV2Main(spotify)));
|
2021-04-06 07:57:38 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-04-05 20:22:32 +00:00
|
|
|
super.initState();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void dispose() {
|
|
|
|
//Stop server
|
2021-09-01 12:38:32 +00:00
|
|
|
spotify.cancelAuthorize();
|
2021-04-05 20:22:32 +00:00
|
|
|
|
|
|
|
super.dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Scaffold(
|
|
|
|
appBar: FreezerAppBar("Spotify Importer v2".i18n),
|
|
|
|
body: ListView(
|
|
|
|
children: [
|
|
|
|
Padding(
|
|
|
|
padding: EdgeInsets.symmetric(vertical: 12.0, horizontal: 8.0),
|
|
|
|
child: Text(
|
2021-09-01 12:38:32 +00:00
|
|
|
"This importer requires Spotify Client ID and Client Secret. To obtain them:"
|
|
|
|
.i18n,
|
2021-04-05 20:22:32 +00:00
|
|
|
textAlign: TextAlign.center,
|
|
|
|
style: TextStyle(
|
|
|
|
fontSize: 18.0,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
Padding(
|
2021-09-01 12:38:32 +00:00
|
|
|
padding: EdgeInsets.symmetric(horizontal: 8.0),
|
|
|
|
child: Text(
|
|
|
|
"1. Go to: developer.spotify.com/dashboard and create an app."
|
|
|
|
.i18n,
|
|
|
|
textAlign: TextAlign.center,
|
|
|
|
style: TextStyle(
|
|
|
|
fontSize: 16.0,
|
|
|
|
),
|
|
|
|
)),
|
2021-04-05 20:22:32 +00:00
|
|
|
Padding(
|
|
|
|
padding: EdgeInsets.symmetric(vertical: 4.0, horizontal: 16.0),
|
|
|
|
child: ElevatedButton(
|
|
|
|
child: Text("Open in Browser".i18n),
|
2020-06-25 12:28:56 +00:00
|
|
|
onPressed: () {
|
2021-04-05 20:22:32 +00:00
|
|
|
launch("https://developer.spotify.com/dashboard");
|
2020-06-25 12:28:56 +00:00
|
|
|
},
|
|
|
|
),
|
2021-04-05 20:22:32 +00:00
|
|
|
),
|
|
|
|
Container(height: 16.0),
|
|
|
|
Padding(
|
2021-09-01 12:38:32 +00:00
|
|
|
padding: EdgeInsets.symmetric(horizontal: 8.0),
|
|
|
|
child: Text(
|
|
|
|
"2. In the app you just created go to settings, and set the Redirect URL to: "
|
|
|
|
.i18n +
|
|
|
|
"http://localhost:42069",
|
|
|
|
textAlign: TextAlign.center,
|
|
|
|
style: TextStyle(
|
|
|
|
fontSize: 16.0,
|
|
|
|
),
|
|
|
|
)),
|
2021-04-05 20:22:32 +00:00
|
|
|
Padding(
|
|
|
|
padding: EdgeInsets.symmetric(vertical: 4.0, horizontal: 16.0),
|
|
|
|
child: ElevatedButton(
|
|
|
|
child: Text("Copy the Redirect URL".i18n),
|
2020-10-19 19:28:45 +00:00
|
|
|
onPressed: () async {
|
2021-09-01 12:38:32 +00:00
|
|
|
await Clipboard.setData(
|
|
|
|
new ClipboardData(text: "http://localhost:42069"));
|
|
|
|
Fluttertoast.showToast(
|
|
|
|
msg: "Copied".i18n,
|
|
|
|
gravity: ToastGravity.BOTTOM,
|
|
|
|
toastLength: Toast.LENGTH_SHORT);
|
2020-06-25 12:28:56 +00:00
|
|
|
},
|
|
|
|
),
|
2021-04-05 20:22:32 +00:00
|
|
|
),
|
|
|
|
Padding(
|
|
|
|
padding: EdgeInsets.symmetric(horizontal: 16.0, vertical: 16.0),
|
|
|
|
child: Row(
|
|
|
|
mainAxisSize: MainAxisSize.max,
|
|
|
|
children: [
|
|
|
|
Flexible(
|
|
|
|
child: TextField(
|
|
|
|
controller: TextEditingController(text: _clientId),
|
2021-09-01 12:38:32 +00:00
|
|
|
decoration: InputDecoration(labelText: "Client ID".i18n),
|
2021-04-05 20:22:32 +00:00
|
|
|
onChanged: (v) => setState(() => _clientId = v),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
Container(width: 16.0),
|
|
|
|
Flexible(
|
|
|
|
child: TextField(
|
|
|
|
controller: TextEditingController(text: _clientSecret),
|
|
|
|
obscureText: true,
|
2021-09-01 12:38:32 +00:00
|
|
|
decoration:
|
|
|
|
InputDecoration(labelText: "Client Secret".i18n),
|
2021-04-05 20:22:32 +00:00
|
|
|
onChanged: (v) => setState(() => _clientSecret = v),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
],
|
2020-06-25 12:28:56 +00:00
|
|
|
),
|
2021-04-05 20:22:32 +00:00
|
|
|
),
|
|
|
|
Padding(
|
|
|
|
padding: EdgeInsets.symmetric(vertical: 4.0, horizontal: 16.0),
|
|
|
|
child: ElevatedButton(
|
2021-09-01 12:38:32 +00:00
|
|
|
child: Text("Authorize".i18n),
|
|
|
|
onPressed: (_clientId != null &&
|
|
|
|
_clientSecret != null &&
|
|
|
|
!_authorizing)
|
|
|
|
? () => _authorize()
|
|
|
|
: null),
|
2021-04-05 20:22:32 +00:00
|
|
|
),
|
|
|
|
if (_authorizing)
|
|
|
|
Padding(
|
|
|
|
padding: EdgeInsets.symmetric(vertical: 8.0),
|
|
|
|
child: Row(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
2021-09-01 12:38:32 +00:00
|
|
|
children: [CircularProgressIndicator()],
|
2021-04-05 20:22:32 +00:00
|
|
|
),
|
|
|
|
)
|
|
|
|
],
|
|
|
|
),
|
2020-06-25 12:28:56 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-05 20:22:32 +00:00
|
|
|
class SpotifyImporterV2Main extends StatefulWidget {
|
2021-09-01 12:38:32 +00:00
|
|
|
final SpotifyAPIWrapper spotify;
|
|
|
|
SpotifyImporterV2Main(this.spotify, {Key? key}) : super(key: key);
|
2021-04-05 20:22:32 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
_SpotifyImporterV2MainState createState() => _SpotifyImporterV2MainState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _SpotifyImporterV2MainState extends State<SpotifyImporterV2Main> {
|
2021-09-01 12:38:32 +00:00
|
|
|
late String _url;
|
2021-04-05 20:22:32 +00:00
|
|
|
bool _urlLoading = false;
|
2021-09-01 12:38:32 +00:00
|
|
|
spotify.Playlist? _urlPlaylist;
|
2021-04-05 20:22:32 +00:00
|
|
|
bool _playlistsLoading = true;
|
2021-09-01 12:38:32 +00:00
|
|
|
List<spotify.PlaylistSimple>? _playlists;
|
2021-04-05 20:22:32 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
_loadPlaylists();
|
|
|
|
super.initState();
|
|
|
|
}
|
2020-06-25 12:28:56 +00:00
|
|
|
|
2021-04-05 20:22:32 +00:00
|
|
|
//Load playlists
|
|
|
|
Future _loadPlaylists() async {
|
2021-09-01 12:38:32 +00:00
|
|
|
var pages = widget.spotify.spotify.users.playlists(widget.spotify.me.id!);
|
2021-04-05 20:22:32 +00:00
|
|
|
_playlists = List.from(await pages.all());
|
|
|
|
setState(() => _playlistsLoading = false);
|
|
|
|
}
|
|
|
|
|
|
|
|
Future _loadUrl() async {
|
|
|
|
setState(() => _urlLoading = true);
|
|
|
|
//Resolve URL
|
|
|
|
try {
|
2021-09-01 12:38:32 +00:00
|
|
|
String? uri = await SpotifyScrapper.resolveUrl(_url);
|
2021-04-05 20:22:32 +00:00
|
|
|
//Error/NonPlaylist
|
|
|
|
if (uri == null || uri.split(':')[1] != 'playlist') {
|
|
|
|
throw Exception();
|
|
|
|
}
|
|
|
|
//Get playlist
|
2021-09-01 12:38:32 +00:00
|
|
|
spotify.Playlist playlist =
|
|
|
|
await widget.spotify.spotify.playlists.get(uri.split(":")[2]);
|
2021-04-05 20:22:32 +00:00
|
|
|
setState(() {
|
|
|
|
_urlLoading = false;
|
|
|
|
_urlPlaylist = playlist;
|
|
|
|
});
|
|
|
|
} catch (e) {
|
2021-09-01 12:38:32 +00:00
|
|
|
Fluttertoast.showToast(
|
|
|
|
msg: "Invalid/Unsupported URL".i18n,
|
|
|
|
gravity: ToastGravity.BOTTOM,
|
|
|
|
toastLength: Toast.LENGTH_SHORT);
|
2021-04-05 20:22:32 +00:00
|
|
|
setState(() => _urlLoading = false);
|
|
|
|
return;
|
2020-06-25 12:28:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-01 12:38:32 +00:00
|
|
|
Future _startImport(String? title, String? description, String? id) async {
|
2021-04-05 20:22:32 +00:00
|
|
|
//Show loading dialog
|
|
|
|
showDialog(
|
2021-09-01 12:38:32 +00:00
|
|
|
context: context,
|
|
|
|
barrierDismissible: false,
|
|
|
|
builder: (context) => WillPopScope(
|
|
|
|
onWillPop: () => Future.value(false),
|
|
|
|
child: AlertDialog(
|
|
|
|
title: Text("Please wait...".i18n),
|
|
|
|
content: Row(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
|
|
children: [CircularProgressIndicator()],
|
|
|
|
))));
|
2021-04-05 20:22:32 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
//Fetch entire playlist
|
|
|
|
var pages = widget.spotify.spotify.playlists.getTracksByPlaylistId(id);
|
|
|
|
var all = await pages.all();
|
|
|
|
//Map to importer track
|
2021-09-01 12:38:32 +00:00
|
|
|
List<ImporterTrack> tracks = all
|
|
|
|
.map((t) => ImporterTrack(
|
|
|
|
t.name, t.artists!.map((a) => a.name).toList(),
|
|
|
|
isrc: t.externalIds!.isrc))
|
|
|
|
.toList();
|
2021-04-05 20:22:32 +00:00
|
|
|
await importer.start(context, title, description, tracks);
|
|
|
|
//Route
|
|
|
|
Navigator.of(context).pop();
|
2021-09-01 12:38:32 +00:00
|
|
|
Navigator.of(context).pushReplacement(
|
|
|
|
MaterialPageRoute(builder: (context) => ImporterStatusScreen()));
|
2021-04-05 20:22:32 +00:00
|
|
|
} catch (e) {
|
2021-09-01 12:38:32 +00:00
|
|
|
Fluttertoast.showToast(
|
|
|
|
msg: e.toString(),
|
|
|
|
gravity: ToastGravity.BOTTOM,
|
|
|
|
toastLength: Toast.LENGTH_SHORT);
|
2021-04-05 20:22:32 +00:00
|
|
|
Navigator.of(context).pop();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-25 12:28:56 +00:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Scaffold(
|
2021-09-01 12:38:32 +00:00
|
|
|
appBar: FreezerAppBar("Spotify Importer v2".i18n),
|
|
|
|
body: ListView(
|
|
|
|
children: [
|
2021-04-05 20:22:32 +00:00
|
|
|
Padding(
|
|
|
|
padding: EdgeInsets.symmetric(vertical: 8.0, horizontal: 16.0),
|
|
|
|
child: Text(
|
2021-09-01 12:38:32 +00:00
|
|
|
'Logged in as: '.i18n + widget.spotify.me.displayName!,
|
|
|
|
maxLines: 1,
|
|
|
|
textAlign: TextAlign.center,
|
|
|
|
style:
|
|
|
|
TextStyle(fontSize: 18.0, fontWeight: FontWeight.bold)),
|
2021-04-05 20:22:32 +00:00
|
|
|
),
|
|
|
|
FreezerDivider(),
|
|
|
|
Container(height: 4.0),
|
|
|
|
Text(
|
|
|
|
"Options".i18n,
|
|
|
|
textAlign: TextAlign.center,
|
2021-09-01 12:38:32 +00:00
|
|
|
style: TextStyle(fontSize: 18.0, fontWeight: FontWeight.bold),
|
2021-04-05 20:22:32 +00:00
|
|
|
),
|
|
|
|
ImporterSettings(),
|
|
|
|
FreezerDivider(),
|
|
|
|
Container(height: 4.0),
|
|
|
|
Text(
|
|
|
|
"Import playlists by URL".i18n,
|
|
|
|
textAlign: TextAlign.center,
|
2021-09-01 12:38:32 +00:00
|
|
|
style: TextStyle(fontSize: 18.0, fontWeight: FontWeight.bold),
|
2021-04-05 20:22:32 +00:00
|
|
|
),
|
|
|
|
Padding(
|
2021-09-01 12:38:32 +00:00
|
|
|
padding: EdgeInsets.symmetric(horizontal: 16.0, vertical: 8.0),
|
|
|
|
child: Row(
|
|
|
|
children: [
|
|
|
|
Expanded(
|
|
|
|
child: TextField(
|
|
|
|
decoration: InputDecoration(hintText: "URL".i18n),
|
|
|
|
onChanged: (v) => setState(() => _url = v)),
|
2020-06-25 12:28:56 +00:00
|
|
|
),
|
2021-09-01 12:38:32 +00:00
|
|
|
IconButton(
|
|
|
|
icon: Icon(Icons.search),
|
|
|
|
onPressed: () => _loadUrl(),
|
|
|
|
)
|
|
|
|
],
|
|
|
|
)),
|
2021-04-05 20:22:32 +00:00
|
|
|
if (_urlLoading)
|
|
|
|
Row(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
|
|
children: [
|
|
|
|
Padding(
|
|
|
|
padding: EdgeInsets.symmetric(vertical: 4.0),
|
|
|
|
child: CircularProgressIndicator(),
|
|
|
|
)
|
|
|
|
],
|
2020-06-25 12:28:56 +00:00
|
|
|
),
|
2021-04-05 20:22:32 +00:00
|
|
|
if (_urlPlaylist != null)
|
|
|
|
ListTile(
|
2021-09-01 12:38:32 +00:00
|
|
|
title: Text(_urlPlaylist!.name!),
|
|
|
|
subtitle: Text(_urlPlaylist!.description ?? ''),
|
|
|
|
leading: Image.network(_urlPlaylist!.images!.first.url ??
|
|
|
|
"http://cdn-images.deezer.com/images/cover//256x256-000000-80-0-0.jpg")),
|
2021-04-05 20:22:32 +00:00
|
|
|
if (_urlPlaylist != null)
|
|
|
|
Padding(
|
2021-09-01 12:38:32 +00:00
|
|
|
padding: EdgeInsets.symmetric(horizontal: 16.0),
|
|
|
|
child: ElevatedButton(
|
|
|
|
child: Text("Import".i18n),
|
|
|
|
onPressed: () {
|
|
|
|
_startImport(_urlPlaylist!.name,
|
|
|
|
_urlPlaylist!.description, _urlPlaylist!.id);
|
|
|
|
})),
|
2021-04-05 20:22:32 +00:00
|
|
|
|
|
|
|
// Playlists
|
|
|
|
FreezerDivider(),
|
|
|
|
Container(height: 4.0),
|
2021-09-01 12:38:32 +00:00
|
|
|
Text("Playlists".i18n,
|
|
|
|
textAlign: TextAlign.center,
|
|
|
|
style: TextStyle(fontSize: 18.0, fontWeight: FontWeight.bold)),
|
2021-04-05 20:22:32 +00:00
|
|
|
Container(height: 4.0),
|
|
|
|
if (_playlistsLoading)
|
|
|
|
Row(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
|
|
children: [
|
|
|
|
Padding(
|
|
|
|
padding: EdgeInsets.symmetric(vertical: 4.0),
|
|
|
|
child: CircularProgressIndicator(),
|
|
|
|
)
|
|
|
|
],
|
|
|
|
),
|
|
|
|
if (!_playlistsLoading && _playlists != null)
|
2021-09-01 12:38:32 +00:00
|
|
|
...List.generate(_playlists!.length, (i) {
|
|
|
|
spotify.PlaylistSimple p = _playlists![i];
|
2020-06-25 12:28:56 +00:00
|
|
|
return ListTile(
|
2021-09-01 12:38:32 +00:00
|
|
|
title: Text(p.name!, maxLines: 1),
|
|
|
|
subtitle: Text(p.owner!.displayName!, maxLines: 1),
|
|
|
|
leading: Image.network(p.images!.first.url ??
|
|
|
|
"http://cdn-images.deezer.com/images/cover//256x256-000000-80-0-0.jpg"),
|
2021-04-05 20:22:32 +00:00
|
|
|
onTap: () {
|
|
|
|
_startImport(p.name, "", p.id);
|
|
|
|
},
|
2020-06-25 12:28:56 +00:00
|
|
|
);
|
|
|
|
})
|
2021-09-01 12:38:32 +00:00
|
|
|
],
|
|
|
|
));
|
2020-06-25 12:28:56 +00:00
|
|
|
}
|
|
|
|
}
|