Add chapter threshold logic for prev chapter button#1551
Merged
Conversation
There was a problem hiding this comment.
Pull request overview
Extends the existing “chapter start threshold” behavior (already used for rewind clamping) to the previous chapter button so it follows the common media-player convention: when you’re mid-chapter, the first back action restarts the current chapter; when you’re already near the start, it steps to the previous chapter.
Changes:
- Added
PlayableItem.chapterStartThresholdandPlayableItem.isNearChapterStartto centralize the “near chapter start” calculation. - Updated
PlayerManager.skipToPreviousChapter()to restart the current chapter when not near its start, otherwise step to the previous chapter (or previous item).
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| Shared/CoreData/Lightweight-Models/PlayableItem.swift | Centralizes the chapter-start threshold logic and reuses it for rewind clamping. |
| BookPlayer/Player/PlayerManager.swift | Applies the threshold logic to the previous-chapter button behavior and ensures progress notifications are posted on all paths. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
|
||
| /// Seconds from a chapter's start within which the playhead is still considered to be "at the start". | ||
| /// Shared by rewind clamping and the previous-chapter button so they stay in lockstep. | ||
| public static let chapterStartThreshold: TimeInterval = 3 |
Comment on lines
+803
to
811
| if !currentItem.isNearChapterStart { | ||
| /// First press while into the chapter goes back to its start | ||
| jumpToChapter(currentChapter) | ||
| } else if let previousChapter = currentItem.previousChapter(before: currentChapter) { | ||
| /// Already near the chapter start, so step back to the previous chapter | ||
| jumpToChapter(previousChapter) | ||
| } else { | ||
| playPreviousItem() | ||
| } |
Comment on lines
793
to
809
| func skipToPreviousChapter() { | ||
| if let currentChapter = currentItem?.currentChapter, | ||
| let previousChapter = currentItem?.previousChapter(before: currentChapter) | ||
| { | ||
| defer { NotificationCenter.default.post(name: .listeningProgressChanged, object: nil) } | ||
|
|
||
| guard let currentItem, | ||
| let currentChapter = currentItem.currentChapter | ||
| else { | ||
| playPreviousItem() | ||
| return | ||
| } | ||
|
|
||
| if !currentItem.isNearChapterStart { | ||
| /// First press while into the chapter goes back to its start | ||
| jumpToChapter(currentChapter) | ||
| } else if let previousChapter = currentItem.previousChapter(before: currentChapter) { | ||
| /// Already near the chapter start, so step back to the previous chapter | ||
| jumpToChapter(previousChapter) | ||
| } else { |
Comment on lines
+241
to
+252
| func testSkipToPreviousChapterMidChapterRestartsCurrentChapter() { | ||
| let item = generateChapteredItem() | ||
| item.currentChapter = item.chapters[1] // chapter 2, starts at 51 | ||
| item.currentTime = 100 // well past the start threshold | ||
| sut.currentItem = item | ||
|
|
||
| sut.skipToPreviousChapter() | ||
|
|
||
| // Restarts the current chapter rather than stepping back | ||
| XCTAssertEqual(sut.currentItem?.currentChapter?.index, 2) | ||
| XCTAssertEqual(playbackServiceMock.getPlayableItemBeforeParentFolderCallsCount, 0) | ||
| } |
Comment on lines
+254
to
+265
| func testSkipToPreviousChapterNearStartStepsToPreviousChapter() { | ||
| let item = generateChapteredItem() | ||
| item.currentChapter = item.chapters[1] // chapter 2, starts at 51 | ||
| item.currentTime = 52 // within the start threshold | ||
| sut.currentItem = item | ||
|
|
||
| sut.skipToPreviousChapter() | ||
|
|
||
| // Steps back to the previous chapter | ||
| XCTAssertEqual(sut.currentItem?.currentChapter?.index, 1) | ||
| XCTAssertEqual(playbackServiceMock.getPlayableItemBeforeParentFolderCallsCount, 0) | ||
| } |
Comment on lines
+267
to
+278
| func testSkipToPreviousChapterFirstChapterMidChapterRestartsInstead() { | ||
| let item = generateChapteredItem() | ||
| item.currentChapter = item.chapters[0] // chapter 1, starts at 0 | ||
| item.currentTime = 30 // mid-chapter, past the threshold | ||
| sut.currentItem = item | ||
|
|
||
| sut.skipToPreviousChapter() | ||
|
|
||
| // Restarts chapter 1 instead of jumping to the previous item | ||
| XCTAssertEqual(sut.currentItem?.currentChapter?.index, 1) | ||
| XCTAssertEqual(playbackServiceMock.getPlayableItemBeforeParentFolderCallsCount, 0) | ||
| } |
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.
Purpose
Related tasks
#1549