<commit_before>//
//  Policeman.swift
//  NOCore
//
//  Created by <NAME>/<NAME> on 27/07/15.
//  Copyright (c) 2015 Nodes. All rights reserved.
//

import UIKit
import Alamofire

/**
 Blobfish can present general error messages related to URL Requests in a meaningful way. Pass an object conforming to 
 the *Blobable* protocol to it whenever you  have a request that fails with a non-endpoint-specific error.
 */
public class Blobfish {

    public typealias ErrorHandlerAlertCompletion = (_ retryButtonClicked:Bool) -> Void
    public typealias ErrorHandlerShowAlertBlock = (_ title:String, _ message:String?, _ actions:[Blob.AlertAction]) -> Void
    
    public static let sharedInstance = Blobfish()

    var reachabilityManager: NetworkReachabilityManager?

    lazy var overlayBar = MessageBar(frame: UIApplication.shared.statusBarFrame)
    
    var alertWindow = UIWindow(frame: UIScreen.main.bounds) {
        didSet {
            alertWindow.windowLevel = UIWindowLevelAlert + 1
        }
    }
    
    var alreadyShowingAlert: Bool {
        return Blobfish.sharedInstance.alertWindow.isHidden == false
    }

    /**
     The content of this closure is responsible for showing showing the UI for an error whose style is MessageStyle.Alert. The default value shows a native alert using UIAlertController.

     Override this to use a custom alert for your app.
     */

    public var showAlertBlock: ErrorHandlerShowAlertBlock = {
        (title, message, actions) in

        let alert = UIAlertController(title: title, message:message, preferredStyle: UIAlertControllerStyle.alert)
        for action in actions {
            alert.addAction(UIAlertAction(title: action.title, style: .default, handler: { (_) in
                Blobfish.hideAlertWindow()
                action.handler?()
            }))
        }

        Blobfish.sharedInstance.presentViewController(alert)

    }

    /**
     The content of this closure is responsible for showing showing the UI for an error whose style is Overlay. The default value shows a native alert using UIAlertController.

     Override this to use a custom alert for your app.

     If you want to customize the appearance of the overlay bar, see the overlayBarConfiguration property.
     */

    public var showOverlayBlock: (_ title:String) -> Void = { message in
        Blobfish.sharedInstance.overlayBar.label.text = message
        Blobfish.sharedInstance.showOverlayBar()
    }

    /**
     The content of this closure is responsible for showing showing the UI for an error whose style is Overlay. The default value shows a native alert using UIAlertController.

     Override this to use a custom alert for your app.

     If you want to customize the appearance of the overlay bar, see the overlayBarConfiguration property.
     */

    public var overlayBarConfiguration:((_ bar:MessageBar) -> Void)?

    // MARK: - Init & Deinit -

    private init() {
        setupReachability()

        NotificationCenter.default.addObserver(self,
                                               selector: #selector(Blobfish.aCallWentThrough(_:)),
                                               name: NSNotification.Name(rawValue: "APICallSucceededNotification"),
                                               object: nil)
    }

    deinit {
        NotificationCenter.default.removeObserver(self)
    }

    // MARK: - Reachability -

    private func setupReachability() {
        reachabilityManager = NetworkReachabilityManager(host: "http://google.com")
        reachabilityManager?.listener = { state in
            switch state {
            case .reachable(_):
                self.hideOverlayBar()
            default:
                break
            }
        }
        reachabilityManager?.startListening()
    }
    
    // MARK: - Alert -
    
    /**
    This method can be used for presenting a custom viewcontroller while still using the Blobfish boilerplate code for keeping track of already presented alerts. IMPORTANT: If this method is used you must call Blobfish.hideAlertWindow() somewhere in every alert action to regain interaction with app
    */
    
    public func presentViewController(_ viewController:UIViewController) {
        
        if Blobfish.sharedInstance.alertWindow.rootViewController == nil {
            Blobfish.sharedInstance.alertWindow.rootViewController = UIViewController()
        }
        
        Blobfish.sharedInstance.alertWindow.makeKeyAndVisible()
        Blobfish.sharedInstance.alertWindow.rootViewController!.present(viewController, animated: true, completion: nil)
    }
    
    /**
     This method is used for manually hiding the window used for displaying alerts. MUST be called after dismissing a custom viewcontroller presented with Blobfish.sharedInstance.presentViewController(viewController:UIViewController)
     */
    
    public static func hideAlertWindow() {
        Blobfish.sharedInstance.alertWindow.isHidden = true
    }

    //MARK: - Overlay -
    
    private func showOverlayBar() {
        if (self.overlayBar.isHidden) { // Not already shown
            // Do not re-animate
            self.overlayBar.frame.origin.y = -overlayBar.frame.height
        }
        
        self.overlayBar.isHidden = false
        
        UIView.animate(withDuration: 0.5, animations: { () -> Void in
            self.overlayBar.frame.origin.y = 0
            }) { (finished) -> Void in
                
                self.statusBarDidChangeFrame()
        }
    }
    
