BrowserHomeView.swift 5.42 KB
//
//  MainView.swift
//  browser
//
//  Created by Artem Talko on 25.09.2023.
//

import UIKit
import SnapKit

final class BrowserHomeView: UIView {
    let removeAddButton: UIButton = {
        let obj = UIButton()
        obj.backgroundColor = ColorConstants.buttonsBackgroundColor
        obj.setTitle("Remove advert", for: .normal)
        obj.titleLabel?.font = FontConstants.semiboldFont_15
        obj.translatesAutoresizingMaskIntoConstraints = false
        obj.setTitleColor(.white, for: .normal)
        obj.setImage(UIImage(systemName: "shield.lefthalf.filled")?.withTintColor(.white, renderingMode: .alwaysOriginal), for: .normal)
        obj.layer.cornerRadius = 10
        obj.contentEdgeInsets = UIEdgeInsets(top: 10.sizeW, left: 10.sizeW, bottom: 10.sizeW, right: 10.sizeW)
        obj.clipsToBounds = true
        obj.layer.masksToBounds = false
        
        return obj
    }()
    
    let settingButton: UIButton = {
        let obj = UIButton()
        obj.setImage(UIImage(named: "Settings")?.withTintColor(.systemBlue), for: .normal)
        
        return obj
    }()
    
    private let titleLabel: UILabel = {
        let obj  = UILabel()
        obj.text = StringConstants.frequentlyVisited
        obj.font = FontConstants.semiboldFont_18
        
        return obj
    }()
    
    let frequentlyVisitedCollectionView: UICollectionView = {
        let obj = UICollectionViewFlowLayout()
        obj.minimumInteritemSpacing = 12.sizeW
        obj.scrollDirection = .horizontal
        obj.sectionInset = UIEdgeInsets(top: 4.sizeH, left: 4.sizeW, bottom: 4.sizeH, right: 4.sizeW)
        
        let collectionView = UICollectionView(frame: .zero, collectionViewLayout: obj)
        collectionView.backgroundColor = .clear
        collectionView.showsHorizontalScrollIndicator = false
        
        return collectionView
    }()
    
    let toolbarView: ToolbarView = {
        let obj = ToolbarView()
        
        return obj
    }()
    
    let searchBarContainer: SearchBarContainer = {
        let obj = SearchBarContainer()
        obj.backgroundColor = .clear
        
        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(titleLabel)
        addSubview(frequentlyVisitedCollectionView)
        addSubview(removeAddButton)
        addSubview(settingButton)
        addSubview(searchBarContainer)
        addSubview(toolbarView)
        
        setupConstraints()
    }
    
    private func setupConstraints() {
        removeAddButton.snp.makeConstraints { make in
            make.top.equalTo(safeAreaLayoutGuide.snp.top).offset(16.sizeH)
            make.leading.equalTo(safeAreaLayoutGuide.snp.leading).offset(20.sizeW)
            make.trailing.equalToSuperview().offset(-158.sizeW)
        }
        settingButton.snp.makeConstraints { make in
            make.trailing.equalToSuperview().offset(-16.sizeW)
            make.height.equalTo(24.sizeH)
            make.width.equalTo(24.sizeH)
            make.centerY.equalTo(removeAddButton)
        }
        titleLabel.snp.makeConstraints { make in
            make.top.equalTo(removeAddButton.snp.bottom).offset(64.sizeH)
            make.leading.equalTo(safeAreaLayoutGuide.snp.leading).offset(16.sizeW)
        }
        frequentlyVisitedCollectionView.snp.makeConstraints { make in
            make.top.equalTo(titleLabel.snp.bottom).offset(8.sizeH)
            make.leading.trailing.equalToSuperview().inset(24.sizeW)
            make.height.equalTo(120.sizeH)
        }
        toolbarView.snp.makeConstraints { make in
            make.leading.equalToSuperview()
            make.bottom.equalTo(safeAreaLayoutGuide.snp.bottom)
            make.trailing.equalToSuperview()
        }
        searchBarContainer.snp.makeConstraints { make in
            make.leading.equalToSuperview()
            make.bottom.equalToSuperview().offset(-90.sizeH)
            make.trailing.equalToSuperview()
        }
    }
}

//MARK: - childView extension
extension BrowserHomeView {
    func addSearchView(_ childView: UIView){
        addSubview(childView)
        
        childView.snp.makeConstraints { make in
            make.top.equalToSuperview()
            make.leading.trailing.equalToSuperview()
            make.bottom.equalTo(searchBarContainer.snp.top).offset(-2.sizeH)
        }
    }
}

//MARK: - Search bar extention
extension BrowserHomeView {
    func animateSearchBar(_ keyboardHeight: CGFloat) {
        UIView.animate(withDuration: 0.3) {
            self.searchBarContainer.backgroundColor = UIColor(red: 0.808, green: 0.823, blue: 0.85, alpha: 0.9)
            self.searchBarContainer.snp.updateConstraints { make in
                make.leading.equalToSuperview()
                make.trailing.equalToSuperview()
                make.bottom.equalToSuperview().offset(-keyboardHeight)
            }
            self.layoutIfNeeded()
        }
    }
    
    func animateSearchBarDismiss() {
        BrowserHomeView.animate(withDuration: 0.3) {
            self.searchBarContainer.backgroundColor = .clear
            self.searchBarContainer.snp.updateConstraints { make in
                make.leading.equalToSuperview()
                make.trailing.equalToSuperview()
                make.bottom.equalToSuperview().offset(-90.sizeH)
            }
        }
        self.layoutIfNeeded()
    }
}