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

71 lines
2.4 KiB
Swift

//
// ItemCollectionViewCell.swift
// Reader
//
// Created by Shadowfacts on 1/9/22.
//
import UIKit
import SwiftSoup
class ItemCollectionViewCell: UICollectionViewCell {
private let titleLabel = UILabel()
private let feedTitleLabel = UILabel()
private let contentLabel = UILabel()
override init(frame: CGRect) {
super.init(frame: frame)
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)
contentLabel.textColor = UIColor(dynamicProvider: { traitCollection in
if traitCollection.userInterfaceStyle == .dark {
return .lightGray
} else {
return .darkGray
}
})
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),
])
}
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
}
}