-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathFeedbackStyle+Toolkit.swift
111 lines (96 loc) · 2.61 KB
/
FeedbackStyle+Toolkit.swift
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
//
// FeedbackStyle+Toolkit.swift
//
// Made with ❤️ by Novum
//
// Copyright © Telefonica. All rights reserved.
//
import Foundation
import Lottie
import UIKit
@frozen
public enum FeedbackStyle: Equatable {
case success
case informative
case error
case feedback(icon: UIImage)
}
enum FeedbackIconStyle {
case none
case asset(UIImage)
case animation(Lottie.LottieAnimation)
}
extension FeedbackStyle {
var shouldUseInverseFeedbacks: Bool {
switch self {
case .success:
return true
case .informative, .error, .feedback:
return false
}
}
var feedbackTextPrimary: UIColor {
shouldUseInverseFeedbacks ? .textPrimaryInverse : .textPrimary
}
var feedbackTextSecondary: UIColor {
shouldUseInverseFeedbacks ? .textPrimaryInverse : .textSecondary
}
var feedbackPrimary: Button.Style {
shouldUseInverseFeedbacks ? .primaryInverse : .primary
}
var feedbackSecondary: Button.Style {
shouldUseInverseFeedbacks ? .secondaryInverse : .secondary
}
var feedbackLink: Button.Style {
shouldUseInverseFeedbacks ? .linkInverse : .link
}
var shouldAnimate: Bool {
animation != nil
}
var iconStyle: FeedbackIconStyle {
switch self {
case .success, .error:
guard let animation = animation else { return .none }
return .animation(animation)
case .informative:
guard let iconNotificationInfo = UIImage.iconNotificationInfo?.withRenderingMode(.alwaysTemplate) else { return .none }
return .asset(iconNotificationInfo)
case .feedback(let icon):
return .asset(icon)
}
}
private var animation: Lottie.LottieAnimation? {
switch self {
case .success:
return NSDataAsset.successAnimation.lottieAnimation
case .error:
return NSDataAsset.errorAnimation.lottieAnimation
case .informative, .feedback:
return nil
}
}
var hapticFeedbackStyle: UINotificationFeedbackGenerator.FeedbackType? {
switch self {
case .success:
return .success
case .informative:
return .warning
case .error:
return .error
case .feedback:
return nil
}
}
var hapticFeedbackDelay: TimeInterval? {
switch self {
case .success:
return 0.45
case .informative:
return 0
case .error:
return 0.5
case .feedback:
return nil
}
}
}