jsdw_ios/QuickLocation/Common/Constant.swift

117 lines
4.0 KiB
Swift
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// Constant.swift
// DLSDK
//
// Created by osell on 2023/3/6.
//
import Foundation
import SwiftyUserDefaults
/// NSUserDefaults
extension DefaultsKeys {
///
var currentLatitude: DefaultsKey<Double?> { .init("currentLatitude") }
///
var updateLocationEnable: DefaultsKey<Bool> { .init("updateLocationEnable", defaultValue: true) }
///
var isVendorCallback: DefaultsKey<Bool> { .init("isVendorCallback", defaultValue: false) }
/// IMid
var isReLogin: DefaultsKey<Bool> { .init("isReLogin", defaultValue: false) }
///
var guideShowVersion: DefaultsKey<Int?> { .init("guideShowVersion") }
}
///
extension Notification.Name {
///
static let RefreshGroupInfoNotification = Notification.Name("RefreshGroupInfoNotification")
}
// MARK: -
/// KeyWindow
public var kKeyWindow: UIWindow? { UIApplication.keyWindow }
/// KeyWindow
public var kSafeAreaInsets: UIEdgeInsets { kKeyWindow?.safeAreaInsets ?? .zero }
/// size
public let kScreenSize = UIScreen.main.bounds.size
///
public let kScreenWidth = UIScreen.main.bounds.size.width
///
public let kScreenHeight = UIScreen.main.bounds.size.height
///
public var kStatusBarHeight: CGFloat {
if #available(iOS 13.0, *) {
let statusBarHeight = UIApplication.shared.windows
.filter { $0.isKeyWindow }
.first?
.windowScene?
.statusBarManager?
.statusBarFrame
.height ?? 0
return statusBarHeight
} else {
return UIApplication.shared.statusBarFrame.height
}
}
/// +
public let kNaviHeight = 44 + kStatusBarHeight
/// App
public let kAppName = Bundle.main.infoDictionary!["CFBundleDisplayName"] as! String //App
/// AppBundle
public let kAppVersion = Bundle.main.infoDictionary!["CFBundleVersion"] as! String //App
/// AppBundleShort
public let kAppShortVersion = Bundle.main.infoDictionary!["CFBundleShortVersionString"] as! String
/// AppIdentifier
public let kBundleIdentifier = Bundle.main.infoDictionary!["CFBundleIdentifier"] as! String
public let kIsFullScreen = kScreenHeight > 800 ? true : false
public let kSafeBottomMargin: CGFloat = kIsFullScreen ? 34.0 : 0.0
///
public let kPageSize = 20
///
/// - Parameters:
/// - lock:
/// - closure:
public func synchronized(_ lock: Any, closure: () -> ()) {
objc_sync_enter(lock)
closure()
objc_sync_exit(lock)
}
/// 线
/// - Parameter closure:
public func MainAsync(_ closure: (() -> Void)?) {
guard let closure = closure else { return }
Thread.isMainThread ? closure() : DispatchQueue.main.async(execute: closure)
}
///
/// - Parameter closure:
public func GlobalAsync(qos: DispatchQoS.QoSClass = .default, _ closure: (() -> Void)?) {
guard let closure = closure else { return }
Thread.isMultiThreaded() ? closure() : DispatchQueue.global(qos: qos).async(execute: closure)
}
extension DL {
/// nil
public static func safeString(_ value: Any?) -> String {
guard let value = value, !(value is NSNull) else { return "" }
if let string = value as? String { return string }
if let data = value as? Data { return String(data: data, encoding: .utf8) ?? "" }
if let object = value as? NSObjectProtocol { return object.description }
return String(describing: value)
}
/// nil
public static func safeNumber(_ value: Any?) -> NSNumber {
guard let value = value else { return NSNumber(value: 0) }
if let number = value as? NSNumber { return number }
return safeString(value).number ?? NSNumber(value: 0)
}
}