Merged
Conversation
The favorites feature was not working correctly when a song was initiated from outside the Library screen (e.g., from the HomeScreen). The favorite button in the player would not update its state, and the song's favorite status was not being saved. This was caused by a potential race condition and state management issue in the `PlayerViewModel`. The favorite status was being updated manually, which could lead to inconsistencies depending on the timing of state updates from different sources. This commit refactors the `PlayerViewModel` to handle the favorite status in a purely reactive way: 1. **Removed `isCurrentSongFavorite` from `StablePlayerState`:** The `isCurrentSongFavorite` boolean was removed from the main player state data class to prevent the possibility of this state becoming stale. 2. **Created a Reactive `isCurrentSongFavorite` StateFlow:** A new `StateFlow<Boolean>` was created that is derived by combining the `stablePlayerState` (to get the current song) and the `favoriteSongIds` flow from the repository. This new flow automatically emits the correct favorite status whenever the current song changes or the list of favorites is updated. 3. **Simplified Logic:** With the new reactive flow, the manual calls to `updateFavoriteStatusForCurrentSong()` were no longer needed and have been removed. This simplifies the code and eliminates the source of the race condition. 4. **Updated UI:** The `UnifiedPlayerSheet` (the player UI) was updated to collect its state from this new, always-correct `isCurrentSongFavorite` flow. This change ensures a single, reactive source of truth for a song's favorite status, making the feature robust and functional across the entire application.
The `isCurrentSongFavorite` property within the `PlayerUiState` has been commented out and removed from its update logic in `PlayerViewModel.kt`. This change simplifies the state by removing a property that was no longer being utilized.
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.
No description provided.