BrowserHomeView.swift
5.42 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
//
// 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()
}
}