freezer/lib/ui/android_auto.dart

253 lines
7.1 KiB
Dart
Raw Normal View History

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