Skip to content

Commit 7b4928d

Browse files
authored
Add WatchKit Sensory Feedback Implementation (#672)
1 parent 027c339 commit 7b4928d

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
//
2+
// WatchKitFeedbackImplementation.swift
3+
// OpenSwiftUI
4+
//
5+
// Audited for 6.5.4
6+
// Status: Complete
7+
8+
#if os(watchOS)
9+
import WatchKit
10+
import OpenSwiftUICore
11+
12+
// MARK: - View + platformSensoryFeedback
13+
14+
extension View {
15+
nonisolated func platformSensoryFeedback<Base>(
16+
_ base: Base
17+
) -> some View where Base: SensoryFeedbackGeneratorModifier {
18+
modifier(base)
19+
}
20+
}
21+
22+
// MARK: - WatchKitFeedbackImplementation
23+
24+
struct WatchKitFeedbackImplementation: PlatformSensoryFeedback {
25+
var haptic: WKHapticType
26+
27+
func setUp() {
28+
_openSwiftUIEmptyStub()
29+
}
30+
31+
func tearDown() {
32+
_openSwiftUIEmptyStub()
33+
}
34+
35+
func generate() {
36+
WKInterfaceDevice.current().play(haptic)
37+
}
38+
}
39+
40+
// MARK: - FeedbackRequestContext
41+
42+
struct FeedbackRequestContext {
43+
func implementation(type: SensoryFeedback.FeedbackType) -> (any PlatformSensoryFeedback)? {
44+
switch type {
45+
case .success: WatchKitFeedbackImplementation(haptic: .success)
46+
case .warning: WatchKitFeedbackImplementation(haptic: .retry)
47+
case .error: WatchKitFeedbackImplementation(haptic: .failure)
48+
case .increase: WatchKitFeedbackImplementation(haptic: .directionUp)
49+
case .decrease: WatchKitFeedbackImplementation(haptic: .directionDown)
50+
case .start: WatchKitFeedbackImplementation(haptic: .start)
51+
case .stop: WatchKitFeedbackImplementation(haptic: .stop)
52+
case .selection, .impactWeight, .impactFlexibility: WatchKitFeedbackImplementation(haptic: .click)
53+
default: nil
54+
}
55+
}
56+
}
57+
#endif

0 commit comments

Comments
 (0)