-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathHaptic.swift
More file actions
66 lines (53 loc) · 1.62 KB
/
Copy pathHaptic.swift
File metadata and controls
66 lines (53 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
//
// Haptic.swift
// nRF-Connect
// iOSCommonLibraries
//
// Created by Dinesh Harjani on 11/02/2019.
// Created by Dinesh Harjani on 21/04/2026.
// Copyright © 2026 Nordic Semiconductor. All rights reserved.
//
#if os(iOS) || targetEnvironment(macCatalyst)
import UIKit
// MARK: - Haptic
public struct Haptic {
// MARK: private init
private init() {}
// MARK: Feedback
public enum Feedback {
case error
case success
case selectionChanged
case cancel
// MARK: callAsFunction()
@MainActor
static let feedbackGenerator = UINotificationFeedbackGenerator()
@MainActor
static let selectionGenerator = UISelectionFeedbackGenerator()
@MainActor
fileprivate func callAsFunction() {
switch self {
case .error:
Self.feedbackGenerator.prepare()
Self.feedbackGenerator.notificationOccurred(.error)
case .success:
Self.feedbackGenerator.prepare()
Self.feedbackGenerator.notificationOccurred(.success)
case .selectionChanged:
Self.selectionGenerator.prepare()
Self.selectionGenerator.selectionChanged()
case .cancel:
Self.feedbackGenerator.prepare()
Self.feedbackGenerator.notificationOccurred(.warning)
}
}
}
// MARK: generate
nonisolated
public static func generate(feedbackFor feedback: Haptic.Feedback) {
Task { @MainActor in
feedback()
}
}
}
#endif