From 5143c2585fb42432ae1dc3534ae0286bad48d694 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rasmus=20Kr=C3=A4mer?= Date: Fri, 20 Dec 2024 10:38:35 +0100 Subject: [PATCH] Reset sleep timer --- .../SPPlayback/AudioPlayer+Playback.swift | 1 + .../SleepTimer/SleepTimer+Timer.swift | 1 + .../SPPlayback/SleepTimer/SleepTimer.swift | 17 +++++++++++++++++ 3 files changed, 19 insertions(+) diff --git a/ShelfPlayerKit/Sources/SPPlayback/AudioPlayer+Playback.swift b/ShelfPlayerKit/Sources/SPPlayback/AudioPlayer+Playback.swift index a528d5d0..34c0d5bd 100644 --- a/ShelfPlayerKit/Sources/SPPlayback/AudioPlayer+Playback.swift +++ b/ShelfPlayerKit/Sources/SPPlayback/AudioPlayer+Playback.swift @@ -57,6 +57,7 @@ public extension AudioPlayer { } audioPlayer.removeAllItems() + SleepTimer.shared.reset() NotificationCenter.default.post(name: Self.itemDidChangeNotification, object: nil) NotificationCenter.default.post(name: Self.queueDidChangeNotification, object: nil) diff --git a/ShelfPlayerKit/Sources/SPPlayback/SleepTimer/SleepTimer+Timer.swift b/ShelfPlayerKit/Sources/SPPlayback/SleepTimer/SleepTimer+Timer.swift index b23592fd..ca7b3138 100644 --- a/ShelfPlayerKit/Sources/SPPlayback/SleepTimer/SleepTimer+Timer.swift +++ b/ShelfPlayerKit/Sources/SPPlayback/SleepTimer/SleepTimer+Timer.swift @@ -10,6 +10,7 @@ import Foundation internal extension SleepTimer { func setupTimer() { guard let deadline else { + suspend() return } diff --git a/ShelfPlayerKit/Sources/SPPlayback/SleepTimer/SleepTimer.swift b/ShelfPlayerKit/Sources/SPPlayback/SleepTimer/SleepTimer.swift index c86e0fb0..363ead30 100644 --- a/ShelfPlayerKit/Sources/SPPlayback/SleepTimer/SleepTimer.swift +++ b/ShelfPlayerKit/Sources/SPPlayback/SleepTimer/SleepTimer.swift @@ -73,6 +73,14 @@ public final class SleepTimer { return } + if Defaults[.smartRewind] && !AudioPlayer.shared.playing { + if AudioPlayer.shared.chapterCurrentTime > 7 { + AudioPlayer.shared.itemCurrentTime -= 7 + } else { + AudioPlayer.shared.chapterCurrentTime = 0 + } + } + switch lastSetting { case .time(let interval): if let expiresAt { @@ -87,6 +95,15 @@ public final class SleepTimer { expiresAtChapterEnd = amount } } + + self.lastSetting = lastSetting + } + public func reset() { + expiredAt = nil + expiredAt = nil + + expiresAtChapterEnd = nil + } enum SleepTimerSetting {