This repository was archived by the owner on Sep 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathLinkCell.swift
285 lines (240 loc) · 11.8 KB
/
LinkCell.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
//
// LinkCell.swift
// reddift
//
// Created by sonson on 2016/02/15.
// Copyright © 2016年 sonson. All rights reserved.
//
import UIKit
let LinkCellDidTapActionNotification = Notification.Name(rawValue: "LinkCellDidTapActionNotification")
let LinkCellDidTapCommentNotification = Notification.Name(rawValue: "LinkCellDidTapCommentNotification")
let LinkCellDidTapNameNotification = Notification.Name(rawValue: "LinkCellDidTapNameNotification")
let LinkCellDidTapTitleNotification = Notification.Name(rawValue: "LinkCellDidTapTitleNotification")
let LinkCellDidTapThumbnailNotification = Notification.Name(rawValue: "LinkCellDidTapThumbnailNotification")
class LinkCell: UITableViewCell {
var titleTextView: UZTextView = UZTextView(frame: CGRect.zero)
var contentInfoView: ContentInfoView = ContentInfoView(frame: CGRect.zero)
var contentToolbar: ContentToolbar = ContentToolbar(frame: CGRect.zero)
static let verticalTopMargin = CGFloat(5)
static let verticalBotttomMargin = CGFloat(5)
static let titleLeftMargin = CGFloat(5)
static let titleRightMargin = CGFloat(5)
// MARK: - Class method
class func estimateTitleSize(attributedString: NSAttributedString, withBountWidth: CGFloat, margin: UIEdgeInsets) -> CGSize {
let width = withBountWidth - titleLeftMargin - titleRightMargin
return UZTextView.size(for: attributedString, withBoundWidth: width, margin: UIEdgeInsets.zero)
}
class func estimateHeight(titleHeight: CGFloat) -> CGFloat {
return ThumbnailLinkCell.verticalTopMargin + titleHeight + ThumbnailLinkCell.verticalBotttomMargin + ContentInfoView.height + ContentToolbar.height
}
// MARK: - Support 3D touch
func userAt(_ location: CGPoint, peekView: UIView) -> (String, CGRect)? {
let p = peekView.convert(location, to: contentInfoView)
if contentInfoView.nameButton.frame.contains(p) {
let sourceRect = contentInfoView.convert(contentInfoView.nameButton.frame, to: peekView)
if let name = contentInfoView.nameButton.titleLabel?.text {
return (name, sourceRect)
}
}
return nil
}
func commentAt(_ location: CGPoint, peekView: UIView) -> (CGRect, LinkContainable)? {
guard let container = container else { return nil }
if "self.\(container.link.subreddit)" == container.link.domain {
let p = peekView.convert(location, to: contentView)
if titleTextView.frame.contains(p) {
let sourceRect = contentView.convert(titleTextView.frame, to: peekView)
return (sourceRect, container)
}
}
let p = peekView.convert(location, to: contentToolbar)
if contentToolbar.commentBaseButton.frame.contains(p) {
let sourceRect = contentToolbar.convert(contentToolbar.commentBaseButton.frame, to: peekView)
return (sourceRect, container)
}
return nil
}
func urlAt(_ location: CGPoint, peekView: UIView) -> (URL, CGRect)? {
guard let container = container else { return nil }
if "self.\(container.link.subreddit)" == container.link.domain {
return nil
}
let p = peekView.convert(location, to: contentView)
if titleTextView.frame.contains(p) {
let sourceRect = contentView.convert(titleTextView.frame, to: peekView)
if let url = URL(string: container.link.url) {
return (url, sourceRect)
}
}
return nil
}
func thumbnailAt(_ location: CGPoint, peekView: UIView) -> (Thumbnail, CGRect, LinkContainable, Int)? {
if let mediaLinkCell = self as? MediaLinkCell {
if let thumbnailView = mediaLinkCell.thumbnailView {
let p = peekView.convert(location, to: thumbnailView)
for i in 0 ..< thumbnailView.imageViews.count {
if thumbnailView.imageViews[i].frame.contains(p) {
let index = i
let thumbnail = thumbnailView.thumbnails[index]
let sourceRect = thumbnailView.convert(thumbnailView.imageViews[index].frame, to: peekView)
if let container = container {
return (thumbnail, sourceRect, container, index)
}
}
}
}
}
return nil
}
// MARK: - Override
override func awakeFromNib() {
super.awakeFromNib()
setup()
}
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
setup()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
setup()
}
// MARK: - Instance method
func coloringForDebug() {
// contentInfoView.backgroundColor = UIColor.lightGray
// contentToolbar.backgroundColor = UIColor.darkGray
}
// MARK: - getter/setter
var container: LinkContainable? = nil {
didSet {
if let container = container as? LinkContainer {
titleTextView.attributedString = container.attributedTitle
contentInfoView.setNameText(name: container.link.author)
contentInfoView.setDateText(created: container.link.createdUtc)
contentInfoView.setDomainText(domain: container.link.domain)
contentToolbar.setCommentCount(count: container.link.numComments)
contentToolbar.setVotingCount(count: container.link.score)
contentToolbar.setVotingDirection(direction: container.link.likes)
contentToolbar.setSaved(saved: container.link.saved)
}
}
}
// MARK: - Setup
func setupDispatching() {
self.contentToolbar.upVoteButton.addTarget(self, action: #selector(LinkCell.tapUpVoteButton(sender:)), for: .touchUpInside)
self.contentToolbar.downVoteButton.addTarget(self, action: #selector(LinkCell.tapDownVoteButton(sender:)), for: .touchUpInside)
self.contentInfoView.nameButton.addTarget(self, action: #selector(LinkCell.didTapNameButton(sender:)), for: .touchUpInside)
self.contentToolbar.actionButton.addTarget(self, action: #selector(LinkCell.didTapActionButton(sender:)), for: .touchUpInside)
self.contentToolbar.saveButton.addTarget(self, action: #selector(LinkCell.didTapSaveButton(sender:)), for: .touchUpInside)
self.contentToolbar.commentBaseButton.addTarget(self, action: #selector(LinkCell.didTapCommentButton(sender:)), for: .touchUpInside)
}
func setupViews() {
selectionStyle = .none
layoutMargins = UIEdgeInsets.zero
separatorInset = UIEdgeInsets.zero
titleTextView.isUserInteractionEnabled = false
titleTextView.backgroundColor = UIColor.clear
self.contentView.addSubview(titleTextView)
self.contentView.addSubview(contentInfoView)
self.contentView.addSubview(contentToolbar)
self.contentView.addSubview(titleTextView)
}
func setupConstraints() {
contentInfoView.translatesAutoresizingMaskIntoConstraints = false
contentToolbar.translatesAutoresizingMaskIntoConstraints = false
titleTextView.translatesAutoresizingMaskIntoConstraints = false
let views: [String: Any] = [
"titleTextView": titleTextView,
"contentInfoView": contentInfoView,
"contentToolbar": contentToolbar
]
let metric = [
"left": LinkCell.titleLeftMargin,
"right": LinkCell.titleRightMargin,
"contentInfoViewHeight": ContentInfoView.height,
"contentToolbarHeight": ContentToolbar.height,
"verticalTopMargin": LinkCell.verticalTopMargin,
"verticalBottomMargin": LinkCell.verticalBotttomMargin
]
["contentInfoView", "contentToolbar"].forEach({
self.contentView.addConstraints (
NSLayoutConstraint.constraints(withVisualFormat: "H:|-0-[\($0)]-0-|", options: NSLayoutFormatOptions(), metrics: nil, views: views)
)
})
self.contentView.addConstraints (
NSLayoutConstraint.constraints(withVisualFormat: "H:|-left-[titleTextView]-right-|", options: NSLayoutFormatOptions(), metrics: metric, views: views)
)
self.contentView.addConstraints (
NSLayoutConstraint.constraints(withVisualFormat: "V:|-verticalTopMargin-[titleTextView]-verticalBottomMargin-[contentInfoView(==contentInfoViewHeight)]-0-[contentToolbar(==contentToolbarHeight)]-0-|", options: NSLayoutFormatOptions(), metrics: metric, views: views)
)
}
func setup() {
setupViews()
setupConstraints()
setupDispatching()
let rec = UITapGestureRecognizer(target: self, action: #selector(LinkCell.didTapTitleGesture(recognizer:)))
self.addGestureRecognizer(rec)
}
@objc func didTapTitleGesture(recognizer: UITapGestureRecognizer) {
let location = recognizer.location(in: self.contentView)
if titleTextView.frame.contains(location) {
if let container = container {
let userInfo: [String: Any] = ["link": container.link, "contents": container]
NotificationCenter.default.post(name: LinkCellDidTapTitleNotification, object: nil, userInfo: userInfo)
}
}
}
// MARK: - Action
@objc func didTapNameButton(sender: Any) {
if let container = container {
let userInfo: [String: Any] = ["name": container.link.author, "contents": container]
NotificationCenter.default.post(name: LinkCellDidTapNameNotification, object: nil, userInfo: userInfo)
}
}
@objc func didTapActionButton(sender: Any) {
if let container = container {
let userInfo: [String: Any] = ["link": container.link, "contents": container]
NotificationCenter.default.post(name: LinkCellDidTapActionNotification, object: nil, userInfo: userInfo)
}
}
@objc func didTapSaveButton(sender: Any) {
if let container = container {
container.save(saved: !container.link.saved)
contentToolbar.setSaved(saved: !container.link.saved)
}
}
@objc func didTapCommentButton(sender: Any) {
if let container = container {
let userInfo: [String: Any] = ["link": container.link, "contents": container]
NotificationCenter.default.post(name: LinkCellDidTapCommentNotification, object: nil, userInfo: userInfo)
}
}
@objc func tapUpVoteButton(sender: Any) {
if let contents = self.container {
switch contents.link.likes {
case .up:
contentToolbar.setVotingDirection(direction: .up)
case .down:
contentToolbar.setVotingDirection(direction: .none)
contents.vote(voteDirection: .none)
case .none:
contentToolbar.setVotingDirection(direction: .up)
contents.vote(voteDirection: .up)
}
}
}
@objc func tapDownVoteButton(sender: Any) {
if let contents = self.container {
switch contents.link.likes {
case .up:
contentToolbar.setVotingDirection(direction: .none)
contents.vote(voteDirection: .none)
case .down:
contentToolbar.setVotingDirection(direction: .down)
case .none:
contentToolbar.setVotingDirection(direction: .down)
contents.vote(voteDirection: .down)
}
}
}
}