frenzy-ios/Reader/Screens/Items/ItemCollectionViewCell.swift

68 lines
2.4 KiB
Swift
Raw Normal View History

2022-01-09 23:34:17 +00:00
//
// ItemCollectionViewCell.swift
// Reader
//
// Created by Shadowfacts on 1/9/22.
//
import UIKit
import SwiftSoup
2022-01-10 00:23:22 +00:00
class ItemCollectionViewCell: UICollectionViewListCell {
2022-01-09 23:34:17 +00:00
private let titleLabel = UILabel()
private let feedTitleLabel = UILabel()
private let contentLabel = UILabel()
override init(frame: CGRect) {
super.init(frame: frame)
2022-01-10 00:23:22 +00:00
backgroundConfiguration = .clear()
2022-01-09 23:34:17 +00:00
let descriptor = UIFontDescriptor.preferredFontDescriptor(withTextStyle: .title3).withSymbolicTraits(.traitBold)!.withDesign(.serif)!
titleLabel.font = UIFont(descriptor: descriptor, size: 0)
titleLabel.numberOfLines = 0
feedTitleLabel.font = UIFont(descriptor: .preferredFontDescriptor(withTextStyle: .subheadline), size: 0)
feedTitleLabel.textColor = .tintColor
contentLabel.font = UIFont(descriptor: .preferredFontDescriptor(withTextStyle: .body).withDesign(.serif)!, size: 0)
2022-01-10 00:23:22 +00:00
contentLabel.textColor = .appContentPreviewLabel
2022-01-09 23:34:17 +00:00
contentLabel.numberOfLines = 0
let stack = UIStackView(arrangedSubviews: [
titleLabel,
feedTitleLabel,
contentLabel,
])
stack.translatesAutoresizingMaskIntoConstraints = false
stack.spacing = 8
stack.axis = .vertical
addSubview(stack)
NSLayoutConstraint.activate([
stack.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 20),
stack.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -20),
stack.topAnchor.constraint(equalTo: topAnchor, constant: 8),
stack.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -8),
2022-01-10 00:23:22 +00:00
separatorLayoutGuide.leadingAnchor.constraint(equalTo: stack.leadingAnchor),
2022-01-09 23:34:17 +00:00
])
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func updateUI(item: Item) {
titleLabel.text = item.title
feedTitleLabel.text = item.feed!.title ?? item.feed!.url?.host
if let content = item.content {
let doc = try! SwiftSoup.parse(content)
contentLabel.text = try! doc.select("p").first()?.text()
} else {
contentLabel.text = ""
}
contentLabel.isHidden = contentLabel.text?.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty ?? true
}
}