<commit_before>//
//  ReusableView.swift
//  Alicerce
//
//  Created by <NAME> on 16/12/2016.
//  Copyright © 2016 Mindera. All rights reserved.
//

import UIKit

public protocol ReusableView: View {
    static var reuseIdentifier: String { get }
}

public extension ReusableView where Self: UIView {
    static var reuseIdentifier: String { return "\(self)" }
}

public extension ReusableView where Self: UITableViewCell {
    init() {
        self.init(style: .default, reuseIdentifier: Self.reuseIdentifier)

        setupLayout()
    }
}

public extension ReusableView where Self: UITableViewHeaderFooterView {
    init() {
        self.init(reuseIdentifier: Self.reuseIdentifier)

        setupLayout()
    }
}

// MARK: - UICollectionView Reusable properties

public extension UICollectionView {

    func cell<T: UICollectionViewCell>(`for` indexPath: IndexPath) -> T
    where T: ReusableView {
        guard let cell = dequeueReusableCell(withReuseIdentifier: T.reuseIdentifier, for: indexPath) as? T else {
            assertionFailure("🔥 Did you forget to register cell with identifier `\(T.reuseIdentifier)` for type: `\(T.self)`")
            return T()
        }

        return cell
    }

    func register<T: UICollectionViewCell>(_ cellType: T.Type)
    where T: ReusableView {
        register(cellType, forCellWithReuseIdentifier: cellType.reuseIdentifier)
    }

    func register<T: UICollectionReusableView>(_ viewType: T.Type, forSupplementaryViewOfKind kind: String)
    where T: ReusableView {
        register(viewType, forSupplementaryViewOfKind: kind, withReuseIdentifier: viewType.reuseIdentifier)
    }

    func supplementaryView<T: UICollectionReusableView>(forElementKind elementKind: String,
                                                        at indexPath: IndexPath) -> T
    where T: ReusableView {

        guard let supplementaryView = dequeueReusableSupplementaryView(ofKind: elementKind,
                                                                            withReuseIdentifier: T.reuseIdentifier,
                                                                            for: indexPath) as? T else {
            assertionFailure("🔥 SupplementaryView with identifier `\(T.reuseIdentifier)` not registered for type: `\(T.self)`!")
            return T()
        }

        return supplementaryView
    }
}

// MARK: - UITableView Reusable properties

public extension UITableView {

    func cell<T: UITableViewCell>(`for` indexPath: IndexPath) -> T
    where T: ReusableView {
        guard let cell = dequeueReusableCell(withIdentifier: T.reuseIdentifier, for: indexPath) as? T else {
            assertionFailure("🔥 Did you forget to register cell with identifier `\(T.reuseIdentifier)` for type: `\(T.self)`")
            return T()
        }

        return cell
    }

    func headerFooterView<T: UITableViewCell>() -> T
    where T: ReusableView {
        guard let view = dequeueReusableHeaderFooterView(withIdentifier: T.reuseIdentifier) as? T else {
            assertionFailure("🔥 Did you forget to register view with identifier `\(T.reuseIdentifier)` for type: `\(T.self)`")
            return T()
        }

        return view
    }

    func register<T: UITableViewCell>(_ cellType: T.Type)
    where T: ReusableView {
        register(cellType, forCellReuseIdentifier: cellType.reuseIdentifier)
    }

