<commit_before>//
//  HomeNewsCell.swift
//  PennMobile
//
//  Created by <NAME> on 2/7/19.
//  Copyright © 2019 PennLabs. All rights reserved.
//

import Foundation

final class HomeNewsCell: UITableViewCell, HomeCellConformable {
    static var identifier: String = "homeNewsCell"
    
    var delegate: ModularTableViewCellDelegate!
    var item: ModularTableViewItem! {
        didSet {
            guard let item = item as? HomeNewsCellItem else { return }
            setupCell(with: item)
        }
    }
    
    var article: NewsArticle!
    
    // MARK: Cell Height
    
    static let titleFont: UIFont = UIFont.primaryInformationFont!.withSize(18)
    static let titleEdgeOffset: CGFloat = 16
    
    static let subtitleFont: UIFont = UIFont.primaryInformationFont!.withSize(14)
    
    private static var titleHeightDictionary = [String: CGFloat]()
    private static var subtitleHeightDictionary = [String: CGFloat]()
    
    fileprivate static let showSubtitle = false
    
    static func getCellHeight(for item: ModularTableViewItem) -> CGFloat {
        guard let item = item as? HomeNewsCellItem else { return 0 }
        let imageHeight = getImageHeight()
        let width: CGFloat = UIScreen.main.bounds.width - 2 * 20 - 2 * titleEdgeOffset
        
        let titleHeight: CGFloat
        if let height = titleHeightDictionary[item.article.title] {
            titleHeight = height
        } else {
            titleHeight = item.article.title.dynamicHeight(font: titleFont, width: width)
            titleHeightDictionary[item.article.title] = titleHeight
        }
        
        let subtitleHeight: CGFloat
        if !showSubtitle {
            subtitleHeight = 0
        } else if let height = subtitleHeightDictionary[item.article.subtitle] {
            subtitleHeight = height
        } else {
            subtitleHeight = item.article.subtitle.dynamicHeight(font: subtitleFont, width: width)
            subtitleHeightDictionary[item.article.subtitle] = subtitleHeight
        }
        let height = imageHeight + HomeViewController.cellSpacing + titleHeight + subtitleHeight + 66
        return height
    }
    
    // MARK: UI Elements
    
    var cardView: UIView! = UIView()
    
    fileprivate var articleImageView: UIImageView!
    fileprivate var sourceLabel: UILabel!
    fileprivate var titleLabel: UILabel!
    fileprivate var subtitleLabel: UILabel!
    fileprivate var dateLabel: UILabel!
    fileprivate var moreButton: UIButton!
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        prepareHomeCell()
        prepareUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

// MARK: - Setup Item
extension HomeNewsCell {
    fileprivate func setupCell(with item: HomeNewsCellItem) {
        self.article = item.article
        self.articleImageView.image = item.image
        self.sourceLabel.text = article.source
        self.titleLabel.text = article.title
        self.subtitleLabel?.text = article.subtitle
        self.dateLabel.text = article.timestamp
    }
}

// MARK: - Gesture Recognizer
extension HomeNewsCell {
    fileprivate func getTapGestureRecognizer() -> UITapGestureRecognizer {
        let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(handleTapped(_:)))
        tapGestureRecognizer.numberOfTapsRequired = 1
        return tapGestureRecognizer
    }
    
    @objc fileprivate func handleTapped(_ sender: Any) {
        guard let delegate = delegate as? URLSelectable else { return }
        delegate.handleUrlPressed(article.articleUrl)
    }
}

// MARK: - Prepare UI
extension HomeNewsCell {
    fileprivate func prepareUI() {
        prepareImageView()
        prepareSourceLabel()
        preprareTitleLabel()
        if HomeNewsCell.showSubtitle {
            prepareSubtitleLabel()
        }
        prepareDateLabel()
    }
    
