cinder92 / react-native-get-music-files

React Native package to get music files from local and sd for iOS and Android
MIT License
130 stars 66 forks source link

Add nil checks for assetURL and other properties in TurboSongs module #117

Open nahooni0511 opened 6 months ago

nahooni0511 commented 6 months ago

This commit introduces essential nil checks across the TurboSongs module to ensure stability and error handling for properties that might not always be present, such as the assetURL, title, albumTitle, and more. Notably, this update addresses a common scenario with Apple Music, where downloaded songs for offline use do not have an assetURL. By adding these checks, the module now safely handles such cases, preventing potential crashes and improving the user experience when working with local and downloaded media items. This enhancement is crucial for developers integrating with the Apple Music library, ensuring that their applications can gracefully handle a variety of media item states.

To reproduce the issue, follow these steps: Navigate to Apple Music, select a song, add it to your library, then select the song from within your library and download it for offline use. In this scenario, the song's assetURL may be absent, leading to the issue this pull request addresses.

cinder92 commented 4 months ago

hey @nahooni0511 thanks for your PR, i would like to return the complete object, even if asserUrl or any other object property is absent, so we can match the Song / Album interfaces

interface Song {
  url: string;
  title: string;
  album: string;
  artist: string;
  duration: number;
  genre: string;
  cover: string;
}

interface Album {
  url: string;
  album: string;
  artist: string;
  numberOfSongs: string;
  cover: string;
}

You could use ternary operator for such case. i.e

[songDictionary setValue:(assetURL && assetURL.absoluteString) ? [NSString stringWithString:assetURL.absoluteString] : @"" forKey:@"url"];

Thanks again.