    func registerHeaderFooterView<T: UITableViewCell>(_ viewType: T.Type)
    where T: ReusableView {
        register(viewType, forHeaderFooterViewReuseIdentifier: T.reuseIdentifier)
    }
}<commit_msg>Enhance cell helpers (#55)<commit_after>//
//  ReusableView.swift
//  Alicerce
//
//  Created by <NAME> on 16/12/2016.
//  Copyright © 2016 Mindera. All rights reserved.
//

import UIKit

public protocol ReusableView: View {
    static var reuseIdentifier: String { get }
}

public extension ReusableView where Self: UIView {
    static var reuseIdentifier: String { return "\(self)" }
}

public extension ReusableView where Self: UITableViewCell {
    init() {
        self.init(style: .default, reuseIdentifier: Self.reuseIdentifier)

        setupLayout()
    }
}

public extension ReusableView where Self: UITableViewHeaderFooterView {
    init() {
        self.init(reuseIdentifier: Self.reuseIdentifier)

        setupLayout()
    }
}

// MARK: - UICollectionView Reusable properties

public extension UICollectionView {

    func dequeueCell<T: UICollectionViewCell>(`for` indexPath: IndexPath) -> T
    where T: ReusableView {
        guard let cell = dequeueReusableCell(withReuseIdentifier: T.reuseIdentifier, for: indexPath) as? T else {
            assertionFailure("🔥 Did you forget to register cell with identifier `\(T.reuseIdentifier)` for type: `\(T.self)`")
            return T()
        }

        return cell
    }
    
    func cell<T: UICollectionViewCell>(`for` indexPath: IndexPath) -> T
    where T: ReusableView {
        guard let cell = cellForItem(at: indexPath) as? T else {
            assertionFailure("🔥 Cell at \(indexPath) is not of type: `\(T.self)`")
            return T()
        }
        
        return cell
    }

    func register<T: UICollectionViewCell>(_ cellType: T.Type)
    where T: ReusableView {
        register(cellType, forCellWithReuseIdentifier: cellType.reuseIdentifier)
    }

    func register<T: UICollectionReusableView>(_ viewType: T.Type, forSupplementaryViewOfKind kind: String)
    where T: ReusableView {
        register(viewType, forSupplementaryViewOfKind: kind, withReuseIdentifier: viewType.reuseIdentifier)
    }

    func dequeueSupplementaryView<T: UICollectionReusableView>(forElementKind elementKind: String,
                                                        at indexPath: IndexPath) -> T
    where T: ReusableView {

        guard let supplementaryView = dequeueReusableSupplementaryView(ofKind: elementKind,
                                                                            withReuseIdentifier: T.reuseIdentifier,
                                                                            for: indexPath) as? T else {
            assertionFailure("🔥 SupplementaryView with identifier `\(T.reuseIdentifier)` not registered for type: `\(T.self)`!")
            return T()
        }

        return supplementaryView
    }
    
    @available(iOS 9, *)
    func supplementaryView<T: UICollectionReusableView>(forElementKind elementKind: String,
                                  at indexPath: IndexPath) -> T
    where T: ReusableView {
        
        guard let supplementaryView = supplementaryView(forElementKind: elementKind, at: indexPath) as? T else {
                                                                        assertionFailure("🔥 SupplementaryView with identifier `\(T.reuseIdentifier)` not registered for type: `\(T.self)`!")
                                                                        return T()
        }
        
        return supplementaryView
    }
}

// MARK: - UITableView Reusable properties

public extension UITableView {

    func dequeueCell<T: UITableViewCell>(`for` indexPath: IndexPath) -> T
    where T: ReusableView {
        guard let cell = dequeueReusableCell(withIdentifier: T.reuseIdentifier, for: indexPath) as? T else {
            assertionFailure("🔥 Did you forget to register cell with identifier `\(T.reuseIdentifier)` for type: `\(T.self)`")
            return T()
        }

        return cell
    }

    func dequeueHeaderFooterView<T: UITableViewCell>() -> T
    where T: ReusableView {
        guard let view = dequeueReusableHeaderFooterView(withIdentifier: T.reuseIdentifier) as? T else {
            assertionFailure("🔥 Did you forget to register view with identifier `\(T.reuseIdentifier)` for type: `\(T.self)`")
            return T()
        }

        return view
    }
    
    func cell<T: UITableViewCell>(`for` indexPath: IndexPath) -> T
    where T: ReusableView {
        guard let cell = cellForRow(at: indexPath) as? T else {
            assertionFailure("🔥 Cell for row at \(indexPath) is not of type: `\(T.self)`")
            return T()
        }
        
        return cell
    }
    
    func headerView<T: UITableViewCell>(forSection section: Int) -> T
    where T: ReusableView {
        guard let view = headerView(forSection: section) as? T else {
            assertionFailure("🔥 Header view at section \(section) is not of type: `\(T.self)`")
            return T()
        }
        
        return view
    }
    
    func footerView<T: UITableViewCell>(forSection section: Int) -> T
    where T: ReusableView {
        guard let view = footerView(forSection: section) as? T else {
            assertionFailure("🔥 Footer view at section \(section) is not of type: `\(T.self)`")
            return T()
        }
        
        return view
    }

    func register<T: UITableViewCell>(_ cellType: T.Type)
    where T: ReusableView {
        register(cellType, forCellReuseIdentifier: cellType.reuseIdentifier)
    }

    func registerHeaderFooterView<T: UITableViewCell>(_ viewType: T.Type)
    where T: ReusableView {
        register(viewType, forHeaderFooterViewReuseIdentifier: T.reuseIdentifier)
    }
}
{'fraction_non_alphanumeric': 0.09100817438692098, 'fraction_numerical': 0.0029427792915531336, 'mean_word_length': 2.323433538572981, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 23, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5508203', 'n_tokens_mistral': 2636, 'n_tokens_neox': 2480, 'n_words': 697}