// DO NOT REMOVE THIS !! //static TrackList data = new TrackList(); //public static IEnumerable<TrackViewModel> GetTrackViewModelList() //{ // foreach (var track in data) // yield return TrackViewModel.Create(track.TrackId, // track.Name, // track.AlbumId, // track.MediaTypeId, // track.GenreId, // track.Composer, // track.Milliseconds, // track.Bytes); //} //public static void PersistTrack(TrackViewModel track) //{ // TrackInfo pTrack = track.TrackId.HasValue ? // data.First(t => t.TrackId == track.TrackId) : // new TrackInfo(); // pTrack.Name = track.Name; // pTrack.AlbumId = track.AlbumId; // pTrack.MediaTypeId = track.MediaTypeId; // pTrack.GenreId = track.GenreId; // pTrack.Composer = track.Composer; // pTrack.Milliseconds = track.Milliseconds; // pTrack.Bytes = track.Bytes; // if (!track.TrackId.HasValue) // data.Add(pTrack); //} public static IEnumerable <TrackViewModel> GetTrackViewModelList() { using (var ctx = new ChinookContext()) { var albumLookupData = (from album in ctx.Album select new LookupItem { Key = album.AlbumId, Value = album.Title }).ToList(); var mediaLookupData = (from media in ctx.MediaType select new LookupItem() { Key = media.MediaTypeId, Value = media.Name }).ToList(); var genreLookupData = (from genre in ctx.Genre select new LookupItem { Key = genre.GenreId, Value = genre.Name }).ToList(); foreach (var track in ctx.Track) { yield return(TrackViewModel.Create(track.TrackId, track.Name, track.AlbumId, track.MediaTypeId, track.GenreId, track.Composer, track.Milliseconds, track.Bytes, albumLookupData, mediaLookupData, genreLookupData)); } } }
public void TestCancelCommand() { var vm = TrackViewModel.Create(new TrackInfo() { Name = INITIAL_TRACKNAME }); vm.Name = MODIFIED_TRACKNAME; //Testing the Revert command... vm.Revert(); Assert.That(vm.Name, Is.EqualTo(INITIAL_TRACKNAME)); }
public void TestSaveCommand() { var vm = TrackViewModel.Create(new TrackInfo() { Name = INITIAL_TRACKNAME }); vm.Name = MODIFIED_TRACKNAME; //Testing the Save command... vm.Save(); Assert.That(vm.Name, Is.EqualTo(MODIFIED_TRACKNAME)); }
public void TestResetNameCommandYes() { var vm = TrackViewModel.Create(new TrackInfo() { Name = INITIAL_TRACKNAME }); var serviceContainer = (vm as ISupportServices).ServiceContainer; IMessageBoxService msgSvc = new DummyServiceForMessageBox(MessageResult.Yes); serviceContainer.RegisterService(msgSvc); //Testing the ResetName command while clicking Yes on the confirmation dialog... vm.ResetName(); Assert.That(vm.Name, Is.EqualTo("")); }
public static IEnumerable <AlbumViewModel> GetAlbumViewModelList() { using (var ctx = new ChinookContext()) { List <TrackViewModel> tracks = new List <TrackViewModel>(); //load tracks in memory foreach (var track in ctx.Track) { tracks.Add(TrackViewModel.Create(track.TrackId, track.Name, track.AlbumId, track.MediaTypeId, track.GenreId, track.Composer, track.Milliseconds, track.Bytes)); } foreach (var album in ctx.Album) { yield return(AlbumViewModel.Create(album.AlbumId, album.Title, tracks.FindAll(x => x.AlbumId == album.AlbumId))); } } }
private async Task IndexFolders(IEnumerable <GetFolderFilesResult> folders) { async Task <IndexTrackTransactionResult> IndexTrackTransactionAsync(StorageFile f, Folder folder) { var tf = TagLib.File.Create(await UwpFileAbstraction.CreateAsync(f)); var tag = tf.Tag; using (var tr = await db.Database.BeginTransactionAsync()) { try { var track = await CreateTrackAsync(f, tf); await db.Tracks.AddAsync(track); await db.SaveChangesAsync(); // create artist entity var artistName = string.IsNullOrEmpty(tag.FirstPerformer) ? "Unknown" : tag.FirstPerformer; var artist = await db.Artists .Include(x => x.Albums) .FirstOrDefaultAsync(a => a.Name == artistName); var createArtist = artist == default; if (createArtist) { artist = new Artist() { Name = artistName, }; await db.Artists.AddAsync(artist); } await db.SaveChangesAsync(); // create album entity var albumTitle = string.IsNullOrEmpty(tag.Album) ? "Unknown" : tag.Album; var album = artist.Albums.FirstOrDefault(x => x.Title == albumTitle); var createAlbum = album == default; if (createAlbum) { album = new Album() { Title = albumTitle, }; await db.Albums.AddAsync(album); } if (string.IsNullOrEmpty(album.CoverCacheToken)) { var picData = await FindAlbumCoverAsync(tag, f.Path); if (picData != default(IBuffer)) { var cover = await CacheService.CacheAsync(picData); album.CoverCacheToken = cover; } } await db.SaveChangesAsync(); track.Album = album; track.Folder = folder; album.Tracks.Add(track); if (createAlbum) { album.Artist = artist; artist.Albums.Add(album); } await db.SaveChangesAsync(); await tr.CommitAsync(); return(new IndexTrackTransactionResult() { Successful = true, Track = track, ArtistCreated = createArtist, Artist = artist, AlbumCreated = createAlbum, Album = album }); } catch (Exception) { await tr.RollbackAsync(); return(new IndexTrackTransactionResult() { Successful = false }); } } } var group = await folders.GroupJoinAsync(TrackSource.Items, x => x.ViewModel.Id, x => x.FolderId, (mix, dbFiles) => (mix, dbFiles.ToList())); await group.ForEachAsync(async g => { var dbFiles = g.Item2; var diskFiles = g.mix.Files; var dbFolder = await db.Folders.SingleAsync(x => x.Id == g.mix.ViewModel.Id); await diskFiles.ForEachAsync(async f => { var trackRx = await dbFiles.FirstOrDefaultAsync(x => x.FileName == f.Name && x.Path == f.Path); if (trackRx == null) { var result = await IndexTrackTransactionAsync(f, dbFolder); if (result.Successful == false) { return; } if (result.ArtistCreated) { var artistVm = ArtistViewModel.Create(result.Artist); _artistSource.AddOrUpdate(artistVm); } else { var artistVm = _artistSource.Lookup(result.Artist.Id).Value; if (result.AlbumCreated) { var albumVm = AlbumViewModel.Create(artistVm, result.Album); artistVm.Albums.AddOrUpdate(albumVm); } else { var albumVm = artistVm.Albums.Items.Single(x => x.Id == result.Album.Id); var trackVm = TrackViewModel.Create(albumVm, result.Track); albumVm.Tracks.AddOrUpdate(trackVm); } } } else { dbFiles.Remove(dbFiles.First(x => x.Id == trackRx.Id)); } }); await dbFiles.ForEachAsync(async f => { var track = await db.Tracks.SingleAsync(x => x.Id == f.Id); await RemoveTrackAsync(track); }); }); }