Skip to content

Commit

Permalink
[Accessibility] Make Rating Input (UIKit) adjustable to enable the ra…
Browse files Browse the repository at this point in the history
…ting
  • Loading branch information
xavier-daleau authored and michael-zimmermann committed May 31, 2024
1 parent 0100cc3 commit 5bba58d
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions core/Sources/Components/Rating/View/UIKit/RatingInputUIView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ public final class RatingInputUIView: UIControl {
self.setupView()
self.enableTouch()
self.addPanGestureToPreventCancelTracking()
self.setupAccessibility()
}

required init?(coder: NSCoder) {
Expand Down Expand Up @@ -148,6 +149,27 @@ public final class RatingInputUIView: UIControl {
self.accessibilityIdentifier = RatingInputAccessibilityIdentifier.identifier
}

// MARK: - Accessibility
private func setupAccessibility() {
self.isAccessibilityElement = true
self.accessibilityTraits = .adjustable
self.updateAccessibilityValue()
}

public override func accessibilityIncrement() {
let incrementedRating = min(self.rating + 1, CGFloat(self.ratingDisplay.count.rawValue))
self.ratingStarSelected(Int(incrementedRating) - 1)
}

public override func accessibilityDecrement() {
let decrementedRating = max(self.rating - 1, 1)
self.ratingStarSelected(Int(decrementedRating) - 1)
}

private func updateAccessibilityValue() {
self.accessibilityValue = "\(Int(self.rating))/\(self.ratingDisplay.count.rawValue)"
}

// MARK: - Handling touch actions
private func handleTouch(_ touch: UITouch, with event: UIEvent?) -> Bool {

Expand Down Expand Up @@ -183,6 +205,7 @@ public final class RatingInputUIView: UIControl {
self.subject.send(rating)
self.sendActions(for: .valueChanged)
self.delegate?.rating(self, didChangeRating: rating)
self.updateAccessibilityValue()
}

private func ratingStarHighlighted(_ index: Int) {
Expand Down

0 comments on commit 5bba58d

Please sign in to comment.