<commit_before>//
//  HomeLaundryCell.swift
//  PennMobile
//
//  Created by <NAME> on 1/17/18.
//  Copyright © 2018 PennLabs. All rights reserved.
//

import Foundation
import UIKit

final class HomeLaundryCell: UITableViewCell, HomeCellConformable {
    static var identifier: String = "laundryCell"
    static func getCellHeight(for item: ModularTableViewItem) -> CGFloat {
        return 380.0 + HomeViewController.cellSpacing
    }
    
    var delegate: ModularTableViewCellDelegate!
    var item: ModularTableViewItem! {
        didSet {
            guard let item = item as? HomeLaundryCellItem else { return }
            setupCell(with: item)
        }
    }
    
    var room: LaundryRoom!
    
    var cardView: UIView! = UIView()

    fileprivate var roomLabel: UILabel!
    fileprivate var buildingLabel: UILabel!
    fileprivate var hairline: UIView!
    
    fileprivate var washerView: LaundryMachinesView!
    fileprivate var dryerView: LaundryMachinesView!
    fileprivate var graphView: LaundryGraphView!
    
    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 HomeLaundryCell {
    fileprivate func setupCell(with item: HomeLaundryCellItem) {
        room = item.room
        roomLabel.text = room.name
        buildingLabel.text = room.building
        washerView.reloadData()
        dryerView.reloadData()
        graphView.reload(with: room.usageData)
    }
}

// MARK: - LaundryMachinesViewDataSource
extension HomeLaundryCell: LaundryMachinesViewDataSource {
    func getMachines(_ machinesView: LaundryMachinesView) -> [LaundryMachine] {
        return machinesView.isWasher ? room.washers : room.dryers
    }
}

// MARK: - LaundryMachineViewDelegate
extension HomeLaundryCell: LaundryMachineViewDelegate {
    var allowMachineNotifications: Bool {
        guard let delegate = delegate as? LaundryMachineCellTappable else { return false }
        return delegate.allowMachineNotifications
    }
    
    func handleMachineCellTapped(for machine: LaundryMachine, _ updateCellIfNeeded: @escaping () -> Void) {
        guard let delegate = delegate as? LaundryMachineCellTappable else { return }
        delegate.handleMachineCellTapped(for: machine, updateCellIfNeeded)
    }
}

// MARK: - Prepare UI Elements
extension HomeLaundryCell {
    fileprivate func prepareUI() {
        prepareLabels()
        prepareHairline()
        prepareWasherDryerMachineViews()
        prepareGraphView()
    }
    
    // MARK: Labels
    
    fileprivate func prepareLabels() {
        roomLabel = getRoomLabel(fontSize: 24)
        buildingLabel = getRoomLabel(fontSize: 14)
        buildingLabel.textColor = .warmGrey
        
        cardView.addSubview(roomLabel)
        cardView.addSubview(buildingLabel)
        
        roomLabel.leadingAnchor.constraint(equalTo: cardView.leadingAnchor, constant: 20).isActive = true
        roomLabel.topAnchor.constraint(equalTo: cardView.topAnchor, constant: 10).isActive = true
        
        buildingLabel.leadingAnchor.constraint(equalTo: roomLabel.leadingAnchor).isActive = true
        buildingLabel.topAnchor.constraint(equalTo: roomLabel.bottomAnchor, constant: 5).isActive = true
    }
    
    private func getRoomLabel(fontSize: CGFloat) -> UILabel {
        let label = UILabel()
        label.font = UIFont(name: "HelveticaNeue", size: fontSize)
        label.textColor = .black
        label.textAlignment = .left
        label.translatesAutoresizingMaskIntoConstraints = false
        return label
    }
    
    // MARK: Machine View
    private func prepareWasherDryerMachineViews() {
        washerView = getMachineView(isWasher: true)
        dryerView = getMachineView(isWasher: false)
        
        cardView.addSubview(washerView)
        cardView.addSubview(dryerView)
        
        let height = LaundryMachinesView.height
        _ = washerView.anchor(hairline.bottomAnchor, left: cardView.leftAnchor, bottom: nil, right: cardView.rightAnchor, topConstant: 16, leftConstant: 0, bottomConstant: 10, rightConstant: 0, widthConstant: 0, heightConstant: height)
        
        _ = dryerView.anchor(washerView.bottomAnchor, left: cardView.leftAnchor, bottom: nil, right: cardView.rightAnchor, topConstant: 6, leftConstant: 0, bottomConstant: 10, rightConstant: 0, widthConstant: 0, heightConstant: height)
    }
    
