ahmedattia213
5/27/2019 - 5:36 PM

SystemHelper.swift

import Foundation

/// Static class with some utility functions regarding system state and functions
class SystemHelper {
    static var versionString: String {
        let version = Environment().configuration(ConfigKey.version)
        return "Version \(version)"
    }

    private static var onConnected = [() -> Void]()
    private static var isSubscribedToNetworkChanges = false
    static func whenConnected(_ closure: @escaping () -> Void) {
        guard !isConnected else {
            closure()
            return
        }

        onConnected.append(closure)
        if !self.isSubscribedToNetworkChanges {
            NotificationCenter.default.addObserver(self,
                                                   selector: #selector(connectionStatusDidChange),
                                                   name: NSNotification.Name(rawValue: reachabilityStatusChangedNotification), object: nil)
            Reach().monitorReachabilityChanges()
        }
    }

    @objc static func connectionStatusDidChange(notification: Notification) {
        while isConnected && !onConnected.isEmpty {
            onConnected.removeFirst()()
        }
    }

    static var languageString: String {
        if let lang = Locale.current.languageCode, lang == "de" {
            return "de"
        }

        return "en"
    }

    static var isConnected: Bool {
        switch Reach().connectionStatus() {
        case .online:
            return true
        default:
            return false
        }
    }
}