diff --git a/ios/App/AudiobookshelfUnitTests/Shared/player/util/PlayerTimeUtilsTests.swift b/ios/App/AudiobookshelfUnitTests/Shared/player/util/PlayerTimeUtilsTests.swift index 0ff8702e..91bc8403 100644 --- a/ios/App/AudiobookshelfUnitTests/Shared/player/util/PlayerTimeUtilsTests.swift +++ b/ios/App/AudiobookshelfUnitTests/Shared/player/util/PlayerTimeUtilsTests.swift @@ -16,6 +16,13 @@ final class PlayerTimeUtilsTests: XCTestCase { let lastPlayedMs = threeSecondsAgo.timeIntervalSince1970 * 1000 XCTAssertEqual(PlayerTimeUtils.calcSeekBackTime(currentTime: currentTime, lastPlayedMs: lastPlayedMs), 998) } + + func testCalcSeekBackTimeWithZeroCurrentTime() { + let currentTime: Double = 0 + let threeHundredSecondsAgo = Date(timeIntervalSinceNow: -300) + let lastPlayedMs = threeHundredSecondsAgo.timeIntervalSince1970 * 1000 + XCTAssertEqual(PlayerTimeUtils.calcSeekBackTime(currentTime: currentTime, lastPlayedMs: lastPlayedMs), 0) + } func testTimeSinceLastPlayed() throws { let fiveSecondsAgo = Date(timeIntervalSinceNow: -5) diff --git a/ios/App/Shared/player/util/PlayerTimeUtils.swift b/ios/App/Shared/player/util/PlayerTimeUtils.swift index d8514e11..24fc00a1 100644 --- a/ios/App/Shared/player/util/PlayerTimeUtils.swift +++ b/ios/App/Shared/player/util/PlayerTimeUtils.swift @@ -14,7 +14,8 @@ class PlayerTimeUtils { static func calcSeekBackTime(currentTime: TimeInterval, lastPlayedMs: Double?) -> TimeInterval { let sinceLastPlayed = timeSinceLastPlayed(lastPlayedMs) let timeToSeekBack = timeToSeekBackForSinceLastPlayed(sinceLastPlayed) - return currentTime.advanced(by: -timeToSeekBack) + let currentTimeAfterSeekBack = currentTime.advanced(by: -timeToSeekBack) + return max(currentTimeAfterSeekBack, 0) } static internal func timeSinceLastPlayed(_ lastPlayedMs: Double?) -> TimeInterval? {