SearchBarView.swift 2.43 KB
//
//  SearchBarView.swift
//  browser
//
//  Created by Artem Talko on 26.09.2023.
//

import UIKit

final class SearchBarView: UIView {
    private let searchImageView: UIImageView = {
        let obj = UIImageView()
        let magnifyImage = UIImage(systemName: "magnifyingglass")?.withTintColor(.gray, renderingMode: .alwaysOriginal)
        obj.image = magnifyImage
        
        return obj
    }()
    
    let searchTextFieldView: UITextField = {
        let obj = UITextField()
        obj.text = "Search"
        obj.textColor =  ColorConstants.gray
        
        return obj
    }()
    
    let cleanTextFieldButton: UIButton = {
        let obj = UIButton()
        obj.setImage(UIImage(systemName: "xmark.circle.fill"), for: .normal)
        obj.tintColor = .black
        obj.isHidden = true
        
        return obj
    }()
    
    let progressBar: UIProgressView = {
        let obj = UIProgressView(progressViewStyle: .default)
        obj.progressTintColor = .systemIndigo
        obj.progress = 0
        obj.translatesAutoresizingMaskIntoConstraints = false
        
        return obj
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setup()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setup() {
        addSubview(progressBar)
        addSubview(searchImageView)
        addSubview(searchTextFieldView)
        addSubview(cleanTextFieldButton)
        
        setupConstraints()
    }
    
    private func setupConstraints() {
        searchImageView.snp.makeConstraints { make in
            make.top.bottom.equalToSuperview().inset(8.sizeH)
            make.leading.equalToSuperview().offset(16.sizeW)
        }
        
        searchTextFieldView.snp.makeConstraints { make in
            make.top.bottom.equalToSuperview().inset(8.sizeH)
            make.leading.equalTo(searchImageView).offset(32.sizeW)
            make.trailing.equalToSuperview().offset(-16.sizeW)
        }
        
        cleanTextFieldButton.snp.makeConstraints { make in
            make.top.bottom.equalToSuperview().inset(8.sizeH)
            make.trailing.equalToSuperview().offset(-16.sizeW)
        }
        
        progressBar.snp.makeConstraints { make in
            make.leading.trailing.equalToSuperview()
            make.bottom.equalToSuperview().inset(2.sizeH)
            make.height.equalTo(2)
        }
    }
}