コード例 #1
0
        public async Task RemoveMediaFromCollectionAndDatabase(IMediaItem media)
        {
            if (media is TrackItem)
            {
                var trackItem = media as TrackItem;
                var trackDB   = LoadTrackById(trackItem.Id);
                if (trackDB == null)
                {
                    return;
                }
                musicDatabase.Remove(trackDB);

                var albumDB = LoadAlbum(trackItem.AlbumId);
                if (albumDB == null)
                {
                    return;
                }
                var albumTracks = LoadTracksByAlbumId(albumDB.Id);
                if (!albumTracks.Any())
                {
                    Albums.Remove(Albums.FirstOrDefault(x => x.Id == trackItem.AlbumId));
                    musicDatabase.Remove(albumDB);
                }

                var artistDB = LoadArtist(trackItem.ArtistId);
                if (artistDB == null)
                {
                    return;
                }
                var artistAlbums = LoadAlbums(artistDB.Id);
                if (!artistAlbums.Any())
                {
                    Artists.Remove(Artists.FirstOrDefault(x => x.Id == trackItem.ArtistId));
                    musicDatabase.Remove(artistDB);
                }

                await DispatchHelper.InvokeAsync(CoreDispatcherPriority.Normal, () =>
                {
                    Tracks.Remove(Tracks.FirstOrDefault(x => x.Path == trackItem.Path));

                    var playingTrack = Locator.MediaPlaybackViewModel.PlaybackService.Playlist.FirstOrDefault(x => x.Id == trackItem.Id);
                    if (playingTrack != null)
                    {
                        Locator.MediaPlaybackViewModel.PlaybackService.Playlist.Remove(playingTrack);
                    }
                });
            }
            else if (media is VideoItem)
            {
                var videoItem = media as VideoItem;
                var videoDb   = LoadVideoById(videoItem.Id);
                if (videoDb == null)
                {
                    return;
                }
                videoDatabase.Remove(videoDb);

                Videos.Remove(Videos.FirstOrDefault(x => x.Path == videoItem.Path));
            }
        }