<commit_before>//
//  ActionSheet.swift
//  SmartReceipts
//
//  Created by <NAME> on 24/11/2019.
//  Copyright © 2017 <NAME>. All rights reserved.
//

import Foundation
import RxSwift

class ActionSheet {
    private var sheetViewController: ActionSheetViewController
    
    init(closable: Bool = true) {
        sheetViewController = ActionSheetViewController.create()
        sheetViewController.closable = closable
        sheetViewController.modalPresentationStyle = .custom
        sheetViewController.loadViewIfNeeded()
    }
    
    @discardableResult
    func addAction(title: String, image: UIImage? = nil, style: ActionButtonView.Style = .normal) -> Observable<Void> {
        return sheetViewController.addAction(title: title, image: image, style: style)
    }
    
    func show() {
        UIApplication.shared.topViewCtonroller?.present(sheetViewController, animated: true, completion: nil)
    }
}

class ActionSheetViewController: UIViewController, Storyboardable, Containerable {
    private let bag = DisposeBag()
    
    @IBOutlet private weak var backView: UIView!
    @IBOutlet private weak var stackView: UIStackView!
    @IBOutlet private var separator: UIView!
    @IBOutlet private var closeButton: UIButton!
    
    @IBOutlet private weak var tapGesture: UITapGestureRecognizer!
    
    private let transitionDelegate = BottomSheetTransitionDelegate()
    fileprivate var closable: Bool = true
    
    var container: UIView { return backView }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        transitioningDelegate = transitionDelegate
        
        backView.layer.cornerRadius = 24
        backView.layer.shadowRadius = 32
        backView.layer.shadowColor = UIColor.black.cgColor
        backView.layer.shadowOffset = .init(width: 0, height: 4)
        backView.layer.shadowOpacity = 0.24
        
        stackView.arrangedSubviews
            .forEach { $0.removeFromSuperview() }
        
        separator.isHidden = !closable
        closeButton.isHidden = !closable
        
        Observable.merge(closeButton.rx.tap.asObservable(), tapGesture.rx.event.map({ _ in }))
            .filter { [weak self] in self?.closable == true }
            .subscribe(onNext: { [weak self] in
                self?.dismiss(animated: true, completion: nil)
            }).disposed(by: bag)
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        stackView.addArrangedSubview(separator)
        stackView.addArrangedSubview(closeButton)
    }
    
    func addAction(title: String, image: UIImage? = nil, style: ActionButtonView.Style) -> Observable<Void> {
        let buttonView = ActionButtonView(title: title, image: image, style: style)
        stackView.addArrangedSubview(buttonView)
        
        return buttonView.rx.tap.flatMap { _ -> Observable<Void> in
            return .create { [weak self] observer -> Disposable in
                self?.dismiss(animated: true, completion: { observer.onNext() })
                return Disposables.create()
            }
        }
    }
}

class ActionButtonView: UIButton {
    
    convenience init(title: String, image: UIImage? = nil, style: Style) {
        self.init(type: .system)
        setImage(image, for: .normal)
        tintColor = style.color
        setTitle(title, for: .normal)
        
        titleLabel?.font = .regular16
        
        backgroundColor = .srBGR
        layer.cornerRadius = 12
        
        heightAnchor.constraint(equalToConstant: 52).isActive = true
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        titleLabel?.sizeToFit()
        titleLabel?.frame.origin.x = UI_MARGIN_16
        
        titleLabel?.center.y = bounds.midY
        
        guard let imageView = imageView else { return }
        imageView.center.y = bounds.midY
        imageView.frame.origin.x = bounds.width - imageView.bounds.width - 19
    }
    
    enum Style {
        case normal, destructive
        
        var color: UIColor {
            switch self {
            case .normal: return .srViolet2
            case .destructive: return .red
            }
        }
    }
}
<commit_msg>Removed "magic" numbers from ActionSheet<commit_after>//
//  ActionSheet.swift
//  SmartReceipts
//
//  Created by <NAME> on 24/11/2019.
//  Copyright © 2017 <NAME>. All rights reserved.
//

