<commit_before>//
//  TableViewHelp.swift
//  SnapKitchen
//
//  Created by <NAME> on 4/21/16.
//  Copyright © 2016 Snap Kitchen. All rights reserved.
//

import UIKit


public protocol TableCellConfigurer {
    func configureIn(_ definition: TableRow, indexPath: IndexPath)
}

public extension TableCellConfigurer {
    public func configureIn(_ definition: TableRow, indexPath: IndexPath) {
        definition.configureIn(self, indexPath: indexPath)
    }
}

public enum TableRowSource {
    case nib(String)
    case `class`(String)
    case prototype
}

//MARK: Table Row

// Implement TableRow, add rows to sections to build the table
public protocol TableRow: class {
    // Need either a nib or a class to register with

    var source: TableRowSource {get}

    var reuseIdentifier: String { get }
    var height: CGFloat { get }
    var action: String? { get }

    func configureIn(_ cell: TableCellConfigurer, indexPath: IndexPath)
}

public protocol TableSection {
    var rows: [TableRow] { get set }
}

// A section with header or footer
public protocol TableSectionWithSupplementaryViews: TableSection {
    var headerHeight: CGFloat { get }
    func headerViewForTableView(_ tableView: UITableView) -> UIView?
}

// A regular section with no headers or footers
open class DefaultTableSection: TableSection {
    open var rows = [TableRow]()
    public init() {}
}

// MARK: - Table view data source

open class TableViewDataSourceHelper: NSObject, UITableViewDataSource {
    open var sections = [TableSection]()

    // Iterate through all the rows ensuring each cell's NIB and class is registered with the table
    open func registerConfigurers(_ tableView: UITableView) {

        for section in sections {
            for row in section.rows {
                switch row.source {
                case .nib(let nibName):
                    let nib = UINib(nibName: nibName, bundle: nil)
                    tableView.register(nib, forCellReuseIdentifier: row.reuseIdentifier)
                case .class(let className):
                    let clazz:AnyClass = className.classFromClassName()
                    tableView.register(clazz, forCellReuseIdentifier: row.reuseIdentifier)
                case .prototype:
                    // Prototype cells are registered by storyboard loader. 
                    break
                }

            }
        }
    }

    /// Register nib with name identical to its reuse ID
    static open func registerNib(_ tableView: UITableView, name: String) {
        let nib = UINib(nibName: name, bundle: nil)
        tableView.register(nib, forCellReuseIdentifier: name)
    }


    @objc open func numberOfSections(in tableView: UITableView) -> Int {
        return sections.count
    }


    @objc open func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return sections[section].rows.count
    }


    @objc open func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let section = sections[(indexPath as NSIndexPath).section]
        let row = section.rows[(indexPath as NSIndexPath).row]

        let cell = tableView.dequeueReusableCell(withIdentifier: row.reuseIdentifier, for: indexPath)

        if let configurer = cell as? TableCellConfigurer {
            configurer.configureIn(row, indexPath: indexPath)
        }

        return cell
    }

}

// MARK: - Table view delegate

open class TableViewDelegateHelper: NSObject, UITableViewDelegate {

    public init(actionTarget: NSObjectProtocol) {
        self.target = actionTarget
        super.init()
    }

    public weak var target: NSObjectProtocol?

    @objc open func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let dataSource = tableView.dataSource as! TableViewDataSourceHelper

        let section = dataSource.sections[(indexPath as NSIndexPath).section]
        let row = section.rows[(indexPath as NSIndexPath).row]

        if let action = row.action, let target = target {
            if action.hasSuffix("::") {
                target.perform(Selector(action), with: row, with: indexPath)
            }
            else {
                target.perform(Selector(action), with: row)
            }
        }
    }
}


open class TableViewDelegateVariableRowHeightHelper: TableViewDelegateHelper {
    public override init(actionTarget: NSObjectProtocol) {
        super.init(actionTarget: actionTarget)
    }

    @objc open func tableView(_ tableView: UITableView, heightForRowAtIndexPath indexPath: IndexPath) -> CGFloat {
        let dataSource = tableView.dataSource as! TableViewDataSourceHelper

        let section = dataSource.sections[(indexPath as NSIndexPath).section]
        let row = section.rows[(indexPath as NSIndexPath).row]
        return row.height
    }
}

// Table with section header or footers
open class TableViewDelegateWithSupplementaryViewsHelper: TableViewDelegateVariableRowHeightHelper {

    public override init(actionTarget: NSObjectProtocol) {
        super.init(actionTarget: actionTarget)
    }