    private func prepareImageView() {
        articleImageView = UIImageView()
        if #available(iOS 11.0, *) {
            articleImageView.layer.cornerRadius = cardView.layer.cornerRadius
            articleImageView.layer.maskedCorners = [.layerMinXMinYCorner, .layerMaxXMinYCorner]
        }
        articleImageView.clipsToBounds = true
        articleImageView.contentMode = .scaleAspectFill
        
        let tapGestureRecognizer = getTapGestureRecognizer()
        articleImageView.addGestureRecognizer(tapGestureRecognizer)
        articleImageView.isUserInteractionEnabled = true
        
        cardView.addSubview(articleImageView)
        let height = HomeNewsCell.getImageHeight()
        _ = articleImageView.anchor(cardView.topAnchor, left: cardView.leftAnchor, bottom: nil, right: cardView.rightAnchor, topConstant: 0, leftConstant: 0, bottomConstant: 0, rightConstant: 0, widthConstant: 0, heightConstant: height)
    }
    
    fileprivate static func getImageHeight() -> CGFloat {
        let cardWidth = UIScreen.main.bounds.width - 40
        return 0.5 * cardWidth
    }
    
    private func prepareSourceLabel() {
        sourceLabel = UILabel()
        sourceLabel.font = UIFont(name: "HelveticaNeue", size: 14)
        sourceLabel.textColor = UIColor.warmGrey
        sourceLabel.numberOfLines = 1
        
        cardView.addSubview(sourceLabel)
        _ = sourceLabel.anchor(articleImageView.bottomAnchor, left: cardView.leftAnchor, bottom: nil, right: cardView.rightAnchor, topConstant: 12, leftConstant: HomeNewsCell.titleEdgeOffset, bottomConstant: 0, rightConstant: HomeNewsCell.titleEdgeOffset, widthConstant: 0, heightConstant: 0)
    }
    
    private func preprareTitleLabel() {
        titleLabel = UILabel()
        titleLabel.font = HomeNewsCell.titleFont
        titleLabel.numberOfLines = 8
        
        let tapGestureRecognizer = getTapGestureRecognizer()
        titleLabel.addGestureRecognizer(tapGestureRecognizer)
        titleLabel.isUserInteractionEnabled = true
        
        cardView.addSubview(titleLabel)
        _ = titleLabel.anchor(sourceLabel.bottomAnchor, left: cardView.leftAnchor, bottom: nil, right: cardView.rightAnchor, topConstant: 8, leftConstant: HomeNewsCell.titleEdgeOffset, bottomConstant: 0, rightConstant: HomeNewsCell.titleEdgeOffset, widthConstant: 0, heightConstant: 0)
    }
    
    private func prepareSubtitleLabel() {
        subtitleLabel = UILabel()
        subtitleLabel.font = HomeNewsCell.subtitleFont
        subtitleLabel.textColor = UIColor.warmGrey
        subtitleLabel.numberOfLines = 3
        
        cardView.addSubview(subtitleLabel)
        _ = subtitleLabel.anchor(titleLabel.bottomAnchor, left: titleLabel.leftAnchor, bottom: nil, right: titleLabel.rightAnchor, topConstant: 8, leftConstant: 0, bottomConstant: 0, rightConstant: 0, widthConstant: 0, heightConstant: 0)
    }
    
    private func prepareDateLabel() {
        dateLabel = UILabel()
        dateLabel.font = UIFont(name: "HelveticaNeue", size: 14)
        dateLabel.textColor = UIColor.warmGrey
        
        cardView.addSubview(dateLabel)
        _ = dateLabel.anchor(nil, left: titleLabel.leftAnchor, bottom: cardView.bottomAnchor, right: nil, topConstant: 0, leftConstant: 0, bottomConstant: 8, rightConstant: 0, widthConstant: 0, heightConstant: 0)
    }
}<commit_msg>moved timestamp from bottom to top<commit_after>//
//  HomeNewsCell.swift
//  PennMobile
//
//  Created by <NAME> on 2/7/19.
//  Copyright © 2019 PennLabs. All rights reserved.
//

import Foundation

final class HomeNewsCell: UITableViewCell, HomeCellConformable {
    static var identifier: String = "homeNewsCell"
    
