ProHUD/Source/ProHUD.swift

121 lines
3.0 KiB
Swift
Raw 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.

//
// ProHUD.swift
// ProHUD
//
// Created by xaoxuu on 2019/7/23.
// Copyright © 2019 Titan Studio. All rights reserved.
//
import UIKit
public class ProHUD {
public static let shared = ProHUD()
public var config: Configuration {
return cfg
}
public struct Scene {
private var id = "unknown"
public var identifier: String {
return id
}
public var image: UIImage?
public var alertDuration: TimeInterval?
public var toastDuration: TimeInterval? = 3
public var title: String?
public var message: String?
init() {
}
}
}
//
public extension ProHUD.Scene {
init(identifier: String) {
self.init()
id = identifier
}
static var `default`: ProHUD.Scene {
var scene = ProHUD.Scene.init(identifier: "default")
scene.image = ProHUD.image(named: "ProHUDMessage")
return scene
}
static var loading: ProHUD.Scene {
var scene = ProHUD.Scene.init(identifier: "loading")
scene.alertDuration = 0
scene.toastDuration = 0
scene.image = ProHUD.image(named: "ProHUDLoading")
return scene
}
static var success: ProHUD.Scene {
var scene = ProHUD.Scene.init(identifier: "success")
scene.alertDuration = 2
scene.image = ProHUD.image(named: "ProHUDSuccess")
return scene
}
static var warning: ProHUD.Scene {
var scene = ProHUD.Scene.init(identifier: "warning")
scene.alertDuration = 2
scene.toastDuration = 5
scene.image = ProHUD.image(named: "ProHUDWarning")
return scene
}
static var error: ProHUD.Scene {
var scene = ProHUD.Scene.init(identifier: "error")
scene.alertDuration = 2
scene.toastDuration = 5
scene.image = ProHUD.image(named: "ProHUDError")
return scene
}
}
// MARK: - Utilities
internal extension ProHUD {
/// Bundle
static var bundle: Bundle {
let path = Bundle(for: HUDController.self).path(forResource: "ProHUD", ofType: "bundle")
return Bundle(path: path ?? "") ?? Bundle.main
}
/// Image
static func image(named: String) -> UIImage? {
return UIImage(named: named) ?? UIImage(named: named, in: bundle, compatibleWith: nil)
}
}
///
internal var isPortrait: Bool {
if UIDevice.current.userInterfaceIdiom == .phone {
if UIApplication.shared.statusBarOrientation.isPortrait {
debug("当前是手机竖屏模式")
return true
} else {
debug("当前是手机横屏模式")
}
} else {
debug("非手机设备unspecified、iPad、tv、carPlay")
}
return false
}
/// Debug
internal func debug(_ items: Any..., separator: String = " ", terminator: String = "\n") {
if cfg.enableDebugPrint {
debugPrint(items, separator: separator, terminator: terminator)
}
}