<commit_before>//
//  SHNetworkManager.swift
//  SHRestClientExample
//
//  Created by <NAME> on 11/06/18.

//

import UIKit
import Reachability

final class SHNetworkObserver: NSObject {
    
    private static let reachability = Reachability()!
    
    private static var noConnectionVC: UIViewController = NoConnectionVC()
    
    private static var connection: Reachability.Connection = .none
    
    static var isNetworkAvailable : Bool {
        return connection != .none
    }
    
    //TODO: Implement Delegates
    
//    static var whenReachable: Reachability.NetworkReachable? {
//        set {
//            SHNetworkObserver.reachability.whenReachable = newValue
//        }
//        get {
//            return SHNetworkObserver.reachability.whenReachable
//        }
//    }
//
//    static var whenUnReachable: Reachability.NetworkUnreachable? {
//        set {
//            SHNetworkObserver.reachability.whenUnreachable = newValue
//        }
//        get {
//            return SHNetworkObserver.reachability.whenUnreachable
//        }
//    }
    
    class func startMonitoring() {
        
        NotificationCenter.default.addObserver(SHNetworkObserver.self, selector: #selector(SHNetworkObserver.networkStatusChanged(_:)), name: .reachabilityChanged, object: SHNetworkObserver.reachability)
        
        do {
            try SHNetworkObserver.reachability.startNotifier()
        } catch {
            print("Error while starting reachability notifier : \(error)")
        }
    }
    
    class func presentViewControllerOnNoNetwork(_ viewController: UIViewController) {
        SHNetworkObserver.noConnectionVC = viewController
    }
    
    @objc class func networkStatusChanged(_ notification: Notification) {
        guard let reachability = notification.object as? Reachability else {
            print("Notification object is not of type Reachability")
            return
        }
        SHNetworkObserver.connection = reachability.connection
        switch reachability.connection {
        case .none:
        
            print("Network Unreachable")
            let appDel = UIApplication.shared.delegate
            appDel?.window??.rootViewController?.present(SHNetworkObserver.noConnectionVC, animated: true, completion: nil)
//            AppDelegate.topViewController?.present(SHNetworkObserver.noConnectionVC, animated: true, completion: nil)
        case .cellular, .wifi:
            if AppDelegate.topViewController == SHNetworkObserver.noConnectionVC {
                AppDelegate.topViewController?.dismiss(animated: true, completion: nil)
            }
            print("Network Reachable")
       
        }
        
    }
    
    class func stopMonitoring() {
        SHNetworkObserver.reachability.stopNotifier()
        NotificationCenter.default.removeObserver(SHNetworkObserver.self, name: .reachabilityChanged, object: SHNetworkObserver.reachability)
    }
    
}
<commit_msg>Added delegation for network observer<commit_after>//
//  SHNetworkManager.swift
//  SHRestClientExample
//
//  Created by <NAME> on 11/06/18.

//

import UIKit
import Reachability

@objc public protocol SHNetworkObserverDelegate : class {
    func networkStatus( isReachable: Bool)
}

final class SHNetworkObserver: NSObject {
    
    private static let reachability = Reachability()!
    
    private static var noConnectionVC: UIViewController = NoConnectionVC()
    
    private static var connection: Reachability.Connection = .none
    
    private static weak var delegate: SHNetworkObserverDelegate?
    
    static var isNetworkAvailable : Bool {
        return connection != .none
    }
    
    class func addDelegate(delegate: SHNetworkObserverDelegate) {
        SHNetworkObserver.delegate = delegate
    }
    
    class func removeDelegate(delegate: SHNetworkObserverDelegate) {
        SHNetworkObserver.delegate  = nil
    }
    
    class func recheckConnection() {
        if SHNetworkObserver.delegate != nil {
            SHNetworkObserver.delegate?.networkStatus(isReachable: SHNetworkObserver.isNetworkAvailable)
        }
    }
    
    class func startMonitoring() {
        
        NotificationCenter.default.addObserver(SHNetworkObserver.self, selector: #selector(SHNetworkObserver.networkStatusChanged(_:)), name: .reachabilityChanged, object: SHNetworkObserver.reachability)
        
        do {
            try SHNetworkObserver.reachability.startNotifier()
        } catch {
            print("Error while starting reachability notifier : \(error)")
        }
    }
    
    class func presentViewControllerOnNoNetwork(_ viewController: UIViewController) {
        SHNetworkObserver.noConnectionVC = viewController
    }
    
    @objc class func networkStatusChanged(_ notification: Notification) {
        guard let reachability = notification.object as? Reachability else {
            print("Notification object is not of type Reachability")
            return
        }
        
        SHNetworkObserver.connection = reachability.connection
        
        switch reachability.connection {
        
        case .none:
            print("Network Unreachable")
            let appDel = UIApplication.shared.delegate
            appDel?.window??.rootViewController?.present(SHNetworkObserver.noConnectionVC, animated: true, completion: nil)
            
        case .cellular, .wifi:
            if AppDelegate.topViewController == SHNetworkObserver.noConnectionVC {
                AppDelegate.topViewController?.dismiss(animated: true, completion: nil)
            }
            print("Network Reachable")
       
        }
        
        if SHNetworkObserver.delegate != nil {
            SHNetworkObserver.delegate?.networkStatus(isReachable: reachability.connection != .none)
        }
        
    }
    
    class func stopMonitoring() {
        SHNetworkObserver.reachability.stopNotifier()
        NotificationCenter.default.removeObserver(SHNetworkObserver.self, name: .reachabilityChanged, object: SHNetworkObserver.reachability)
    }
    
}

{'fraction_non_alphanumeric': 0.07925215089344805, 'fraction_numerical': 0.001985440105890139, 'mean_word_length': 2.5002895193978, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '3431921', 'n_tokens_mistral': 1523, 'n_tokens_neox': 1485, 'n_words': 323}