<commit_before>//
//  SwiftyNotificationsMessage.swift
//  SwiftyNotifications
//
//  Copyright © 2017 <NAME>. All rights reserved.
//
//  Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
//
//  The above copyright notice and this permission notice shall be included in all
//  copies or substantial portions of the Software.
//
//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
//  SOFTWARE.

import UIKit

public class SwiftyNotificationsMessage: UIView {

    @IBOutlet public weak var messageLabel: UILabel!

    public var delegate: SwiftyNotificationsDelegate?
    public var direction: SwiftyNotificationsDirection!

    private var dismissDelay: TimeInterval?
    private var dismissTimer: Timer?
    private var touchHandler: SwiftyNotificationsTouchHandler?

    private var topConstraint: NSLayoutConstraint!
    private var bottomConstraint: NSLayoutConstraint!
    private var dynamicHeight = CGFloat(40.0)

    class func instanceFromNib() -> SwiftyNotificationsMessage {
        let bundle = Bundle(for: self.classForCoder())
        return bundle.loadNibNamed("SwiftyNotificationsMessage",
                                   owner: nil,
                                   options: nil)?.first as! SwiftyNotificationsMessage
    }

    internal override init(frame: CGRect) {
        super.init(frame: frame)
    }

    required public init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    public static func withBackgroundColor(color: UIColor,
                                           message: String,
                                           direction: SwiftyNotificationsDirection) -> SwiftyNotificationsMessage {
        let notification = SwiftyNotificationsMessage.withBackgroundColor(color: color,
                                                                          message: message,
                                                                          dismissDelay: 0,
                                                                          direction: direction)
        return notification
    }

    public static func withBackgroundColor(color: UIColor,
                                           message: String,
                                           dismissDelay: TimeInterval,
                                           direction: SwiftyNotificationsDirection) -> SwiftyNotificationsMessage {
        let notification = SwiftyNotificationsMessage.withBackgroundColor(color: color,
                                                                          message: message,
                                                                          dismissDelay: dismissDelay,
                                                                          direction: direction,
                                                                          touchHandler: nil)
        return notification
    }

    public static func withBackgroundColor(color: UIColor,
                                           message: String,
                                           dismissDelay: TimeInterval,
                                           direction: SwiftyNotificationsDirection,
                                           touchHandler: SwiftyNotificationsTouchHandler?) -> SwiftyNotificationsMessage {
        let notification = SwiftyNotificationsMessage.instanceFromNib()
        notification.direction = direction
        notification.backgroundColor = color
        notification.translatesAutoresizingMaskIntoConstraints = false
        if dismissDelay > 0 {
            notification.dismissDelay = dismissDelay
        }
        if touchHandler != nil {
            notification.touchHandler = touchHandler
            let tapHandler = UITapGestureRecognizer(target: notification, action: #selector(SwiftyNotificationsMessage.handleTap))
            notification.addGestureRecognizer(tapHandler)
        }
        notification.setMessage(message: message)
        return notification
    }

    public func setMessage(message: String) {
        let height = message.height(withConstrainedWidth: messageLabel.frame.size.width, font: messageLabel.font)
        messageLabel.text = message
        messageLabel.frame.size.height = height
        messageLabel.frame.size.width = self.frame.size.width
        dynamicHeight = height + 20
    }

    public override func didMoveToSuperview() {
        super.didMoveToSuperview()
        if superview == nil {
            return
        }
        self.direction == .top ? updateTopConstraint(hide: true) : updateBottomConstraint(hide: true)
    }

    internal func updateTopConstraint(hide: Bool) {
        let constant = hide == true ? -dynamicHeight : 0
        if topConstraint != nil && (superview?.constraints.contains(topConstraint))! {
            topConstraint.constant = constant
        } else {
            topConstraint = NSLayoutConstraint(item: self,
                                               attribute: .top,
                                               relatedBy: .equal,
                                               toItem: superview,
                                               attribute: .top,
                                               multiplier: 1.0,
                                               constant: constant)
            let widthConstraint = NSLayoutConstraint(item: self,
                                                     attribute: .width,
                                                     relatedBy: .equal,
                                                     toItem: superview,
                                                     attribute: .width,
                                                     multiplier: 1.0,
                                                     constant: 0.0)
            superview?.addConstraint(topConstraint)
            superview?.addConstraint(widthConstraint)
        }
    }

    internal func updateBottomConstraint(hide: Bool) {
        let constant = hide == true ? dynamicHeight : 0
        if bottomConstraint != nil && (superview?.constraints.contains(bottomConstraint))! {
            bottomConstraint.constant = constant
        } else {
            bottomConstraint = NSLayoutConstraint(item: self,
                                                  attribute: .bottom,
                                                  relatedBy: .equal,
                                                  toItem: superview,
                                                  attribute: .bottom,
                                                  multiplier: 1.0,
                                                  constant: constant)
            let widthConstraint = NSLayoutConstraint(item: self,
                                                     attribute: .width,
                                                     relatedBy: .equal,
                                                     toItem: superview,
                                                     attribute: .width,
                                                     multiplier: 1.0,
                                                     constant: 0.0)
            superview?.addConstraint(bottomConstraint)
            superview?.addConstraint(widthConstraint)
        }
    }

    internal func canDisplay() -> Bool {
        return self.superview != nil ? true : false
    }

    internal func handleTap() {
        touchHandler?()
    }

    public func show() {
        if canDisplay() {
            self.delegate?.willShowNotification?(notification: self)
            self.direction == .top ? updateTopConstraint(hide: false) : updateBottomConstraint(hide: false)
            UIView.animate(withDuration: 0.6, delay: 0.0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0.3, options: .allowAnimatedContent, animations: {
                self.superview?.layoutIfNeeded()
            }, completion: { (finished) in
                if self.dismissDelay != nil && self.dismissDelay! > 0 {
                    self.dismissTimer = Timer.scheduledTimer(timeInterval: self.dismissDelay!,
                                                             target: self,
                                                             selector: #selector(SwiftyNotificationsMessage.dismiss),
                                                             userInfo: nil,
                                                             repeats: false)
                }
                self.delegate?.didShowNotification?(notification: self)
            })
        } else {
            NSException(name: NSExceptionName.internalInconsistencyException,
                        reason: "Must have a superview before calling show",
                        userInfo: nil).raise()
        }
    }

    public func dismiss() {
        self.delegate?.willDismissNotification?(notification: self)
        if self.dismissTimer != nil {
            self.dismissTimer!.invalidate()
            self.dismissTimer = nil
        }
        self.direction == .top ? updateTopConstraint(hide: true) : updateBottomConstraint(hide: true)
        UIView.animate(withDuration: 0.6, delay: 0.0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0.3, options: .allowAnimatedContent, animations: {
            self.superview?.layoutIfNeeded()
        }, completion: { (finished) in
            self.delegate?.didDismissNotification?(notification: self)
        })
    }

}<commit_msg>change accessibility for notifications message<commit_after>//
//  SwiftyNotificationsMessage.swift
//  SwiftyNotifications
//
//  Copyright © 2017 <NAME>. All rights reserved.
//
//  Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
//
//  The above copyright notice and this permission notice shall be included in all
//  copies or substantial portions of the Software.
//
//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
//  SOFTWARE.

import UIKit

open class SwiftyNotificationsMessage: UIView {