    var delegate: ModularTableViewCellDelegate!
    var item: ModularTableViewItem! {
        didSet {
            guard let item = item as? HomeNewsCellItem else { return }
            setupCell(with: item)
        }
    }
    
    var article: NewsArticle!
    
    // MARK: Cell Height
    
    static let titleFont: UIFont = UIFont.primaryInformationFont!.withSize(18)
    static let titleEdgeOffset: CGFloat = 16
    
    static let subtitleFont: UIFont = UIFont.primaryInformationFont!.withSize(14)
    
    private static var titleHeightDictionary = [String: CGFloat]()
    private static var subtitleHeightDictionary = [String: CGFloat]()
    
    fileprivate static let showSubtitle = false
    
    static func getCellHeight(for item: ModularTableViewItem) -> CGFloat {
        guard let item = item as? HomeNewsCellItem else { return 0 }
        let imageHeight = getImageHeight()
        let width: CGFloat = UIScreen.main.bounds.width - 2 * 20 - 2 * titleEdgeOffset
        
        let titleHeight: CGFloat
        if let height = titleHeightDictionary[item.article.title] {
            titleHeight = height
        } else {
            titleHeight = item.article.title.dynamicHeight(font: titleFont, width: width)
            titleHeightDictionary[item.article.title] = titleHeight
        }
        
        let subtitleHeight: CGFloat
        if !showSubtitle {
            subtitleHeight = 0
        } else if let height = subtitleHeightDictionary[item.article.subtitle] {
            subtitleHeight = height
        } else {
            subtitleHeight = item.article.subtitle.dynamicHeight(font: subtitleFont, width: width)
            subtitleHeightDictionary[item.article.subtitle] = subtitleHeight
        }
        let height = imageHeight + HomeViewController.cellSpacing + titleHeight + subtitleHeight + 48
        return height
    }
    
    // MARK: UI Elements
    
    var cardView: UIView! = UIView()
    
    fileprivate var articleImageView: UIImageView!
    fileprivate var sourceLabel: UILabel!
    fileprivate var titleLabel: UILabel!
    fileprivate var subtitleLabel: UILabel!
    fileprivate var dateLabel: UILabel!
    fileprivate var moreButton: UIButton!
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        prepareHomeCell()
        prepareUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

// MARK: - Setup Item
extension HomeNewsCell {
    fileprivate func setupCell(with item: HomeNewsCellItem) {
        self.article = item.article
        self.articleImageView.image = item.image
        self.sourceLabel.text = article.source
        self.titleLabel.text = article.title
        self.subtitleLabel?.text = article.subtitle
        self.dateLabel.text = article.timestamp
    }
}

// MARK: - Gesture Recognizer
extension HomeNewsCell {
    fileprivate func getTapGestureRecognizer() -> UITapGestureRecognizer {
        let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(handleTapped(_:)))
        tapGestureRecognizer.numberOfTapsRequired = 1
        return tapGestureRecognizer
    }
    
    @objc fileprivate func handleTapped(_ sender: Any) {
        guard let delegate = delegate as? URLSelectable else { return }
        delegate.handleUrlPressed(article.articleUrl)
    }
}

// MARK: - Prepare UI
extension HomeNewsCell {
    fileprivate func prepareUI() {
        prepareImageView()
        prepareSourceLabel()
        preprareTitleLabel()
        if HomeNewsCell.showSubtitle {
            prepareSubtitleLabel()
        }
        prepareDateLabel()
    }
    
    private func prepareImageView() {
        articleImageView = UIImageView()
        if #available(iOS 11.0, *) {
            articleImageView.layer.cornerRadius = cardView.layer.cornerRadius
            articleImageView.layer.maskedCorners = [.layerMinXMinYCorner, .layerMaxXMinYCorner]
        }
        articleImageView.clipsToBounds = true
        articleImageView.contentMode = .scaleAspectFill
        
