OpenedTabsCollectionViewCell.swift
4.58 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
163
164
165
166
167
168
169
170
171
172
173
//
// OpenedTabsCellDelegate.swift
// browser
//
// Created by Artem Talko on 28.09.2023.
//
//MARK: Checked
import UIKit
import SnapKit
protocol OpenedTabsCellDelegate: AnyObject {
func deleteTabDelegateFunc(cell: OpenedTabsCollectionViewCell)
}
final class OpenedTabsCollectionViewCell: UICollectionViewCell {
static let cellID = String(describing: OpenedTabsCollectionViewCell.self)
var model: BrowserTabDataBase? {
didSet {
handleUI()
}
}
weak var delegate: OpenedTabsCellDelegate?
private let siteNameLabel: UILabel = {
let obj = UILabel()
obj.font = FontConstants.regularFont_14
return obj
}()
private let sitePreviewImageContainer: UIView = {
let obj = UIView()
obj.clipsToBounds = true
obj.layer.cornerRadius = 10
return obj
}()
private let sitePreviewImage: UIImageView = {
let obj = UIImageView()
obj.contentMode = .scaleAspectFill
return obj
}()
let closeTabButton: UIButton = {
let obj = UIButton()
obj.setImage(UIImage(systemName: "xmark"), for: .normal)
obj.contentMode = .scaleAspectFill
obj.tintColor = .gray
return obj
}()
private var panGestureRecognizer: UIPanGestureRecognizer!
override init(frame: CGRect) {
super.init(frame: frame)
setup()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
private func setup() {
self.backgroundColor = .white
layer.cornerRadius = 10
contentView.addSubview(siteNameLabel)
contentView.addSubview(closeTabButton)
contentView.addSubview(sitePreviewImageContainer)
sitePreviewImageContainer.addSubview(sitePreviewImage)
shadowSetup()
setupConstraints()
addingTargets()
setupPanGesture()
}
private func setupConstraints() {
closeTabButton.snp.makeConstraints { make in
make.top.equalToSuperview().offset(8)
make.trailing.equalToSuperview().offset(-8)
make.height.equalTo(14)
make.width.equalTo(14)
}
siteNameLabel.snp.makeConstraints { make in
make.top.equalToSuperview().offset(8)
make.leading.equalToSuperview().offset(16)
make.trailing.equalTo(closeTabButton.snp.leading).offset(-2)
}
sitePreviewImageContainer.snp.makeConstraints { make in
make.top.equalTo(closeTabButton.snp.bottom).offset(8)
make.leading.trailing.equalToSuperview().inset(8)
make.bottom.equalToSuperview().offset(-8)
}
sitePreviewImage.snp.makeConstraints { make in
make.edges.equalToSuperview()
}
}
override func layoutSubviews() {
super.layoutSubviews()
shadowSetup()
}
}
//MARK: Helpers
extension OpenedTabsCollectionViewCell {
private func setupPanGesture() {
panGestureRecognizer = UIPanGestureRecognizer(target: self, action: #selector(handlePan(_:)))
addGestureRecognizer(panGestureRecognizer)
}
private func shadowSetup() {
layer.shadowColor = UIColor.black.withAlphaComponent(0.1).cgColor
layer.shadowOpacity = 1
layer.shadowRadius = 30
layer.shadowOffset = CGSize(width: 0, height: 0)
}
private func addingTargets() {
closeTabButton.addTarget(self, action: #selector(deleteTab), for: .touchUpInside)
}
}
//MARK: Handlers
extension OpenedTabsCollectionViewCell {
private func handleUI() {
siteNameLabel.text = model?.tabTitle
sitePreviewImage.image = model?.snapshotImageData?.toImage()
}
}
//MARK: Actions
extension OpenedTabsCollectionViewCell {
@objc
private func deleteTab() {
delegate?.deleteTabDelegateFunc(cell: self)
if let tabId = model?.tabId {
TabManager.shared.deleteTab(tabId: tabId)
}
}
@objc
private func handlePan(_ gesture: UIPanGestureRecognizer) {
let translation = gesture.translation(in: self)
let width = self.frame.width
let threshold = width / 2
if gesture.state == .changed {
transform = CGAffineTransform(translationX: translation.x, y: 0)
} else if gesture.state == .ended {
if abs(translation.x) >= threshold {
deleteTab()
} else {
UIView.animate(withDuration: 0.3) {
self.transform = .identity
}
}
}
}
}