ToolbarView.swift 3.54 KB
//
//  UINavigationToolbar.swift
//  browser
//
//  Created by Artem Talko on 27.09.2023.
//

import UIKit

enum ToolbarElementType {
    case back
    case forward
    case share
    case history
    case tabs
}

final class ToolbarView: UIView {
    var action: ((ToolbarElementType) -> Void)?
    
    let toolbar: UIToolbar = {
        let obj = UIToolbar()
        obj.sizeToFit()
        obj.barTintColor = ColorConstants.lightGray
        obj.clipsToBounds = true
        return obj
    }()
  
    let backBarButtonItem: UIBarButtonItem = {
        let obj = UIBarButtonItem()
        obj.isEnabled = false
        obj.image = UIImage(systemName: "chevron.backward")
        return obj
    }()
    
    let forwardBarButtonItem: UIBarButtonItem = {
        let obj = UIBarButtonItem()
        obj.isEnabled = false
        obj.image = UIImage(systemName: "chevron.forward")
        return obj
    }()
    
    let shareBarButtonItem: UIBarButtonItem = {
        let obj = UIBarButtonItem()
        obj.isEnabled = false
        obj.image = UIImage(systemName: "square.and.arrow.up")
        return obj
    }()
    
    let historyBarButtonItem: UIBarButtonItem = {
        let obj = UIBarButtonItem()
        obj.image = UIImage(systemName: "book")
        return obj
    }()
    
    let tabsBarButtonItem: UIBarButtonItem = {
        let obj = UIBarButtonItem()
        obj.image = UIImage(systemName: "square.on.square")
        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(toolbar)
        let flexibleSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
        toolbar.items = [
            backBarButtonItem,
            flexibleSpace,
            forwardBarButtonItem,
            flexibleSpace,
            shareBarButtonItem,
            flexibleSpace,
            historyBarButtonItem,
            flexibleSpace,
            tabsBarButtonItem,
        ]
        initActions()
        setupConstraints()
    }
    
    private func setupConstraints() {
        toolbar.snp.makeConstraints { make in
            make.top.equalToSuperview()
            make.leading.trailing.equalToSuperview()
            make.bottom.equalTo(safeAreaLayoutGuide.snp.bottom)
        }
    }
}

// MARK: - Helpers
extension ToolbarView {
    private func initActions() {
        backBarButtonItem.action = #selector(didItemTapped(_:))
        backBarButtonItem.target = self
        forwardBarButtonItem.action = #selector(didItemTapped(_:))
        forwardBarButtonItem.target = self
        shareBarButtonItem.action = #selector(didItemTapped(_:))
        shareBarButtonItem.target = self
        historyBarButtonItem.action = #selector(didItemTapped(_:))
        historyBarButtonItem.target = self
        tabsBarButtonItem.action = #selector(didItemTapped(_:))
        tabsBarButtonItem.target = self
    }
}

// MARK: - Actions
extension ToolbarView {
    @objc 
    private func didItemTapped(_ sender: UIBarButtonItem) {
        guard let action else {
            return
        }
        switch sender {
        case backBarButtonItem:
            action(.back)
        case forwardBarButtonItem:
            action(.forward)
        case shareBarButtonItem:
            action(.share)
        case historyBarButtonItem:
            action(.history)
        case tabsBarButtonItem:
            action(.tabs)
        default:
            break
        }
    }
}