You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

234 lines
8.1 KiB

//
// Const.swift
// VeSync
//
// Created by Sheldon on 2019/8/7.
// Copyright © 2019 Etekcity. All rights reserved.
//
import Foundation
import UIKit
// MARK: - Siwft
@_exported import RxSwift
@_exported import RxCocoa
@_exported import RxDataSources
@_exported import ReusableKit
@_exported import Then
@_exported import SnapKit
@_exported import HandyJSON
@_exported import SwiftyJSON
@_exported import NSObject_Rx
@_exported import SVProgressHUD
@_exported import NVActivityIndicatorView
// MARK: -
let kScreenBounds = UIScreen.main.bounds
let kScreenScale = UIScreen.main.scale
let kScreenSize = kScreenBounds.size
let kScreenW = kScreenSize.width
let kScreenH = kScreenSize.height
let kNavBarItemMargin: CGFloat = 20.0
// MARK: -
let kMainLgihtGray = kHexColor(0xf7f7f7)
let kMainGreen = kHexColor(0x0da778)
let kBackgroundColor = kHexColor(0xffffff)
// MARK: -
let kUserDefualt = UserDefaults.standard
let kNotificationCenter = NotificationCenter.default
let keyWindow = UIApplication.shared.keyWindow!
// MARK: -
typealias ActionClosure = () -> Void
// MARK: - APP&
let kAppInfoDict = Bundle.main.infoDictionary
let kAppCurrentVersion = kAppInfoDict!["CFBundleShortVersionString"]
let kAppBuildVersion = kAppInfoDict!["CFBundleVersion"]
let kDeviceIosVersion = UIDevice.current.systemVersion
let kDeviceIdentifierNumber = UIDevice.current.identifierForVendor
let kDeviceSystemName = UIDevice.current.systemName
let kDeviceModel = UIDevice.current.model
let kDeviceLocalizedModel = UIDevice.current.localizedModel
let isIPhone = (UIDevice.current.userInterfaceIdiom == UIUserInterfaceIdiom.phone ? true : false)
let isIPad = (UIDevice.current.userInterfaceIdiom == UIUserInterfaceIdiom.pad ? true : false)
let isIPhone4 = (max(kScreenW, kScreenH) < 568.0 ? true : false)
let isIPhone5 = (max(kScreenW, kScreenH) == 568.0 ? true : false)
let isIPhone6 = (max(kScreenW, kScreenH) == 667.0 ? true : false)
let isIPhone6P = (max(kScreenW, kScreenH) == 736.0 ? true : false)
let isIPhoneX = ((kScreenH == 812.0 || kScreenH == 896.0) ? true : false)
let kNavibarH: CGFloat = isIPhoneX ? 88.0 : 64.0
let kTabbarH: CGFloat = isIPhoneX ? 49.0 + 34.0 : 49.0
let kBatteryH: CGFloat = isIPhoneX ? 24.0 : 20.0
let kStatusbarH: CGFloat = isIPhoneX ? 44.0 : 20.0
let iPhoneXBottomH: CGFloat = 34.0
let iPhoneXTopH: CGFloat = 24.0
let kIOS8 = (kDeviceIosVersion as NSString).doubleValue >= 8.0
let kIOS9 = (kDeviceIosVersion as NSString).doubleValue >= 9.0
let kIOS10 = (kDeviceIosVersion as NSString).doubleValue >= 10.0
let kIOS11 = (kDeviceIosVersion as NSString).doubleValue >= 11.0
// MARK: -
func SystemRegularFont(_ size: CGFloat, isScale: Bool = true) -> UIFont {
let fontSize = isScale ? kScaleWidth(size) : size
let font = UIFont.init(name: "PingFangSC-Regular", size: fontSize)
return font!
}
func SystemMediumFont(_ size: CGFloat, isScale: Bool = true) -> UIFont {
let fontSize = isScale ? kScaleWidth(size) : size
let font = UIFont.init(name: "PingFangSC-Medium", size: fontSize)
return font!
}
func SystemSemiboldFont(_ size: CGFloat, isScale: Bool = true) -> UIFont {
let fontSize = isScale ? kScaleWidth(size) : size
let font = UIFont.init(name: "PingFangSC-Semibold", size: fontSize)
return font!
}
func SystemLightFont(_ size: CGFloat, isScale: Bool = true) -> UIFont {
let fontSize = isScale ? kScaleWidth(size) : size
let font = UIFont.init(name: "PingFangSC-Light", size: fontSize)
return font!
}
func BebasFont(_ size: CGFloat, isScale: Bool = true) -> UIFont {
let fontSize = isScale ? kScaleWidth(size) : size
let font = UIFont.init(name: "BEBAS_s", size: fontSize)
return font!
}
///
func DigitalFont(_ size: CGFloat) -> UIFont {
let font = UIFont.init(name: "DIN Condensed", size: kScaleWidth(size))
return font!
}
// MARK: -
func kScaleWidth(_ width: CGFloat) -> CGFloat {
return width * kScreenW / 375
}
func kScaleHeight(_ height: CGFloat) -> CGFloat {
return isIPhoneX ? height: height * kScreenH / 667
}
/// MARK:-
func currentViewController() -> UIViewController? {
guard let vc = UIApplication.shared.keyWindow?.rootViewController else{
return nil
}
if vc.isKind(of: UINavigationController.self){
guard let vc = (vc as! UINavigationController).visibleViewController else{
return nil
}
return vc
}else if vc.isKind(of: UITabBarController.self){
guard let vc = (vc as! UITabBarController).selectedViewController else{
return nil
}
if vc.isKind(of: UINavigationController.self) {
guard let vc = (vc as! UINavigationController).visibleViewController else{
return nil
}
return vc
}
return nil
}
return nil
}
func currentNavtigationController() -> UINavigationController? {
return currentViewController()?.navigationController
}
func currentTabbarController() -> UITabBarController? {
return currentViewController()?.tabBarController
}
// MARK: -
func kRGBA(_ r: CGFloat, _ g: CGFloat, _ b: CGFloat, _ a: CGFloat) -> UIColor {
return UIColor (red: r/255.0, green: g/255.0, blue: b/255.0, alpha: a)
}
func kHexColor(_ hex: Int64) -> UIColor {
return UIColor.hexColor(hex)
}
func kHexColor(_ hex: Int64, _ alpha: CGFloat) -> UIColor {
return UIColor.hexColor(hex, alpha)
}
func kRandomColor() -> UIColor {
return kRGBA(CGFloat(arc4random_uniform(256)), CGFloat(arc4random_uniform(256)), CGFloat(arc4random_uniform(256)), 1)
}
// MARK:-
func showAlert(_ viewController: UIViewController, _ contextText: String, confirmText:String, handle: @escaping () -> ()) {
let alertController = UIAlertController(title: contextText, message: nil, preferredStyle: .alert)
let confirmAction = UIAlertAction(title: confirmText, style: .default) { _ in
handle()
}
alertController.addAction(confirmAction)
viewController.present(alertController, animated: true, completion: nil)
}
// MARK:-
func showAlert(_ viewController: UIViewController, _ contextText: String, cancelText: String, confirmText:String, handle: @escaping (Bool) -> ()) {
let alertController = UIAlertController(title: contextText, message: nil, preferredStyle: .alert)
let cancelAction = UIAlertAction(title: cancelText, style: .cancel) { _ in
handle(false)
}
let confirmAction = UIAlertAction(title: confirmText, style: .default) { _ in
handle(true)
}
alertController.addAction(cancelAction)
alertController.addAction(confirmAction)
viewController.present(alertController, animated: true, completion: nil)
}
// MARK: -
func LFLogs<T>(_ message : T, file : String = #file, funcName : String = #function, lineNum : Int = #line) {
#if DEBUG
let fileName = (file as NSString).lastPathComponent
print("\(DateClass.todayIntegrateString()):\(fileName):[\(lineNum)]:\(message)")
#endif
}
func logDebugs(_ format: String, _ logins: CVarArg..., file: String = #file, funcName: String = #function, lineNum: Int = #line) {
#if DEBUG
let file = (file as NSString).lastPathComponent
let str = String(format: "\(file):\(funcName):(\(lineNum))--\(format)", logins)
NSLog(str)
#endif
}
/// RxSwift
func logResourcesCountss(file: String = #file, funcName: String = #function, lineNum: Int = #line) {
#if DEBUG
let file = (file as NSString).lastPathComponent
let format = "RxSwift resources count: \(RxSwift.Resources.total)"
let str = "\(file):\(funcName):(\(lineNum))--\(format)"
NSLog(str)
#endif
}
///
func rxswiftResourcesTotalDebugLogs() {
#if DEBUG
_ = Observable<Int>.interval(.seconds(10), scheduler: MainScheduler.instance)
.subscribe(onNext: { _ in
print("Rx Resource Count: \(RxSwift.Resources.total)")
})
#endif
}
// MARK:-
extension String {
var localized: String {
return Bundle.main.localizedString(forKey: self, value: "", table: nil)
}
}