Improve shuffle function when no songs are playing or displayed#129
Merged
theovilardo merged 4 commits intotheovilardo:masterfrom Sep 9, 2025
Merged
Improve shuffle function when no songs are playing or displayed#129theovilardo merged 4 commits intotheovilardo:masterfrom
theovilardo merged 4 commits intotheovilardo:masterfrom
Conversation
This commit refactors the shuffle functionality within `PlayerViewModel` for a more robust and predictable user experience.
Key changes:
- **Shuffle Logic Update:**
- When shuffle mode is enabled and the current media item count is zero, a new shuffled queue is generated from the master list of all songs. The `playSongs` function is then called with this new queue, starting playback from the first song in the shuffled list.
- The `updateCurrentPlaybackQueueFromPlayer` method has been modified. When shuffle mode is enabled, it now calls a new `createShuffledQueue` function. This function creates a new shuffled list of songs and updates the `currentPlaybackQueue` in the UI state.
- **New `createShuffledQueue` Function:**
- This new private function takes a list of `Song` objects, randomly selects one song to be the first in the queue, and then shuffles the remaining songs. The selected first song is then added to the beginning of the shuffled list.
- **Timeline Change Handling:**
- Logging has been added to `onTimelineChanged` when the reason for the change is `TIMELINE_CHANGE_REASON_PLAYLIST_CHANGED`. The direct call to `updateCurrentPlaybackQueueFromPlayer` has been commented out in this callback, likely to prevent redundant updates or potential race conditions with the new shuffle logic.
- **Logging:**
- Additional logging has been introduced in various parts of the `PlayerViewModel`, particularly around queue updates and song playback, to aid in debugging and understanding the flow of operations.
This commit refactors the `PlayerViewModel` to simplify queue handling and remove unnecessary logging statements. Key changes: - **Simplified `updateCurrentPlaybackQueueFromPlayer`**: The logic for updating the current playback queue when shuffle mode is enabled has been removed. The queue now directly reflects the order from the `MediaController`'s timeline. - **Removed redundant logging**: Several `Log.d` and `println` statements used for debugging have been removed from various functions, including `combine` for `favoriteSongs`, `updateCurrentPlaybackQueueFromPlayer`, `setupMediaControllerListeners`, `internalPlaySongs`, and `loadAndPlaySong`. - **Improved `createShuffledQueue`**: Added a check for an empty songs list to prevent potential errors.
The previous shuffle implementation would only toggle the shuffle mode and then either pause the current song or start playing the first song in the library. This change introduces a new `shuffleAllSongs` function in the `PlayerViewModel` that, when called: 1. Enables shuffle mode on the media player. 2. Selects a random song from the full list of songs. 3. Starts playback with the random song and a shuffled queue of all songs. The shuffle button in the library screen is updated to call this new function, providing the user with the expected shuffle behavior. A unit test has been added to verify that the `shuffleAllSongs` function correctly calls the underlying `playSongs` method with a random starting song and the full song list.
Refactor library shuffle button functionality to play a random song when there are no songs playing / displayed, and generate a shuffled queue.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This change is focused on the library songs tab shuffle button. Previously when this button was touched the first song of the master list of songs would play, and there would be no queued songs afterwards. Now when the same button is clicked, a random song is chosen from the master song list and a queue of shuffled songs is added.