diff --git a/BookPlayer/Base.lproj/Localizable.strings b/BookPlayer/Base.lproj/Localizable.strings index 2c5c8782..7dc533b6 100644 --- a/BookPlayer/Base.lproj/Localizable.strings +++ b/BookPlayer/Base.lproj/Localizable.strings @@ -202,8 +202,6 @@ "error_loading_chapters" = "Chapters couldn't be loaded from: \n%@"; "error_empty_chapters" = "Chapters are empty, please verify the contents of the folder: %@"; "sleeptimer_option_custom" = "Custom"; -"sleeptimer_option_sticky_on" = "Sticky ✓"; -"sleeptimer_option_sticky_off" = "Sticky"; "sleeptimer_custom_alert_title" = "Custom sleep timer"; "settings_progresslabels_title" = "Progress Labels"; "settings_playerinterface_list_title" = "Opens"; @@ -305,3 +303,5 @@ We're working hard on providing a seamless experience, if possible, please conta "storage_artwork_cache_title" = "Artwork cache size"; "settings_share_debug_information" = "Share debug information"; "settings_autlock_section_title" = "Autolock"; +"settings_sleeptimer_auto_title" = "Auto Sleep Timer"; +"settings_sleeptimer_auto_description" = "Restart the last active sleep timer when playback is resumed"; diff --git a/BookPlayer/Player/Player Screen/PlayerViewModel.swift b/BookPlayer/Player/Player Screen/PlayerViewModel.swift index 8456e1e2..b39b6db5 100755 --- a/BookPlayer/Player/Player Screen/PlayerViewModel.swift +++ b/BookPlayer/Player/Player Screen/PlayerViewModel.swift @@ -395,23 +395,7 @@ class PlayerViewModel: ViewModelProtocol { ) ) - let formatter = DateComponentsFormatter() - - formatter.unitsStyle = .full - formatter.allowedUnits = [.hour, .minute] - - for interval in SleepTimer.shared.intervals { - guard let formattedDuration = formatter.string(from: interval) else { continue } - - actions.append( - BPActionItem( - title: String.localizedStringWithFormat("sleep_interval_title".localized, formattedDuration), - handler: { - SleepTimer.shared.setTimer(.countdown(interval)) - } - ) - ) - } + actions.append(contentsOf: getCountdownActions()) actions.append( BPActionItem( @@ -431,21 +415,6 @@ class PlayerViewModel: ViewModelProtocol { ) ) - let stickyAction = SleepTimer.shared.getSticky() - ? BPActionItem( - title: "sleeptimer_option_sticky_on".localized, - handler: { - SleepTimer.shared.setSticky(stickyState: false) - } - ) - : BPActionItem( - title: "sleeptimer_option_sticky_off".localized, - handler: { - SleepTimer.shared.setSticky(stickyState: true) - } - ) - - actions.append(stickyAction) actions.append(BPActionItem.cancelAction) sendEvent(.sleepTimerAlert( @@ -488,6 +457,23 @@ class PlayerViewModel: ViewModelProtocol { return storedTime } + + private func getCountdownActions() -> [BPActionItem] { + let formatter = DateComponentsFormatter() + formatter.unitsStyle = .full + formatter.allowedUnits = [.hour, .minute] + + return SleepTimer.shared.intervals.compactMap { interval in + guard let formattedDuration = formatter.string(from: interval) else { return nil } + + return BPActionItem( + title: String.localizedStringWithFormat("sleep_interval_title".localized, formattedDuration), + handler: { + SleepTimer.shared.setTimer(.countdown(interval)) + } + ) + } + } } extension PlayerViewModel { diff --git a/BookPlayer/Player/PlayerManager.swift b/BookPlayer/Player/PlayerManager.swift index abdf05fd..875c4784 100755 --- a/BookPlayer/Player/PlayerManager.swift +++ b/BookPlayer/Player/PlayerManager.swift @@ -739,8 +739,8 @@ extension PlayerManager { handleSmartRewind(currentItem) - handleStickyTimer() - + handleAutoTimer() + fadeTimer?.invalidate() shakeMotionService.stopMotionUpdates() boostVolume = UserDefaults.standard.bool(forKey: Constants.UserDefaults.boostVolumeEnabled) @@ -775,10 +775,10 @@ extension PlayerManager { } } - func handleStickyTimer() { - if SleepTimer.shared.getSticky() { - SleepTimer.shared.restartTimer() - } + func handleAutoTimer() { + guard UserDefaults.standard.bool(forKey: Constants.UserDefaults.autoTimerEnabled) else { return } + + SleepTimer.shared.restartTimer() } func setSpeed(_ newValue: Float) { diff --git a/BookPlayer/Player/SleepTimer.swift b/BookPlayer/Player/SleepTimer.swift index e0b44430..9deedb2a 100644 --- a/BookPlayer/Player/SleepTimer.swift +++ b/BookPlayer/Player/SleepTimer.swift @@ -39,8 +39,6 @@ final class SleepTimer { 1800.0, 3600.0 ] - - public var sticky = false /// Publisher when the countdown timer reaches the defined threshold public var countDownThresholdPublisher = PassthroughSubject() @@ -92,25 +90,17 @@ final class SleepTimer { } // MARK: Public methods - - public func setSticky(stickyState: Bool) { - sticky = stickyState - } - - public func getSticky() -> Bool { - return sticky - } public func setTimer(_ newState: SleepTimerState) { /// Always cancel any ongoing timer reset() state = newState - lastActiveState = newState switch newState { case .off: donateTimerIntent(with: .cancel) case .countdown(let interval): + lastActiveState = newState if let option = TimeParser.getTimerOption(from: interval) { donateTimerIntent(with: option) } @@ -120,11 +110,12 @@ final class SleepTimer { self?.update() } case .endOfChapter: + lastActiveState = newState donateTimerIntent(with: .endChapter) NotificationCenter.default.addObserver(self, selector: #selector(self.end), name: .chapterChange, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(self.end), name: .bookEnd, object: nil) } - } + } public func restartTimer() { setTimer(lastActiveState) diff --git a/BookPlayer/Settings/Base.lproj/Settings.storyboard b/BookPlayer/Settings/Base.lproj/Settings.storyboard index 5d3e4301..dbfce254 100644 --- a/BookPlayer/Settings/Base.lproj/Settings.storyboard +++ b/BookPlayer/Settings/Base.lproj/Settings.storyboard @@ -294,7 +294,7 @@ SKAN is Apple's privacy-first attribution solution that enables measuring how effective a campaign was without compromising the user privacy - + @@ -1998,12 +1998,51 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Doubles the volume. Use with caution and care for your hearing. - + @@ -2042,7 +2081,7 @@ Use with caution and care for your hearing. - + @@ -2081,7 +2120,7 @@ Use with caution and care for your hearing. - + @@ -2117,7 +2156,7 @@ Use with caution and care for your hearing. Toggle between displaying the remaining time, total duration and progress of either the chapter or the book in the player screen - + @@ -2152,19 +2191,19 @@ Use with caution and care for your hearing. - + - + -