        let tapGestureRecognizer = getTapGestureRecognizer()
        articleImageView.addGestureRecognizer(tapGestureRecognizer)
        articleImageView.isUserInteractionEnabled = true
        
        cardView.addSubview(articleImageView)
        let height = HomeNewsCell.getImageHeight()
        _ = articleImageView.anchor(cardView.topAnchor, left: cardView.leftAnchor, bottom: nil, right: cardView.rightAnchor, topConstant: 0, leftConstant: 0, bottomConstant: 0, rightConstant: 0, widthConstant: 0, heightConstant: height)
    }
    
    fileprivate static func getImageHeight() -> CGFloat {
        let cardWidth = UIScreen.main.bounds.width - 40
        return 0.5 * cardWidth
    }
    
    private func prepareSourceLabel() {
        sourceLabel = UILabel()
        sourceLabel.font = UIFont(name: "HelveticaNeue", size: 14)
        sourceLabel.textColor = UIColor.warmGrey
        sourceLabel.numberOfLines = 1
        
        cardView.addSubview(sourceLabel)
        _ = sourceLabel.anchor(articleImageView.bottomAnchor, left: cardView.leftAnchor, bottom: nil, right: cardView.rightAnchor, topConstant: 12, leftConstant: HomeNewsCell.titleEdgeOffset, bottomConstant: 0, rightConstant: HomeNewsCell.titleEdgeOffset, widthConstant: 0, heightConstant: 0)
    }
    
    private func preprareTitleLabel() {
        titleLabel = UILabel()
        titleLabel.font = HomeNewsCell.titleFont
        titleLabel.numberOfLines = 8
        
        let tapGestureRecognizer = getTapGestureRecognizer()
        titleLabel.addGestureRecognizer(tapGestureRecognizer)
        titleLabel.isUserInteractionEnabled = true
        
        cardView.addSubview(titleLabel)
        _ = titleLabel.anchor(sourceLabel.bottomAnchor, left: cardView.leftAnchor, bottom: nil, right: cardView.rightAnchor, topConstant: 8, leftConstant: HomeNewsCell.titleEdgeOffset, bottomConstant: 0, rightConstant: HomeNewsCell.titleEdgeOffset, widthConstant: 0, heightConstant: 0)
    }
    
    private func prepareSubtitleLabel() {
        subtitleLabel = UILabel()
        subtitleLabel.font = HomeNewsCell.subtitleFont
        subtitleLabel.textColor = UIColor.warmGrey
        subtitleLabel.numberOfLines = 3
        
        cardView.addSubview(subtitleLabel)
        _ = subtitleLabel.anchor(titleLabel.bottomAnchor, left: titleLabel.leftAnchor, bottom: nil, right: titleLabel.rightAnchor, topConstant: 8, leftConstant: 0, bottomConstant: 0, rightConstant: 0, widthConstant: 0, heightConstant: 0)
    }
    
    private func prepareDateLabel() {
        dateLabel = UILabel()
        dateLabel.font = UIFont(name: "HelveticaNeue", size: 14)
        dateLabel.textColor = UIColor.warmGrey
        dateLabel.translatesAutoresizingMaskIntoConstraints = false
        
        cardView.addSubview(dateLabel)
//        _ = dateLabel.anchor(nil, left: titleLabel.leftAnchor, bottom: cardView.bottomAnchor, right: nil, topConstant: 0, leftConstant: 0, bottomConstant: 8, rightConstant: 0, widthConstant: 0, heightConstant: 0)
        dateLabel.centerYAnchor.constraint(equalTo: sourceLabel.centerYAnchor).isActive = true
        dateLabel.rightAnchor.constraint(equalTo: articleImageView.rightAnchor, constant: -HomeNewsCell.titleEdgeOffset).isActive = true
    }
}
{'fraction_non_alphanumeric': 0.08, 'fraction_numerical': 0.00835820895522388, 'mean_word_length': 2.942468619246862, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4143510', 'n_tokens_mistral': 4401, 'n_tokens_neox': 4148, 'n_words': 951}