<commit_before>//
//  DetailTableViewCell+CollectionView.swift
//  Percolator
//
//  Created by <NAME> on 2016-7-29.
//  Copyright © 2016年 <NAME>. All rights reserved.
//

import UIKit
import QuartzCore
import AlamofireImage

class DetailTableViewCell_CollectionView: DetailTableViewCell {
    
    typealias DetailItem = DetailTableViewModel.DetailItem

    @IBOutlet weak var headlineLabel: UILabel!
    @IBOutlet weak var collectionView: CMKCollectionView!

    override func configure(with item: ItemType) {
        super.configure(with: item)
        
        defer {
            collectionView.reloadData()
        }
        
        guard case let ItemType.success(result) = item,
        case let DetailItem.collection(dataSource, delegate, headline, indexPath) = result else {
            return
        }
        
        headlineLabel.text = headline
        collectionView.dataSource = dataSource
        collectionView.delegate = delegate
        collectionView.tag = indexPath.row
        
        collectionView.translatesAutoresizingMaskIntoConstraints = false
    }
    
    override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
        super.traitCollectionDidChange(previousTraitCollection)
        
        if traitCollection.horizontalSizeClass != previousTraitCollection?.horizontalSizeClass || traitCollection.verticalSizeClass != previousTraitCollection?.verticalSizeClass {
            setCollectionViewEdgeInset()
            layoutIfNeeded()
        }
    }
    
    private func setCollectionViewEdgeInset() {
        let isCompact = (traitCollection.horizontalSizeClass == .compact) ? true : false
        
        (collectionView.collectionViewLayout as? UICollectionViewFlowLayout)?.sectionInset = isCompact ? UIEdgeInsets(top: 0, left: superview?.layoutMargins.left ?? 16, bottom: 0, right: superview?.layoutMargins.right ?? 8) : UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 8)
    }

}

class CMKCollectionView: UICollectionView {
    
    let gradientLayer: CAGradientLayer = {
        let layer = CAGradientLayer()
        layer.colors = [UIColor.white.withAlphaComponent(1).cgColor,
                        UIColor.white.withAlphaComponent(0).cgColor,
                        UIColor.white.withAlphaComponent(0).cgColor,
                        UIColor.white.withAlphaComponent(1).cgColor]
        layer.locations = [0.0, 0.02, 0.98, 1.0]
        layer.startPoint = CGPoint(x: 0.0, y: 0.5)
        layer.endPoint = CGPoint(x: 1.0, y: 0.5)
        layer.shouldRasterize = true
        layer.rasterizationScale = UIScreen.main.scale
        layer.drawsAsynchronously = true
        
        return layer
    }()
    
    override func awakeFromNib() {
        super.awakeFromNib()

        self.clipsToBounds = true
        self.layer.insertSublayer(gradientLayer, at: 0)
        self.layer.drawsAsynchronously = true
        self.layer.shouldRasterize = true
        self.layer.rasterizationScale = UIScreen.main.scale
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        layoutGradientLayer()
    }
    
    private func layoutGradientLayer() {
        let threshold = 20.0 / self.bounds.width
        
        // Fix layout delay issue
        CATransaction.begin()
        CATransaction.setDisableActions(true)
        gradientLayer.frame = self.bounds
        gradientLayer.locations = [0.0, min(threshold, (abs(self.contentOffset.x) + 0.5) / self.bounds.width) as NSNumber, 0.97, 1.0]
        CATransaction.commit()
    }
    
    override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
        super.traitCollectionDidChange(previousTraitCollection)
        
        if traitCollection.horizontalSizeClass == .compact {
            gradientLayer.isHidden = true
            self.clipsToBounds = false
        } else {
            gradientLayer.isHidden = false
            self.clipsToBounds = true
        }
    }
    
}

class CMKCollectionViewCell: UICollectionViewCell, ConfigurableCell {
    typealias ItemType = DetailTableViewModel.CollectionItem
    
    @IBOutlet weak var itemImageView: UIImageView!
    @IBOutlet weak var titleLabel: UILabel!
    @IBOutlet weak var subtitleLabel: UILabel!
    
    var isLast: Bool = false
    
