Skip to content

Commit f2dc8ae

Browse files
authored
Merge pull request #201 from mattrubin/prevent-haptic-overload
Prevent haptic overload
2 parents 949345c + c18d16a commit f2dc8ae

File tree

2 files changed

+18
-13
lines changed

2 files changed

+18
-13
lines changed

Authenticator/Source/AppController.swift

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -166,21 +166,11 @@ class AppController {
166166

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

176171
case let .showSuccessMessage(message):
177172
SVProgressHUD.showSuccess(withStatus: message)
178-
179-
// Provide haptic feedback
180-
if #available(iOS 10.0, *) {
181-
let feedbackGenerator = UINotificationFeedbackGenerator()
182-
feedbackGenerator.notificationOccurred(.success)
183-
}
173+
generateHapticFeedback(for: .success)
184174

185175
case .showApplicationSettings:
186176
guard let applicationSettingsURL = URL(string: UIApplicationOpenSettingsURLString) else {
@@ -208,6 +198,13 @@ class AppController {
208198
return topViewController(presentedFrom: presentedViewController)
209199
}
210200

201+
private func generateHapticFeedback(for notificationFeedbackType: UINotificationFeedbackType) {
202+
if #available(iOS 10.0, *) {
203+
let feedbackGenerator = UINotificationFeedbackGenerator()
204+
feedbackGenerator.notificationOccurred(notificationFeedbackType)
205+
}
206+
}
207+
211208
// MARK: - Public
212209

213210
var rootViewController: UIViewController {

Authenticator/Source/TokenScannerViewController.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,10 +184,18 @@ final class TokenScannerViewController: UIViewController, QRScannerDelegate {
184184

185185
// MARK: QRScannerDelegate
186186

187+
private var lastScanTime = Date(timeIntervalSince1970: 0)
188+
private let minimumScanInterval: TimeInterval = 1
189+
187190
func handleDecodedText(_ text: String) {
188191
guard viewModel.isScanning else {
189192
return
190193
}
191-
dispatchAction(.scannerDecodedText(text))
194+
195+
let now = Date()
196+
if now.timeIntervalSince(lastScanTime) > minimumScanInterval {
197+
lastScanTime = now
198+
dispatchAction(.scannerDecodedText(text))
199+
}
192200
}
193201
}

0 commit comments

Comments
 (0)