2020-05-16 11:21:55 +02:00
|
|
|
//
|
|
|
|
// 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: class {
|
|
|
|
|
|
|
|
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!
|
|
|
|
|
2020-09-20 13:28:22 +02:00
|
|
|
@IBOutlet weak var imageHeightContraint: NSLayoutConstraint!
|
|
|
|
|
2020-05-16 11:21:55 +02:00
|
|
|
// MARK: - Actions
|
|
|
|
|
|
|
|
@IBAction func cameraButtonPressed() {
|
2020-09-20 13:28:22 +02:00
|
|
|
if isShowingCapImage {
|
|
|
|
discardImage()
|
|
|
|
} else {
|
|
|
|
delegate?.capAccessoryCameraButtonPressed()
|
|
|
|
}
|
2020-05-16 11:21:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@IBAction func saveButtonPressed() {
|
|
|
|
if let image = capImage.image {
|
|
|
|
delegate?.capAccessory(shouldSave: image)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - Variables
|
|
|
|
|
|
|
|
var view: UIView?
|
|
|
|
|
|
|
|
weak var delegate: CapAccessoryDelegate?
|
2020-06-18 22:55:51 +02:00
|
|
|
|
2020-05-16 11:21:55 +02:00
|
|
|
var currentImage: UIImage? {
|
|
|
|
capImage.image
|
|
|
|
}
|
|
|
|
|
2020-09-20 13:28:22 +02:00
|
|
|
var isShowingCapImage: Bool {
|
|
|
|
capImage.image != nil
|
|
|
|
}
|
|
|
|
|
2020-05-16 11:21:55 +02:00
|
|
|
// 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!)
|
|
|
|
|
2020-09-20 13:28:22 +02:00
|
|
|
hideImageView()
|
2020-05-16 11:21:55 +02:00
|
|
|
|
|
|
|
searchBar.text = nil
|
|
|
|
searchBar.setShowsCancelButton(false, animated: false)
|
|
|
|
searchBar.delegate = self
|
|
|
|
}
|
2020-06-18 22:55:51 +02:00
|
|
|
|
2020-05-16 11:21:55 +02:00
|
|
|
// MARK: Search bar
|
|
|
|
|
|
|
|
func dismissAndClearSearchBar() {
|
|
|
|
searchBar.resignFirstResponder()
|
|
|
|
searchBar.text = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: Cap image
|
|
|
|
|
2020-09-20 13:28:22 +02:00
|
|
|
func showImageView(with image: UIImage) {
|
2020-05-16 11:21:55 +02:00
|
|
|
capImage.image = image
|
2020-09-20 13:28:22 +02:00
|
|
|
cameraButton.setImage(UIImage(systemName: "xmark"), for: .normal)
|
2020-06-18 22:55:51 +02:00
|
|
|
|
2020-09-20 13:28:22 +02:00
|
|
|
imageHeightContraint.constant = 90
|
|
|
|
capImage.alpha = 1
|
|
|
|
capImage.isHidden = false
|
|
|
|
saveButton.isHidden = false
|
2020-05-16 11:21:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func discardImage() {
|
2020-06-18 22:55:51 +02:00
|
|
|
DispatchQueue.main.async {
|
|
|
|
self.dismissAndClearSearchBar()
|
|
|
|
self.hideImageView()
|
|
|
|
}
|
2020-05-16 11:21:55 +02:00
|
|
|
delegate?.capAccessoryDidDiscardImage()
|
|
|
|
}
|
|
|
|
|
|
|
|
func hideImageView() {
|
2020-09-20 13:28:22 +02:00
|
|
|
capImage.image = nil
|
|
|
|
cameraButton.setImage(UIImage(systemName: "camera"), for: .normal)
|
2020-05-16 11:21:55 +02:00
|
|
|
|
2020-09-20 13:28:22 +02:00
|
|
|
//imageHeightContraint.constant = 0
|
|
|
|
capImage.alpha = 0
|
|
|
|
capImage.isHidden = true
|
|
|
|
saveButton.isHidden = true
|
2020-05-16 11:21:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
}
|