<commit_before>//
//  ShadowLayer.swift
//  AwesomeUIMagic
//
//  Created by <NAME> on 2/9/18.
//

import UIKit

public class ShadowLayer: UIView {
    
    fileprivate var shadowView: UIView?
    
    public func setProperties(shadowColor: UIColor,
                              shadowOffset: CGSize,
                              shadowOpacity: Float,
                              shadowRadius: CGFloat) {
        
        backgroundColor = UIColor.clear
        layer.shadowColor = shadowColor.cgColor
        layer.shadowPath = UIBezierPath(roundedRect: bounds, cornerRadius: cornerRadius).cgPath
        layer.shadowOffset = shadowOffset
        layer.shadowOpacity = shadowOpacity
        layer.shadowRadius = shadowRadius
        layer.shouldRasterize = true
        layer.masksToBounds = true
        clipsToBounds = false
    }
    
    public override func layoutSubviews() {
        super.layoutSubviews()
        
        updateLayout()
    }
    
    public func updateLayout() {
        guard let superview = superview else {
            return
        }
        
        guard let shadowView = shadowView else {
            return
        }
        
        guard shadowView.constraints.count > 0 else {
            return
        }
        
        superview.removeConstraints(constraints)
        
        translatesAutoresizingMaskIntoConstraints = false
        
        superview.addConstraint(NSLayoutConstraint(item: self, attribute: .centerX, relatedBy: .equal, toItem: shadowView, attribute: .centerX, multiplier: 1, constant: 0))
        
        superview.addConstraint(NSLayoutConstraint(item: self, attribute: .centerY, relatedBy: .equal, toItem: shadowView, attribute: .centerY, multiplier: 1, constant: 0))
        
        superview.addConstraint(NSLayoutConstraint(item: self, attribute: .width, relatedBy: .equal, toItem: shadowView, attribute: .width, multiplier: 1, constant: 0))
        
        superview.addConstraint(NSLayoutConstraint(item: self, attribute: .height, relatedBy: .equal, toItem: shadowView, attribute: .height, multiplier: 1, constant: 0))
    }
}

extension UIView {
    
    public func addShadowLayer(shadowColor: UIColor = UIColor.black,
                               shadowOffset: CGSize = CGSize(width: 2, height: 2),
                               shadowOpacity: Float = 0.5,
                               shadowRadius: CGFloat = 4) {
        removeShadowLayer()
        
        let shadowLayer = ShadowLayer(frame: self.frame)
        shadowLayer.setProperties(shadowColor: shadowColor,
                                  shadowOffset: shadowOffset,
                                  shadowOpacity: shadowOpacity,
                                  shadowRadius: self.layer.cornerRadius > 0 ? self.layer.cornerRadius/2 : shadowRadius)
        
        superview?.addSubview(shadowLayer)
        superview?.bringSubviewToFront(self)
        
        // add constraints
        superview?.addConstraint(NSLayoutConstraint(item: shadowLayer, attribute: .leading, relatedBy: .equal, toItem: self, attribute: .leading, multiplier: 1, constant: 0))
        superview?.addConstraint(NSLayoutConstraint(item: shadowLayer, attribute: .trailing, relatedBy: .equal, toItem: self, attribute: .trailing, multiplier: 1, constant: 0))
        superview?.addConstraint(NSLayoutConstraint(item: shadowLayer, attribute: .top, relatedBy: .equal, toItem: self, attribute: .top, multiplier: 1, constant: 0))
        superview?.addConstraint(NSLayoutConstraint(item: shadowLayer, attribute: .bottom, relatedBy: .equal, toItem: self, attribute: .bottom, multiplier: 1, constant: 0))<commit_msg>[FIX] ShadowLayer breaking view constraints<commit_after>//
//  ShadowLayer.swift
//  AwesomeUIMagic
//
//  Created by <NAME> on 2/9/18.
//

import UIKit

public class ShadowLayer: UIView {
    
    fileprivate var shadowView: UIView?
    
    public func setProperties(shadowColor: UIColor,
                              shadowOffset: CGSize,
                              shadowOpacity: Float,
                              shadowRadius: CGFloat) {
        
        backgroundColor = UIColor.clear
        layer.shadowColor = shadowColor.cgColor
        layer.shadowPath = UIBezierPath(roundedRect: bounds, cornerRadius: cornerRadius).cgPath
        layer.shadowOffset = shadowOffset
        layer.shadowOpacity = shadowOpacity
        layer.shadowRadius = shadowRadius
        layer.shouldRasterize = true
        layer.masksToBounds = true
        clipsToBounds = false
    }
    
    public override func layoutSubviews() {
        super.layoutSubviews()
        
        updateLayout()
    }
    
    public func updateLayout() {
        guard let superview = superview else {
            return
        }
        
        guard let shadowView = shadowView else {
            return
        }
        
        guard shadowView.constraints.count > 0 else {
            return
        }
        
        superview.removeConstraints(constraints)
        
        translatesAutoresizingMaskIntoConstraints = false
        
        superview.addConstraint(NSLayoutConstraint(item: self, attribute: .centerX, relatedBy: .equal, toItem: shadowView, attribute: .centerX, multiplier: 1, constant: 0))
        
        superview.addConstraint(NSLayoutConstraint(item: self, attribute: .centerY, relatedBy: .equal, toItem: shadowView, attribute: .centerY, multiplier: 1, constant: 0))
        
        superview.addConstraint(NSLayoutConstraint(item: self, attribute: .width, relatedBy: .equal, toItem: shadowView, attribute: .width, multiplier: 1, constant: 0))
        
        superview.addConstraint(NSLayoutConstraint(item: self, attribute: .height, relatedBy: .equal, toItem: shadowView, attribute: .height, multiplier: 1, constant: 0))
    }
}

extension UIView {
    
    public func addShadowLayer(shadowColor: UIColor = UIColor.black,
                               shadowOffset: CGSize = CGSize(width: 2, height: 2),
                               shadowOpacity: Float = 0.5,
                               shadowRadius: CGFloat = 4) {
        removeShadowLayer()
        
        let shadowLayer = ShadowLayer(frame: self.frame)
        shadowLayer.setProperties(shadowColor: shadowColor,
                                  shadowOffset: shadowOffset,
                                  shadowOpacity: shadowOpacity,
                                  shadowRadius: self.layer.cornerRadius > 0 ? self.layer.cornerRadius/2 : shadowRadius)
        
        superview?.addSubview(shadowLayer)
        superview?.bringSubviewToFront(self)
        
        // add constraints
        NSLayoutConstraint.activate([
            self.leadingAnchor.constraint(equalTo: shadowLayer.leadingAnchor),
            self.trailingAnchor.constraint(equalTo: shadowLayer.trailingAnchor),
            self.topAnchor.constraint(equalTo: shadowLayer.topAnchor),
            self.bottomAnchor.constraint(equalTo: shadowLayer.bottomAnchor)
        ])
{'fraction_non_alphanumeric': 0.08354866494401378, 'fraction_numerical': 0.0068906115417743325, 'mean_word_length': 2.1524886877828053, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1361965', 'n_tokens_mistral': 1837, 'n_tokens_neox': 1765, 'n_words': 414}