2020-08-13 19:39:22 +02:00
|
|
|
import 'package:connectivity/connectivity.dart';
|
2020-06-23 21:23:12 +02:00
|
|
|
import 'package:flutter/material.dart';
|
2020-10-16 20:54:04 +02:00
|
|
|
import 'package:freezer/api/cache.dart';
|
2020-06-23 21:23:12 +02:00
|
|
|
import 'package:freezer/api/download.dart';
|
|
|
|
import 'package:freezer/api/player.dart';
|
|
|
|
import 'package:freezer/ui/details_screens.dart';
|
|
|
|
import 'package:freezer/ui/menu.dart';
|
2020-09-18 19:36:41 +02:00
|
|
|
import 'package:freezer/translations.i18n.dart';
|
2020-06-23 21:23:12 +02:00
|
|
|
|
|
|
|
import 'tiles.dart';
|
|
|
|
import '../api/deezer.dart';
|
|
|
|
import '../api/definitions.dart';
|
|
|
|
import 'error.dart';
|
|
|
|
|
2020-10-09 20:52:45 +02:00
|
|
|
|
|
|
|
openScreenByURL(BuildContext context, String url) async {
|
|
|
|
DeezerLinkResponse res = await deezerAPI.parseLink(url);
|
|
|
|
if (res == null) return;
|
|
|
|
|
|
|
|
switch (res.type) {
|
|
|
|
case DeezerLinkType.TRACK:
|
|
|
|
Track t = await deezerAPI.track(res.id);
|
|
|
|
MenuSheet(context).defaultTrackMenu(t);
|
|
|
|
break;
|
|
|
|
case DeezerLinkType.ALBUM:
|
|
|
|
Album a = await deezerAPI.album(res.id);
|
|
|
|
Navigator.of(context).push(MaterialPageRoute(builder: (context) => AlbumDetails(a)));
|
|
|
|
break;
|
|
|
|
case DeezerLinkType.ARTIST:
|
|
|
|
Artist a = await deezerAPI.artist(res.id);
|
|
|
|
Navigator.of(context).push(MaterialPageRoute(builder: (context) => ArtistDetails(a)));
|
|
|
|
break;
|
|
|
|
case DeezerLinkType.PLAYLIST:
|
|
|
|
Playlist p = await deezerAPI.playlist(res.id);
|
|
|
|
Navigator.of(context).push(MaterialPageRoute(builder: (context) => PlaylistDetails(p)));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-06-23 21:23:12 +02:00
|
|
|
class SearchScreen extends StatefulWidget {
|
|
|
|
@override
|
|
|
|
_SearchScreenState createState() => _SearchScreenState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _SearchScreenState extends State<SearchScreen> {
|
|
|
|
|
|
|
|
String _query;
|
2020-08-13 19:39:22 +02:00
|
|
|
bool _offline = false;
|
2020-10-09 20:52:45 +02:00
|
|
|
bool _loading = false;
|
2020-08-16 22:17:22 +02:00
|
|
|
TextEditingController _controller = new TextEditingController();
|
2020-09-18 19:36:41 +02:00
|
|
|
List _suggestions = [];
|
2020-06-23 21:23:12 +02:00
|
|
|
|
2020-10-09 20:52:45 +02:00
|
|
|
void _submit(BuildContext context, {String query}) async {
|
2020-06-23 21:23:12 +02:00
|
|
|
if (query != null) _query = query;
|
2020-10-09 20:52:45 +02:00
|
|
|
|
|
|
|
//URL
|
|
|
|
if (_query.startsWith('http')) {
|
|
|
|
setState(() => _loading = true);
|
|
|
|
try {
|
|
|
|
await openScreenByURL(context, _query);
|
|
|
|
} catch (e) {}
|
|
|
|
setState(() => _loading = false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-10-16 20:54:04 +02:00
|
|
|
//Add to search history
|
|
|
|
try {cache.searchHistory.remove(_query);} catch (_) {}
|
|
|
|
cache.searchHistory.add(_query);
|
|
|
|
|
2020-06-23 21:23:12 +02:00
|
|
|
Navigator.of(context).push(
|
|
|
|
MaterialPageRoute(builder: (context) => SearchResultsScreen(_query, offline: _offline,))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-08-13 19:39:22 +02:00
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
//Check for connectivity and enable offline mode
|
|
|
|
Connectivity().checkConnectivity().then((res) {
|
|
|
|
if (res == ConnectivityResult.none) setState(() {
|
|
|
|
_offline = true;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
super.initState();
|
|
|
|
}
|
|
|
|
|
2020-09-18 19:36:41 +02:00
|
|
|
//Load search suggestions
|
|
|
|
Future<List<String>> _loadSuggestions() async {
|
2020-10-09 20:52:45 +02:00
|
|
|
if (_query == null || _query.length < 2 || _query.startsWith('http')) return null;
|
2020-09-18 19:36:41 +02:00
|
|
|
String q = _query;
|
|
|
|
await Future.delayed(Duration(milliseconds: 300));
|
|
|
|
if (q != _query) return null;
|
|
|
|
//Load
|
2020-10-10 22:51:20 +02:00
|
|
|
List sugg;
|
|
|
|
try {
|
|
|
|
sugg = await deezerAPI.searchSuggestions(_query);
|
|
|
|
} catch (e) {}
|
|
|
|
|
|
|
|
if (sugg != null)
|
|
|
|
setState(() => _suggestions = sugg);
|
2020-09-18 19:36:41 +02:00
|
|
|
}
|
|
|
|
|
2020-06-23 21:23:12 +02:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Scaffold(
|
2020-09-18 19:36:41 +02:00
|
|
|
appBar: AppBar(title: Text('Search'.i18n),),
|
2020-06-23 21:23:12 +02:00
|
|
|
body: ListView(
|
|
|
|
children: <Widget>[
|
|
|
|
Container(height: 16.0),
|
|
|
|
Padding(
|
|
|
|
padding: EdgeInsets.symmetric(horizontal: 16.0),
|
|
|
|
child: Row(
|
|
|
|
children: <Widget>[
|
|
|
|
Expanded(
|
2020-08-16 22:17:22 +02:00
|
|
|
child: Stack(
|
|
|
|
alignment: Alignment(1.0, 1.0),
|
|
|
|
children: [
|
|
|
|
TextField(
|
2020-09-18 19:36:41 +02:00
|
|
|
onChanged: (String s) {
|
|
|
|
setState(() => _query = s);
|
|
|
|
_loadSuggestions();
|
|
|
|
},
|
2020-08-16 22:17:22 +02:00
|
|
|
decoration: InputDecoration(
|
2020-10-09 20:52:45 +02:00
|
|
|
labelText: 'Search or paste URL'.i18n
|
2020-08-16 22:17:22 +02:00
|
|
|
),
|
|
|
|
controller: _controller,
|
|
|
|
onSubmitted: (String s) => _submit(context, query: s),
|
|
|
|
),
|
|
|
|
IconButton(
|
|
|
|
icon: Icon(Icons.clear),
|
|
|
|
onPressed: () {
|
2020-09-22 19:13:54 +02:00
|
|
|
setState(() {
|
|
|
|
_suggestions = [];
|
|
|
|
_query = '';
|
|
|
|
});
|
2020-08-16 22:17:22 +02:00
|
|
|
_controller.clear();
|
|
|
|
},
|
|
|
|
),
|
|
|
|
],
|
|
|
|
)
|
2020-06-23 21:23:12 +02:00
|
|
|
),
|
2020-08-16 22:17:22 +02:00
|
|
|
Padding(
|
|
|
|
padding: EdgeInsets.fromLTRB(0, 8, 0, 0),
|
|
|
|
child: IconButton(
|
|
|
|
icon: Icon(Icons.search),
|
|
|
|
onPressed: () => _submit(context),
|
|
|
|
),
|
2020-06-23 21:23:12 +02:00
|
|
|
)
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
ListTile(
|
2020-09-18 19:36:41 +02:00
|
|
|
title: Text('Offline search'.i18n),
|
2020-06-23 21:23:12 +02:00
|
|
|
leading: Switch(
|
|
|
|
value: _offline,
|
|
|
|
onChanged: (v) {
|
2020-08-13 19:39:22 +02:00
|
|
|
setState(() => _offline = !_offline);
|
2020-06-23 21:23:12 +02:00
|
|
|
},
|
|
|
|
),
|
2020-09-18 19:36:41 +02:00
|
|
|
),
|
2020-10-09 20:52:45 +02:00
|
|
|
if (_loading)
|
|
|
|
LinearProgressIndicator(),
|
2020-09-18 19:36:41 +02:00
|
|
|
Divider(),
|
2020-10-16 20:54:04 +02:00
|
|
|
|
|
|
|
//History
|
|
|
|
if (cache.searchHistory.length > 0 && (_query??'').length == 0)
|
|
|
|
...List.generate(cache.searchHistory.length > 10 ? 10 : cache.searchHistory.length, (int i) => ListTile(
|
|
|
|
title: Text(cache.searchHistory[i]),
|
|
|
|
leading: Icon(Icons.history),
|
|
|
|
onTap: () {
|
|
|
|
setState(() => _query = cache.searchHistory[i]);
|
|
|
|
_submit(context);
|
|
|
|
},
|
|
|
|
)),
|
|
|
|
|
|
|
|
//Suggestions
|
2020-09-18 19:36:41 +02:00
|
|
|
...List.generate((_suggestions??[]).length, (i) => ListTile(
|
|
|
|
title: Text(_suggestions[i]),
|
|
|
|
leading: Icon(Icons.search),
|
|
|
|
onTap: () {
|
|
|
|
setState(() => _query = _suggestions[i]);
|
|
|
|
_submit(context);
|
|
|
|
},
|
|
|
|
))
|
2020-06-23 21:23:12 +02:00
|
|
|
],
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class SearchResultsScreen extends StatelessWidget {
|
|
|
|
|
|
|
|
final String query;
|
|
|
|
final bool offline;
|
|
|
|
|
|
|
|
SearchResultsScreen(this.query, {this.offline});
|
|
|
|
|
|
|
|
Future _search() async {
|
|
|
|
if (offline??false) {
|
|
|
|
return await downloadManager.search(query);
|
|
|
|
}
|
|
|
|
return await deezerAPI.search(query);
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Scaffold(
|
|
|
|
appBar: AppBar(
|
2020-09-18 19:36:41 +02:00
|
|
|
title: Text('Search Results'.i18n),
|
2020-06-23 21:23:12 +02:00
|
|
|
),
|
|
|
|
body: FutureBuilder(
|
|
|
|
future: _search(),
|
|
|
|
builder: (BuildContext context, AsyncSnapshot snapshot) {
|
|
|
|
|
|
|
|
if (!snapshot.hasData) return Center(child: CircularProgressIndicator(),);
|
|
|
|
if (snapshot.hasError) return ErrorScreen();
|
|
|
|
|
|
|
|
SearchResults results = snapshot.data;
|
|
|
|
|
|
|
|
if (results.empty)
|
|
|
|
return Center(
|
|
|
|
child: Column(
|
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
children: <Widget>[
|
|
|
|
Icon(
|
|
|
|
Icons.warning,
|
|
|
|
size: 64,
|
|
|
|
),
|
2020-09-18 19:36:41 +02:00
|
|
|
Text('No results!'.i18n)
|
2020-06-23 21:23:12 +02:00
|
|
|
],
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
//Tracks
|
|
|
|
List<Widget> tracks = [];
|
|
|
|
if (results.tracks != null && results.tracks.length != 0) {
|
|
|
|
tracks = [
|
|
|
|
Text(
|
2020-09-18 19:36:41 +02:00
|
|
|
'Tracks'.i18n,
|
2020-06-23 21:23:12 +02:00
|
|
|
textAlign: TextAlign.center,
|
|
|
|
style: TextStyle(
|
2020-07-18 23:45:48 +02:00
|
|
|
fontSize: 26.0,
|
|
|
|
fontWeight: FontWeight.bold
|
2020-06-23 21:23:12 +02:00
|
|
|
),
|
|
|
|
),
|
|
|
|
...List.generate(3, (i) {
|
|
|
|
if (results.tracks.length <= i) return Container(width: 0, height: 0,);
|
|
|
|
Track t = results.tracks[i];
|
|
|
|
return TrackTile(
|
|
|
|
t,
|
|
|
|
onTap: () {
|
|
|
|
playerHelper.playFromTrackList(results.tracks, t.id, QueueSource(
|
2020-09-18 19:36:41 +02:00
|
|
|
text: 'Search'.i18n,
|
2020-06-23 21:23:12 +02:00
|
|
|
id: query,
|
|
|
|
source: 'search'
|
|
|
|
));
|
|
|
|
},
|
|
|
|
onHold: () {
|
|
|
|
MenuSheet m = MenuSheet(context);
|
|
|
|
m.defaultTrackMenu(t);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}),
|
|
|
|
ListTile(
|
2020-09-18 19:36:41 +02:00
|
|
|
title: Text('Show all tracks'.i18n),
|
2020-06-23 21:23:12 +02:00
|
|
|
onTap: () {
|
|
|
|
Navigator.of(context).push(
|
|
|
|
MaterialPageRoute(builder: (context) => TrackListScreen(results.tracks, QueueSource(
|
|
|
|
id: query,
|
|
|
|
source: 'search',
|
2020-09-18 19:36:41 +02:00
|
|
|
text: 'Search'.i18n
|
2020-06-23 21:23:12 +02:00
|
|
|
)))
|
|
|
|
);
|
|
|
|
},
|
|
|
|
)
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
//Albums
|
|
|
|
List<Widget> albums = [];
|
|
|
|
if (results.albums != null && results.albums.length != 0) {
|
|
|
|
albums = [
|
|
|
|
Text(
|
2020-09-18 19:36:41 +02:00
|
|
|
'Albums'.i18n,
|
2020-06-23 21:23:12 +02:00
|
|
|
textAlign: TextAlign.center,
|
|
|
|
style: TextStyle(
|
2020-07-18 23:45:48 +02:00
|
|
|
fontSize: 26.0,
|
|
|
|
fontWeight: FontWeight.bold
|
2020-06-23 21:23:12 +02:00
|
|
|
),
|
|
|
|
),
|
|
|
|
...List.generate(3, (i) {
|
|
|
|
if (results.albums.length <= i) return Container(height: 0, width: 0,);
|
|
|
|
Album a = results.albums[i];
|
|
|
|
return AlbumTile(
|
|
|
|
a,
|
|
|
|
onHold: () {
|
|
|
|
MenuSheet m = MenuSheet(context);
|
|
|
|
m.defaultAlbumMenu(a);
|
|
|
|
},
|
|
|
|
onTap: () {
|
|
|
|
Navigator.of(context).push(
|
|
|
|
MaterialPageRoute(builder: (context) => AlbumDetails(a))
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}),
|
|
|
|
ListTile(
|
2020-09-18 19:36:41 +02:00
|
|
|
title: Text('Show all albums'.i18n),
|
2020-06-23 21:23:12 +02:00
|
|
|
onTap: () {
|
|
|
|
Navigator.of(context).push(
|
|
|
|
MaterialPageRoute(builder: (context) => AlbumListScreen(results.albums))
|
|
|
|
);
|
|
|
|
},
|
|
|
|
)
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
//Artists
|
|
|
|
List<Widget> artists = [];
|
|
|
|
if (results.artists != null && results.artists.length != 0) {
|
|
|
|
artists = [
|
|
|
|
Text(
|
2020-09-18 19:36:41 +02:00
|
|
|
'Artists'.i18n,
|
2020-06-23 21:23:12 +02:00
|
|
|
textAlign: TextAlign.center,
|
|
|
|
style: TextStyle(
|
2020-07-18 23:45:48 +02:00
|
|
|
fontSize: 26.0,
|
|
|
|
fontWeight: FontWeight.bold
|
2020-06-23 21:23:12 +02:00
|
|
|
),
|
|
|
|
),
|
|
|
|
Container(height: 4),
|
|
|
|
SingleChildScrollView(
|
|
|
|
scrollDirection: Axis.horizontal,
|
|
|
|
child: Row(
|
|
|
|
children: List.generate(results.artists.length, (int i) {
|
|
|
|
Artist a = results.artists[i];
|
|
|
|
return ArtistTile(
|
|
|
|
a,
|
|
|
|
onTap: () {
|
|
|
|
Navigator.of(context).push(
|
|
|
|
MaterialPageRoute(builder: (context) => ArtistDetails(a))
|
|
|
|
);
|
|
|
|
},
|
|
|
|
onHold: () {
|
|
|
|
MenuSheet m = MenuSheet(context);
|
|
|
|
m.defaultArtistMenu(a);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
)
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
//Playlists
|
|
|
|
List<Widget> playlists = [];
|
|
|
|
if (results.playlists != null && results.playlists.length != 0) {
|
|
|
|
playlists = [
|
|
|
|
Text(
|
2020-09-18 19:36:41 +02:00
|
|
|
'Playlists'.i18n,
|
2020-06-23 21:23:12 +02:00
|
|
|
textAlign: TextAlign.center,
|
|
|
|
style: TextStyle(
|
2020-07-18 23:45:48 +02:00
|
|
|
fontSize: 26.0,
|
|
|
|
fontWeight: FontWeight.bold
|
2020-06-23 21:23:12 +02:00
|
|
|
),
|
|
|
|
),
|
|
|
|
...List.generate(3, (i) {
|
|
|
|
if (results.playlists.length <= i) return Container(height: 0, width: 0,);
|
|
|
|
Playlist p = results.playlists[i];
|
|
|
|
return PlaylistTile(
|
|
|
|
p,
|
|
|
|
onTap: () {
|
|
|
|
Navigator.of(context).push(
|
|
|
|
MaterialPageRoute(builder: (context) => PlaylistDetails(p))
|
|
|
|
);
|
|
|
|
},
|
|
|
|
onHold: () {
|
|
|
|
MenuSheet m = MenuSheet(context);
|
|
|
|
m.defaultPlaylistMenu(p);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}),
|
|
|
|
ListTile(
|
2020-09-18 19:36:41 +02:00
|
|
|
title: Text('Show all playlists'.i18n),
|
2020-06-23 21:23:12 +02:00
|
|
|
onTap: () {
|
|
|
|
Navigator.of(context).push(
|
|
|
|
MaterialPageRoute(builder: (context) => SearchResultPlaylists(results.playlists))
|
|
|
|
);
|
|
|
|
},
|
|
|
|
)
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
return ListView(
|
|
|
|
children: <Widget>[
|
|
|
|
Container(height: 8.0,),
|
|
|
|
...tracks,
|
|
|
|
Container(height: 8.0,),
|
|
|
|
...albums,
|
|
|
|
Container(height: 8.0,),
|
|
|
|
...artists,
|
|
|
|
Container(height: 8.0,),
|
|
|
|
...playlists
|
|
|
|
],
|
|
|
|
);
|
|
|
|
},
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//List all tracks
|
|
|
|
class TrackListScreen extends StatelessWidget {
|
|
|
|
|
|
|
|
final QueueSource queueSource;
|
|
|
|
final List<Track> tracks;
|
|
|
|
|
|
|
|
TrackListScreen(this.tracks, this.queueSource);
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Scaffold(
|
2020-09-18 19:36:41 +02:00
|
|
|
appBar: AppBar(title: Text('Tracks'.i18n),),
|
2020-06-23 21:23:12 +02:00
|
|
|
body: ListView.builder(
|
|
|
|
itemCount: tracks.length,
|
|
|
|
itemBuilder: (BuildContext context, int i) {
|
|
|
|
Track t = tracks[i];
|
|
|
|
return TrackTile(
|
|
|
|
t,
|
|
|
|
onTap: () {
|
|
|
|
playerHelper.playFromTrackList(tracks, t.id, queueSource);
|
|
|
|
},
|
|
|
|
onHold: () {
|
|
|
|
MenuSheet m = MenuSheet(context);
|
|
|
|
m.defaultTrackMenu(t);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//List all albums
|
|
|
|
class AlbumListScreen extends StatelessWidget {
|
|
|
|
|
|
|
|
final List<Album> albums;
|
|
|
|
AlbumListScreen(this.albums);
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Scaffold(
|
2020-09-18 19:36:41 +02:00
|
|
|
appBar: AppBar(title: Text('Albums'.i18n),),
|
2020-06-23 21:23:12 +02:00
|
|
|
body: ListView.builder(
|
|
|
|
itemCount: albums.length,
|
|
|
|
itemBuilder: (context, i) {
|
|
|
|
Album a = albums[i];
|
|
|
|
return AlbumTile(
|
|
|
|
a,
|
|
|
|
onTap: () {
|
|
|
|
Navigator.of(context).push(
|
|
|
|
MaterialPageRoute(builder: (context) => AlbumDetails(a))
|
|
|
|
);
|
|
|
|
},
|
|
|
|
onHold: () {
|
|
|
|
MenuSheet m = MenuSheet(context);
|
|
|
|
m.defaultAlbumMenu(a);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class SearchResultPlaylists extends StatelessWidget {
|
|
|
|
|
|
|
|
final List<Playlist> playlists;
|
|
|
|
SearchResultPlaylists(this.playlists);
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Scaffold(
|
2020-09-18 19:36:41 +02:00
|
|
|
appBar: AppBar(title: Text('Playlists'.i18n),),
|
2020-06-23 21:23:12 +02:00
|
|
|
body: ListView.builder(
|
|
|
|
itemCount: playlists.length,
|
|
|
|
itemBuilder: (context, i) {
|
|
|
|
Playlist p = playlists[i];
|
|
|
|
return PlaylistTile(
|
|
|
|
p,
|
|
|
|
onTap: () {
|
|
|
|
Navigator.of(context).push(
|
|
|
|
MaterialPageRoute(builder: (context) => PlaylistDetails(p))
|
|
|
|
);
|
|
|
|
},
|
|
|
|
onHold: () {
|
|
|
|
MenuSheet m = MenuSheet(context);
|
|
|
|
m.defaultPlaylistMenu(p);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|