TabsToolbarView.swift
2.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
//
// Tabs.swift
// browser
//
// Created by Artem Talko on 28.09.2023.
//
//MARK: Checked
import UIKit
enum HistoryToolbarElementType {
case add
case tabs
case done
}
final class TabsToolbarView: UIView {
var action: ((HistoryToolbarElementType) -> Void)?
private let toolbar: UIToolbar = {
let obj = UIToolbar()
obj.barTintColor = .white
return obj
}()
private let newTabToolbarButtonItem: UIBarButtonItem = {
let obj = UIBarButtonItem()
obj.image = UIImage(systemName: "plus")
obj.tintColor = .gray
return obj
}()
let showedCountOfTabs: UIBarButtonItem = {
let obj = UIBarButtonItem()
obj.tintColor = .gray
return obj
}()
private let doneTabToolbarButtonItem: UIBarButtonItem = {
let obj = UIBarButtonItem()
obj.title = "Done"
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)
setupToolbar()
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 TabsToolbarView {
private func initActions() {
newTabToolbarButtonItem.target = self
newTabToolbarButtonItem.action = #selector(didItemTapped(_:))
showedCountOfTabs.target = self
showedCountOfTabs.action = #selector(didItemTapped(_:))
doneTabToolbarButtonItem.target = self
doneTabToolbarButtonItem.action = #selector(didItemTapped(_:))
}
private func setupToolbar() {
let flexibleSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
toolbar.items = [ newTabToolbarButtonItem,
flexibleSpace, showedCountOfTabs,
flexibleSpace, doneTabToolbarButtonItem ]
toolbar.backgroundColor = .white
}
}
// MARK: - Actions
extension TabsToolbarView {
@objc
private func didItemTapped(_ sender: UIBarButtonItem) {
guard let action else {
return
}
switch sender {
case newTabToolbarButtonItem:
action(.add)
case showedCountOfTabs:
action(.tabs)
case doneTabToolbarButtonItem:
action(.done)
default:
break
}
}
}