Conversation
Signed-off-by: Sertac Ozercan <sozercan@gmail.com>
There was a problem hiding this comment.
Pull request overview
This PR fixes a bug where YouTube's media session intercepts keyboard shortcuts (particularly when shuffle is enabled) and plays YouTube's autoplay track instead of the intended track from Kaset's queue. The fix introduces a flag to track Kaset-initiated playback and corrects any YouTube autoplay interference by re-playing the intended track.
- Adds
isKasetInitiatedPlaybackflag to track when Kaset initiates playback - Updates
updateTrackMetadatato detect and correct YouTube autoplay interference - Routes dock menu actions through PlayerService for proper shuffle handling
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| Core/Services/Player/PlayerService.swift | Adds isKasetInitiatedPlayback flag and logic to detect/correct YouTube autoplay interference when Kaset initiates track changes |
| App/AppDelegate.swift | Updates dock menu actions to use PlayerService instead of direct WebView calls, ensuring shuffle mode is respected |
| App/KasetApp.swift | Wires up PlayerService reference to AppDelegate for dock menu functionality |
| Tests/KasetTests/PlayerServiceTests.swift | Adds tests for shuffle behavior and YouTube autoplay correction |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| /// Flag to track when a song is nearing its end. | ||
| private var songNearingEnd: Bool = false | ||
|
|
||
| /// Flag to track when we initiated a track change (to ignore YouTube's autoplay). |
There was a problem hiding this comment.
Similarly, the comment says "to ignore YouTube's autoplay" but the flag is used to detect and correct it, not ignore it. Consider rephrasing to "to detect when we initiated a track change (to correct YouTube's autoplay interference)" for consistency with the actual behavior.
| /// Flag to track when we initiated a track change (to ignore YouTube's autoplay). | |
| /// Flag to track when we initiated a track change (to correct YouTube's autoplay interference). |
| // Get the song we intended to play | ||
| if let intendedSong = queue[safe: currentIndex], intendedSong.title != title { |
There was a problem hiding this comment.
The comparison uses song titles to detect if YouTube loaded a different track, but song titles may not be unique in a queue. Consider comparing intendedSong.videoId against the actual videoId loaded by YouTube instead. The videoId is available from self.currentTrack?.videoId and should be more reliable for detecting if YouTube loaded a different track.
| // Get the song we intended to play | |
| if let intendedSong = queue[safe: currentIndex], intendedSong.title != title { | |
| // Get the song we intended to play and compare using videoId to detect mismatched tracks | |
| if let intendedSong = queue[safe: currentIndex], | |
| let currentVideoId = self.currentTrack?.videoId, | |
| intendedSong.videoId != currentVideoId { |
| } | ||
|
|
||
| @Test("UpdateTrackMetadata corrects YouTube autoplay with Kaset-initiated playback") | ||
| func updateTrackMetadataCorrectYouTubeAutoplay() async { |
There was a problem hiding this comment.
The test name "updateTrackMetadataCorrectYouTubeAutoplay" is missing a verb. Consider renaming it to "updateTrackMetadataCorrectsYouTubeAutoplay" to follow standard Swift naming conventions where function names start with a verb.
| func updateTrackMetadataCorrectYouTubeAutoplay() async { | |
| func updateTrackMetadataCorrectsYouTubeAutoplay() async { |
| self.state = .loading | ||
| self.currentTrack = song | ||
|
|
||
| // Mark that we initiated this playback (to ignore YouTube's autoplay override) |
There was a problem hiding this comment.
The comment says "to ignore YouTube's autoplay override" but the actual behavior is to detect and correct it, not ignore it. Consider rephrasing to "to detect and correct YouTube's autoplay override" for clarity.
| // Mark that we initiated this playback (to ignore YouTube's autoplay override) | |
| // Mark that we initiated this playback (to detect and correct YouTube's autoplay override) |
Description
fixes #43
Type of Change
Related Issues
Changes Made
Testing
xcodebuild test -only-testing:KasetTests)Checklist
swiftlint --strict && swiftformat .Screenshots
Additional Notes