<commit_before>//
//  ACAlertControllerCore.swift
//  ACAlertControllerDemo
//
//  Created by Yury on 21/09/16.
//  Copyright © 2016 Avtolic. All rights reserved.
//

import Foundation
import UIKit

extension UIEdgeInsets {
    var topPlusBottom: CGFloat { return top + bottom }
    var leftPlusRight: CGFloat { return left + right }
    init(top: CGFloat, bottom: CGFloat) {
        self.init(top: top, left: 0, bottom: bottom, right: 0)
    }
}

protocol ACAlertListViewProtocol {
    var contentHeight: CGFloat { get }
    var view: UIView { get }
}

protocol ACAlertListViewProvider {
    func alertView(items : [(ACAlertItemProtocol, UIEdgeInsets?)], width: CGFloat) -> ACAlertListViewProtocol
    func alertView(actions : [(ACAlertActionProtocol, UIEdgeInsets?)], width: CGFloat) -> ACAlertListViewProtocol
}

class ACAlertListView: ACAlertListViewProtocol {

    var contentHeight: CGFloat = 0
    var view: UIView = UIView()
    
    init(height: CGFloat, color: UIColor) {
        contentHeight = height
        view.backgroundColor = color
    }
}

class TabledItemsViewProvider: NSObject, ACAlertListViewProvider {
    func alertView(items: [(ACAlertItemProtocol, UIEdgeInsets?)], width: CGFloat) -> ACAlertListViewProtocol {
        return ACAlertListView(height: CGFloat(items.count) * 30, color: UIColor.green)
    }
    func alertView(actions: [(ACAlertActionProtocol, UIEdgeInsets?)], width: CGFloat) -> ACAlertListViewProtocol {
        return ACAlertListView(height: CGFloat(actions.count) * 45, color: UIColor.orange)
    }
}

//    var items: [(ACAlertItemProtocol, UIEdgeInsets)] = []
//    let tableView = UITableView()
//    let cellName = "Cell"
//    
//    var margins: UIEdgeInsets
//    
//    func set(items: [(ACAlertItemProtocol, UIEdgeInsets)], width: CGFloat) {
//        self.items = items
//        tableView.bounds.size.width = width
//        tableView.reloadData()
//    }
//
//    override init() {
//        super.init()
//        tableView.dataSource = self
//        tableView.register(UITableViewCell.self, forCellReuseIdentifier: cellName)
//    }
//    
//    func view(height: CGFloat) -> UIView {
//        tableView.bounds.size.height = height
//        return tableView
//    }
//
//    var contentHeight: CGFloat {
//        return tableView.contentSize.height
//    }
//}

//extension TabledItemsViewProvider: UITableViewDataSource {
//
//    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
//        let cell = tableView.dequeueReusableCell(withIdentifier: cellName, for: indexPath)
//        let (item, inset) = items[indexPath.row]
//
//        return cell
//    }
//    
//    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
//        return items.count
//    }
//}

class ACAlertControllerBase : UIViewController{

    fileprivate(set) open var items: [(ACAlertItemProtocol, UIEdgeInsets?)] = []
    fileprivate(set) open var actions: [(ACAlertActionProtocol, UIEdgeInsets?)] = []
    
//    open var items: [ACAlertItemProtocol] { return items.map{ $0.0 } }
//    fileprivate(set) open var actions: [ACAlertActionProtocol] = []
    
    open var backgroundColor = UIColor.brown//UIColor.white.withAlphaComponent(0.25)
    
    open var viewMargins = UIEdgeInsets(top: 5, left: 5, bottom: 5, right: 5)//UIEdgeInsets(top: 15, bottom: 15)
//    open var itemsMargins = UIEdgeInsets(top: 4, bottom: 4)
//    open var actionsMargins = UIEdgeInsets(top: 4, bottom: 4)
    
    open var alertWidth: CGFloat = 270
    open var cornerRadius: CGFloat = 16
    var separatorHeight: CGFloat = 0.5
    
    var alertListsProvider: ACAlertListViewProvider = TabledItemsViewProvider()
    
    var itemsAlertList: ACAlertListViewProtocol?
    var actionsAlertList: ACAlertListViewProtocol?
    
