mirror of
https://github.com/Dimillian/IceCubesApp.git
synced 2024-09-28 22:41:55 +00:00
90ec3d419c
* Check for haptic capabilities
* Make manager do most of work
* ABC enum
* Fix spelling 😊
* Small tweak
57 lines
1.6 KiB
Swift
57 lines
1.6 KiB
Swift
import CoreHaptics
|
|
import UIKit
|
|
|
|
public class HapticManager {
|
|
public static let shared: HapticManager = .init()
|
|
|
|
public enum HapticType {
|
|
case buttonPress
|
|
case dataRefresh(intensity: CGFloat)
|
|
case notification(_ type: UINotificationFeedbackGenerator.FeedbackType)
|
|
case tabSelection
|
|
case timeline
|
|
}
|
|
|
|
private let selectionGenerator = UISelectionFeedbackGenerator()
|
|
private let impactGenerator = UIImpactFeedbackGenerator(style: .heavy)
|
|
private let notificationGenerator = UINotificationFeedbackGenerator()
|
|
|
|
private let userPreferences = UserPreferences.shared
|
|
|
|
private init() {
|
|
selectionGenerator.prepare()
|
|
impactGenerator.prepare()
|
|
}
|
|
|
|
@MainActor
|
|
public func fireHaptic(of type: HapticType) {
|
|
guard supportsHaptics else { return }
|
|
|
|
switch type {
|
|
case .buttonPress:
|
|
if userPreferences.hapticButtonPressEnabled {
|
|
impactGenerator.impactOccurred()
|
|
}
|
|
case let .dataRefresh(intensity):
|
|
if userPreferences.hapticTimelineEnabled {
|
|
impactGenerator.impactOccurred(intensity: intensity)
|
|
}
|
|
case let .notification(type):
|
|
if userPreferences.hapticButtonPressEnabled {
|
|
notificationGenerator.notificationOccurred(type)
|
|
}
|
|
case .tabSelection:
|
|
if userPreferences.hapticTabSelectionEnabled {
|
|
selectionGenerator.selectionChanged()
|
|
}
|
|
case .timeline:
|
|
if userPreferences.hapticTimelineEnabled {
|
|
selectionGenerator.selectionChanged()
|
|
}
|
|
}
|
|
}
|
|
|
|
public var supportsHaptics: Bool {
|
|
CHHapticEngine.capabilitiesForHardware().supportsHaptics
|
|
}
|
|
}
|