2023-07-29 02:17:26 +00:00
|
|
|
import 'dart:async';
|
|
|
|
import 'dart:math';
|
|
|
|
|
|
|
|
import 'package:connectivity_plus/connectivity_plus.dart';
|
2024-01-24 17:55:25 +00:00
|
|
|
import 'package:dio/dio.dart';
|
2023-07-29 02:17:26 +00:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter/services.dart';
|
|
|
|
import 'package:flutter_screenutil/flutter_screenutil.dart';
|
|
|
|
import 'package:freezer/api/cache.dart';
|
|
|
|
import 'package:freezer/api/download.dart';
|
2023-10-18 15:08:05 +00:00
|
|
|
import 'package:freezer/api/player/audio_handler.dart';
|
2024-02-14 21:39:35 +00:00
|
|
|
import 'package:freezer/icons.dart';
|
2023-07-29 02:17:26 +00:00
|
|
|
import 'package:freezer/notifiers/list_notifier.dart';
|
|
|
|
import 'package:freezer/ui/details_screens.dart';
|
|
|
|
import 'package:freezer/ui/elements.dart';
|
|
|
|
import 'package:freezer/ui/menu.dart';
|
|
|
|
import 'package:freezer/translations.i18n.dart';
|
|
|
|
|
|
|
|
import 'tiles.dart';
|
|
|
|
import '../api/deezer.dart';
|
|
|
|
import '../api/definitions.dart';
|
|
|
|
import 'error.dart';
|
|
|
|
|
2023-09-26 00:06:59 +00:00
|
|
|
FutureOr openScreenByURL(BuildContext context, String url) async {
|
2023-07-29 02:17:26 +00:00
|
|
|
DeezerLinkResponse? res = await deezerAPI.parseLink(url);
|
|
|
|
if (res == null) return;
|
|
|
|
|
|
|
|
switch (res.type) {
|
|
|
|
case DeezerLinkType.TRACK:
|
2024-02-12 02:37:26 +00:00
|
|
|
Track t = await deezerAPI.track(res.id!);
|
2024-02-13 22:56:53 +00:00
|
|
|
MenuSheet(context).defaultTrackMenu(t, optionsTop: [
|
|
|
|
MenuSheetOption(Text('Play'.i18n),
|
|
|
|
icon: const Icon(Icons.play_arrow),
|
|
|
|
onTap: () => playerHelper.playSearchMixDeferred(t)),
|
|
|
|
]);
|
2023-07-29 02:17:26 +00:00
|
|
|
break;
|
|
|
|
case DeezerLinkType.ALBUM:
|
|
|
|
Album a = await deezerAPI.album(res.id);
|
2023-09-26 00:06:59 +00:00
|
|
|
return Navigator.of(context)
|
2023-07-29 02:17:26 +00:00
|
|
|
.push(MaterialPageRoute(builder: (context) => AlbumDetails(a)));
|
|
|
|
case DeezerLinkType.ARTIST:
|
|
|
|
Artist a = await deezerAPI.artist(res.id);
|
2023-09-26 00:06:59 +00:00
|
|
|
return Navigator.of(context)
|
2023-07-29 02:17:26 +00:00
|
|
|
.push(MaterialPageRoute(builder: (context) => ArtistDetails(a)));
|
|
|
|
case DeezerLinkType.PLAYLIST:
|
|
|
|
Playlist p = await deezerAPI.playlist(res.id);
|
2024-01-29 00:24:04 +00:00
|
|
|
if (p.tracks == null || p.tracks!.isEmpty) {
|
|
|
|
ScaffoldMessenger.of(context)
|
|
|
|
.snack('The playlist is empty or private.'.i18n);
|
|
|
|
return;
|
|
|
|
}
|
2023-09-26 00:06:59 +00:00
|
|
|
return Navigator.of(context)
|
2023-07-29 02:17:26 +00:00
|
|
|
.push(MaterialPageRoute(builder: (context) => PlaylistDetails(p)));
|
|
|
|
default:
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class SearchScreen extends StatefulWidget {
|
|
|
|
final String? defaultText;
|
|
|
|
const SearchScreen({this.defaultText, Key? key}) : super(key: key);
|
|
|
|
@override
|
2023-10-12 22:09:37 +00:00
|
|
|
State<SearchScreen> createState() => _SearchScreenState();
|
2023-07-29 02:17:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class _SearchScreenState extends State<SearchScreen> {
|
|
|
|
bool _offline = false;
|
|
|
|
late final _controller = TextEditingController(text: widget.defaultText);
|
|
|
|
final _suggestions = ListNotifier<String>([]);
|
|
|
|
final _showingSuggestions = ValueNotifier(false);
|
|
|
|
final _loading = ValueNotifier(false);
|
2024-01-24 17:55:25 +00:00
|
|
|
CancelToken? _searchCancelToken;
|
2023-07-29 02:17:26 +00:00
|
|
|
Timer? _searchTimer;
|
|
|
|
final _focus = FocusNode();
|
2023-09-26 00:06:59 +00:00
|
|
|
final _textFieldFocusNode = FocusNode();
|
2023-07-29 02:17:26 +00:00
|
|
|
|
|
|
|
Future<void> _submit() async {
|
2023-09-26 00:06:59 +00:00
|
|
|
// dismiss keyboard
|
|
|
|
_textFieldFocusNode.unfocus();
|
|
|
|
|
2023-10-12 22:09:37 +00:00
|
|
|
if (_controller.text.isEmpty) return;
|
|
|
|
|
2023-07-29 02:17:26 +00:00
|
|
|
//URL
|
|
|
|
if (_controller.text.startsWith('http')) {
|
|
|
|
_loading.value = true;
|
|
|
|
try {
|
2023-09-26 00:06:59 +00:00
|
|
|
final f = openScreenByURL(context, _controller.text);
|
2023-10-12 22:09:37 +00:00
|
|
|
if (f is Future) {
|
2023-09-26 00:06:59 +00:00
|
|
|
f.whenComplete(() => _textFieldFocusNode.requestFocus());
|
2023-10-12 22:09:37 +00:00
|
|
|
}
|
2023-07-29 02:17:26 +00:00
|
|
|
} catch (e) {}
|
|
|
|
_loading.value = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-09-26 00:06:59 +00:00
|
|
|
Navigator.of(context)
|
|
|
|
.pushRoute(
|
|
|
|
builder: (context) => SearchResultsScreen(
|
|
|
|
_controller.text,
|
|
|
|
offline: _offline,
|
|
|
|
))
|
|
|
|
.whenComplete(() => _textFieldFocusNode.requestFocus());
|
2023-07-29 02:17:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
//Check for connectivity and enable offline mode
|
|
|
|
Connectivity().checkConnectivity().then((res) {
|
2023-10-12 22:09:37 +00:00
|
|
|
if (res == ConnectivityResult.none) {
|
2023-07-29 02:17:26 +00:00
|
|
|
setState(() {
|
|
|
|
_offline = true;
|
|
|
|
});
|
2023-10-12 22:09:37 +00:00
|
|
|
}
|
2023-07-29 02:17:26 +00:00
|
|
|
});
|
|
|
|
_suggestions.addListener(
|
|
|
|
() => _showingSuggestions.value = _suggestions.value.isNotEmpty);
|
|
|
|
|
|
|
|
super.initState();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void dispose() {
|
|
|
|
_focus.dispose();
|
2023-09-26 00:06:59 +00:00
|
|
|
_textFieldFocusNode.dispose();
|
2023-07-29 02:17:26 +00:00
|
|
|
|
|
|
|
super.dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
//Load search suggestions
|
|
|
|
Future<void> _loadSuggestions() async {
|
|
|
|
if (_controller.text.isEmpty ||
|
|
|
|
_controller.text.length < 2 ||
|
|
|
|
_controller.text.startsWith('http')) return;
|
|
|
|
_loading.value = true;
|
2024-01-24 17:55:25 +00:00
|
|
|
_searchCancelToken?.cancel();
|
|
|
|
|
2023-07-29 02:17:26 +00:00
|
|
|
//Load
|
2024-01-24 17:55:25 +00:00
|
|
|
final List<String>? suggestions;
|
2023-07-29 02:17:26 +00:00
|
|
|
try {
|
2024-01-24 17:55:25 +00:00
|
|
|
_searchCancelToken = CancelToken();
|
|
|
|
suggestions = await deezerAPI.searchSuggestions(_controller.text,
|
|
|
|
cancelToken: _searchCancelToken);
|
|
|
|
} on DioException catch (e) {
|
|
|
|
if (e.type != DioExceptionType.cancel) rethrow;
|
|
|
|
return;
|
2023-07-29 02:17:26 +00:00
|
|
|
} catch (e) {
|
|
|
|
print(e);
|
2024-01-24 17:55:25 +00:00
|
|
|
return;
|
2023-07-29 02:17:26 +00:00
|
|
|
}
|
|
|
|
|
2024-01-24 17:55:25 +00:00
|
|
|
_loading.value = false;
|
|
|
|
if (suggestions != null) _suggestions.value = suggestions;
|
2023-07-29 02:17:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Widget _removeHistoryItemWidget(int index) {
|
|
|
|
return IconButton(
|
|
|
|
icon: Icon(
|
|
|
|
Icons.close,
|
|
|
|
semanticLabel: "Remove".i18n,
|
|
|
|
),
|
|
|
|
onPressed: () async {
|
|
|
|
cache.searchHistory.removeAt(index);
|
|
|
|
setState(() {});
|
|
|
|
await cache.save();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return SafeArea(
|
|
|
|
child: Scaffold(
|
|
|
|
appBar: PreferredSize(
|
2023-10-12 22:09:37 +00:00
|
|
|
preferredSize: const Size.fromHeight(76.0),
|
2023-07-29 02:17:26 +00:00
|
|
|
child: Column(
|
|
|
|
children: [
|
|
|
|
Expanded(
|
|
|
|
child: ListTile(
|
|
|
|
focusColor: Theme.of(context).listTileTheme.tileColor ??
|
|
|
|
Theme.of(context).colorScheme.background,
|
|
|
|
mouseCursor: MaterialStateMouseCursor.textable,
|
2023-10-12 22:09:37 +00:00
|
|
|
leading: Navigator.canPop(context)
|
|
|
|
? IconButton(
|
|
|
|
icon: const Icon(Icons.arrow_back),
|
|
|
|
onPressed: () => Navigator.pop(context))
|
|
|
|
: const Icon(Icons.search),
|
2023-07-29 02:17:26 +00:00
|
|
|
trailing: ValueListenableBuilder<TextEditingValue>(
|
|
|
|
valueListenable: _controller,
|
|
|
|
builder: (context, value, child) =>
|
|
|
|
value.text.isEmpty ? const SizedBox() : child!,
|
|
|
|
child: IconButton(
|
|
|
|
icon: Icon(
|
|
|
|
Icons.clear,
|
|
|
|
semanticLabel: "Clear".i18n,
|
|
|
|
),
|
|
|
|
onPressed: () {
|
|
|
|
_suggestions.clear();
|
|
|
|
_controller.clear();
|
|
|
|
})),
|
|
|
|
title: RawKeyboardListener(
|
|
|
|
focusNode: _focus,
|
|
|
|
onKey: (event) {
|
|
|
|
// For Android TV: quit search textfield
|
|
|
|
if (event is RawKeyUpEvent) {
|
|
|
|
LogicalKeyboardKey key = event.data.logicalKey;
|
|
|
|
if (key == LogicalKeyboardKey.arrowDown) {
|
2023-09-26 00:06:59 +00:00
|
|
|
_textFieldFocusNode.unfocus();
|
2023-07-29 02:17:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
child: TextField(
|
|
|
|
onChanged: (query) {
|
|
|
|
if (query.isEmpty) {
|
|
|
|
_suggestions.clear();
|
|
|
|
} else {
|
2024-01-24 17:55:25 +00:00
|
|
|
_searchTimer ??=
|
|
|
|
Timer(const Duration(milliseconds: 1), () {
|
2023-07-29 02:17:26 +00:00
|
|
|
_searchTimer = null;
|
|
|
|
_loadSuggestions();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
2023-09-26 00:06:59 +00:00
|
|
|
focusNode: _textFieldFocusNode,
|
2023-07-29 02:17:26 +00:00
|
|
|
autofocus: true,
|
|
|
|
decoration: InputDecoration(
|
|
|
|
hintText: 'Search or paste URL'.i18n,
|
|
|
|
border: InputBorder.none,
|
|
|
|
),
|
|
|
|
controller: _controller,
|
|
|
|
onSubmitted: (String s) => _submit(),
|
|
|
|
)),
|
2023-09-26 00:06:59 +00:00
|
|
|
onTap: () => _textFieldFocusNode.requestFocus()),
|
2023-07-29 02:17:26 +00:00
|
|
|
),
|
|
|
|
SizedBox(
|
|
|
|
height: 3.0,
|
|
|
|
child: ValueListenableBuilder<bool>(
|
|
|
|
valueListenable: _loading,
|
|
|
|
builder: (context, loading, _) => loading
|
2023-10-12 22:09:37 +00:00
|
|
|
? const LinearProgressIndicator()
|
2023-07-29 02:17:26 +00:00
|
|
|
: const SizedBox()),
|
|
|
|
)
|
|
|
|
],
|
2023-10-12 22:09:37 +00:00
|
|
|
)),
|
2023-07-29 02:17:26 +00:00
|
|
|
body: FocusScope(
|
|
|
|
child: ListView(
|
|
|
|
children: <Widget>[
|
|
|
|
ListTile(
|
|
|
|
title: Text('Offline search'.i18n),
|
2023-10-12 22:09:37 +00:00
|
|
|
leading: const Icon(Icons.offline_pin),
|
2023-07-29 02:17:26 +00:00
|
|
|
trailing: Switch(
|
|
|
|
value: _offline,
|
|
|
|
onChanged: (v) {
|
|
|
|
setState(() => _offline = !_offline);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
),
|
2023-10-12 22:09:37 +00:00
|
|
|
const FreezerDivider(),
|
2023-07-29 02:17:26 +00:00
|
|
|
ValueListenableBuilder<bool>(
|
|
|
|
valueListenable: _showingSuggestions,
|
|
|
|
builder: (context, showingSuggestions, child) =>
|
|
|
|
showingSuggestions ? const SizedBox() : child!,
|
|
|
|
child: Column(
|
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
|
|
children: [
|
|
|
|
Padding(
|
2023-10-12 22:09:37 +00:00
|
|
|
padding: const EdgeInsets.symmetric(
|
2023-07-29 02:17:26 +00:00
|
|
|
vertical: 8.0, horizontal: 16.0),
|
|
|
|
child: Text(
|
|
|
|
'Quick access'.i18n,
|
2023-10-12 22:09:37 +00:00
|
|
|
style: const TextStyle(
|
2023-07-29 02:17:26 +00:00
|
|
|
fontSize: 20.0, fontWeight: FontWeight.bold),
|
|
|
|
),
|
|
|
|
),
|
2023-09-26 00:06:59 +00:00
|
|
|
SearchBrowseCard(
|
2023-10-12 22:09:37 +00:00
|
|
|
color: const Color(0xff11b192),
|
2023-09-26 00:06:59 +00:00
|
|
|
text: 'Flow'.i18n,
|
2024-02-14 21:39:35 +00:00
|
|
|
icon: const Icon(FreezerIcons.waves),
|
2023-09-26 00:06:59 +00:00
|
|
|
onTap: () async {
|
|
|
|
await playerHelper.playFromSmartTrackList(
|
|
|
|
SmartTrackList(id: 'flow'));
|
|
|
|
},
|
2023-07-29 02:17:26 +00:00
|
|
|
),
|
|
|
|
if (cache.searchHistory.isNotEmpty) ...[
|
2023-10-12 22:09:37 +00:00
|
|
|
const FreezerDivider(),
|
2023-07-29 02:17:26 +00:00
|
|
|
Padding(
|
2023-10-12 22:09:37 +00:00
|
|
|
padding: const EdgeInsets.symmetric(
|
2023-07-29 02:17:26 +00:00
|
|
|
vertical: 8.0, horizontal: 16.0),
|
|
|
|
child: Text(
|
|
|
|
'History'.i18n,
|
2023-10-12 22:09:37 +00:00
|
|
|
style: const TextStyle(
|
2023-07-29 02:17:26 +00:00
|
|
|
fontSize: 20.0, fontWeight: FontWeight.bold),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
...List.generate(min(cache.searchHistory.length, 10),
|
|
|
|
(int i) {
|
2024-01-24 17:55:25 +00:00
|
|
|
switch (cache.searchHistory[i]) {
|
|
|
|
case final Track data:
|
2023-10-16 22:22:50 +00:00
|
|
|
return TrackTile.fromTrack(
|
2023-07-29 02:17:26 +00:00
|
|
|
data,
|
|
|
|
onTap: () {
|
2024-01-24 17:55:25 +00:00
|
|
|
final queue = cache.searchHistory
|
|
|
|
.whereType<Track>()
|
2023-07-29 02:17:26 +00:00
|
|
|
.toList();
|
|
|
|
playerHelper.playFromTrackList(
|
|
|
|
queue,
|
|
|
|
data.id,
|
|
|
|
QueueSource(
|
|
|
|
text: 'Search history'.i18n,
|
|
|
|
source: 'searchhistory',
|
|
|
|
id: 'searchhistory'));
|
|
|
|
},
|
2023-10-16 22:22:50 +00:00
|
|
|
onSecondary: (details) => MenuSheet(context)
|
|
|
|
.defaultTrackMenu(data, details: details),
|
2023-07-29 02:17:26 +00:00
|
|
|
trailing: _removeHistoryItemWidget(i),
|
|
|
|
);
|
2024-01-24 17:55:25 +00:00
|
|
|
case final Album data:
|
2023-07-29 02:17:26 +00:00
|
|
|
return AlbumTile(
|
|
|
|
data,
|
|
|
|
onTap: () {
|
|
|
|
Navigator.of(context).pushRoute(
|
|
|
|
builder: (context) => AlbumDetails(data));
|
|
|
|
},
|
2023-10-16 22:22:50 +00:00
|
|
|
onSecondary: (details) => MenuSheet(context)
|
|
|
|
.defaultAlbumMenu(data, details: details),
|
2023-07-29 02:17:26 +00:00
|
|
|
trailing: _removeHistoryItemWidget(i),
|
|
|
|
);
|
2024-01-24 17:55:25 +00:00
|
|
|
case final Artist data:
|
2023-07-29 02:17:26 +00:00
|
|
|
return ArtistHorizontalTile(
|
|
|
|
data,
|
|
|
|
onTap: () {
|
|
|
|
Navigator.of(context).pushRoute(
|
|
|
|
builder: (context) =>
|
|
|
|
ArtistDetails(data));
|
|
|
|
},
|
|
|
|
onHold: () =>
|
|
|
|
MenuSheet(context).defaultArtistMenu(data),
|
|
|
|
trailing: _removeHistoryItemWidget(i),
|
|
|
|
);
|
2024-01-24 17:55:25 +00:00
|
|
|
case final Playlist data:
|
2023-07-29 02:17:26 +00:00
|
|
|
return PlaylistTile(
|
|
|
|
data,
|
|
|
|
onTap: () {
|
|
|
|
Navigator.of(context).pushRoute(
|
|
|
|
builder: (context) =>
|
|
|
|
PlaylistDetails(data));
|
|
|
|
},
|
2023-10-16 22:22:50 +00:00
|
|
|
onSecondary: (details) => MenuSheet(context)
|
|
|
|
.defaultPlaylistMenu(data,
|
|
|
|
details: details),
|
2023-07-29 02:17:26 +00:00
|
|
|
trailing: _removeHistoryItemWidget(i),
|
|
|
|
);
|
|
|
|
default:
|
|
|
|
return const SizedBox();
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
if (cache.searchHistory.isNotEmpty)
|
|
|
|
ListTile(
|
|
|
|
title: Text('Clear search history'.i18n),
|
2023-10-12 22:09:37 +00:00
|
|
|
leading: const Icon(Icons.clear_all),
|
2023-07-29 02:17:26 +00:00
|
|
|
onTap: () {
|
|
|
|
cache.searchHistory.clear();
|
|
|
|
cache.save();
|
|
|
|
setState(() {});
|
|
|
|
},
|
|
|
|
),
|
|
|
|
],
|
|
|
|
])),
|
|
|
|
|
|
|
|
//Suggestions
|
|
|
|
ValueListenableBuilder<List<String>>(
|
|
|
|
valueListenable: _suggestions,
|
|
|
|
builder: (context, suggestions, _) => Column(
|
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
children: List.generate(
|
|
|
|
suggestions.length,
|
|
|
|
(index) => ListTile(
|
|
|
|
title: Text(suggestions[index]),
|
2023-10-12 22:09:37 +00:00
|
|
|
leading: const Icon(Icons.search),
|
2023-07-29 02:17:26 +00:00
|
|
|
onTap: () {
|
|
|
|
setState(() =>
|
|
|
|
_controller.text = suggestions[index]);
|
|
|
|
_submit();
|
|
|
|
},
|
|
|
|
)))),
|
|
|
|
],
|
|
|
|
)),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class SearchBrowseCard extends StatelessWidget {
|
|
|
|
final Color color;
|
|
|
|
final Widget? icon;
|
|
|
|
final Function onTap;
|
|
|
|
final String text;
|
2023-10-12 22:09:37 +00:00
|
|
|
const SearchBrowseCard(
|
|
|
|
{super.key,
|
|
|
|
required this.color,
|
2023-07-29 02:17:26 +00:00
|
|
|
required this.onTap,
|
|
|
|
required this.text,
|
|
|
|
this.icon});
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Card(
|
|
|
|
color: color,
|
|
|
|
child: InkWell(
|
2023-10-12 22:09:37 +00:00
|
|
|
onTap: onTap as void Function()?,
|
|
|
|
child: SizedBox(
|
2023-07-29 02:17:26 +00:00
|
|
|
width: MediaQuery.of(context).size.width / 2 - 32,
|
|
|
|
height: 75,
|
|
|
|
child: Center(
|
|
|
|
child: Row(
|
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
children: [
|
|
|
|
if (icon != null) icon!,
|
|
|
|
if (icon != null) Container(width: 8.0),
|
|
|
|
Text(
|
|
|
|
text,
|
|
|
|
textAlign: TextAlign.center,
|
|
|
|
maxLines: 2,
|
|
|
|
overflow: TextOverflow.ellipsis,
|
|
|
|
style: TextStyle(
|
|
|
|
fontSize: 18.0,
|
|
|
|
fontWeight: FontWeight.bold,
|
|
|
|
color: (color.computeLuminance() > 0.5)
|
|
|
|
? Colors.black
|
|
|
|
: Colors.white),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
)),
|
|
|
|
),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class SearchResultsScreen extends StatelessWidget {
|
|
|
|
final String? query;
|
|
|
|
final bool? offline;
|
|
|
|
|
2023-10-12 22:09:37 +00:00
|
|
|
const SearchResultsScreen(this.query, {super.key, this.offline});
|
2023-07-29 02:17:26 +00:00
|
|
|
|
|
|
|
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(title: Text(query!)),
|
|
|
|
body: FutureBuilder(
|
|
|
|
future: _search(),
|
|
|
|
builder: (BuildContext context, AsyncSnapshot snapshot) {
|
2023-10-12 22:09:37 +00:00
|
|
|
if (!snapshot.hasData) {
|
|
|
|
return const Center(
|
2023-07-29 02:17:26 +00:00
|
|
|
child: CircularProgressIndicator(),
|
|
|
|
);
|
2023-10-12 22:09:37 +00:00
|
|
|
}
|
|
|
|
if (snapshot.hasError) return const ErrorScreen();
|
2023-07-29 02:17:26 +00:00
|
|
|
|
|
|
|
SearchResults results = snapshot.data;
|
|
|
|
|
2023-10-12 22:09:37 +00:00
|
|
|
if (results.empty) {
|
2023-07-29 02:17:26 +00:00
|
|
|
return Center(
|
|
|
|
child: Column(
|
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
children: <Widget>[
|
|
|
|
Icon(
|
|
|
|
Icons.warning,
|
|
|
|
size: 64.sp,
|
|
|
|
),
|
|
|
|
Text('No results!'.i18n)
|
|
|
|
],
|
|
|
|
),
|
|
|
|
);
|
2023-10-12 22:09:37 +00:00
|
|
|
}
|
2023-07-29 02:17:26 +00:00
|
|
|
|
|
|
|
return ListView(
|
|
|
|
children: <Widget>[
|
|
|
|
if (results.tracks != null && results.tracks!.isNotEmpty) ...[
|
|
|
|
Padding(
|
2023-10-12 22:09:37 +00:00
|
|
|
padding: const EdgeInsets.symmetric(
|
|
|
|
horizontal: 16.0, vertical: 4.0),
|
2023-07-29 02:17:26 +00:00
|
|
|
child: Text(
|
|
|
|
'Tracks'.i18n,
|
|
|
|
textAlign: TextAlign.left,
|
2023-10-12 22:09:37 +00:00
|
|
|
style: const TextStyle(
|
2023-07-29 02:17:26 +00:00
|
|
|
fontSize: 20.0, fontWeight: FontWeight.bold),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
for (final track in results.tracks!
|
|
|
|
.getRange(0, min(results.tracks!.length, 3)))
|
2023-10-16 22:22:50 +00:00
|
|
|
TrackTile.fromTrack(track, onTap: () {
|
2023-07-29 02:17:26 +00:00
|
|
|
cache.addToSearchHistory(track);
|
2024-01-24 17:55:25 +00:00
|
|
|
playerHelper.playSearchMixDeferred(track);
|
2023-10-16 22:22:50 +00:00
|
|
|
}, onSecondary: (details) {
|
2023-07-29 02:17:26 +00:00
|
|
|
MenuSheet m = MenuSheet(context);
|
2023-10-16 22:22:50 +00:00
|
|
|
m.defaultTrackMenu(track, details: details);
|
2023-07-29 02:17:26 +00:00
|
|
|
}),
|
|
|
|
ListTile(
|
|
|
|
title: Text('Show all tracks'.i18n),
|
|
|
|
onTap: () {
|
|
|
|
Navigator.of(context).pushRoute(
|
2024-01-24 17:55:25 +00:00
|
|
|
builder: (context) =>
|
|
|
|
TrackListScreen(results.tracks, null));
|
2023-07-29 02:17:26 +00:00
|
|
|
},
|
|
|
|
),
|
2023-10-12 22:09:37 +00:00
|
|
|
const FreezerDivider(),
|
2023-07-29 02:17:26 +00:00
|
|
|
],
|
|
|
|
if (results.albums != null && results.albums!.isNotEmpty) ...[
|
|
|
|
const SizedBox(height: 8.0),
|
|
|
|
Padding(
|
2023-10-12 22:09:37 +00:00
|
|
|
padding: const EdgeInsets.symmetric(
|
|
|
|
horizontal: 16.0, vertical: 4.0),
|
2023-07-29 02:17:26 +00:00
|
|
|
child: Text(
|
|
|
|
'Albums'.i18n,
|
|
|
|
textAlign: TextAlign.left,
|
2023-10-12 22:09:37 +00:00
|
|
|
style: const TextStyle(
|
2023-07-29 02:17:26 +00:00
|
|
|
fontSize: 20.0, fontWeight: FontWeight.bold),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
for (final album in results.albums!
|
|
|
|
.getRange(0, min(results.albums!.length, 3)))
|
2023-10-16 22:22:50 +00:00
|
|
|
AlbumTile(album, onSecondary: (details) {
|
2023-07-29 02:17:26 +00:00
|
|
|
MenuSheet m = MenuSheet(context);
|
2023-10-16 22:22:50 +00:00
|
|
|
m.defaultAlbumMenu(album, details: details);
|
2023-07-29 02:17:26 +00:00
|
|
|
}, onTap: () {
|
|
|
|
cache.addToSearchHistory(album);
|
|
|
|
Navigator.of(context)
|
|
|
|
.pushRoute(builder: (context) => AlbumDetails(album));
|
|
|
|
}),
|
|
|
|
ListTile(
|
|
|
|
title: Text('Show all albums'.i18n),
|
|
|
|
onTap: () {
|
|
|
|
Navigator.of(context).pushRoute(
|
|
|
|
builder: (context) =>
|
|
|
|
AlbumListScreen(results.albums));
|
|
|
|
},
|
|
|
|
),
|
2023-10-12 22:09:37 +00:00
|
|
|
const FreezerDivider()
|
2023-07-29 02:17:26 +00:00
|
|
|
],
|
|
|
|
if (results.artists != null && results.artists!.isNotEmpty) ...[
|
|
|
|
const SizedBox(height: 8.0),
|
|
|
|
Padding(
|
2023-10-12 22:09:37 +00:00
|
|
|
padding: const EdgeInsets.symmetric(
|
|
|
|
vertical: 4.0, horizontal: 16.0),
|
2023-07-29 02:17:26 +00:00
|
|
|
child: Text(
|
|
|
|
'Artists'.i18n,
|
|
|
|
textAlign: TextAlign.left,
|
2023-10-12 22:09:37 +00:00
|
|
|
style: const TextStyle(
|
2023-07-29 02:17:26 +00:00
|
|
|
fontSize: 20.0, fontWeight: FontWeight.bold),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
const SizedBox(height: 4.0),
|
2023-10-17 14:03:39 +00:00
|
|
|
SizedBox(
|
|
|
|
height: 136.0,
|
|
|
|
child: ListView.builder(
|
|
|
|
primary: false,
|
2023-07-29 02:17:26 +00:00
|
|
|
scrollDirection: Axis.horizontal,
|
2023-10-17 14:03:39 +00:00
|
|
|
itemCount: results.artists!.length,
|
|
|
|
itemBuilder: (context, index) {
|
|
|
|
final artist = results.artists![index];
|
|
|
|
return Padding(
|
|
|
|
padding: const EdgeInsets.symmetric(horizontal: 8.0),
|
|
|
|
child: ArtistTile(
|
2023-07-29 02:17:26 +00:00
|
|
|
artist,
|
|
|
|
onTap: () {
|
|
|
|
cache.addToSearchHistory(artist);
|
|
|
|
Navigator.of(context).pushRoute(
|
|
|
|
builder: (context) => ArtistDetails(artist));
|
|
|
|
},
|
2023-10-16 22:22:50 +00:00
|
|
|
onSecondary: (details) {
|
2023-07-29 02:17:26 +00:00
|
|
|
MenuSheet m = MenuSheet(context);
|
2023-10-16 22:22:50 +00:00
|
|
|
m.defaultArtistMenu(artist, details: details);
|
2023-07-29 02:17:26 +00:00
|
|
|
},
|
|
|
|
),
|
2023-10-17 14:03:39 +00:00
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
),
|
2023-10-12 22:09:37 +00:00
|
|
|
const FreezerDivider()
|
2023-07-29 02:17:26 +00:00
|
|
|
],
|
|
|
|
if (results.playlists != null &&
|
|
|
|
results.playlists!.isNotEmpty) ...[
|
|
|
|
const SizedBox(height: 8.0),
|
|
|
|
Padding(
|
2023-10-12 22:09:37 +00:00
|
|
|
padding: const EdgeInsets.symmetric(
|
|
|
|
vertical: 4.0, horizontal: 16.0),
|
2023-07-29 02:17:26 +00:00
|
|
|
child: Text(
|
|
|
|
'Playlists'.i18n,
|
|
|
|
textAlign: TextAlign.left,
|
2023-10-12 22:09:37 +00:00
|
|
|
style: const TextStyle(
|
2023-07-29 02:17:26 +00:00
|
|
|
fontSize: 20.0, fontWeight: FontWeight.bold),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
for (final playlist in results.playlists!
|
|
|
|
.getRange(0, min(results.playlists!.length, 3)))
|
|
|
|
PlaylistTile(
|
|
|
|
playlist,
|
|
|
|
onTap: () {
|
|
|
|
cache.addToSearchHistory(playlist);
|
|
|
|
Navigator.of(context).pushRoute(
|
|
|
|
builder: (context) => PlaylistDetails(playlist));
|
|
|
|
},
|
2023-10-16 22:22:50 +00:00
|
|
|
onSecondary: (details) {
|
2023-07-29 02:17:26 +00:00
|
|
|
MenuSheet m = MenuSheet(context);
|
2023-10-16 22:22:50 +00:00
|
|
|
m.defaultPlaylistMenu(playlist, details: details);
|
2023-07-29 02:17:26 +00:00
|
|
|
},
|
|
|
|
),
|
|
|
|
ListTile(
|
|
|
|
title: Text('Show all playlists'.i18n),
|
|
|
|
onTap: () {
|
|
|
|
Navigator.of(context).pushRoute(
|
|
|
|
builder: (context) =>
|
|
|
|
SearchResultPlaylists(results.playlists));
|
|
|
|
},
|
|
|
|
),
|
2023-10-12 22:09:37 +00:00
|
|
|
const FreezerDivider(),
|
2023-07-29 02:17:26 +00:00
|
|
|
],
|
|
|
|
if (results.shows != null && results.shows!.isNotEmpty) ...[
|
|
|
|
const SizedBox(height: 8.0),
|
|
|
|
Padding(
|
2023-10-12 22:09:37 +00:00
|
|
|
padding: const EdgeInsets.symmetric(
|
|
|
|
vertical: 4.0, horizontal: 16.0),
|
2023-07-29 02:17:26 +00:00
|
|
|
child: Text(
|
|
|
|
'Shows'.i18n,
|
|
|
|
textAlign: TextAlign.left,
|
2023-10-12 22:09:37 +00:00
|
|
|
style: const TextStyle(
|
2023-07-29 02:17:26 +00:00
|
|
|
fontSize: 20.0, fontWeight: FontWeight.bold),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
for (final show in results.shows!
|
|
|
|
.getRange(0, min(results.shows!.length, 3)))
|
|
|
|
ShowTile(
|
|
|
|
show,
|
|
|
|
onTap: () async {
|
|
|
|
Navigator.of(context)
|
|
|
|
.pushRoute(builder: (context) => ShowScreen(show));
|
|
|
|
},
|
|
|
|
),
|
|
|
|
ListTile(
|
|
|
|
title: Text('Show all shows'.i18n),
|
|
|
|
onTap: () {
|
|
|
|
Navigator.of(context).pushRoute(
|
|
|
|
builder: (context) => ShowListScreen(results.shows));
|
|
|
|
},
|
|
|
|
),
|
2023-10-12 22:09:37 +00:00
|
|
|
const FreezerDivider()
|
2023-07-29 02:17:26 +00:00
|
|
|
],
|
|
|
|
if (results.episodes != null &&
|
|
|
|
results.episodes!.isNotEmpty) ...[
|
|
|
|
const SizedBox(height: 8.0),
|
|
|
|
Padding(
|
2023-10-12 22:09:37 +00:00
|
|
|
padding: const EdgeInsets.symmetric(
|
|
|
|
vertical: 4.0, horizontal: 16.0),
|
2023-07-29 02:17:26 +00:00
|
|
|
child: Text(
|
|
|
|
'Episodes'.i18n,
|
|
|
|
textAlign: TextAlign.left,
|
2023-10-12 22:09:37 +00:00
|
|
|
style: const TextStyle(
|
2023-07-29 02:17:26 +00:00
|
|
|
fontSize: 20.0, fontWeight: FontWeight.bold),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
for (final episode in results.episodes!
|
|
|
|
.getRange(0, min(results.episodes!.length, 3)))
|
|
|
|
ShowEpisodeTile(
|
|
|
|
episode,
|
|
|
|
trailing: IconButton(
|
|
|
|
icon: Icon(
|
|
|
|
Icons.more_vert,
|
|
|
|
semanticLabel: "Options".i18n,
|
|
|
|
),
|
|
|
|
onPressed: () {
|
|
|
|
MenuSheet m = MenuSheet(context);
|
|
|
|
m.defaultShowEpisodeMenu(episode.show!, episode);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
onTap: () async {
|
|
|
|
//Load entire show, then play
|
|
|
|
List<ShowEpisode> episodes = (await deezerAPI
|
|
|
|
.allShowEpisodes(episode.show!.id))!;
|
|
|
|
await playerHelper.playShowEpisode(
|
|
|
|
episode.show!, episodes,
|
|
|
|
index: episodes
|
|
|
|
.indexWhere((ep) => episode.id == ep.id));
|
|
|
|
},
|
|
|
|
),
|
|
|
|
ListTile(
|
|
|
|
title: Text('Show all episodes'.i18n),
|
|
|
|
onTap: () {
|
|
|
|
Navigator.of(context).pushRoute(
|
|
|
|
builder: (context) =>
|
|
|
|
EpisodeListScreen(results.episodes));
|
|
|
|
})
|
|
|
|
]
|
|
|
|
],
|
|
|
|
);
|
|
|
|
},
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//List all tracks
|
|
|
|
class TrackListScreen extends StatelessWidget {
|
2024-01-24 17:55:25 +00:00
|
|
|
final QueueSource? queueSource;
|
|
|
|
final List<Track>? tracks;
|
2023-07-29 02:17:26 +00:00
|
|
|
|
2023-10-12 22:09:37 +00:00
|
|
|
const TrackListScreen(this.tracks, this.queueSource, {super.key});
|
2023-07-29 02:17:26 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Scaffold(
|
|
|
|
appBar: AppBar(title: Text('Tracks'.i18n)),
|
|
|
|
body: ListView.builder(
|
|
|
|
itemCount: tracks!.length,
|
|
|
|
itemBuilder: (BuildContext context, int i) {
|
2024-01-24 17:55:25 +00:00
|
|
|
Track t = tracks![i];
|
2023-10-16 22:22:50 +00:00
|
|
|
return TrackTile.fromTrack(
|
2023-07-29 02:17:26 +00:00
|
|
|
t,
|
|
|
|
onTap: () {
|
2024-01-24 17:55:25 +00:00
|
|
|
if (queueSource == null) {
|
|
|
|
playerHelper.playSearchMixDeferred(t);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
playerHelper.playFromTrackList(tracks!, t.id, queueSource!);
|
2023-07-29 02:17:26 +00:00
|
|
|
},
|
2023-10-16 22:22:50 +00:00
|
|
|
onSecondary: (details) {
|
2023-07-29 02:17:26 +00:00
|
|
|
MenuSheet m = MenuSheet(context);
|
2023-10-16 22:22:50 +00:00
|
|
|
m.defaultTrackMenu(t, details: details);
|
2023-07-29 02:17:26 +00:00
|
|
|
},
|
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//List all albums
|
|
|
|
class AlbumListScreen extends StatelessWidget {
|
|
|
|
final List<Album?>? albums;
|
2023-10-12 22:09:37 +00:00
|
|
|
const AlbumListScreen(this.albums, {super.key});
|
2023-07-29 02:17:26 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Scaffold(
|
|
|
|
appBar: AppBar(title: Text('Albums'.i18n)),
|
|
|
|
body: ListView.builder(
|
|
|
|
itemCount: albums!.length,
|
|
|
|
itemBuilder: (context, i) {
|
|
|
|
Album? a = albums![i];
|
|
|
|
return AlbumTile(
|
|
|
|
a,
|
|
|
|
onTap: () {
|
|
|
|
Navigator.of(context)
|
|
|
|
.pushRoute(builder: (context) => AlbumDetails(a));
|
|
|
|
},
|
2023-10-16 22:22:50 +00:00
|
|
|
onSecondary: (details) {
|
2023-07-29 02:17:26 +00:00
|
|
|
MenuSheet m = MenuSheet(context);
|
2023-10-16 22:22:50 +00:00
|
|
|
m.defaultAlbumMenu(a!, details: details);
|
2023-07-29 02:17:26 +00:00
|
|
|
},
|
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class SearchResultPlaylists extends StatelessWidget {
|
|
|
|
final List<Playlist?>? playlists;
|
2023-10-12 22:09:37 +00:00
|
|
|
const SearchResultPlaylists(this.playlists, {super.key});
|
2023-07-29 02:17:26 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Scaffold(
|
|
|
|
appBar: AppBar(title: Text('Playlists'.i18n)),
|
|
|
|
body: ListView.builder(
|
|
|
|
itemCount: playlists!.length,
|
|
|
|
itemBuilder: (context, i) {
|
|
|
|
Playlist? p = playlists![i];
|
|
|
|
return PlaylistTile(
|
|
|
|
p,
|
|
|
|
onTap: () {
|
|
|
|
Navigator.of(context)
|
|
|
|
.pushRoute(builder: (context) => PlaylistDetails(p));
|
|
|
|
},
|
2023-10-16 22:22:50 +00:00
|
|
|
onSecondary: (details) {
|
2023-07-29 02:17:26 +00:00
|
|
|
MenuSheet m = MenuSheet(context);
|
2023-10-16 22:22:50 +00:00
|
|
|
m.defaultPlaylistMenu(p!, details: details);
|
2023-07-29 02:17:26 +00:00
|
|
|
},
|
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class ShowListScreen extends StatelessWidget {
|
|
|
|
final List<Show>? shows;
|
2023-10-12 22:09:37 +00:00
|
|
|
const ShowListScreen(this.shows, {super.key});
|
2023-07-29 02:17:26 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Scaffold(
|
|
|
|
appBar: AppBar(title: Text('Shows'.i18n)),
|
|
|
|
body: ListView.builder(
|
|
|
|
itemCount: shows!.length,
|
|
|
|
itemBuilder: (context, i) {
|
|
|
|
Show s = shows![i];
|
|
|
|
return ShowTile(
|
|
|
|
s,
|
|
|
|
onTap: () {
|
|
|
|
Navigator.of(context)
|
|
|
|
.push(MaterialPageRoute(builder: (context) => ShowScreen(s)));
|
|
|
|
},
|
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class EpisodeListScreen extends StatelessWidget {
|
|
|
|
final List<ShowEpisode>? episodes;
|
2023-10-12 22:09:37 +00:00
|
|
|
const EpisodeListScreen(this.episodes, {super.key});
|
2023-07-29 02:17:26 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Scaffold(
|
|
|
|
appBar: AppBar(title: Text('Episodes'.i18n)),
|
|
|
|
body: ListView.builder(
|
|
|
|
itemCount: episodes!.length,
|
|
|
|
itemBuilder: (context, i) {
|
|
|
|
ShowEpisode e = episodes![i];
|
|
|
|
return ShowEpisodeTile(
|
|
|
|
e,
|
|
|
|
trailing: IconButton(
|
|
|
|
icon: Icon(
|
|
|
|
Icons.more_vert,
|
|
|
|
semanticLabel: "Options".i18n,
|
|
|
|
),
|
|
|
|
onPressed: () {
|
|
|
|
MenuSheet m = MenuSheet(context);
|
|
|
|
m.defaultShowEpisodeMenu(e.show!, e);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
onTap: () async {
|
|
|
|
//Load entire show, then play
|
|
|
|
List<ShowEpisode> episodes =
|
|
|
|
(await deezerAPI.allShowEpisodes(e.show!.id))!;
|
|
|
|
await playerHelper.playShowEpisode(e.show!, episodes,
|
|
|
|
index: episodes.indexWhere((ep) => e.id == ep.id));
|
|
|
|
},
|
|
|
|
);
|
|
|
|
},
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|