import Foundation
import RxSwift

class ActionSheet {
    private var sheetViewController: ActionSheetViewController
    
    init(closable: Bool = true) {
        sheetViewController = ActionSheetViewController.create()
        sheetViewController.closable = closable
        sheetViewController.modalPresentationStyle = .custom
        sheetViewController.loadViewIfNeeded()
    }
    
    @discardableResult
    func addAction(title: String, image: UIImage? = nil, style: ActionButtonView.Style = .normal) -> Observable<Void> {
        return sheetViewController.addAction(title: title, image: image, style: style)
    }
    
    func show() {
        UIApplication.shared.topViewCtonroller?.present(sheetViewController, animated: true, completion: nil)
    }
}

class ActionSheetViewController: UIViewController, Storyboardable, Containerable {
    private let bag = DisposeBag()
    
    @IBOutlet private weak var backView: UIView!
    @IBOutlet private weak var stackView: UIStackView!
    @IBOutlet private var separator: UIView!
    @IBOutlet private var closeButton: UIButton!
    
    @IBOutlet private weak var tapGesture: UITapGestureRecognizer!
    
    private let transitionDelegate = BottomSheetTransitionDelegate()
    fileprivate var closable: Bool = true
    
    var container: UIView { return backView }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        transitioningDelegate = transitionDelegate
        
        backView.layer.cornerRadius = 24
        backView.layer.shadowRadius = 32
        backView.layer.shadowColor = UIColor.black.cgColor
        backView.layer.shadowOffset = .init(width: 0, height: 4)
        backView.layer.shadowOpacity = 0.24
        
        stackView.arrangedSubviews
            .forEach { $0.removeFromSuperview() }
        
        separator.isHidden = !closable
        closeButton.isHidden = !closable
        
        Observable.merge(closeButton.rx.tap.asObservable(), tapGesture.rx.event.map({ _ in }))
            .filter { [weak self] in self?.closable == true }
            .subscribe(onNext: { [weak self] in
                self?.dismiss(animated: true, completion: nil)
            }).disposed(by: bag)
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        stackView.addArrangedSubview(separator)
        stackView.addArrangedSubview(closeButton)
    }
    
    func addAction(title: String, image: UIImage? = nil, style: ActionButtonView.Style) -> Observable<Void> {
        let buttonView = ActionButtonView(title: title, image: image, style: style)
        stackView.addArrangedSubview(buttonView)
        
        return buttonView.rx.tap.flatMap { _ -> Observable<Void> in
            return .create { [weak self] observer -> Disposable in
                self?.dismiss(animated: true, completion: { observer.onNext() })
                return Disposables.create()
            }
        }
    }
}

class ActionButtonView: UIButton {
    
    convenience init(title: String, image: UIImage? = nil, style: Style) {
        self.init(type: .system)
        setImage(image, for: .normal)
        tintColor = style.color
        setTitle(title, for: .normal)
        
        titleLabel?.font = .regular16
        
        backgroundColor = .srBGR
        layer.cornerRadius = 12
        
        heightAnchor.constraint(equalToConstant: Constants.buttonHeight).isActive = true
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        titleLabel?.sizeToFit()
        titleLabel?.frame.origin.x = UI_MARGIN_16
        
        titleLabel?.center.y = bounds.midY
        
        guard let imageView = imageView else { return }
        imageView.center.y = bounds.midY
        imageView.frame.origin.x = bounds.width - imageView.bounds.width - Constants.imageRightMargin
    }
    
    private enum Constants {
        static let imageRightMargin: CGFloat = 19
        static let buttonHeight: CGFloat = 52
    }
    
    enum Style {
        case normal, destructive
        
        var color: UIColor {
            switch self {
            case .normal: return .srViolet2
            case .destructive: return .red
            }
        }
    }
}

{'fraction_non_alphanumeric': 0.08877586406195816, 'fraction_numerical': 0.007629175817824529, 'mean_word_length': 2.3341040462427745, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 21, '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': '4038703', 'n_tokens_mistral': 2494, 'n_tokens_neox': 2376, 'n_words': 553}