Skip to content

Commit fa48007

Browse files
committed
Prevent haptic feedback from being triggered too soon after the last vibration
1 parent 949345c commit fa48007

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

Authenticator/Source/AppController.swift

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ class AppController {
5353
dispatchAction: self.handleAction
5454
)
5555
}()
56+
private var lastHapticFeedback = Date(timeIntervalSince1970: 0)
5657

5758
init() {
5859
do {
@@ -166,21 +167,11 @@ class AppController {
166167

167168
case let .showErrorMessage(message):
168169
SVProgressHUD.showError(withStatus: message)
169-
170-
// Provide haptic feedback
171-
if #available(iOS 10.0, *) {
172-
let feedbackGenerator = UINotificationFeedbackGenerator()
173-
feedbackGenerator.notificationOccurred(.error)
174-
}
170+
generateFeedback(for: .error)
175171

176172
case let .showSuccessMessage(message):
177173
SVProgressHUD.showSuccess(withStatus: message)
178-
179-
// Provide haptic feedback
180-
if #available(iOS 10.0, *) {
181-
let feedbackGenerator = UINotificationFeedbackGenerator()
182-
feedbackGenerator.notificationOccurred(.success)
183-
}
174+
generateFeedback(for: .success)
184175

185176
case .showApplicationSettings:
186177
guard let applicationSettingsURL = URL(string: UIApplicationOpenSettingsURLString) else {
@@ -208,6 +199,18 @@ class AppController {
208199
return topViewController(presentedFrom: presentedViewController)
209200
}
210201

202+
// Provide haptic feedback to accompany success and error messages
203+
private func generateFeedback(for notificationFeedbackType: UINotificationFeedbackType) {
204+
if #available(iOS 10.0, *) {
205+
let now = Date()
206+
if now.timeIntervalSince(lastHapticFeedback) > 1 {
207+
lastHapticFeedback = now
208+
let feedbackGenerator = UINotificationFeedbackGenerator()
209+
feedbackGenerator.notificationOccurred(notificationFeedbackType)
210+
}
211+
}
212+
}
213+
211214
// MARK: - Public
212215

213216
var rootViewController: UIViewController {

0 commit comments

Comments
 (0)