    public func hideOverlayBar(_ animated:Bool = true) {
<commit_msg>Calling the overlay bar configuration block if set<commit_after>//
//  Policeman.swift
//  NOCore
//
//  Created by <NAME>/<NAME> on 27/07/15.
//  Copyright (c) 2015 Nodes. All rights reserved.
//

import UIKit
import Alamofire

/**
 Blobfish can present general error messages related to URL Requests in a meaningful way. Pass an object conforming to 
 the *Blobable* protocol to it whenever you  have a request that fails with a non-endpoint-specific error.
 */
public class Blobfish {

    public typealias ErrorHandlerAlertCompletion = (_ retryButtonClicked:Bool) -> Void
    public typealias ErrorHandlerShowAlertBlock = (_ title:String, _ message:String?, _ actions:[Blob.AlertAction]) -> Void
    
    public static let sharedInstance = Blobfish()

    var reachabilityManager: NetworkReachabilityManager?

    lazy var overlayBar = MessageBar(frame: UIApplication.shared.statusBarFrame)
    
    var alertWindow = UIWindow(frame: UIScreen.main.bounds) {
        didSet {
            alertWindow.windowLevel = UIWindowLevelAlert + 1
        }
    }
    
    var alreadyShowingAlert: Bool {
        return Blobfish.sharedInstance.alertWindow.isHidden == false
    }

    /**
     The content of this closure is responsible for showing showing the UI for an error whose style is MessageStyle.Alert. The default value shows a native alert using UIAlertController.

     Override this to use a custom alert for your app.
     */

    public var showAlertBlock: ErrorHandlerShowAlertBlock = {
        (title, message, actions) in

        let alert = UIAlertController(title: title, message:message, preferredStyle: UIAlertControllerStyle.alert)
        for action in actions {
            alert.addAction(UIAlertAction(title: action.title, style: .default, handler: { (_) in
                Blobfish.hideAlertWindow()
                action.handler?()
            }))
        }

        Blobfish.sharedInstance.presentViewController(alert)

    }

    /**
     The content of this closure is responsible for showing showing the UI for an error whose style is Overlay. The default value shows a native alert using UIAlertController.

     Override this to use a custom alert for your app.

     If you want to customize the appearance of the overlay bar, see the overlayBarConfiguration property.
     */

    public var showOverlayBlock: (_ title:String) -> Void = { message in
        Blobfish.sharedInstance.overlayBar.label.text = message
        Blobfish.sharedInstance.showOverlayBar()
    }

    /**
     The content of this closure is responsible for showing showing the UI for an error whose style is Overlay. The default value shows a native alert using UIAlertController.

     Override this to use a custom alert for your app.

     If you want to customize the appearance of the overlay bar, see the overlayBarConfiguration property.
     */

    public var overlayBarConfiguration:((_ bar:MessageBar) -> Void)?

    // MARK: - Init & Deinit -

    private init() {
        setupReachability()

        NotificationCenter.default.addObserver(self,
                                               selector: #selector(Blobfish.aCallWentThrough(_:)),
                                               name: NSNotification.Name(rawValue: "APICallSucceededNotification"),
                                               object: nil)
    }

    deinit {
        NotificationCenter.default.removeObserver(self)
    }

    // MARK: - Reachability -

    private func setupReachability() {
        reachabilityManager = NetworkReachabilityManager(host: "http://google.com")
        reachabilityManager?.listener = { state in
            switch state {
            case .reachable(_):
                self.hideOverlayBar()
            default:
                break
            }
        }
        reachabilityManager?.startListening()
    }
    
    // MARK: - Alert -
    
    /**
    This method can be used for presenting a custom viewcontroller while still using the Blobfish boilerplate code for keeping track of already presented alerts. IMPORTANT: If this method is used you must call Blobfish.hideAlertWindow() somewhere in every alert action to regain interaction with app
    */
    
    public func presentViewController(_ viewController:UIViewController) {
        
        if Blobfish.sharedInstance.alertWindow.rootViewController == nil {
            Blobfish.sharedInstance.alertWindow.rootViewController = UIViewController()
        }
        
        Blobfish.sharedInstance.alertWindow.makeKeyAndVisible()
        Blobfish.sharedInstance.alertWindow.rootViewController!.present(viewController, animated: true, completion: nil)
    }
    
    /**
     This method is used for manually hiding the window used for displaying alerts. MUST be called after dismissing a custom viewcontroller presented with Blobfish.sharedInstance.presentViewController(viewController:UIViewController)
     */
    
    public static func hideAlertWindow() {
        Blobfish.sharedInstance.alertWindow.isHidden = true
    }

    //MARK: - Overlay -
    
    private func showOverlayBar() {
        overlayBarConfiguration?(Blobfish.sharedInstance.overlayBar)    
        
        if (self.overlayBar.isHidden) { // Not already shown
            // Do not re-animate
            self.overlayBar.frame.origin.y = -overlayBar.frame.height
        }
        
        self.overlayBar.isHidden = false
        
        UIView.animate(withDuration: 0.5, animations: { () -> Void in
            self.overlayBar.frame.origin.y = 0
            }) { (finished) -> Void in
                
                self.statusBarDidChangeFrame()
        }
    }
    
    public func hideOverlayBar(_ animated:Bool = true) {

{'fraction_non_alphanumeric': 0.07242265756581863, 'fraction_numerical': 0.002482049463700027, 'mean_word_length': 2.4167171411265898, 'pattern_counts': {'":': 0, '<': 7, '<?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': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '903173', 'n_tokens_mistral': 2903, 'n_tokens_neox': 2717, 'n_words': 892}