fix: handle empty albums and null mappings (#301)

This commit is contained in:
eddyizm 2025-12-07 10:05:30 -08:00 committed by GitHub
commit 3fbadc2521
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 108 additions and 74 deletions

View file

@ -205,6 +205,8 @@ public class AlbumRepository {
} }
public void getInstantMix(AlbumID3 album, int count, MediaCallback callback) { public void getInstantMix(AlbumID3 album, int count, MediaCallback callback) {
Log.d("AlbumRepository", "Attempting getInstantMix for AlbumID: " + album.getId());
App.getSubsonicClientInstance(false) App.getSubsonicClientInstance(false)
.getBrowsingClient() .getBrowsingClient()
.getSimilarSongs2(album.getId(), count) .getSimilarSongs2(album.getId(), count)
@ -213,8 +215,17 @@ public class AlbumRepository {
public void onResponse(@NonNull Call<ApiResponse> call, @NonNull Response<ApiResponse> response) { public void onResponse(@NonNull Call<ApiResponse> call, @NonNull Response<ApiResponse> response) {
List<Child> songs = new ArrayList<>(); List<Child> songs = new ArrayList<>();
if (response.isSuccessful() && response.body() != null && response.body().getSubsonicResponse().getSimilarSongs2() != null) { if (response.isSuccessful()
songs.addAll(response.body().getSubsonicResponse().getSimilarSongs2().getSongs()); && response.body() != null
&& response.body().getSubsonicResponse().getSimilarSongs2() != null) {
List<Child> similarSongs = response.body().getSubsonicResponse().getSimilarSongs2().getSongs();
if (similarSongs == null) {
Log.w("AlbumRepository", "API successful but 'songs' list was NULL for AlbumID: " + album.getId());
} else {
songs.addAll(similarSongs);
}
} }
callback.onLoadMedia(songs); callback.onLoadMedia(songs);

View file

@ -2,6 +2,7 @@ package com.cappielloantonio.tempo.util;
import android.net.Uri; import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.util.Log;
import androidx.annotation.OptIn; import androidx.annotation.OptIn;
import androidx.lifecycle.LifecycleOwner; import androidx.lifecycle.LifecycleOwner;
@ -35,21 +36,31 @@ public class MappingUtil {
return mediaItems; return mediaItems;
} }
private static final String TAG = "MappingUtil";
public static MediaItem mapMediaItem(Child media) { public static MediaItem mapMediaItem(Child media) {
try {
Uri uri = getUri(media); Uri uri = getUri(media);
Uri artworkUri = Uri.parse(CustomGlideRequest.createUrl(media.getCoverArtId(), Preferences.getImageSize())); String coverArtId = media.getCoverArtId();
Uri artworkUri = null;
if (coverArtId != null) {
artworkUri = Uri.parse(CustomGlideRequest.createUrl(coverArtId, Preferences.getImageSize()));
}
Bundle bundle = new Bundle(); Bundle bundle = new Bundle();
bundle.putString("id", media.getId()); bundle.putString("id", media.getId());
bundle.putString("parentId", media.getParentId()); bundle.putString("parentId", media.getParentId());
bundle.putBoolean("isDir", media.isDir()); bundle.putBoolean("isDir", media.isDir());
bundle.putString("title", media.getTitle()); bundle.putString("title", media.getTitle());
bundle.putString("album", media.getAlbum()); bundle.putString("album", media.getAlbum());
bundle.putString("artist", media.getArtist()); bundle.putString("artist", media.getArtist());
bundle.putInt("track", media.getTrack() != null ? media.getTrack() : 0); bundle.putInt("track", media.getTrack() != null ? media.getTrack() : 0);
bundle.putInt("year", media.getYear() != null ? media.getYear() : 0); bundle.putInt("year", media.getYear() != null ? media.getYear() : 0);
bundle.putString("genre", media.getGenre()); bundle.putString("genre", media.getGenre());
bundle.putString("coverArtId", media.getCoverArtId()); bundle.putString("coverArtId", coverArtId);
bundle.putLong("size", media.getSize() != null ? media.getSize() : 0); bundle.putLong("size", media.getSize() != null ? media.getSize() : 0);
bundle.putString("contentType", media.getContentType()); bundle.putString("contentType", media.getContentType());
bundle.putString("suffix", media.getSuffix()); bundle.putString("suffix", media.getSuffix());
@ -74,9 +85,10 @@ public class MappingUtil {
bundle.putInt("originalWidth", media.getOriginalWidth() != null ? media.getOriginalWidth() : 0); bundle.putInt("originalWidth", media.getOriginalWidth() != null ? media.getOriginalWidth() : 0);
bundle.putInt("originalHeight", media.getOriginalHeight() != null ? media.getOriginalHeight() : 0); bundle.putInt("originalHeight", media.getOriginalHeight() != null ? media.getOriginalHeight() : 0);
bundle.putString("uri", uri.toString()); bundle.putString("uri", uri.toString());
bundle.putString("assetLinkSong", AssetLinkUtil.buildLink(AssetLinkUtil.TYPE_SONG, media.getId()));
bundle.putString("assetLinkAlbum", AssetLinkUtil.buildLink(AssetLinkUtil.TYPE_ALBUM, media.getAlbumId())); bundle.putString("assetLinkSong", media.getId() != null ? AssetLinkUtil.buildLink(AssetLinkUtil.TYPE_SONG, media.getId()) : null);
bundle.putString("assetLinkArtist", AssetLinkUtil.buildLink(AssetLinkUtil.TYPE_ARTIST, media.getArtistId())); bundle.putString("assetLinkAlbum", media.getAlbumId() != null ? AssetLinkUtil.buildLink(AssetLinkUtil.TYPE_ALBUM, media.getAlbumId()) : null);
bundle.putString("assetLinkArtist", media.getArtistId() != null ? AssetLinkUtil.buildLink(AssetLinkUtil.TYPE_ARTIST, media.getArtistId()) : null);
bundle.putString("assetLinkGenre", AssetLinkUtil.buildLink(AssetLinkUtil.TYPE_GENRE, media.getGenre())); bundle.putString("assetLinkGenre", AssetLinkUtil.buildLink(AssetLinkUtil.TYPE_GENRE, media.getGenre()));
Integer year = media.getYear(); Integer year = media.getYear();
bundle.putString("assetLinkYear", year != null && year != 0 ? AssetLinkUtil.buildLink(AssetLinkUtil.TYPE_YEAR, String.valueOf(year)) : null); bundle.putString("assetLinkYear", year != null && year != 0 ? AssetLinkUtil.buildLink(AssetLinkUtil.TYPE_YEAR, String.valueOf(year)) : null);
@ -113,6 +125,17 @@ public class MappingUtil {
.setMimeType(MimeTypes.BASE_TYPE_AUDIO) .setMimeType(MimeTypes.BASE_TYPE_AUDIO)
.setUri(uri) .setUri(uri)
.build(); .build();
} catch (Exception e) {
String id = media != null ? media.getId() : "NULL_MEDIA_OBJECT";
String title = media != null ? media.getTitle() : "N/A";
Log.e(TAG, "Instant Mix CRASH! Failed to map song to MediaItem. " +
"Problematic Song ID: " + id +
", Title: " + title +
". Inspect this song's Subsonic data for missing fields.", e);
throw new RuntimeException("Mapping failed for song ID: " + id, e);
}
} }
public static MediaItem mapMediaItem(MediaItem old) { public static MediaItem mapMediaItem(MediaItem old) {