Code fixes and added accessibility.

This commit is contained in:
Lekshmi S 2020-09-08 20:25:46 +05:30
parent 04bbf58e6a
commit 06df235fb2
4 changed files with 53 additions and 10 deletions

View File

@ -8,7 +8,8 @@
import UIKit
open class Heart: Control {
@objcMembers open class Heart: Control {
//--------------------------------------------------
// MARK: - Properties
//--------------------------------------------------
@ -16,6 +17,7 @@ open class Heart: Control {
@objc public override var isSelected: Bool {
didSet {
heartModel?.isActive = isSelected
updateAccessibilityLabel()
}
}
public var delegateObject: MVMCoreUIDelegateObject?
@ -24,9 +26,15 @@ open class Heart: Control {
}
//--------------------------------------------------
// MARK: - Lifecycle
// MARK: - Constraints
//--------------------------------------------------
open override func draw(_ layer: CALayer, in ctx: CGContext) {
public var widthConstraint: NSLayoutConstraint?
public var heightConstraint: NSLayoutConstraint?
//------------------------------------------------------
// MARK: - State Handling
//------------------------------------------------------
open override func draw(_ rect: CGRect) {
//Draw the heart
heartLayer?.removeFromSuperlayer()
let heart = drawHeart()
@ -36,7 +44,7 @@ open class Heart: Control {
func drawHeart() -> CAShapeLayer {
let heart = CAShapeLayer()
let rect = CGRect(x: 0, y: 0, width: 22, height: 22)
let rect = CGRect(x: 0, y: 0, width: 10, height: 10)
let leftArc = rect.width * 0.4
let rightArc = rect.height * 0.3
let arcRadius = sqrt(leftArc*leftArc + rightArc*rightArc)/2
@ -60,15 +68,19 @@ open class Heart: Control {
}
//--------------------------------------------------
// MARK: - MVMViewProtocol
// MARK: - Lifecycle
//--------------------------------------------------
open override func setupView() {
super.setupView()
}
open override func updateView(_ size: CGFloat) {
super.updateView(size)
layer.setNeedsDisplay()
addTarget(self, action: #selector(tapAction), for: .touchUpInside)
widthConstraint = widthAnchor.constraint(equalToConstant: 10)
widthConstraint?.isActive = true
heightConstraint = heightAnchor.constraint(equalTo: widthAnchor, multiplier: 1)
heightConstraint?.isActive = true
isAccessibilityElement = true
accessibilityHint = MVMCoreUIUtility.hardcodedString(withKey: "heart_action_hint")
accessibilityTraits = .button
updateAccessibilityLabel()
}
public override func set(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable: Any]?) {
@ -77,6 +89,22 @@ open class Heart: Control {
guard let model = model as? HeartModel else { return }
isSelected = model.isActive
}
//--------------------------------------------------
// MARK: - Methods
//--------------------------------------------------
/// Adjust accessibility label based on selection of Heart.
func updateAccessibilityLabel() {
if let message = MVMCoreUIUtility.hardcodedString(withKey: "heart"),
let selectedState = MVMCoreUIUtility.hardcodedString(withKey: isSelected ? "heart_selected_state" : "heart_not_selected_state") {
accessibilityLabel = message + selectedState
}
}
func tapAction() {
isSelected = !isSelected
setNeedsDisplay()
}
}
extension Int {

View File

@ -67,6 +67,11 @@
"AccOff" = "off";
"AccToggleHint" = "double tap to toggle";
// MARK: Heart
"heart" = "Heart";
"heart_action_hint" = "Double tap to select";
"heart_selected_state" = "Selected";
"heart_not_selected_state" = "Not Selected";
// MARK: Carousel
"MVMCoreUIPageControl_currentpage_index" = "page %@ of %d";

View File

@ -49,6 +49,10 @@
"AccOn" = "encendido";
"AccOff" = "apagado";
"AccToggleHint" = "toca dos veces para alternar";
// Heart
"heart_action_hint" = "Toca dos veces para seleccionar.";
"heart_selected_state" = "Seleccionado";
"heart_not_selected_state" = "No Seleccionado";
// Carousel
"MVMCoreUIPageControl_currentpage_index" = "página %@ de %d";
"MVMCoreUIPageControlslides_currentpage_index" = "diapositiva %@ of %d";

View File

@ -51,6 +51,12 @@
"AccOn" = "encendido";
"AccOff" = "apagado";
"AccToggleHint" = "toca dos veces para alternar";
// Heart
"heart_action_hint" = "Toca dos veces para seleccionar.";
"heart_selected_state" = "Seleccionado";
"heart_not_selected_state" = "No Seleccionado";
// Carousel
"MVMCoreUIPageControl_currentpage_index" = "página %@ de %d";
"MVMCoreUIPageControlslides_currentpage_index" = "diapositiva %@ of %d";