    @objc open func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let dataSource = tableView.dataSource as! TableViewDataSourceHelper
        let section = dataSource.sections[section] as! TableSectionWithSupplementaryViews
        return section.headerViewForTableView(tableView)<commit_msg>Update table method to match API.<commit_after>//
//  TableViewHelp.swift
//  SnapKitchen
//
//  Created by <NAME> on 4/21/16.
//  Copyright © 2016 Snap Kitchen. All rights reserved.
//

import UIKit


public protocol TableCellConfigurer {
    func configureIn(_ definition: TableRow, indexPath: IndexPath)
}

public extension TableCellConfigurer {
    public func configureIn(_ definition: TableRow, indexPath: IndexPath) {
        definition.configureIn(self, indexPath: indexPath)
    }
}

public enum TableRowSource {
    case nib(String)
    case `class`(String)
    case prototype
}

//MARK: Table Row

// Implement TableRow, add rows to sections to build the table
public protocol TableRow: class {
    // Need either a nib or a class to register with

    var source: TableRowSource {get}

    var reuseIdentifier: String { get }
    var height: CGFloat { get }
    var action: String? { get }

    func configureIn(_ cell: TableCellConfigurer, indexPath: IndexPath)
}

public protocol TableSection {
    var rows: [TableRow] { get set }
}

// A section with header or footer
public protocol TableSectionWithSupplementaryViews: TableSection {
    var headerHeight: CGFloat { get }
    func headerViewForTableView(_ tableView: UITableView) -> UIView?
}

// A regular section with no headers or footers
open class DefaultTableSection: TableSection {
    open var rows = [TableRow]()
    public init() {}
}

// MARK: - Table view data source

open class TableViewDataSourceHelper: NSObject, UITableViewDataSource {
    open var sections = [TableSection]()

    // Iterate through all the rows ensuring each cell's NIB and class is registered with the table
    open func registerConfigurers(_ tableView: UITableView) {

        for section in sections {
            for row in section.rows {
                switch row.source {
                case .nib(let nibName):
                    let nib = UINib(nibName: nibName, bundle: nil)
                    tableView.register(nib, forCellReuseIdentifier: row.reuseIdentifier)
                case .class(let className):
                    let clazz:AnyClass = className.classFromClassName()
                    tableView.register(clazz, forCellReuseIdentifier: row.reuseIdentifier)
                case .prototype:
                    // Prototype cells are registered by storyboard loader. 
                    break
                }

            }
        }
    }

    /// Register nib with name identical to its reuse ID
    static open func registerNib(_ tableView: UITableView, name: String) {
        let nib = UINib(nibName: name, bundle: nil)
        tableView.register(nib, forCellReuseIdentifier: name)
    }


    @objc open func numberOfSections(in tableView: UITableView) -> Int {
        return sections.count
    }


    @objc open func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return sections[section].rows.count
    }


    @objc open func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let section = sections[(indexPath as NSIndexPath).section]
        let row = section.rows[(indexPath as NSIndexPath).row]

        let cell = tableView.dequeueReusableCell(withIdentifier: row.reuseIdentifier, for: indexPath)

        if let configurer = cell as? TableCellConfigurer {
            configurer.configureIn(row, indexPath: indexPath)
        }

        return cell
    }

}

// MARK: - Table view delegate

open class TableViewDelegateHelper: NSObject, UITableViewDelegate {

    public init(actionTarget: NSObjectProtocol) {
        self.target = actionTarget
        super.init()
    }

    public weak var target: NSObjectProtocol?

    @objc open func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let dataSource = tableView.dataSource as! TableViewDataSourceHelper

        let section = dataSource.sections[(indexPath as NSIndexPath).section]
        let row = section.rows[(indexPath as NSIndexPath).row]

        if let action = row.action, let target = target {
            if action.hasSuffix("::") {
                target.perform(Selector(action), with: row, with: indexPath)
            }
            else {
                target.perform(Selector(action), with: row)
            }
        }
    }
}


open class TableViewDelegateVariableRowHeightHelper: TableViewDelegateHelper {
    public override init(actionTarget: NSObjectProtocol) {
        super.init(actionTarget: actionTarget)
    }

    @objc open func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        let dataSource = tableView.dataSource as! TableViewDataSourceHelper

        let section = dataSource.sections[(indexPath as NSIndexPath).section]
        let row = section.rows[(indexPath as NSIndexPath).row]
        return row.height
    }
}

// Table with section header or footers
open class TableViewDelegateWithSupplementaryViewsHelper: TableViewDelegateVariableRowHeightHelper {

    public override init(actionTarget: NSObjectProtocol) {
        super.init(actionTarget: actionTarget)
    }

    @objc open func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let dataSource = tableView.dataSource as! TableViewDataSourceHelper
        let section = dataSource.sections[section] as! TableSectionWithSupplementaryViews
        return section.headerViewForTableView(tableView)
{'fraction_non_alphanumeric': 0.07710644540203539, 'fraction_numerical': 0.0016503163106262033, 'mean_word_length': 2.855779427359491, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 17, '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': '5192725', 'n_tokens_mistral': 3008, 'n_tokens_neox': 2819, 'n_words': 884}