in FriendlyPix/FPCardCollectionViewCell.swift [82:183]
func populateContent(post: FPPost, index: Int, isDryRun: Bool) {
if Auth.auth().currentUser!.isAnonymous {
likeButton.isEnabled = false
}
self.post = post
let postAuthor = post.author
if !isDryRun, let profilePictureURL = postAuthor.profilePictureURL {
UIImage.circleImage(with: profilePictureURL, to: authorImageView)
authorImageView.accessibilityLabel = postAuthor.fullname
}
authorLabel.text = postAuthor.fullname
dateLabel.text = post.postDate.timeAgo()
postImageView.tag = index
if !isDryRun {
let trace = Performance.startTrace(name: "post_load")
postImageView?.sd_setImage(with: post.thumbURL, completed: { image, error, cacheType, url in
trace?.incrementMetric(self.convertCacheTypeToString(cacheType), by: 1)
trace?.stop()
})
postImageView.accessibilityLabel = "Photo by \(postAuthor.fullname)"
}
let title = NSMutableAttributedString(string: postAuthor.fullname + " ", attributes: attributes)
let attrString = NSMutableAttributedString(string: post.text)
let regex = try? NSRegularExpression(pattern: "(#[a-zA-Z0-9_\\p{Arabic}\\p{N}]*)", options: [])
if let matches = regex?.matches(in: post.text, options:[], range:NSMakeRange(0, post.text.count)) {
for match in matches {
attrString.addAttribute(NSAttributedString.Key.link, value: (post.text as NSString).substring(with: match.range), range: match.range)
attrString.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.blue , range: match.range)
}
}
title.append(attrString)
title.addAttribute(.paragraphStyle, value: MDCSelfSizingStereoCell.paragraphStyle, range: NSMakeRange(0, title.length))
titleLabel.attributedText = title
titleLabel.accessibilityLabel = "\(post.text), posted by \(postAuthor.fullname)"
titleLabel.addGestureRecognizer(UITapGestureRecognizer(target: self,
action: #selector(handleTapOnProfileLabel(recognizer:))))
likesLabel.text = post.likeCount == 1 ? "1 like" : "\(post.likeCount) likes"
likesLabel.font = UIFont.mdc_preferredFont(forMaterialTextStyle: .body2)
if post.isLiked {
likeButton.setImage(#imageLiteral(resourceName: "ic_favorite"), for: .normal)
likeButton.accessibilityLabel = "you liked this post"
likeButton.accessibilityHint = "double-tap to unlike"
} else {
likeButton.setImage(#imageLiteral(resourceName: "ic_favorite_border"), for: .normal)
likeButton.accessibilityLabel = "you haven't liked this post"
likeButton.accessibilityHint = "double-tap to like"
}
if labelConstraints != nil {
NSLayoutConstraint.deactivate(labelConstraints)
labelConstraints = nil
}
let betweenConstant: CGFloat = 2
let bottomConstant: CGFloat = 12
let commentCount = post.comments.count
switch commentCount {
case 0:
labelConstraints = [contentView.bottomAnchor.constraint(equalTo: titleLabel.bottomAnchor,
constant: bottomConstant)]
viewAllCommentsLabel.isHidden = true
comment1Label.isHidden = true
comment1Label.text = nil
comment2Label.isHidden = true
comment2Label.text = nil
case 1:
labelConstraints = [comment1Label.topAnchor.constraint(equalTo: titleLabel.bottomAnchor,
constant: betweenConstant),
contentView.bottomAnchor.constraint(equalTo: comment1Label.bottomAnchor,
constant: bottomConstant)]
viewAllCommentsLabel.isHidden = true
attributeComment(index: 0)
comment2Label.isHidden = true
comment2Label.text = nil
case 2:
labelConstraints = [comment1Label.topAnchor.constraint(equalTo: titleLabel.bottomAnchor,
constant: betweenConstant),
comment2Label.topAnchor.constraint(equalTo: comment1Label.bottomAnchor,
constant: betweenConstant),
contentView.bottomAnchor.constraint(equalTo: comment2Label.bottomAnchor,
constant: bottomConstant)]
viewAllCommentsLabel.isHidden = true
attributeComment(index: 0)
attributeComment(index: 1)
default:
labelConstraints = [viewAllCommentsLabel.topAnchor.constraint(equalTo: titleLabel.bottomAnchor,
constant: betweenConstant),
comment1Label.topAnchor.constraint(equalTo: viewAllCommentsLabel.bottomAnchor,
constant: betweenConstant),
comment2Label.topAnchor.constraint(equalTo: comment1Label.bottomAnchor,
constant: betweenConstant),
contentView.bottomAnchor.constraint(equalTo: comment2Label.bottomAnchor,
constant: bottomConstant)]
viewAllCommentsLabel.isHidden = false
viewAllCommentsLabel.setTitle("View all \(commentCount) comments", for: .normal)
attributeComment(index: 0)
attributeComment(index: 1)
}
NSLayoutConstraint.activate(labelConstraints)
}