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

import UIKit
import SnapKit

final class BrowserHomeView: GradientView {
    let removeAdvertSegmentControl: UISegmentedControl = {
        let items = StringConstants.segmentedControlItems
        
        let obj = UISegmentedControl(items: items)
        obj.selectedSegmentIndex = 1
        obj.backgroundColor = ColorConstants.segmentControlBackground
        obj.layer.cornerRadius = 10
        obj.selectedSegmentTintColor = UIColor(red: 0.349, green: 0.565, blue: 1, alpha: 1)
        obj.setTitleTextAttributes([.foregroundColor: UIColor.black], for: .normal)
        obj.setTitleTextAttributes([.foregroundColor: UIColor.white], for: .selected)
        
        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()
        obj.backgroundColor = ColorConstants.customLightBlue
        
        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(removeAdvertSegmentControl)
        addSubview(settingButton)
        addSubview(searchBarContainer)
        addSubview(toolbarView)
        
        setupConstraints()
    }
    
    private func setupConstraints() {
       removeAdvertSegmentControl.snp.makeConstraints { make in
            make.top.equalTo(safeAreaLayoutGuide.snp.top).offset(16.sizeH)
            make.leading.equalToSuperview().offset(54.sizeW)
            make.trailing.equalTo(settingButton.snp.leading).offset(-15.sizeW)
            make.height.equalTo(34.sizeH)
        }
        
        settingButton.snp.makeConstraints { make in
            make.trailing.equalToSuperview().offset(-16.sizeW)
            make.height.equalTo(24.sizeH)
            make.width.equalTo(24.sizeH)
            make.centerY.equalTo(removeAdvertSegmentControl)
        }
        
        titleLabel.snp.makeConstraints { make in
            make.top.equalTo(removeAdvertSegmentControl.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.equalToSuperview()
            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 = ColorConstants.customLightBlue
            self.searchBarContainer.searchBarView.backgroundColor = .black
            self.searchBarContainer.searchBarView.searchTextFieldView.textColor = .white
            
            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.searchBarView.backgroundColor = ColorConstants.customLightBlue
            self.searchBarContainer.snp.updateConstraints { make in
                make.leading.equalToSuperview()
                make.trailing.equalToSuperview()
                make.bottom.equalToSuperview().offset(-90.sizeH)
            }
        }
        self.layoutIfNeeded()
    }
}