Skip to content

Commit

Permalink
Extend sleep timer when device is shaked (#126)
Browse files Browse the repository at this point in the history
  • Loading branch information
rasmuslos committed Oct 2, 2024
1 parent a8bfc7b commit 6724b36
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Multiplatform/Intents/PlayMediaIntentHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ internal extension PlayMediaIntentHandler {
}

func resolveResumePlayback(for intent: INPlayMediaIntent) async -> INBooleanResolutionResult {
return .success(with: true)
.success(with: true)
}

func resolvePlaybackRepeatMode(for intent: INPlayMediaIntent) async -> INPlaybackRepeatModeResolutionResult {
Expand Down
8 changes: 8 additions & 0 deletions Multiplatform/NowPlaying/ViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,14 @@ private extension NowPlaying.ViewModel {
}
})

tokens.append(NotificationCenter.default.addObserver(forName: UIWindow.deviceDidShakeNotification, object: nil, queue: nil) { _ in
guard Defaults[.shakeExtendsSleepTimer] else {
return
}

SleepTimer.shared.extend()
})

Task {
for await skipForwardsInterval in Defaults.updates(.skipForwardsInterval) {
await MainActor.withAnimation {
Expand Down
12 changes: 11 additions & 1 deletion Multiplatform/Settings.bundle/Root.plist
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
<key>Title</key>
<string>Playback</string>
<key>FooterText</key>
<string>The Sleep->Timer will be extended if you press play up to 10 seconds after it expires.
<string>The Sleep-&gt;Timer will be extended if you press play up to 10 seconds after it expires.
Smart rewind will skip back ten seconds when playback was inactive for longer than ten minutes.</string>
</dict>
<dict>
Expand Down Expand Up @@ -117,6 +117,16 @@ Smart rewind will skip back ten seconds when playback was inactive for longer th
<key>DefaultValue</key>
<true/>
</dict>
<dict>
<key>Type</key>
<string>PSToggleSwitchSpecifier</string>
<key>Title</key>
<string>Shake extends Sleep-Timer</string>
<key>Key</key>
<string>shakeExtendsSleepTimer</string>
<key>DefaultValue</key>
<true/>
</dict>
<dict>
<key>Type</key>
<string>PSToggleSwitchSpecifier</string>
Expand Down
2 changes: 2 additions & 0 deletions Multiplatform/Utility/Extensions/Defaults+Keys.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ internal extension Defaults.Keys {
static let showAuthorsRow = Key("showAuthorsRow", default: false)
static let disableDiscoverRow = Key("disableDiscoverRow", default: false)

static let shakeExtendsSleepTimer = Key("shakeExtendsSleepTimer", default: true)

// MARK: Filter & sort utility

static let audiobooksSortOrder = Key<AudiobookSortOrder>("audiobooksSortOrder", default: .added)
Expand Down
19 changes: 19 additions & 0 deletions Multiplatform/Utility/Extensions/UIWindow+Shake.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// UIWindow+Shake.swift
// Multiplatform
//
// Created by Rasmus Krämer on 02.10.24.
//

import Foundation
import UIKit

extension UIWindow {
open override func motionEnded(_ motion: UIEvent.EventSubtype, with event: UIEvent?) {
if motion == .motionShake {
NotificationCenter.default.post(name: Self.deviceDidShakeNotification, object: nil)
}
}

static let deviceDidShakeNotification = Notification.Name(rawValue: "io.rfk.shelfPlayer.shake")
}
4 changes: 4 additions & 0 deletions ShelfPlayer.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
3A79A9692AE53555006B61FC /* DownloadIndicator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3A79A9682AE53555006B61FC /* DownloadIndicator.swift */; };
3A7A9C972BE613E200701D58 /* Navigation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3A7A9C962BE613E200701D58 /* Navigation.swift */; };
3A7A9C992BE6177D00701D58 /* NowPlaying.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3A7A9C982BE6177D00701D58 /* NowPlaying.swift */; };
3A7F84A52CAD1F4A00A51EEE /* UIWindow+Shake.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3A7F84A42CAD1F4600A51EEE /* UIWindow+Shake.swift */; };
3A82864D2AEC104400384BC9 /* SleepTimerButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3A82864C2AEC104400384BC9 /* SleepTimerButton.swift */; };
3A83B8732B8893E000A05957 /* CarPlayDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3A83B8722B8893E000A05957 /* CarPlayDelegate.swift */; };
3A853FAF2AD03FA600FACAF6 /* AuthorView+Header.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3A853FAE2AD03FA600FACAF6 /* AuthorView+Header.swift */; };
Expand Down Expand Up @@ -262,6 +263,7 @@
3A79A9682AE53555006B61FC /* DownloadIndicator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DownloadIndicator.swift; sourceTree = "<group>"; };
3A7A9C962BE613E200701D58 /* Navigation.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Navigation.swift; sourceTree = "<group>"; };
3A7A9C982BE6177D00701D58 /* NowPlaying.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NowPlaying.swift; sourceTree = "<group>"; };
3A7F84A42CAD1F4600A51EEE /* UIWindow+Shake.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIWindow+Shake.swift"; sourceTree = "<group>"; };
3A82864B2AEC0DD800384BC9 /* README.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = "<group>"; };
3A82864C2AEC104400384BC9 /* SleepTimerButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SleepTimerButton.swift; sourceTree = "<group>"; };
3A83B8722B8893E000A05957 /* CarPlayDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CarPlayDelegate.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -493,6 +495,7 @@
3A012E262C806B5D0073A619 /* Double+Format.swift */,
3AEFA9522B8BB3CA00220CA3 /* String+Random.swift */,
3A0F99F12BE8C08700F6B7B4 /* UIScreen+Radius.swift */,
3A7F84A42CAD1F4600A51EEE /* UIWindow+Shake.swift */,
3AA097862C7C5E0600EA32D7 /* Environment+Keys.swift */,
3A012E282C806D970073A619 /* View+ReverseMask.swift */,
3A0620922AF7F96A004126D1 /* LocalizedStringKey+Key.swift */,
Expand Down Expand Up @@ -947,6 +950,7 @@
3AE31DCE2B4B3C310092DF36 /* AudiobookAuthorsPanel.swift in Sources */,
3A9824722B6EB0DE0098C06A /* OfflinePodcastView.swift in Sources */,
3A48D2512AD0661500991139 /* SelectLibraryModifier.swift in Sources */,
3A7F84A52CAD1F4A00A51EEE /* UIWindow+Shake.swift in Sources */,
3A98246E2B6EAC7F0098C06A /* ProgressIndicator.swift in Sources */,
3A9BC7252CA44CEE00D085C4 /* RegularModifier.swift in Sources */,
3AB8BBBD2BD7B7290012023A /* PodcastHomePanel.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,11 @@ public final class SleepTimer {

switch lastSetting {
case .time(let interval):
expiresAt = .now().advanced(by: .seconds(Int(interval)))
if let expiresAt {
self.expiresAt = expiresAt.advanced(by: .seconds(Int(interval)))
} else {
expiresAt = .now().advanced(by: .seconds(Int(interval)))
}
case .chapterEnd:
expiresAtChapterEnd = true
}
Expand Down

0 comments on commit 6724b36

Please sign in to comment.