private int AddSong(int songId) { // Keep queue trimmed if (LookupMap.Count >= MAX_QUEUE_SIZE) { PlayQueueEntryModel head = null; if (PlaybackQueue.Count > 0) { head = PlaybackQueue.First(); } if (head != null) { if (CurrentPlaybackQueueEntryId == head.RowId) { // TODO: #6 raise alert about queue being full return(-1); } else { RemoveEntry(head.RowId); } } } PlayQueueEntryModel currentTail = null; if (PlaybackQueue.Count > 0) { currentTail = PlaybackQueue.Last(); } PlayQueueEntryTable newPlayQueueEntry; if (currentTail == null) { newPlayQueueEntry = new PlayQueueEntryTable(songId, 0, 0); DatabaseManager.Current.AddPlayQueueEntry(newPlayQueueEntry); } else { newPlayQueueEntry = new PlayQueueEntryTable(songId, 0, currentTail.RowId); DatabaseManager.Current.AddPlayQueueEntry(newPlayQueueEntry); currentTail.NextId = newPlayQueueEntry.RowId; } PlayQueueEntryModel newEntry = new PlayQueueEntryModel(newPlayQueueEntry); LookupMap.Add(newEntry.RowId, newEntry); PlaybackQueue.Add(newEntry); NotifyPropertyChanged(Properties.NextTrack); NotifyPropertyChanged(Properties.PrevTrack); return(newEntry.RowId); }
public void ResetPlayerToStart() { if (PlaybackQueue.Count > 0) { PlayQueueEntryModel playQueueEntry = PlaybackQueue.First(); CurrentPlaybackQueueEntryId = playQueueEntry.RowId; } }
public void Populate() { Logger.Current.Log(new CallerInfo(), LogLevel.Info, "Calling Popuplate"); List <PlayQueueEntryTable> allEntries = DatabaseManager.Current.FetchPlayQueueEntries(); PlayQueueEntryModel head = null; foreach (PlayQueueEntryTable playQueueEntry in allEntries) { PlayQueueEntryModel newEntry = new PlayQueueEntryModel(playQueueEntry); LookupMap.Add(newEntry.RowId, newEntry); if (newEntry.PrevId == 0) { DebugHelper.Assert(new CallerInfo(), head == null, "Second head found in play queue!!!"); head = newEntry; } } PlayQueueEntryModel currentLocation = head; while (currentLocation != null && PlaybackQueue.Count < LookupMap.Count) { PlaybackQueue.Add(currentLocation); if (LookupMap.ContainsKey(currentLocation.NextId)) { currentLocation = LookupMap[currentLocation.NextId]; } else { currentLocation = null; } } DebugHelper.Assert(new CallerInfo(), currentLocation == null, "Circular reference found in Play Queue"); DebugHelper.Assert(new CallerInfo(), PlaybackQueue.Count == LookupMap.Count, "Missing element found in Play Queue"); CurrentPlaybackQueueEntryId = ApplicationSettings.GetSettingsValue <int>(ApplicationSettings.CURRENT_PLAYQUEUE_POSITION, 0); if (CurrentPlaybackQueueEntryId == 0) { ResetPlayerToStart(); } }
public void RemoveEntry(int entryId) { if (entryId == CurrentPlaybackQueueEntryId) { Stop(); CurrentPlaybackQueueEntryId = 0; } PlayQueueEntryModel songToRemove = null; if (!LookupMap.TryGetValue(entryId, out songToRemove)) { DebugHelper.Alert(new CallerInfo(), "Tried to remove play queue entry {0} but its not in our lookup", entryId); return; } PlayQueueEntryModel previousModel = null; if (LookupMap.TryGetValue(songToRemove.PrevId, out previousModel)) { previousModel.NextId = songToRemove.NextId; } PlayQueueEntryModel nextModel = null; if (LookupMap.TryGetValue(songToRemove.NextId, out nextModel)) { nextModel.PrevId = songToRemove.PrevId; } PlaybackQueue.Remove(songToRemove); LookupMap.Remove(songToRemove.RowId); DatabaseManager.Current.DeletePlayQueueEntry(songToRemove.RowId); NotifyPropertyChanged(Properties.NextTrack); NotifyPropertyChanged(Properties.PrevTrack); }
public void MoveSong(int oldIndex, int newIndex) { if (oldIndex == newIndex) { return; } PlayQueueEntryModel songToMove = PlaybackQueue[oldIndex]; PlayQueueEntryModel target = null; if (newIndex > 0) { if (newIndex < oldIndex) { target = PlaybackQueue[newIndex - 1]; } else { target = PlaybackQueue[newIndex]; } } // Remove from old spot PlayQueueEntryModel previousModel = null; if (LookupMap.TryGetValue(songToMove.PrevId, out previousModel)) { previousModel.NextId = songToMove.NextId; } PlayQueueEntryModel nextModel = null; if (LookupMap.TryGetValue(songToMove.NextId, out nextModel)) { nextModel.PrevId = songToMove.PrevId; } // Insert after new spot if (target == null) { PlayQueueEntryModel head = null; if (PlaybackQueue.Count > 0) { head = PlaybackQueue.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 { PlayQueueEntryModel newNextModel = null; if (LookupMap.TryGetValue(target.NextId, out newNextModel)) { newNextModel.PrevId = songToMove.RowId; } songToMove.NextId = target.NextId; target.NextId = songToMove.RowId; songToMove.PrevId = target.RowId; } PlaybackQueue.Move(oldIndex, newIndex); NotifyPropertyChanged(Properties.NextTrack); NotifyPropertyChanged(Properties.PrevTrack); }