2023-07-29 02:17:26 +00:00
|
|
|
import 'package:audio_service/audio_service.dart';
|
|
|
|
import 'package:freezer/api/deezer.dart';
|
|
|
|
import 'package:freezer/api/definitions.dart';
|
|
|
|
import 'package:freezer/api/player.dart';
|
|
|
|
import 'package:freezer/translations.i18n.dart';
|
|
|
|
|
|
|
|
class AndroidAuto {
|
2023-10-08 10:53:22 +00:00
|
|
|
final DeezerAPI deezerAPI;
|
|
|
|
AndroidAuto({required this.deezerAPI});
|
2023-07-29 02:17:26 +00:00
|
|
|
//Prefix for "playable" MediaItem
|
|
|
|
static const prefix = '_aa_';
|
|
|
|
|
|
|
|
//Get media items for parent id
|
2023-10-08 10:53:22 +00:00
|
|
|
Future<List<MediaItem>> getScreen(String parentId) async {
|
2023-07-29 02:17:26 +00:00
|
|
|
print(parentId);
|
|
|
|
|
|
|
|
//Homescreen
|
|
|
|
if (parentId == 'root' || parentId == null) return homeScreen();
|
|
|
|
|
|
|
|
//Playlists screen
|
|
|
|
if (parentId == 'playlists') {
|
|
|
|
//Fetch
|
|
|
|
List<Playlist> playlists = await deezerAPI.getPlaylists();
|
|
|
|
|
|
|
|
List<MediaItem> out = playlists
|
|
|
|
.map<MediaItem>((p) => MediaItem(
|
|
|
|
id: '${prefix}playlist${p.id}',
|
|
|
|
displayTitle: p.title,
|
|
|
|
title: p.title!,
|
|
|
|
album: '',
|
|
|
|
displaySubtitle: p.trackCount.toString() + ' ' + 'Tracks'.i18n,
|
|
|
|
playable: true,
|
2023-09-26 00:06:59 +00:00
|
|
|
artUri: Uri.parse(p.image!.thumb)))
|
2023-07-29 02:17:26 +00:00
|
|
|
.toList();
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Albums screen
|
|
|
|
if (parentId == 'albums') {
|
|
|
|
List<Album> albums = await deezerAPI.getAlbums();
|
|
|
|
|
|
|
|
List<MediaItem> out = albums
|
|
|
|
.map<MediaItem>((a) => MediaItem(
|
|
|
|
id: '${prefix}album${a.id}',
|
|
|
|
displayTitle: a.title,
|
|
|
|
album: a.title!,
|
|
|
|
title: '',
|
|
|
|
displaySubtitle: a.artistString,
|
|
|
|
playable: true,
|
2023-09-26 00:06:59 +00:00
|
|
|
artUri: Uri.parse(a.art!.thumb),
|
2023-07-29 02:17:26 +00:00
|
|
|
))
|
|
|
|
.toList();
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Artists screen
|
|
|
|
if (parentId == 'artists') {
|
|
|
|
List<Artist> artists = (await deezerAPI.getArtists())!;
|
|
|
|
|
|
|
|
List<MediaItem> out = artists
|
|
|
|
.map<MediaItem>((a) => MediaItem(
|
|
|
|
title: '',
|
|
|
|
album: '',
|
|
|
|
id: 'albums${a.id}',
|
|
|
|
displayTitle: a.name,
|
|
|
|
playable: false,
|
2023-09-26 00:06:59 +00:00
|
|
|
artUri: Uri.parse(a.picture!.thumb)))
|
2023-07-29 02:17:26 +00:00
|
|
|
.toList();
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Artist screen (albums, etc)
|
|
|
|
if (parentId.startsWith('albums')) {
|
|
|
|
List<Album> albums = (await deezerAPI
|
|
|
|
.discographyPage(parentId.replaceFirst('albums', '')))!;
|
|
|
|
|
|
|
|
List<MediaItem> out = albums
|
|
|
|
.map<MediaItem>((a) => MediaItem(
|
|
|
|
id: '${prefix}album${a.id}',
|
|
|
|
displayTitle: a.title,
|
|
|
|
title: '',
|
|
|
|
album: a.title ?? '',
|
|
|
|
displaySubtitle: a.artistString,
|
|
|
|
playable: true,
|
2023-09-26 00:06:59 +00:00
|
|
|
artUri: Uri.parse(a.art!.thumb)))
|
2023-07-29 02:17:26 +00:00
|
|
|
.toList();
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Homescreen
|
|
|
|
if (parentId == 'homescreen') {
|
|
|
|
HomePage hp = await deezerAPI.homePage();
|
|
|
|
List<MediaItem> out = [];
|
2023-10-08 10:53:22 +00:00
|
|
|
for (HomePageSection section in hp.sections) {
|
2023-07-29 02:17:26 +00:00
|
|
|
for (int i = 0; i < section.items!.length; i++) {
|
|
|
|
//Limit to max 5 items
|
|
|
|
if (i == 5) break;
|
|
|
|
|
|
|
|
//Check type
|
|
|
|
var data = section.items![i].value;
|
|
|
|
switch (section.items![i].type) {
|
|
|
|
case HomePageItemType.PLAYLIST:
|
|
|
|
out.add(MediaItem(
|
|
|
|
title: data.title ?? '',
|
|
|
|
album: '',
|
|
|
|
id: '${prefix}playlist${data.id}',
|
|
|
|
displayTitle: data.title,
|
|
|
|
playable: true,
|
|
|
|
artUri: data.image.thumb));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case HomePageItemType.ALBUM:
|
|
|
|
out.add(MediaItem(
|
|
|
|
id: '${prefix}album${data.id}',
|
|
|
|
displayTitle: data.title,
|
|
|
|
album: data.title ?? '',
|
|
|
|
title: '',
|
|
|
|
displaySubtitle: data.artistString,
|
|
|
|
playable: true,
|
|
|
|
artUri: data.art.thumb));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case HomePageItemType.ARTIST:
|
|
|
|
out.add(MediaItem(
|
|
|
|
id: 'albums${data.id}',
|
|
|
|
title: '',
|
|
|
|
album: data.title ?? '',
|
|
|
|
displayTitle: data.name,
|
|
|
|
playable: false,
|
|
|
|
artUri: data.picture.thumb));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case HomePageItemType.SMARTTRACKLIST:
|
|
|
|
out.add(MediaItem(
|
|
|
|
title: data.title ?? '',
|
|
|
|
album: '',
|
|
|
|
id: '${prefix}stl${data.id}',
|
|
|
|
displayTitle: data.title,
|
|
|
|
displaySubtitle: data.subtitle,
|
|
|
|
playable: true,
|
|
|
|
artUri: data.cover.thumb));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
//Load virtual mediaItem
|
2023-10-08 10:53:22 +00:00
|
|
|
Future<void> playItem(String id) async {
|
2023-07-29 02:17:26 +00:00
|
|
|
print(id);
|
|
|
|
|
|
|
|
//Play flow
|
|
|
|
if (id == 'flow' || id == 'stlflow') {
|
|
|
|
await playerHelper.playFromSmartTrackList(
|
|
|
|
SmartTrackList(id: 'flow', title: 'Flow'.i18n));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
//Play library tracks
|
|
|
|
if (id == 'tracks') {
|
|
|
|
//Load tracks
|
|
|
|
Playlist? favPlaylist;
|
|
|
|
try {
|
|
|
|
favPlaylist =
|
|
|
|
await deezerAPI.fullPlaylist(deezerAPI.favoritesPlaylistId);
|
|
|
|
} catch (e) {
|
|
|
|
print(e);
|
|
|
|
}
|
|
|
|
if (favPlaylist == null || favPlaylist.tracks!.isEmpty) return;
|
|
|
|
|
|
|
|
await playerHelper.playFromTrackList(
|
|
|
|
favPlaylist.tracks!,
|
|
|
|
favPlaylist.tracks![0].id,
|
|
|
|
QueueSource(
|
|
|
|
id: 'allTracks',
|
|
|
|
text: 'All offline tracks'.i18n,
|
|
|
|
source: 'offline'));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
//Play playlists
|
|
|
|
if (id!.startsWith('playlist')) {
|
|
|
|
Playlist p =
|
|
|
|
await deezerAPI.fullPlaylist(id.replaceFirst('playlist', ''));
|
|
|
|
await playerHelper.playFromPlaylist(p, p.tracks![0].id);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
//Play albums
|
|
|
|
if (id.startsWith('album')) {
|
|
|
|
Album a = await deezerAPI.album(id.replaceFirst('album', ''));
|
|
|
|
await playerHelper.playFromAlbum(a, a.tracks![0].id);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
//Play smart track list
|
|
|
|
if (id.startsWith('stl')) {
|
|
|
|
SmartTrackList stl =
|
|
|
|
await deezerAPI.smartTrackList(id.replaceFirst('stl', ''));
|
|
|
|
await playerHelper.playFromSmartTrackList(stl);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//Homescreen items
|
|
|
|
List<MediaItem> homeScreen() {
|
|
|
|
return [
|
|
|
|
MediaItem(
|
|
|
|
id: '${prefix}flow',
|
|
|
|
displayTitle: 'Flow'.i18n,
|
|
|
|
playable: true,
|
|
|
|
title: 'Flow'.i18n,
|
|
|
|
album: ''),
|
|
|
|
MediaItem(
|
|
|
|
id: 'homescreen',
|
|
|
|
title: 'Home'.i18n,
|
|
|
|
album: '',
|
|
|
|
displayTitle: 'Home'.i18n,
|
|
|
|
playable: false,
|
|
|
|
),
|
|
|
|
MediaItem(
|
|
|
|
id: '${prefix}tracks',
|
|
|
|
title: 'Loved tracks'.i18n,
|
|
|
|
album: '',
|
|
|
|
displayTitle: 'Loved tracks'.i18n,
|
|
|
|
playable: true,
|
|
|
|
),
|
|
|
|
MediaItem(
|
|
|
|
id: 'playlists',
|
|
|
|
title: 'Playlists'.i18n,
|
|
|
|
album: '',
|
|
|
|
displayTitle: 'Playlists'.i18n,
|
|
|
|
playable: false,
|
|
|
|
),
|
|
|
|
MediaItem(
|
|
|
|
id: 'albums',
|
|
|
|
title: 'Albums'.i18n,
|
|
|
|
album: '',
|
|
|
|
displayTitle: 'Albums'.i18n,
|
|
|
|
playable: false,
|
|
|
|
),
|
|
|
|
MediaItem(
|
|
|
|
id: 'artists',
|
|
|
|
title: 'Artists'.i18n,
|
|
|
|
album: '',
|
|
|
|
displayTitle: 'Artists'.i18n,
|
|
|
|
playable: false,
|
|
|
|
),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|