|
//
// Toast.swift
// PaiaiUIKit
//
// Created by ffib on 2019/1/21.
// Copyright © 2019 yb. All rights reserved.
//
import Foundation
private let globalInstance = Toast()
public class Toast {
public enum ToastType {
case text(String)
case image(UIImage?)
case imageWithText(UIImage?, String)
case activityIndicator(String?)
}
public enum PresentationStyle {
case center
case custom(animator: ToastAnimator)
var animator: ToastAnimator {
switch self {
case .center:
return FadeToastAnimator()
case let .custom(animator):
return animator
}
}
}
public struct Config {
public init() {}
public var toastType: ToastType = .text("")
public var presentationStyle: PresentationStyle = .center
public static var `default`: Config {
return Config()
}
}
private var _toastView: ToastView?
private var _config: Config = .default
}
/// MARK: - APIS
public extension Toast {
func show(config: Config = .default, in view: UIView? = UIApplication.shared.keyWindow) {
let toastView = ToastView(option: .default, type: config.toastType)
view?.addSubview(toastView)
self._toastView = toastView
showAnimation()
}
func show(message: String, in view: UIView? = UIApplication.shared.keyWindow) {
var config = Config()
config.toastType = .text(message)
show(config: config, in: view)
}
func show(image: UIImage?, in view: UIView? = UIApplication.shared.keyWindow) {
var config = Config()
config.toastType = .image(image)
show(config: config, in: view)
}
func show(message: String, image: UIImage?, in view: UIView? = UIApplication.shared.keyWindow) {
var config = Config()
config.toastType = .imageWithText(image, message)
show(config: config, in: view)
}
func showActivity(message: String?, in view: UIView? = UIApplication.shared.keyWindow) {
var config = Config()
config.toastType = .activityIndicator(message)
show(config: config, in: view)
}
func hide() {
hideAnimation()
}
private func showAnimation() {
guard let _toastView = _toastView else { return }
_config.presentationStyle.animator.toastIn(in: _toastView) {[weak self] _ in
guard let `self` = self else { return }
DispatchQueue.main.asyncAfter(deadline: .now() + 1, execute: {
self.hide()
})
}
}
private func hideAnimation() {
guard let _toastView = _toastView else { return }
_config.presentationStyle.animator.toastOut(in: _toastView) {[weak self] (flag) in
guard let `self` = self else { return }
self._toastView?.removeFromSuperview()
}
}
}
/// MARK: - Static APIS
public extension Toast {
static var share: Toast {
return globalInstance
}
static func show(config: Config = .default, in view: UIView? = UIApplication.shared.keyWindow) {
globalInstance.show(config: config, in: view)
}
static func show(message: String, in view: UIView? = UIApplication.shared.keyWindow) {
globalInstance.show(message: message, in: view)
}
static func show(image: UIImage?, in view: UIView? = UIApplication.shared.keyWindow) {
globalInstance.show(image: image, in: view)
}
static func show(message: String, image: UIImage?, in view: UIView? = UIApplication.shared.keyWindow) {
globalInstance.show(message: message, image: image, in: view)
}
static func showActivity(message: String?, in view: UIView? = UIApplication.shared.keyWindow) {
globalInstance.showActivity(message: message, in: view)
}
static func hide() {
globalInstance.hide()
}
}
|