private async void AllMusic_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) { switch (e.Action) { case NotifyCollectionChangedAction.Add: MusicCollection.AddRange(e.NewItems.Cast <LocalMusic>()); // ArtistCollection.AddRange(e.NewItems.Cast<LocalMusic>()); AlbumCollection.AddRange(e.NewItems.Cast <LocalMusic>()); break; case NotifyCollectionChangedAction.Remove: foreach (LocalMusic item in e.OldItems) { MusicCollection.Remove(item); // ArtistCollection.Remove(item); AlbumCollection.Remove(item); } await ArtistCollection.AddRangeAsync(AllMusic.GroupBy(x => x.ArtistsName?.FirstOrDefault() ?? "未知艺术家").ToDictionary(x => x.Key, x => x.ToArray()) .Select(x => new LocalArtist { Name = x.Key, PicPath = x.Value.First().Id3Pic, LocalMusics = x.Value })); break; case NotifyCollectionChangedAction.Replace: break; case NotifyCollectionChangedAction.Move: break; case NotifyCollectionChangedAction.Reset: MusicCollection.Clear(); ArtistCollection.Clear(); AlbumCollection.Clear(); break; default: throw new ArgumentOutOfRangeException(); } }
private async void ChooseDirectoryCommandImpl() { string selectPath = this._fileServices.SelectDirectory(); if (string.IsNullOrEmpty(selectPath)) { return; } AllMusic.Clear(); var result = await Task.Factory.StartNew((path) => { var temp = path.ToString(); if (!string.IsNullOrEmpty(temp)) { List <string> files = new List <string>(); var extensions = Settings.Default.SupportFileExtension.Split('|'); foreach (var item in extensions) { files.AddRange(this._fileServices.GetFiles(temp, true, item)); } return(files.ToArray()); } return(Array.Empty <string>()); }, state : selectPath); int totalPage = (int)Math.Ceiling(result.Length / (double)Settings.Default.LimitPerPage); var tasks = new List <Task <LocalMusic[]> >(); for (int i = 0; i < totalPage; i++) { var tmp = result.Skip(i * Settings.Default.LimitPerPage).Take(Settings.Default.LimitPerPage); tasks.Add(SelectMethod(tmp)); } await Task.WhenAll(tasks); await AllMusic.AddRangeAsync(tasks.AsParallel().SelectMany(x => x.Result).OrderBy(x => x.Title), async y => await ArtistCollection.AddRangeAsync(AllMusic.GroupBy(x => x.ArtistsName?.FirstOrDefault() ?? "未知艺术家").ToDictionary(x => x.Key, x => x.ToArray()) .Select(x => new LocalArtist { Name = x.Key, PicPath = x.Value.First().Id3Pic, LocalMusics = x.Value }))); //foreach (var item in result) //{ // var (title, albumName, artistName, duration, fileSize) = this._fileServices.GetFileId3(item); // AllMusic.Add(new LocalMusic // { // FilePath = item, // AlbumName = albumName, // Title = title, // ArtistsName = artistName, // Duration = duration, // FileSize = fileSize // }); //} }