    var separatorView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.yellow
        return view
    }()
    
    // MARK: Public methods
    
    public init() {
        super.init(nibName: nil, bundle: nil)
        modalPresentationStyle = .overFullScreen
        //        transitioningDelegate = self
    }
    
    required public init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    open func addItem(_ item: ACAlertItemProtocol, inset: UIEdgeInsets? = nil) {
        guard isBeingPresented == false else {
            print("ACAlertController could not be modified if it is already presented")
            return
        }
        items.append((item, inset))
    }
    
    open func addAction(_ action: ACAlertActionProtocol, inset: UIEdgeInsets? = nil) {
        guard isBeingPresented == false else {<commit_msg>Partly working version with UITableView based items<commit_after>//
//  ACAlertControllerCore.swift
//  ACAlertControllerDemo
//
//  Created by Yury on 21/09/16.
//  Copyright © 2016 Avtolic. All rights reserved.
//

import Foundation
import UIKit

extension UIEdgeInsets {
    var topPlusBottom: CGFloat { return top + bottom }
    var leftPlusRight: CGFloat { return left + right }
    init(top: CGFloat, bottom: CGFloat) {
        self.init(top: top, left: 0, bottom: bottom, right: 0)
    }
}

protocol ACAlertListViewProtocol {
    var contentHeight: CGFloat { get }
    var view: UIView { get }
}

protocol ACAlertListViewProvider {
    func alertView(items : [(ACAlertItemProtocol, UIEdgeInsets)], width: CGFloat) -> ACAlertListViewProtocol
    func alertView(actions : [(ACAlertActionProtocol, UIEdgeInsets?)], width: CGFloat) -> ACAlertListViewProtocol
}

class ACAlertListView: ACAlertListViewProtocol {

    var contentHeight: CGFloat = 0
    var view: UIView = UIView()
    
    init(height: CGFloat, color: UIColor) {
        contentHeight = height
        view.backgroundColor = color
    }
}

class TabledItemsViewProvider: NSObject, ACAlertListViewProvider {
    func alertView(items: [(ACAlertItemProtocol, UIEdgeInsets)], width: CGFloat) -> ACAlertListViewProtocol {
        let views = items.map { (item, insets) in return (item.alertItemView, insets) }
        return ACTableAlertListView(views: views)
//        return ACAlertListView(height: CGFloat(items.count) * 30, color: UIColor.green)
    }
    func alertView(actions: [(ACAlertActionProtocol, UIEdgeInsets?)], width: CGFloat) -> ACAlertListViewProtocol {
        return ACAlertListView(height: CGFloat(actions.count) * 45, color: UIColor.orange)
    }
}

class ACTableAlertListView: UITableView, ACAlertListViewProtocol, UITableViewDataSource {
    
    let cells: [UITableViewCell]
    var view: UIView { return self }
    var contentHeight: CGFloat { return contentSize.height }
    
    init(views: [(UIView, UIEdgeInsets)]) {

        cells = views.map{ (view, insets) -> UITableViewCell in
            let cell = UITableViewCell()
            cell.contentView.layoutMargins = insets
//            cell.textLabel?.text = "GGG!"
            cell.contentView.addSubview(view)
            cell.contentView.backgroundColor = UIColor.green
            view.translatesAutoresizingMaskIntoConstraints = false
            ACTableAlertListView.setContent(view: cell.contentView, subview: view)
            
            return cell
        }
        super.init(frame: .zero, style: .plain)
        dataSource = self

        self.rowHeight = UITableViewAutomaticDimension
        self.estimatedRowHeight = 50
        reloadData()
        self.layoutSubviews()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return cells.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = cells[indexPath.row]
//        NSLayoutConstraint(item: cell.contentView, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 200).isActive = true
        return cell
    }
    
    class func setContent(view: UIView, subview: UIView) {
        
        NSLayoutConstraint(item: view, attribute: .leftMargin, relatedBy: .lessThanOrEqual, toItem: subview, attribute: .left, multiplier: 1, constant: 0).isActive = true
        NSLayoutConstraint(item: view, attribute: .rightMargin, relatedBy: .greaterThanOrEqual, toItem: subview, attribute: .right, multiplier: 1, constant: 0).isActive = true
        NSLayoutConstraint(item: view, attribute: .centerXWithinMargins, relatedBy: .equal, toItem: subview, attribute: .centerX, multiplier: 1, constant: 0).isActive = true
        NSLayoutConstraint(item: view, attribute: .topMargin, relatedBy: .equal, toItem: subview, attribute: .top, multiplier: 1, constant: 0).isActive = true
        NSLayoutConstraint(item: view, attribute: .bottomMargin, relatedBy: .equal, toItem: subview, attribute: .bottom, multiplier: 1, constant: 0).isActive = true
    }
}
//    var items: [(ACAlertItemProtocol, UIEdgeInsets)] = []
//    let tableView = UITableView()
//    let cellName = "Cell"
//    
//    var margins: UIEdgeInsets
//    
//    func set(items: [(ACAlertItemProtocol, UIEdgeInsets)], width: CGFloat) {
//        self.items = items
//        tableView.bounds.size.width = width
//        tableView.reloadData()
//    }
//
//    override init() {
//        super.init()
//        tableView.dataSource = self
//        tableView.register(UITableViewCell.self, forCellReuseIdentifier: cellName)
//    }
//    
//    func view(height: CGFloat) -> UIView {
//        tableView.bounds.size.height = height
//        return tableView
//    }
//
//    var contentHeight: CGFloat {
//        return tableView.contentSize.height
//    }
//}

//extension TabledItemsViewProvider: UITableViewDataSource {
//
//    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
//        let cell = tableView.dequeueReusableCell(withIdentifier: cellName, for: indexPath)
//        let (item, inset) = items[indexPath.row]
//
//        return cell
//    }
//    
//    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
//        return items.count
//    }
//}

class ACAlertControllerBase : UIViewController{

    fileprivate(set) open var items: [(ACAlertItemProtocol, UIEdgeInsets)] = []
    fileprivate(set) open var actions: [(ACAlertActionProtocol, UIEdgeInsets?)] = []
    
//    open var items: [ACAlertItemProtocol] { return items.map{ $0.0 } }
//    fileprivate(set) open var actions: [ACAlertActionProtocol] = []
    
    open var backgroundColor = UIColor.brown//UIColor.white.withAlphaComponent(0.25)
    
    open var viewMargins = UIEdgeInsets(top: 5, left: 5, bottom: 5, right: 5)//UIEdgeInsets(top: 15, bottom: 15)
    open var defaultItemsMargins = UIEdgeInsets(top: 2, left: 0, bottom: 2, right: 0) // Applied to items
//    open var itemsMargins = UIEdgeInsets(top: 4, bottom: 4)
//    open var actionsMargins = UIEdgeInsets(top: 4, bottom: 4)
    
    open var alertWidth: CGFloat = 270
    open var cornerRadius: CGFloat = 16
    var separatorHeight: CGFloat = 0.5
    
    var alertListsProvider: ACAlertListViewProvider = TabledItemsViewProvider()
    
    var itemsAlertList: ACAlertListViewProtocol?
    var actionsAlertList: ACAlertListViewProtocol?
    
    var separatorView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.yellow
        return view
    }()
    
    // MARK: Public methods
    
    public init() {
        super.init(nibName: nil, bundle: nil)
        modalPresentationStyle = .overFullScreen
        //        transitioningDelegate = self
    }
    
    required public init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    open func addItem(_ item: ACAlertItemProtocol, inset: UIEdgeInsets? = nil) {
        guard isBeingPresented == false else {
            print("ACAlertController could not be modified if it is already presented")
            return
        }
        items.append((item, inset ?? defaultItemsMargins))
    }
    
    open func addAction(_ action: ACAlertActionProtocol, inset: UIEdgeInsets? = nil) {
        guard isBeingPresented == false else {
{'fraction_non_alphanumeric': 0.10829061915222521, 'fraction_numerical': 0.008298755186721992, 'mean_word_length': 2.951141112182578, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 20, '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': '2484762', 'n_tokens_mistral': 3831, 'n_tokens_neox': 3598, 'n_words': 920}