Skip to content

Commit 57d31bd

Browse files
authored
Add haptic feedback (#1485)
* Add haptic feedback Enhances user experience by providing tactile responses for different button actions. * refactor
1 parent fd8594b commit 57d31bd

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

Mastodon/In Progress New Layout and Datamodel/Common Components/Views/StatefulActionButton.swift

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,33 @@ struct StatefulCountedActionButton: View {
3838
private let iconFont: Font = .body
3939

4040
var body: some View {
41-
Button(action: { doAction?() }) {
41+
Button {
42+
if type == .reply {
43+
// Immediate haptic feedback on tap rather than waiting for the state changes
44+
let generator = UIImpactFeedbackGenerator(style: .light)
45+
generator.impactOccurred()
46+
}
47+
doAction?()
48+
} label: {
4249
HStack(spacing: 4) {
4350
imageComponent
4451
countLabelComponent
4552
}
4653
}
54+
.sensoryFeedback({ () -> SensoryFeedback in
55+
switch actionState.isSelected {
56+
case .isTrue:
57+
switch type {
58+
case .boost: return .impact(weight: .heavy)
59+
case .favourite: return .impact(weight: .medium)
60+
case .reply, .bookmark: return .impact(weight: .light)
61+
}
62+
case .isFalse:
63+
return .impact(weight: .light)
64+
default:
65+
return .impact(weight: .light)
66+
}
67+
}(), trigger: actionState.isSelected)
4768
.buttonStyle(.borderless) // Without this, all the buttons in the row activate when one is tapped. What a remarkably unexpected result with no documentation.
4869
.fontWeight(actionState.isSelected == .isTrue ? .semibold : .regular)
4970
.foregroundStyle(color)
@@ -100,4 +121,3 @@ struct StatefulCountedActionButton: View {
100121
}
101122

102123
}
103-

0 commit comments

Comments
 (0)