This repository has been archived by the owner on May 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 441
/
BackForwardTableViewCell.swift
146 lines (128 loc) · 4.04 KB
/
BackForwardTableViewCell.swift
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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
import UIKit
import Storage
import Shared
import Favicon
class BackForwardTableViewCell: UITableViewCell {
private struct BackForwardViewCellUX {
static let faviconWidth = 29
static let faviconPadding: CGFloat = 20
static let labelPadding = 20
static let borderSmall = 2
static let borderBold = 5
static let iconSize = 23
static let fontSize: CGFloat = 12.0
static let textColor: UIColor = .braveLabel
}
lazy var faviconView: UIImageView = {
let faviconView = UIImageView(image: Favicon.defaultImage)
faviconView.backgroundColor = .braveBackground
faviconView.layer.cornerRadius = 6
faviconView.layer.cornerCurve = .continuous
faviconView.layer.borderWidth = 0.5
faviconView.layer.borderColor = UIColor(white: 0, alpha: 0.1).cgColor
faviconView.layer.masksToBounds = true
faviconView.contentMode = .center
return faviconView
}()
let line = UIView().then {
$0.backgroundColor = .braveSeparator
}
lazy var label: UILabel = {
let label = UILabel()
label.text = " "
label.font = label.font.withSize(BackForwardViewCellUX.fontSize)
label.textColor = BackForwardViewCellUX.textColor
return label
}()
var connectingForwards = true {
didSet {
setNeedsUpdateConstraints()
}
}
var connectingBackwards = true {
didSet {
setNeedsUpdateConstraints()
}
}
var isCurrentTab = false {
didSet {
if isCurrentTab {
label.font = UIFont(name: "HelveticaNeue-Bold", size: BackForwardViewCellUX.fontSize)
}
}
}
var site: Site? {
didSet {
if let s = site {
if InternalURL.isValid(url: s.tileURL) {
faviconView.backgroundColor = .white
faviconView.image = Favicon.defaultImage
} else {
faviconView.loadFavicon(for: s.tileURL)
}
var title = s.title
if title.isEmpty {
title = s.url
}
label.text = title
setNeedsLayout()
}
}
}
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
backgroundColor = .clear
selectionStyle = .none
contentView.addSubview(line)
contentView.addSubview(faviconView)
contentView.addSubview(label)
faviconView.snp.makeConstraints { make in
make.height.equalTo(BackForwardViewCellUX.faviconWidth)
make.width.equalTo(BackForwardViewCellUX.faviconWidth)
make.centerY.equalTo(self)
make.leading.equalTo(self.safeArea.leading).offset(BackForwardViewCellUX.faviconPadding)
}
label.snp.makeConstraints { make in
make.centerY.equalTo(self)
make.leading.equalTo(faviconView.snp.trailing).offset(BackForwardViewCellUX.labelPadding)
make.trailing.equalTo(self.safeArea.trailing).offset(-BackForwardViewCellUX.labelPadding)
}
}
override func updateConstraints() {
super.updateConstraints()
line.snp.remakeConstraints {
if connectingForwards {
$0.top.equalToSuperview()
} else {
$0.top.equalTo(faviconView.snp.centerY)
}
if connectingBackwards {
$0.bottom.equalToSuperview()
} else {
$0.bottom.equalTo(faviconView.snp.centerY)
}
$0.centerX.equalTo(faviconView)
$0.width.equalTo(1.0 / UIScreen.main.scale)
}
}
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func setHighlighted(_ highlighted: Bool, animated: Bool) {
if highlighted {
self.backgroundColor = UIColor(white: 0, alpha: 0.1)
} else {
self.backgroundColor = .clear
}
}
override func prepareForReuse() {
super.prepareForReuse()
connectingForwards = true
connectingBackwards = true
isCurrentTab = false
label.font = UIFont(name: "HelveticaNeue", size: BackForwardViewCellUX.fontSize)
}
}