    private func getMachineView(isWasher: Bool) -> LaundryMachinesView {
        let machinesView = LaundryMachinesView(frame: .zero, isWasher: isWasher)
        machinesView.dataSource = self
        machinesView.delegate = self
        machinesView.translatesAutoresizingMaskIntoConstraints = false
        return machinesView
    }
    
    // MARK: Hairline
    private func prepareHairline() {
        hairline = UIView()
        hairline.backgroundColor = .lightGray
        
        cardView.addSubview(hairline)
        _ = hairline.anchor(buildingLabel.bottomAnchor, left: cardView.leftAnchor, bottom: nil, right: cardView.rightAnchor, topConstant: 10, leftConstant: 8, bottomConstant: 0, rightConstant: 8, widthConstant: 0, heightConstant: 1)
    }
    
    // MARK: Graph View
    private func prepareGraphView() {
        graphView = LaundryGraphView(frame: .zero)
        
        cardView.addSubview(graphView)
        _ = graphView.anchor(dryerView.bottomAnchor, left: cardView.leftAnchor, bottom: cardView.bottomAnchor, right: cardView.rightAnchor, topConstant: 12, leftConstant: 0, bottomConstant: 0, rightConstant: 0, widthConstant: 0, heightConstant: 0)
    }
}<commit_msg>changed the look of home-page-laundry to match rest of cells. copying some changes from home-page-laundry2<commit_after>//
//  HomeLaundryCell.swift
//  PennMobile
//
//  Created by <NAME> on 1/17/18.
//  Copyright © 2018 PennLabs. All rights reserved.
//

import Foundation
import UIKit

final class HomeLaundryCell: UITableViewCell, HomeCellConformable {
    static var identifier: String = "laundryCell"
    static func getCellHeight(for item: ModularTableViewItem) -> CGFloat {
        return 300.0 + HomeViewController.cellSpacing
    }
    
    var delegate: ModularTableViewCellDelegate!
    var item: ModularTableViewItem! {
        didSet {
            guard let item = item as? HomeLaundryCellItem else { return }
            setupCell(with: item)
        }
    }
    
    var room: LaundryRoom!
    
    var cardView: UIView! = UIView()

    // Custom UI elements (some should be abstracted)
    fileprivate let safeInsetValue: CGFloat = 14
    fileprivate var safeArea: UIView!
    
    fileprivate var secondaryTitleLabel: UILabel!
    fileprivate var primaryTitleLabel: UILabel!
    fileprivate var dividerLine: UIView!
    
    fileprivate var washerView: LaundryMachinesView!
    fileprivate var dryerView: LaundryMachinesView!
    
    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 HomeLaundryCell {
    fileprivate func setupCell(with item: HomeLaundryCellItem) {
        room = item.room
        primaryTitleLabel.text = room.name
        secondaryTitleLabel.text = "LAUNDRY - " + room.building.uppercased()
        washerView.reloadData()
        dryerView.reloadData()
    }
}

// MARK: - LaundryMachinesViewDataSource
extension HomeLaundryCell: LaundryMachinesViewDataSource {
    func getMachines(_ machinesView: LaundryMachinesView) -> [LaundryMachine] {
        return machinesView.isWasher ? room.washers : room.dryers
    }
}

// MARK: - LaundryMachineViewDelegate
extension HomeLaundryCell: LaundryMachineViewDelegate {
    var allowMachineNotifications: Bool {
        guard let delegate = delegate as? LaundryMachineCellTappable else { return false }
        return delegate.allowMachineNotifications
    }
    
    func handleMachineCellTapped(for machine: LaundryMachine, _ updateCellIfNeeded: @escaping () -> Void) {
        guard let delegate = delegate as? LaundryMachineCellTappable else { return }
        delegate.handleMachineCellTapped(for: machine, updateCellIfNeeded)
    }
}

// MARK: - Prepare UI Elements
extension HomeLaundryCell {
    fileprivate func prepareUI() {
        prepareSafeArea()
        prepareTitleLabels()
        prepareDividerLine()
        prepareWasherDryerMachineViews()
    }
    
