2019-07-31 17:40:39 +08:00
|
|
|
|
//
|
|
|
|
|
// ProHUD.swift
|
|
|
|
|
// ProHUD
|
|
|
|
|
//
|
|
|
|
|
// Created by xaoxuu on 2019/7/23.
|
|
|
|
|
// Copyright © 2019 Titan Studio. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
import UIKit
|
|
|
|
|
|
2019-08-01 20:22:57 +08:00
|
|
|
|
public class ProHUD {
|
2019-07-31 17:40:39 +08:00
|
|
|
|
|
|
|
|
|
public static let shared = ProHUD()
|
|
|
|
|
|
2021-07-20 16:04:39 +08:00
|
|
|
|
public var config: Configuration { cfg }
|
2019-08-03 17:48:37 +08:00
|
|
|
|
|
2021-07-20 16:04:39 +08:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MARK: - Scene
|
|
|
|
|
public extension ProHUD {
|
|
|
|
|
|
|
|
|
|
struct Scene {
|
|
|
|
|
public let identifier: String
|
2019-08-13 11:32:27 +08:00
|
|
|
|
public var image: UIImage?
|
|
|
|
|
public var alertDuration: TimeInterval?
|
|
|
|
|
public var toastDuration: TimeInterval? = 3
|
|
|
|
|
public var title: String?
|
|
|
|
|
public var message: String?
|
2021-07-20 16:04:39 +08:00
|
|
|
|
public init(identifier: String) {
|
|
|
|
|
self.identifier = identifier
|
2019-08-13 11:32:27 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-19 19:13:33 +08:00
|
|
|
|
// 默认场景,可直接在项目工程中覆写场景参数
|
2019-08-13 11:32:27 +08:00
|
|
|
|
public extension ProHUD.Scene {
|
|
|
|
|
static var `default`: ProHUD.Scene {
|
2020-06-23 20:16:05 +08:00
|
|
|
|
var scene = ProHUD.Scene.init(identifier: "prohud.default")
|
2020-06-19 19:13:33 +08:00
|
|
|
|
scene.image = ProHUD.image(named: "prohud.note")
|
|
|
|
|
return scene
|
|
|
|
|
}
|
|
|
|
|
static var message: ProHUD.Scene {
|
2020-06-23 20:16:05 +08:00
|
|
|
|
var scene = ProHUD.Scene.init(identifier: "prohud.message")
|
2020-06-19 13:54:36 +08:00
|
|
|
|
scene.image = ProHUD.image(named: "prohud.message")
|
2020-06-24 13:27:50 +08:00
|
|
|
|
scene.alertDuration = 2
|
|
|
|
|
scene.toastDuration = 5
|
2019-08-13 11:32:27 +08:00
|
|
|
|
return scene
|
|
|
|
|
}
|
|
|
|
|
static var loading: ProHUD.Scene {
|
2020-06-23 20:16:05 +08:00
|
|
|
|
var scene = ProHUD.Scene.init(identifier: "prohud.loading.rotate")
|
|
|
|
|
scene.image = ProHUD.image(named: "prohud.rainbow.circle")
|
|
|
|
|
scene.title = "Loading"
|
2019-08-13 11:32:27 +08:00
|
|
|
|
scene.alertDuration = 0
|
|
|
|
|
scene.toastDuration = 0
|
|
|
|
|
return scene
|
|
|
|
|
}
|
|
|
|
|
static var success: ProHUD.Scene {
|
2020-06-23 20:16:05 +08:00
|
|
|
|
var scene = ProHUD.Scene.init(identifier: "prohud.success")
|
2020-06-19 19:13:33 +08:00
|
|
|
|
scene.image = ProHUD.image(named: "prohud.checkmark")
|
2020-06-23 20:16:05 +08:00
|
|
|
|
scene.title = "Success"
|
|
|
|
|
scene.alertDuration = 2
|
2019-08-13 11:32:27 +08:00
|
|
|
|
return scene
|
|
|
|
|
}
|
|
|
|
|
static var warning: ProHUD.Scene {
|
2020-06-23 20:16:05 +08:00
|
|
|
|
var scene = ProHUD.Scene.init(identifier: "prohud.warning")
|
|
|
|
|
scene.image = ProHUD.image(named: "prohud.exclamationmark")
|
|
|
|
|
scene.title = "Warning"
|
|
|
|
|
scene.message = "Something happened."
|
2019-08-13 11:32:27 +08:00
|
|
|
|
scene.alertDuration = 2
|
|
|
|
|
scene.toastDuration = 5
|
|
|
|
|
return scene
|
|
|
|
|
}
|
|
|
|
|
static var error: ProHUD.Scene {
|
2020-06-23 20:16:05 +08:00
|
|
|
|
var scene = ProHUD.Scene.init(identifier: "prohud.error")
|
|
|
|
|
scene.image = ProHUD.image(named: "prohud.xmark")
|
|
|
|
|
scene.title = "Error"
|
|
|
|
|
scene.message = "Please try again later."
|
2019-08-13 11:32:27 +08:00
|
|
|
|
scene.alertDuration = 2
|
|
|
|
|
scene.toastDuration = 5
|
2020-06-23 20:16:05 +08:00
|
|
|
|
return scene
|
|
|
|
|
}
|
|
|
|
|
static var failure: ProHUD.Scene {
|
|
|
|
|
var scene = ProHUD.Scene.init(identifier: "prohud.failure")
|
2020-06-19 19:13:33 +08:00
|
|
|
|
scene.image = ProHUD.image(named: "prohud.xmark")
|
2020-06-23 20:16:05 +08:00
|
|
|
|
scene.title = "Failure"
|
|
|
|
|
scene.message = "Please try again later."
|
|
|
|
|
scene.alertDuration = 2
|
|
|
|
|
scene.toastDuration = 5
|
2020-06-19 19:13:33 +08:00
|
|
|
|
return scene
|
|
|
|
|
}
|
|
|
|
|
static var confirm: ProHUD.Scene {
|
2020-06-23 20:16:05 +08:00
|
|
|
|
var scene = ProHUD.Scene.init(identifier: "prohud.confirm")
|
|
|
|
|
scene.image = ProHUD.image(named: "prohud.questionmark")
|
2020-06-19 19:13:33 +08:00
|
|
|
|
scene.alertDuration = 2
|
|
|
|
|
scene.toastDuration = 5
|
|
|
|
|
return scene
|
|
|
|
|
}
|
|
|
|
|
static var privacy: ProHUD.Scene {
|
2020-06-23 20:16:05 +08:00
|
|
|
|
var scene = ProHUD.Scene.init(identifier: "prohud.privacy")
|
|
|
|
|
scene.image = ProHUD.image(named: "prohud.privacy")
|
2020-06-19 19:13:33 +08:00
|
|
|
|
scene.alertDuration = 2
|
|
|
|
|
scene.toastDuration = 5
|
|
|
|
|
return scene
|
|
|
|
|
}
|
|
|
|
|
static var delete: ProHUD.Scene {
|
2020-06-23 20:16:05 +08:00
|
|
|
|
var scene = ProHUD.Scene.init(identifier: "prohud.delete")
|
|
|
|
|
scene.image = ProHUD.image(named: "prohud.trash")
|
2020-06-19 19:13:33 +08:00
|
|
|
|
scene.alertDuration = 2
|
|
|
|
|
scene.toastDuration = 5
|
2019-08-13 11:32:27 +08:00
|
|
|
|
return scene
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2019-07-31 17:40:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-08-13 11:32:27 +08:00
|
|
|
|
|
2019-08-03 17:48:37 +08:00
|
|
|
|
// MARK: - Utilities
|
2019-07-31 17:40:39 +08:00
|
|
|
|
|
|
|
|
|
internal extension ProHUD {
|
|
|
|
|
|
2019-08-03 17:48:37 +08:00
|
|
|
|
/// 获取Bundle
|
2019-08-01 20:22:57 +08:00
|
|
|
|
static var bundle: Bundle {
|
2020-06-15 18:58:13 +08:00
|
|
|
|
let path = Bundle(for: HUDController.self).path(forResource: "ProHUD", ofType: "bundle")
|
|
|
|
|
return Bundle(path: path ?? "") ?? Bundle.main
|
2019-07-31 17:40:39 +08:00
|
|
|
|
}
|
2019-08-05 20:13:17 +08:00
|
|
|
|
|
2019-08-03 17:48:37 +08:00
|
|
|
|
/// 获取Image
|
2019-08-01 20:22:57 +08:00
|
|
|
|
static func image(named: String) -> UIImage? {
|
2020-06-15 18:58:13 +08:00
|
|
|
|
return UIImage(named: named) ?? UIImage(named: named, in: bundle, compatibleWith: nil)
|
2019-07-31 17:40:39 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-03 16:22:50 +08:00
|
|
|
|
|
2019-08-03 17:48:37 +08:00
|
|
|
|
/// 是否是手机竖屏模式
|
|
|
|
|
internal var isPortrait: Bool {
|
|
|
|
|
if UIDevice.current.userInterfaceIdiom == .phone {
|
|
|
|
|
if UIApplication.shared.statusBarOrientation.isPortrait {
|
|
|
|
|
debug("当前是手机竖屏模式")
|
|
|
|
|
return true
|
|
|
|
|
} else {
|
|
|
|
|
debug("当前是手机横屏模式")
|
2019-08-03 16:22:50 +08:00
|
|
|
|
}
|
|
|
|
|
} else {
|
2019-08-03 17:48:37 +08:00
|
|
|
|
debug("非手机设备(unspecified、iPad、tv、carPlay)")
|
2019-08-03 16:22:50 +08:00
|
|
|
|
}
|
2019-08-03 17:48:37 +08:00
|
|
|
|
return false
|
2019-08-03 16:22:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-08-03 17:48:37 +08:00
|
|
|
|
/// 可控Debug输出
|
|
|
|
|
internal func debug(_ items: Any..., separator: String = " ", terminator: String = "\n") {
|
2020-06-19 10:54:55 +08:00
|
|
|
|
if cfg.enablePrint {
|
|
|
|
|
print(items, separator: separator, terminator: terminator)
|
2019-08-03 16:22:50 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-08-03 17:48:37 +08:00
|
|
|
|
|