    @IBOutlet open weak var messageLabel: UILabel!

    open var delegate: SwiftyNotificationsDelegate?
    open var direction: SwiftyNotificationsDirection!

    private var dismissDelay: TimeInterval?
    private var dismissTimer: Timer?
    private var touchHandler: SwiftyNotificationsTouchHandler?

    private var topConstraint: NSLayoutConstraint!
    private var bottomConstraint: NSLayoutConstraint!
    private var dynamicHeight = CGFloat(40.0)

    class func instanceFromNib() -> SwiftyNotificationsMessage {
        let bundle = Bundle(for: self.classForCoder())
        return bundle.loadNibNamed("SwiftyNotificationsMessage",
                                   owner: nil,
                                   options: nil)?.first as! SwiftyNotificationsMessage
    }

    internal override init(frame: CGRect) {
        super.init(frame: frame)
    }

    required public init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    open static func withBackgroundColor(color: UIColor,
                                           message: String,
                                           direction: SwiftyNotificationsDirection) -> SwiftyNotificationsMessage {
        let notification = SwiftyNotificationsMessage.withBackgroundColor(color: color,
                                                                          message: message,
                                                                          dismissDelay: 0,
                                                                          direction: direction)
        return notification
    }

    open static func withBackgroundColor(color: UIColor,
                                           message: String,
                                           dismissDelay: TimeInterval,
                                           direction: SwiftyNotificationsDirection) -> SwiftyNotificationsMessage {
        let notification = SwiftyNotificationsMessage.withBackgroundColor(color: color,
                                                                          message: message,
                                                                          dismissDelay: dismissDelay,
                                                                          direction: direction,
                                                                          touchHandler: nil)
        return notification
    }

