public int AddSong(int songId) { PlaylistEntryModel currentTail = null; if (Songs.Count > 0) { currentTail = Songs.Last(); } PlaylistEntryTable newPlaylistEntry; if (currentTail == null) { newPlaylistEntry = new PlaylistEntryTable(PlaylistId, songId, 0, 0); DatabaseManager.Current.AddPlaylistEntry(newPlaylistEntry); } else { newPlaylistEntry = new PlaylistEntryTable(PlaylistId, songId, 0, currentTail.RowId); DatabaseManager.Current.AddPlaylistEntry(newPlaylistEntry); currentTail.NextId = newPlaylistEntry.RowId; } PlaylistEntryModel newEntry = new PlaylistEntryModel(newPlaylistEntry); IndexLookupMap.Add(newEntry.RowId, newEntry); Songs.Add(newEntry); SongIds.Add(newEntry.SongId); return(newEntry.RowId); }
public void RemoveEntry(int entryId) { PlaylistEntryModel songToRemove = null; if (!IndexLookupMap.TryGetValue(entryId, out songToRemove)) { DebugHelper.Alert(new CallerInfo(), "Tried to remove play queue entry {0} but its not in our lookup", entryId); return; } PlaylistEntryModel previousModel = null; if (IndexLookupMap.TryGetValue(songToRemove.PrevId, out previousModel)) { previousModel.NextId = songToRemove.NextId; } PlaylistEntryModel nextModel = null; if (IndexLookupMap.TryGetValue(songToRemove.NextId, out nextModel)) { nextModel.PrevId = songToRemove.PrevId; } SongIds.Remove(songToRemove.SongId); Songs.Remove(songToRemove); IndexLookupMap.Remove(songToRemove.RowId); DatabaseManager.Current.DeletePlaylistEntry(songToRemove.RowId); }
public void Populate() { Songs.Clear(); List <PlaylistEntryTable> allEntries = DatabaseManager.Current.FetchPlaylistEntriesForPlaylist(PlaylistId); PlaylistEntryModel head = null; foreach (PlaylistEntryTable playlistEntry in allEntries) { PlaylistEntryModel newEntry = new PlaylistEntryModel(playlistEntry); IndexLookupMap.Add(newEntry.RowId, newEntry); SongIds.Add(playlistEntry.SongId); if (newEntry.PrevId == 0) { DebugHelper.Assert(new CallerInfo(), head == null, "Second head found for playlist {0}!", PlaylistId); head = newEntry; } } PlaylistEntryModel currentLocation = head; while (currentLocation != null && Songs.Count < IndexLookupMap.Count) { Songs.Add(currentLocation); if (IndexLookupMap.ContainsKey(currentLocation.NextId)) { currentLocation = IndexLookupMap[currentLocation.NextId]; } else { currentLocation = null; } } DebugHelper.Assert(new CallerInfo(), currentLocation == null, "Circular reference found in Playlist {0}", PlaylistId); DebugHelper.Assert(new CallerInfo(), Songs.Count == IndexLookupMap.Count, "Missing element found in Playlist {0}", PlaylistId); }
public void MoveSong(int oldIndex, int newIndex) { if (oldIndex == newIndex) { return; } PlaylistEntryModel songToMove = Songs[oldIndex]; PlaylistEntryModel target = null; if (newIndex > 0) { if (newIndex < oldIndex) { target = Songs[newIndex - 1]; } else { target = Songs[newIndex]; } } // Remove from old spot PlaylistEntryModel previousModel = null; if (IndexLookupMap.TryGetValue(songToMove.PrevId, out previousModel)) { previousModel.NextId = songToMove.NextId; } PlaylistEntryModel nextModel = null; if (IndexLookupMap.TryGetValue(songToMove.NextId, out nextModel)) { nextModel.PrevId = songToMove.PrevId; } // Insert after new spot if (target == null) { PlaylistEntryModel head = null; if (Songs.Count > 0) { head = Songs.First(); } if (head != null) { songToMove.NextId = head.RowId; head.PrevId = songToMove.RowId; songToMove.PrevId = 0; } else { // Should be redundant songToMove.NextId = 0; songToMove.PrevId = 0; } } else { PlaylistEntryModel newNextModel = null; if (IndexLookupMap.TryGetValue(target.NextId, out newNextModel)) { newNextModel.PrevId = songToMove.RowId; } songToMove.NextId = target.NextId; target.NextId = songToMove.RowId; songToMove.PrevId = target.RowId; } Songs.Move(oldIndex, newIndex); }