// // SearchAndDisplayAccessory.swift // CapCollector // // Created by Christoph on 09.10.19. // Copyright © 2019 CH. All rights reserved. // import UIKit class PassthroughView: UIView { override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? { let view = super.hitTest(point, with: event) return view == self ? nil : view } } protocol CapAccessoryDelegate: AnyObject { func capSearchWasDismissed() func capSearch(didChange text: String) func capAccessoryDidDiscardImage() func capAccessory(shouldSave image: UIImage) func capAccessoryCameraButtonPressed() } class SearchAndDisplayAccessory: PassthroughView { // MARK: - Outlets @IBOutlet weak var capImage: RoundedImageView! @IBOutlet weak var saveButton: UIButton! @IBOutlet weak var cameraButton: UIButton! @IBOutlet weak var searchBar: UISearchBar! @IBOutlet weak var imageHeightContraint: NSLayoutConstraint! // MARK: - Actions @IBAction func cameraButtonPressed() { if isShowingCapImage { discardImage() } else { delegate?.capAccessoryCameraButtonPressed() } } @IBAction func saveButtonPressed() { if let image = capImage.image { delegate?.capAccessory(shouldSave: image) } } // MARK: - Variables var view: UIView? weak var delegate: CapAccessoryDelegate? var currentImage: UIImage? { capImage.image } var isShowingCapImage: Bool { capImage.image != nil } // MARK: - Setup convenience init(width: CGFloat) { let frame = CGRect(origin: .zero, size: CGSize(width: width, height: 145)) self.init(frame: frame) } override init(frame: CGRect) { super.init(frame: frame) setup() } required init?(coder: NSCoder) { super.init(coder: coder) setup() } private func setup() { view = fromNib() view!.frame = bounds //view!.autoresizingMask = .flexibleHeight addSubview(view!) hideImageView() searchBar.text = nil searchBar.setShowsCancelButton(false, animated: false) searchBar.delegate = self } // MARK: Search bar func dismissAndClearSearchBar() { searchBar.resignFirstResponder() searchBar.text = nil } // MARK: Cap image func showImageView(with image: UIImage) { capImage.image = image cameraButton.setImage(UIImage(systemName: "xmark"), for: .normal) imageHeightContraint.constant = 90 capImage.alpha = 1 capImage.isHidden = false saveButton.isHidden = false } func discardImage() { DispatchQueue.main.async { self.dismissAndClearSearchBar() self.hideImageView() } delegate?.capAccessoryDidDiscardImage() } func hideImageView() { capImage.image = nil cameraButton.setImage(UIImage(systemName: "camera"), for: .normal) //imageHeightContraint.constant = 0 capImage.alpha = 0 capImage.isHidden = true saveButton.isHidden = true } } // MARK: - UISearchBarDelegate extension SearchAndDisplayAccessory: UISearchBarDelegate { func searchBarCancelButtonClicked(_ searchBar: UISearchBar) { searchBar.resignFirstResponder() searchBar.text = nil delegate?.capSearchWasDismissed() } func searchBarSearchButtonClicked(_ searchBar: UISearchBar) { searchBar.resignFirstResponder() } func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) { delegate?.capSearch(didChange: searchText) } }