    open static func withBackgroundColor(color: UIColor,
                                           message: String,
                                           dismissDelay: TimeInterval,
                                           direction: SwiftyNotificationsDirection,
                                           touchHandler: SwiftyNotificationsTouchHandler?) -> SwiftyNotificationsMessage {
        let notification = SwiftyNotificationsMessage.instanceFromNib()
        notification.direction = direction
        notification.backgroundColor = color
        notification.translatesAutoresizingMaskIntoConstraints = false
        if dismissDelay > 0 {
            notification.dismissDelay = dismissDelay
        }
        if touchHandler != nil {
            notification.touchHandler = touchHandler
            let tapHandler = UITapGestureRecognizer(target: notification, action: #selector(SwiftyNotificationsMessage.handleTap))
            notification.addGestureRecognizer(tapHandler)
        }
        notification.setMessage(message: message)
        return notification
    }

    open func setMessage(message: String) {
        let height = message.height(withConstrainedWidth: messageLabel.frame.size.width, font: messageLabel.font)
        messageLabel.text = message
        messageLabel.frame.size.height = height
        messageLabel.frame.size.width = self.frame.size.width
        dynamicHeight = height + 20
    }

    open override func didMoveToSuperview() {
        super.didMoveToSuperview()
        if superview == nil {
            return
        }
        self.direction == .top ? updateTopConstraint(hide: true) : updateBottomConstraint(hide: true)
    }

    internal func updateTopConstraint(hide: Bool) {
        let constant = hide == true ? -dynamicHeight : 0
        if topConstraint != nil && (superview?.constraints.contains(topConstraint))! {
            topConstraint.constant = constant
        } else {
            topConstraint = NSLayoutConstraint(item: self,
                                               attribute: .top,
                                               relatedBy: .equal,
                                               toItem: superview,
                                               attribute: .top,
                                               multiplier: 1.0,
                                               constant: constant)
            let widthConstraint = NSLayoutConstraint(item: self,
                                                     attribute: .width,
                                                     relatedBy: .equal,
                                                     toItem: superview,
                                                     attribute: .width,
                                                     multiplier: 1.0,
                                                     constant: 0.0)
            superview?.addConstraint(topConstraint)
            superview?.addConstraint(widthConstraint)
        }
    }

    internal func updateBottomConstraint(hide: Bool) {
        let constant = hide == true ? dynamicHeight : 0
        if bottomConstraint != nil && (superview?.constraints.contains(bottomConstraint))! {
            bottomConstraint.constant = constant
        } else {
            bottomConstraint = NSLayoutConstraint(item: self,
                                                  attribute: .bottom,
                                                  relatedBy: .equal,
                                                  toItem: superview,
                                                  attribute: .bottom,
                                                  multiplier: 1.0,
                                                  constant: constant)
            let widthConstraint = NSLayoutConstraint(item: self,
                                                     attribute: .width,
                                                     relatedBy: .equal,
                                                     toItem: superview,
                                                     attribute: .width,
                                                     multiplier: 1.0,
                                                     constant: 0.0)
            superview?.addConstraint(bottomConstraint)
            superview?.addConstraint(widthConstraint)
        }
    }

    internal func canDisplay() -> Bool {
        return self.superview != nil ? true : false
    }

    internal func handleTap() {
        touchHandler?()
    }

    open func show() {
        if canDisplay() {
            self.delegate?.willShowNotification?(notification: self)
            self.direction == .top ? updateTopConstraint(hide: false) : updateBottomConstraint(hide: false)
            UIView.animate(withDuration: 0.6, delay: 0.0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0.3, options: .allowAnimatedContent, animations: {
                self.superview?.layoutIfNeeded()
            }, completion: { (finished) in
                if self.dismissDelay != nil && self.dismissDelay! > 0 {
                    self.dismissTimer = Timer.scheduledTimer(timeInterval: self.dismissDelay!,
                                                             target: self,
                                                             selector: #selector(SwiftyNotificationsMessage.dismiss),
                                                             userInfo: nil,
                                                             repeats: false)
                }
                self.delegate?.didShowNotification?(notification: self)
            })
        } else {
            NSException(name: NSExceptionName.internalInconsistencyException,
                        reason: "Must have a superview before calling show",
                        userInfo: nil).raise()
        }
    }

    open func dismiss() {
        self.delegate?.willDismissNotification?(notification: self)
        if self.dismissTimer != nil {
            self.dismissTimer!.invalidate()
            self.dismissTimer = nil
        }
        self.direction == .top ? updateTopConstraint(hide: true) : updateBottomConstraint(hide: true)
        UIView.animate(withDuration: 0.6, delay: 0.0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0.3, options: .allowAnimatedContent, animations: {
            self.superview?.layoutIfNeeded()
        }, completion: { (finished) in
            self.delegate?.didDismissNotification?(notification: self)
        })
    }

}
{'fraction_non_alphanumeric': 0.06533079810833212, 'fraction_numerical': 0.004095363463507386, 'mean_word_length': 1.3759990733232943, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 194, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7584471', 'n_tokens_mistral': 4163, 'n_tokens_neox': 3981, 'n_words': 1158}