    // MARK: Safe Area
    fileprivate func prepareSafeArea() {
        safeArea = getSafeAreaView()
        
        cardView.addSubview(safeArea)
        
        safeArea.leadingAnchor.constraint(equalTo: cardView.leadingAnchor, constant: safeInsetValue).isActive = true
        safeArea.trailingAnchor.constraint(equalTo: cardView.trailingAnchor, constant: -safeInsetValue).isActive = true
        safeArea.topAnchor.constraint(equalTo: cardView.topAnchor, constant: safeInsetValue).isActive = true
        safeArea.bottomAnchor.constraint(equalTo: cardView.bottomAnchor, constant: -safeInsetValue).isActive = true
    }
    
    // MARK: Labels
    fileprivate func prepareTitleLabels() {
        secondaryTitleLabel = getSecondaryLabel()
        primaryTitleLabel = getPrimaryLabel()
        
        cardView.addSubview(secondaryTitleLabel)
        cardView.addSubview(primaryTitleLabel)
        
        secondaryTitleLabel.leadingAnchor.constraint(equalTo: safeArea.leadingAnchor).isActive = true
        secondaryTitleLabel.topAnchor.constraint(equalTo: safeArea.topAnchor).isActive = true
        
        primaryTitleLabel.leadingAnchor.constraint(equalTo: safeArea.leadingAnchor).isActive = true
        primaryTitleLabel.topAnchor.constraint(equalTo: secondaryTitleLabel.bottomAnchor, constant: 10).isActive = true
    }
    
    // MARK: Divider Line
    fileprivate func prepareDividerLine() {
        dividerLine = getDividerLine()
        
        cardView.addSubview(dividerLine)
        
        dividerLine.leadingAnchor.constraint(equalTo: safeArea.leadingAnchor).isActive = true
        dividerLine.trailingAnchor.constraint(equalTo: safeArea.trailingAnchor).isActive = true
        dividerLine.topAnchor.constraint(equalTo: primaryTitleLabel.bottomAnchor, constant: 14).isActive = true
        dividerLine.heightAnchor.constraint(equalToConstant: 2).isActive = true
    }
    
    // MARK: Machine View
    private func prepareWasherDryerMachineViews() {
        washerView = getMachineView(isWasher: true)
        dryerView = getMachineView(isWasher: false)
        
        cardView.addSubview(washerView)
        cardView.addSubview(dryerView)
        
        let height = LaundryMachinesView.height
        _ = washerView.anchor(dividerLine.bottomAnchor, left: cardView.leftAnchor, bottom: nil, right: cardView.rightAnchor, topConstant: 16, leftConstant: 0, bottomConstant: 10, rightConstant: 0, widthConstant: 0, heightConstant: height)
        
        _ = dryerView.anchor(washerView.bottomAnchor, left: cardView.leftAnchor, bottom: nil, right: cardView.rightAnchor, topConstant: 6, leftConstant: 0, bottomConstant: 10, rightConstant: 0, widthConstant: 0, heightConstant: height)
    }
    
    private func getMachineView(isWasher: Bool) -> LaundryMachinesView {
        let machinesView = LaundryMachinesView(frame: .zero, isWasher: isWasher)
        machinesView.dataSource = self
        machinesView.delegate = self
        machinesView.translatesAutoresizingMaskIntoConstraints = false
        return machinesView
    }
}

// MARK: - UI Element Definitions
extension HomeLaundryCell {
    
    fileprivate func getSafeAreaView() -> UIView {
        let view = UIView()
        view.translatesAutoresizingMaskIntoConstraints = false
        return view
    }
    
    fileprivate func getSecondaryLabel() -> UILabel {
        let label = UILabel()
        label.font = .secondaryTitleFont
        label.textColor = .secondaryTitleGrey
        label.textAlignment = .left
        label.translatesAutoresizingMaskIntoConstraints = false
        return label
    }
    
    fileprivate func getPrimaryLabel() -> UILabel {
        let label = UILabel()
        label.font = .primaryTitleFont
        label.textColor = .primaryTitleGrey
        label.textAlignment = .left
        label.translatesAutoresizingMaskIntoConstraints = false
        return label
    }
    
    fileprivate func getDividerLine() -> UIView {
        let view = UIView()
        view.backgroundColor = .allbirdsGrey
        view.layer.cornerRadius = 2.0
        view.translatesAutoresizingMaskIntoConstraints = false
        return view
    }
}
{'fraction_non_alphanumeric': 0.07968653010552453, 'fraction_numerical': 0.00659528243327126, 'mean_word_length': 3.0904474769914314, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 18, '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': '6810111', 'n_tokens_mistral': 3787, 'n_tokens_neox': 3564, 'n_words': 808}