Skip to content

Commit

Permalink
Hide from none iOS platform
Browse files Browse the repository at this point in the history
  • Loading branch information
s-faychatelard committed Feb 1, 2024
1 parent 587b8c5 commit 47eb434
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions Sources/AvailableHapticFeedback/AvailableHapticFeedback.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,26 @@ open class AvailableHapticFeedback {
}

open func prepare() {
#if os(iOS)
if #available(iOS 10.0, *) {
feedbackGenerator.prepare()
}
#endif
}

open func generateFeedback() {
#if os(iOS)
if #available(iOS 10.0, *) {
feedbackGenerator.generate(style: style)
}
#endif
}

open func end() {
_anyFeedbackGenerator = nil
}

#if os(iOS)
@available(iOS 10.0, *)
var feedbackGenerator: UIFeedbackGenerator & AvailableHapticFeedbackGenerator {
if nil == _anyFeedbackGenerator {
Expand All @@ -45,11 +50,13 @@ open class AvailableHapticFeedback {
return _anyFeedbackGenerator! as! UIFeedbackGenerator & AvailableHapticFeedbackGenerator
// swiftlint:enable force_cast force_unwrapping
}
#endif

private var _anyFeedbackGenerator: Any?

@available(iOS 10.0, *)
private func createFeedbackGenerator() {
#if os(iOS)
switch style {
case .selection:
_anyFeedbackGenerator = UISelectionFeedbackGenerator()
Expand All @@ -62,13 +69,15 @@ open class AvailableHapticFeedback {
case .notificationSuccess, .notificationWarning, .notificationError:
_anyFeedbackGenerator = UINotificationFeedbackGenerator()
}
#endif
}
}

protocol AvailableHapticFeedbackGenerator {
func generate(style: AvailableHapticFeedback.Style)
}

#if os(iOS)
@available(iOS 10.0, *)
extension UISelectionFeedbackGenerator: AvailableHapticFeedbackGenerator {
func generate(style: AvailableHapticFeedback.Style) {
Expand Down Expand Up @@ -98,3 +107,4 @@ extension UINotificationFeedbackGenerator: AvailableHapticFeedbackGenerator {
notificationOccurred(notificationFeedbackType)
}
}
#endif

0 comments on commit 47eb434

Please sign in to comment.