Skip to content

Commit

Permalink
Minor tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
eonist committed Jan 23, 2025
1 parent 5203c44 commit 60a16ff
Showing 1 changed file with 67 additions and 3 deletions.
70 changes: 67 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,70 @@ Haptic.trigger(.extracted)
.package(url: "https://github.com/sentryco/HapticFeedback", branch: "main")
```

## License

HapticFeedback is available under the MIT license. See the LICENSE file for more info.
## Todo:

- Implement Haptic Feedback Support on macOS

Currently, the haptic feedback functionality is only implemented for iOS, while macOS methods are stubs that do nothing. macOS supports haptic feedback via `NSHapticFeedbackManager`. By implementing haptic feedback on macOS, you can provide a consistent experience across platforms.

**Updated `HapticFeedback.swift`:**

```swift:Sources/HapticFeedback/HapticFeedback.swift
import Foundation

#if os(iOS)
import UIKit
#elseif os(macOS)
import AppKit
#endif

public class HapticFeedback {
public enum Kind {
case entry
case exit
case success
case deny
case failure
case extract
}

public static func trigger(_ kind: Kind) {
#if os(iOS)
playiOS(kind)
#elseif os(macOS)
playmacOS(kind)
#endif
}

#if os(iOS)
private static func playiOS(_ kind: Kind) {
switch kind {
case .entry:
UIImpactFeedbackGenerator(style: .medium).impactOccurred()
case .exit:
UIImpactFeedbackGenerator(style: .light).impactOccurred()
case .success:
UINotificationFeedbackGenerator().notificationOccurred(.success)
case .deny:
UINotificationFeedbackGenerator().notificationOccurred(.warning)
case .failure:
UINotificationFeedbackGenerator().notificationOccurred(.error)
case .extract:
UISelectionFeedbackGenerator().selectionChanged()
}
}
#elseif os(macOS)
private static func playmacOS(_ kind: Kind) {
let feedbackManager = NSHapticFeedbackManager.defaultPerformer
switch kind {
case .entry, .success, .extract:
feedbackManager.perform(.generic, performanceTime: .default)
case .exit:
feedbackManager.perform(.alignment, performanceTime: .default)
case .deny, .failure:
feedbackManager.perform(.levelChange, performanceTime: .default)
}
}
#endif
}
```

0 comments on commit 60a16ff

Please sign in to comment.