Example #1
0
        public async Task AddSongAsync(Song song, string artworkUrl)
        {
            if (Songs.Count(p => p.ProviderId == song.ProviderId) > 0)
            {
                throw new Exception("AlreadySavedToast".FromLanguageResource());
            }

            #region create artist

            if (song.Artist.ProviderId == "lastid.")
            {
                song.Artist.ProviderId = "autc.single." + song.ProviderId;
            }

            var artist = Artists.FirstOrDefault(entry => entry.ProviderId == song.Artist.ProviderId);

            if (artist == null)
            {
                await _sqlService.InsertAsync(song.Artist);

                if (song.Album != null)
                {
                    song.Album.PrimaryArtistId = song.Artist.Id;
                }
                Artists.Insert(0, song.Artist);
            }

            else
            {
                song.Artist = artist;

                if (song.Album != null)
                {
                    song.Album.PrimaryArtistId = artist.Id;
                }
            }
            song.ArtistId = song.Artist.Id;

            #endregion

            #region create album

            if (song.Album == null)
            {
                song.Album = new Album
                {
                    PrimaryArtistId = song.ArtistId,
                    Name            = song.Name + " (Single)",
                    PrimaryArtist   = song.Artist,
                    ProviderId      = "autc.single." + song.ProviderId
                };
                await _sqlService.InsertAsync(song.Album);

                Albums.Insert(0, song.Album);
                song.Artist.Albums.Insert(0, song.Album);
            }
            else
            {
                var album = Albums.FirstOrDefault(p => p.ProviderId == song.Album.ProviderId);

                if (album != null)
                {
                    song.Album = album;
                }
                else
                {
                    await _sqlService.InsertAsync(song.Album);

                    Albums.Insert(0, song.Album);
                    song.Artist.Albums.Insert(0, song.Album);
                }
            }

            song.AlbumId = song.Album.Id;

            #endregion

            #region Download artwork

            if (artworkUrl != null)
            {
//Use the album if one is available
                var filePath = string.Format(CollectionConstant.ArtworkPath, song.Album.Id);

                //Check if the album artwork has already been downloaded
                var artworkExists = await StorageHelper.FileExistsAsync(filePath);

                if (!artworkExists)
                {
                    try
                    {
                        using (var client = new HttpClient())
                        {
                            using (var stream = await client.GetStreamAsync(artworkUrl))
                            {
                                using (
                                    var fileStream =
                                        await
                                            (await StorageHelper.CreateFileAsync(filePath)).OpenStreamForWriteAsync()
                                    )
                                {
                                    await stream.CopyToAsync(fileStream);

                                    //now set it
                                    song.Album.Artwork =
                                        new BitmapImage(new Uri(CollectionConstant.LocalStorageAppPath + filePath));
                                }
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        Debug.WriteLine("Some shit happened saving the artwork, here: " + e);
                    }
                }
            }

            if (song.Album.Artwork == null)
            {
                song.Album.Artwork = CollectionConstant.MissingArtworkImage;
            }

            #endregion

            //Insert to db
            await _sqlService.InsertAsync(song);

            song.Artist.Songs.Insert(0, song);
            song.Album.Songs.Insert(0, song);
            Songs.Insert(0, song);
        }