private SongModel LookupSongByPath(string path) { SongTable songTable = DatabaseManager.Current.LookupSongByPath(path); if (songTable == null) { return(null); } if (songLookupDictionary.ContainsKey(songTable.SongId)) { return(songLookupDictionary[songTable.SongId]); } SongModel songModel = new SongModel(songTable); _allSongs.Add(songModel); songLookupDictionary.Add(songModel.SongId, songModel); return(songModel); }
public SongModel LookupSongById(int songId) { if (songLookupDictionary.ContainsKey(songId)) { return(songLookupDictionary[songId]); } else { SongTable songTable = DatabaseManager.Current.LookupSongById(songId); if (songTable == null) { return(null); } else { SongModel songModel = new SongModel(songTable); _allSongs.Add(songModel); songLookupDictionary.Add(songModel.SongId, songModel); return(songModel); } } }
public SongModel AddNewSong(string artist, string album, string albumArtist, string title, string path, SongOriginSource origin, long duration, uint rating, uint trackNumber) { ArtistModel artistModel = LookupArtistByName(artist); ArtistModel albumArtistModel = LookupArtistByName(albumArtist); AlbumModel albumModel = LookupAlbumByName(album, albumArtistModel.ArtistId); SongModel currentTableEntry = LookupSongByPath(path); if (currentTableEntry == null) { SongTable newSong = new SongTable(albumModel.AlbumId, artistModel.ArtistId, duration, 0, title, origin, 0, rating, path, trackNumber); DatabaseManager.Current.AddSong(newSong); SongModel songModel = new SongModel(newSong); _allSongs.Add(songModel); songLookupDictionary.Add(songModel.SongId, songModel); return(songModel); } return(null); }
// async due to await calls so can't be inside the above lambda expression without farmimg it out to a seperate function private async void UpdateHistoryInternal() { bool canRun = false; lock (historyLock) { if (!isUpdatingHistory) { isUpdatingHistory = true; canRun = true; } } if (!canRun) { return; } Logger.Current.Log(new CallerInfo(), LogLevel.Info, "Updating history items"); List <HistoryTable> historyItems = DatabaseManager.Current.FetchHistoryItems(); // Process everything first so we don't have to wait for scrobbles to see those updates foreach (HistoryTable historyItem in historyItems) { SongModel song = LibraryModel.Current.LookupSongById(historyItem.SongId); if (song != null) { if (!historyItem.Processed) { await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { song.PlayCount++; if (song.LastPlayed < historyItem.DatePlayed) { song.LastPlayed = historyItem.DatePlayed; } }); historyItem.Processed = true; DatabaseManager.Current.Update(historyItem); } } } Logger.Current.Log(new CallerInfo(), LogLevel.Info, "Scrobbling history items"); // TODO: #3 can batch scrobbles foreach (HistoryTable historyItem in historyItems) { if (!historyItem.Scrobbled) { LastfmStatusCode scrobbleResult = await LastFMManager.Current.ScrobbleTrack(historyItem.SongName, historyItem.ArtistName, new DateTime(historyItem.DatePlayed)); Logger.Current.Log(new CallerInfo(), LogLevel.Info, "Scrobbling row {0} result {1}", historyItem.RowId, scrobbleResult); // "Failure" is non-recoverable, so actually mark it as scrobbled so we don't retry later if (scrobbleResult == LastfmStatusCode.Success || scrobbleResult == LastfmStatusCode.Failure) { historyItem.Scrobbled = true; } } if (historyItem.Processed && historyItem.Scrobbled) { DatabaseManager.Current.DeleteHistoryItem(historyItem.RowId); } else { DatabaseManager.Current.Update(historyItem); } } Logger.Current.Log(new CallerInfo(), LogLevel.Info, "Done updating history items"); lock (historyLock) { isUpdatingHistory = false; } }
// TODO: #18 actually use OriginSource: public SongModel GetSongFromSource(SongOriginSource origin, string path) { SongModel currentTableEntry = LookupSongByPath(path); return(currentTableEntry); }
private void LoadCollection() { PerfTracer perfTracer = new PerfTracer("LibraryModel Loading"); IEnumerable <SongTable> allSongs = DatabaseManager.Current.FetchSongs(); foreach (SongTable songEntry in allSongs) { SongModel songModel = new SongModel(songEntry); _allSongs.Add(songModel); songLookupDictionary.Add(songModel.SongId, songModel); } perfTracer.Trace("Songs Added"); IEnumerable <AlbumTable> allAlbums = DatabaseManager.Current.FetchAlbums(); foreach (AlbumTable albumEntry in allAlbums) { AlbumModel albumModel = new AlbumModel(albumEntry); _allAlbums.Add(albumModel); albumLookupDictionary.Add(albumModel.AlbumId, albumModel); } perfTracer.Trace("Albums Added"); IEnumerable <ArtistTable> allArtists = DatabaseManager.Current.FetchArtists(); foreach (ArtistTable artistEntry in allArtists) { ArtistModel artistModel = new ArtistModel(artistEntry); _allArtists.Add(artistModel); artistLookupDictionary.Add(artistModel.ArtistId, artistModel); } perfTracer.Trace("Artists Added"); IEnumerable <PlaylistTable> allPlaylists = DatabaseManager.Current.FetchPlaylists(); foreach (PlaylistTable playlistEntry in allPlaylists) { PlaylistModel playlistModel = new PlaylistModel(playlistEntry); Playlists.Add(playlistModel); playlistLookupDictionary.Add(playlistModel.PlaylistId, playlistModel); playlistModel.Populate(); } perfTracer.Trace("Playlists Added"); IEnumerable <MixTable> allMixes = DatabaseManager.Current.FetchMixes(); foreach (MixTable mixEntry in allMixes) { MixModel mixModel = new MixModel(mixEntry); Mixes.Add(mixModel); mixLookupDictionary.Add(mixModel.MixId, mixModel); mixModel.Populate(); } perfTracer.Trace("Mixes Added"); }