-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathSimpleHighlightCell.swift
169 lines (144 loc) · 6.75 KB
/
SimpleHighlightCell.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/* 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 Shared
import Storage
struct SimpleHighlightCellUX {
static let LabelColor = UIAccessibilityDarkerSystemColorsEnabled() ? UIColor.black : UIColor(rgb: 0x353535)
static let BorderWidth: CGFloat = 0.5
static let CellSideOffset = 20
static let AlternateBottomOffset = 16
static let TitleLabelOffset = 10
static let CellTopBottomOffset = 12
static let SiteImageViewSize = 48
static let IconSize = CGSize(width: 32, height: 32)
static let StatusIconSize = 12
static let DescriptionLabelColor = UIColor(colorString: "919191")
static let SelectedOverlayColor = UIColor(white: 0.0, alpha: 0.25)
static let PlaceholderImage = UIImage(named: "defaultTopSiteIcon")
static let CornerRadius: CGFloat = 3
static let NearestNeighbordScalingThreshold: CGFloat = 24
static let BorderColor = UIColor(white: 0, alpha: 0.1)
}
class SimpleHighlightCell: UITableViewCell {
fileprivate lazy var titleLabel: UILabel = {
let titleLabel = UILabel()
titleLabel.font = DynamicFontHelper.defaultHelper.DeviceFontMediumBold
titleLabel.textColor = SimpleHighlightCellUX.LabelColor
titleLabel.textAlignment = .left
titleLabel.numberOfLines = 3
return titleLabel
}()
fileprivate lazy var descriptionLabel: UILabel = {
let descriptionLabel = UILabel()
descriptionLabel.font = DynamicFontHelper.defaultHelper.DeviceFontDescriptionActivityStream
descriptionLabel.textColor = SimpleHighlightCellUX.DescriptionLabelColor
descriptionLabel.textAlignment = .left
descriptionLabel.numberOfLines = 1
return descriptionLabel
}()
lazy var siteImageView: UIImageView = {
let siteImageView = UIImageView()
siteImageView.contentMode = UIViewContentMode.scaleAspectFit
siteImageView.clipsToBounds = true
siteImageView.contentMode = UIViewContentMode.center
siteImageView.layer.cornerRadius = SimpleHighlightCellUX.CornerRadius
siteImageView.layer.borderColor = SimpleHighlightCellUX.BorderColor.cgColor
siteImageView.layer.borderWidth = SimpleHighlightCellUX.BorderWidth
siteImageView.layer.masksToBounds = true
return siteImageView
}()
fileprivate lazy var statusIcon: UIImageView = {
let statusIcon = UIImageView()
statusIcon.contentMode = UIViewContentMode.scaleAspectFit
statusIcon.clipsToBounds = true
statusIcon.layer.cornerRadius = SimpleHighlightCellUX.CornerRadius
return statusIcon
}()
fileprivate lazy var selectedOverlay: UIView = {
let selectedOverlay = UIView()
selectedOverlay.backgroundColor = SimpleHighlightCellUX.SelectedOverlayColor
selectedOverlay.isHidden = true
return selectedOverlay
}()
override var isSelected: Bool {
didSet {
self.selectedOverlay.isHidden = !isSelected
}
}
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
layer.shouldRasterize = true
layer.rasterizationScale = UIScreen.main.scale
isAccessibilityElement = true
contentView.addSubview(siteImageView)
contentView.addSubview(descriptionLabel)
contentView.addSubview(selectedOverlay)
contentView.addSubview(titleLabel)
contentView.addSubview(statusIcon)
siteImageView.snp.remakeConstraints { make in
make.top.equalTo(contentView).offset(SimpleHighlightCellUX.CellTopBottomOffset)
make.bottom.equalTo(contentView).offset(-SimpleHighlightCellUX.CellTopBottomOffset).priority(10)
make.leading.equalTo(contentView).offset(SimpleHighlightCellUX.CellSideOffset)
make.size.equalTo(SimpleHighlightCellUX.SiteImageViewSize)
}
selectedOverlay.snp.remakeConstraints { make in
make.edges.equalTo(contentView)
}
titleLabel.snp.remakeConstraints { make in
make.leading.equalTo(siteImageView.snp.trailing).offset(SimpleHighlightCellUX.CellTopBottomOffset)
make.trailing.equalTo(contentView).inset(SimpleHighlightCellUX.CellSideOffset)
make.top.equalTo(siteImageView)
}
descriptionLabel.snp.remakeConstraints { make in
make.leading.equalTo(statusIcon.snp.trailing).offset(SimpleHighlightCellUX.TitleLabelOffset)
make.bottom.equalTo(statusIcon)
}
statusIcon.snp.remakeConstraints { make in
make.leading.equalTo(titleLabel)
make.top.equalTo(titleLabel.snp.bottom).offset(SimpleHighlightCellUX.CellTopBottomOffset)
make.size.equalTo(SimpleHighlightCellUX.StatusIconSize)
make.bottom.equalTo(contentView).offset(-SimpleHighlightCellUX.AlternateBottomOffset).priority(1000)
}
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func setImageWithURL(_ url: URL) {
siteImageView.sd_setImage(with: url) { (img, err, type, url) -> Void in
guard let img = img else {
return
}
// Resize an Image to a specfic size to make sure that it doesnt appear bigger than it needs to (32px) inside a larger frame (48px).
self.siteImageView.image = img.createScaled(SimpleHighlightCellUX.IconSize)
self.siteImageView.image?.getColors(scaleDownSize: CGSize(width: 25, height: 25)) { colors in
self.siteImageView.backgroundColor = colors.backgroundColor ?? UIColor.lightGray
}
}
}
override func prepareForReuse() {
super.prepareForReuse()
self.siteImageView.image = nil
}
func configureWithSite(_ site: Site) {
if let icon = site.icon, let url = URL(string:icon.url) {
self.setImageWithURL(url)
} else {
let url = URL(string: site.url)!
self.siteImageView.image = FaviconFetcher.getDefaultFavicon(url)
self.siteImageView.backgroundColor = FaviconFetcher.getDefaultColor(url)
}
self.titleLabel.text = site.title.characters.count <= 1 ? site.url : site.title
configureCellStatus(site)
}
func configureCellStatus(_ site: Site) {
if let bookmarked = site.bookmarked, bookmarked {
self.descriptionLabel.text = "Bookmarked"
self.statusIcon.image = UIImage(named: "context_bookmark")
} else {
self.descriptionLabel.text = "Visited"
self.statusIcon.image = UIImage(named: "context_viewed")
}
}
}