// // TrendingHashtagCollectionViewCell.swift // Tusker // // Created by Shadowfacts on 6/29/22. // Copyright © 2022 Shadowfacts. All rights reserved. // import UIKit import Pachyderm class TrendingHashtagCollectionViewCell: UICollectionViewCell { private let hashtagLabel = UILabel() private let peopleTodayLabel = UILabel() private let historyView = TrendHistoryView() override init(frame: CGRect) { super.init(frame: frame) hashtagLabel.font = .preferredFont(forTextStyle: .title2) hashtagLabel.adjustsFontForContentSizeCategory = true peopleTodayLabel.font = .preferredFont(forTextStyle: .caption1) peopleTodayLabel.adjustsFontForContentSizeCategory = true let vStack = UIStackView(arrangedSubviews: [ hashtagLabel, peopleTodayLabel, ]) vStack.axis = .vertical vStack.alignment = .fill vStack.distribution = .fill vStack.spacing = 0 let hStack = UIStackView(arrangedSubviews: [ vStack, historyView, ]) hStack.axis = .horizontal hStack.alignment = .center hStack.distribution = .fill hStack.spacing = 8 hStack.translatesAutoresizingMaskIntoConstraints = false addSubview(hStack) NSLayoutConstraint.activate([ hStack.leadingAnchor.constraint(equalToSystemSpacingAfter: leadingAnchor, multiplier: 1), trailingAnchor.constraint(equalToSystemSpacingAfter: hStack.trailingAnchor, multiplier: 1), hStack.topAnchor.constraint(equalTo: topAnchor, constant: 8), hStack.bottomAnchor.constraint(equalTo: bottomAnchor, constant: -8), historyView.widthAnchor.constraint(equalToConstant: 100), historyView.heightAnchor.constraint(equalToConstant: 44), ]) } required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } override func updateConfiguration(using state: UICellConfigurationState) { backgroundConfiguration = .appListGroupedCell(for: state) } func updateUI(hashtag: Hashtag) { hashtagLabel.text = "#\(hashtag.name)" historyView.setHistory(hashtag.history) historyView.isHidden = hashtag.history == nil || hashtag.history!.count < 2 if let history = hashtag.history, history.count >= 2 { let sorted = history.sorted(by: { $0.day < $1.day }) let lastTwo = sorted[(sorted.count - 2)...] let accounts = lastTwo.map(\.accounts).reduce(0, +) let uses = lastTwo.map(\.uses).reduce(0, +) let format = NSLocalizedString("trending hashtag info", comment: "trending hashtag posts and people") peopleTodayLabel.text = String.localizedStringWithFormat(format, accounts, uses) peopleTodayLabel.isHidden = false } else { peopleTodayLabel.isHidden = true } } }