// // ArticleExtractorButton.swift // NetNewsWire-iOS // // Created by Maurice Parker on 9/24/19. // Copyright © 2019 Ranchero Software. All rights reserved. // import UIKit enum ArticleExtractorButtonState { case error case animated case on case off } final class ArticleExtractorButton: UIButton { private let activityIndicator: UIActivityIndicatorView = { let indicator = UIActivityIndicatorView(style: .medium) indicator.hidesWhenStopped = true indicator.translatesAutoresizingMaskIntoConstraints = false return indicator }() // Re-assert the visuals even when the state hasn't changed, so a stale button self-heals. var buttonState: ArticleExtractorButtonState = .off { didSet { switch buttonState { case .error: activityIndicator.stopAnimating() isUserInteractionEnabled = true setImage(Assets.Images.articleExtractorError, for: .normal) case .animated: setImage(nil, for: .normal) activityIndicator.startAnimating() isUserInteractionEnabled = false case .on: activityIndicator.stopAnimating() isUserInteractionEnabled = true setImage(Assets.Images.articleExtractorOn, for: .normal) case .off: activityIndicator.stopAnimating() isUserInteractionEnabled = true setImage(Assets.Images.articleExtractorOff, for: .normal) } } } override var accessibilityLabel: String? { get { switch buttonState { case .error: return NSLocalizedString("Error - Reader View", comment: "Error - Reader View") case .animated: return NSLocalizedString("Processing - Reader View", comment: "Processing - Reader View") case .on: return NSLocalizedString("Selected - Reader View", comment: "Selected - Reader View") case .off: return NSLocalizedString("Reader View", comment: "Reader View") } } set { super.accessibilityLabel = newValue } } override init(frame: CGRect) { super.init(frame: frame) commonInit() } required init?(coder: NSCoder) { super.init(coder: coder) commonInit() } override func point(inside point: CGPoint, with event: UIEvent?) -> Bool { // Expand the hit area to cover the Liquid Glass lozenge gap. // Extend to the left edge of the lozenge and to the midpoint // between this button and its neighbor on the right, // and extend to the top and bottom of the lozenge. let expandedBounds = bounds.insetBy(dx: -20, dy: -20) return expandedBounds.contains(point) } private func commonInit() { addSubview(activityIndicator) NSLayoutConstraint.activate([ widthAnchor.constraint(equalToConstant: 44.0), heightAnchor.constraint(equalToConstant: 44.0), activityIndicator.centerXAnchor.constraint(equalTo: centerXAnchor), activityIndicator.centerYAnchor.constraint(equalTo: centerYAnchor) ]) } }