    func configure(with item: ItemType) {
        setupCellStyle()
        
        switch item {
        case .crt(let crt):
            titleLabel.text = crt.name
            subtitleLabel.text = crt.actors.first?.name ?? ""
            if let urlPath = crt.images.gridUrl,
            let url = URL(string: urlPath) {
                itemImageView.af_setImageWithURL(url, placeholderImage: UIImage.fromColor(.placeholder, size: CGSize(width: 80, height: 80)), imageTransition: .crossDissolve(0.2))
            } else {
                itemImageView.image = UIImage(named: "404")!
            }
        case .staff(let staff):
            titleLabel.text = staff.name
            subtitleLabel.text = staff.jobs.first ?? ""
            if let urlPath = staff.images.gridUrl,
                let url = URL(string: urlPath) {
                itemImageView.af_setImageWithURL(url, placeholderImage: UIImage.fromColor(.placeholder, size: CGSize(width: 80, height: 80)), imageTransition: .crossDissolve(0.2))
            } else {
                itemImageView.image = UIImage(named: "404")!
            }<commit_msg>fix: cell item reuse cause af_image setting bug<commit_after>//
//  DetailTableViewCell+CollectionView.swift
//  Percolator
//
//  Created by <NAME> on 2016-7-29.
//  Copyright © 2016年 <NAME>. All rights reserved.
//

import UIKit
import QuartzCore
import AlamofireImage

class DetailTableViewCell_CollectionView: DetailTableViewCell {
    
    typealias DetailItem = DetailTableViewModel.DetailItem

    @IBOutlet weak var headlineLabel: UILabel!
    @IBOutlet weak var collectionView: CMKCollectionView!

    override func configure(with item: ItemType) {
        super.configure(with: item)
        
        defer {
            collectionView.reloadData()
        }
        
        guard case let ItemType.success(result) = item,
        case let DetailItem.collection(dataSource, delegate, headline, indexPath) = result else {
            return
        }
        
        headlineLabel.text = headline
        collectionView.dataSource = dataSource
        collectionView.delegate = delegate
        collectionView.tag = indexPath.row
        
        collectionView.translatesAutoresizingMaskIntoConstraints = false
    }
    
    override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
        super.traitCollectionDidChange(previousTraitCollection)
        
        if traitCollection.horizontalSizeClass != previousTraitCollection?.horizontalSizeClass || traitCollection.verticalSizeClass != previousTraitCollection?.verticalSizeClass {
            setCollectionViewEdgeInset()
            layoutIfNeeded()
        }
    }
    
    private func setCollectionViewEdgeInset() {
        let isCompact = (traitCollection.horizontalSizeClass == .compact) ? true : false
        
        (collectionView.collectionViewLayout as? UICollectionViewFlowLayout)?.sectionInset = isCompact ? UIEdgeInsets(top: 0, left: superview?.layoutMargins.left ?? 16, bottom: 0, right: superview?.layoutMargins.right ?? 8) : UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 8)
    }

}

class CMKCollectionView: UICollectionView {
    
    let gradientLayer: CAGradientLayer = {
        let layer = CAGradientLayer()
        layer.colors = [UIColor.white.withAlphaComponent(1).cgColor,
                        UIColor.white.withAlphaComponent(0).cgColor,
                        UIColor.white.withAlphaComponent(0).cgColor,
                        UIColor.white.withAlphaComponent(1).cgColor]
        layer.locations = [0.0, 0.02, 0.98, 1.0]
        layer.startPoint = CGPoint(x: 0.0, y: 0.5)
        layer.endPoint = CGPoint(x: 1.0, y: 0.5)
        layer.shouldRasterize = true
        layer.rasterizationScale = UIScreen.main.scale
        layer.drawsAsynchronously = true
        
        return layer
    }()
    
    override func awakeFromNib() {
        super.awakeFromNib()

        self.clipsToBounds = true
        self.layer.insertSublayer(gradientLayer, at: 0)
        self.layer.drawsAsynchronously = true
        self.layer.shouldRasterize = true
        self.layer.rasterizationScale = UIScreen.main.scale
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        layoutGradientLayer()
    }
    
    private func layoutGradientLayer() {
        let threshold = 20.0 / self.bounds.width
        
        // Fix layout delay issue
        CATransaction.begin()
        CATransaction.setDisableActions(true)
        gradientLayer.frame = self.bounds
        gradientLayer.locations = [0.0, min(threshold, (abs(self.contentOffset.x) + 0.5) / self.bounds.width) as NSNumber, 0.97, 1.0]
        CATransaction.commit()
    }
    
    override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
        super.traitCollectionDidChange(previousTraitCollection)
        
        if traitCollection.horizontalSizeClass == .compact {
            gradientLayer.isHidden = true
            self.clipsToBounds = false
        } else {
            gradientLayer.isHidden = false
            self.clipsToBounds = true
        }
    }
    
}

class CMKCollectionViewCell: UICollectionViewCell, ConfigurableCell {
    typealias ItemType = DetailTableViewModel.CollectionItem
    
    @IBOutlet weak var itemImageView: UIImageView!
    @IBOutlet weak var titleLabel: UILabel!
    @IBOutlet weak var subtitleLabel: UILabel!
    
    var isLast: Bool = false
    
    func configure(with item: ItemType) {
        setupCellStyle()
        
        itemImageView.af_cancelImageRequest()
        
        switch item {
        case .crt(let crt):
            titleLabel.text = crt.name
            subtitleLabel.text = crt.actors.first?.name ?? ""
            if let urlPath = crt.images.gridUrl,
            let url = URL(string: urlPath) {
                itemImageView.af_setImageWithURL(url, placeholderImage: UIImage.fromColor(.placeholder, size: CGSize(width: 80, height: 80)), imageTransition: .crossDissolve(0.2))
            } else {
                itemImageView.image = UIImage(named: "404")!
            }
        case .staff(let staff):
            titleLabel.text = staff.name
            subtitleLabel.text = staff.jobs.first ?? ""
            if let urlPath = staff.images.gridUrl,
                let url = URL(string: urlPath) {
                itemImageView.af_setImageWithURL(url, placeholderImage: UIImage.fromColor(.placeholder, size: CGSize(width: 80, height: 80)), imageTransition: .crossDissolve(0.2))
            } else {
                itemImageView.image = UIImage(named: "404")!
            }
{'fraction_non_alphanumeric': 0.08806531510870562, 'fraction_numerical': 0.013393266672782313, 'mean_word_length': 2.3952039862971035, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '676941', 'n_tokens_mistral': 3192, 'n_tokens_neox': 3010, 'n_words': 620}