//
|
// YYAlertKit.swift
|
// WashCar
|
//
|
// Created by alvin_y on 2020/7/9.
|
// Copyright © 2020 yangwang. All rights reserved.
|
//
|
|
import Foundation
|
import SwiftEntryKit
|
import UIKit
|
class YYAlertKit {
|
|
static func dismiss() {
|
SwiftEntryKit.dismiss()
|
}
|
|
static func display(message: String?) {
|
|
guard let message = message else { return }
|
|
let view = MessageView(string: message, textColor: .white)
|
|
var attributes = EKAttributes()
|
attributes = .float
|
attributes.position = .center
|
attributes.windowLevel = .alerts
|
attributes.displayDuration = .init(1.7)
|
|
attributes.entranceAnimation = .init(translate: .init(duration: 0.3, spring: .init(damping: 0.9, initialVelocity: 0)),
|
scale: .init(from: 0.8, to: 1, duration: 0.3, spring: .init(damping: 0.8, initialVelocity: 0)),
|
fade: .init(from: 0.7, to: 1, duration: 0.3))
|
attributes.exitAnimation = .init(translate: .init(duration: 0.3),
|
scale: .init(from: 1, to: 0.8, duration: 0.3),
|
fade: .init(from: 1, to: 0, duration: 0.3))
|
|
attributes.entryInteraction = .dismiss
|
attributes.screenInteraction = .forward
|
|
attributes.entryBackground = .visualEffect(style: .dark)
|
attributes.shadow = .active(with: .init(color: .black, opacity: 0.3, radius: 6))
|
attributes.roundCorners = .all(radius: 5)
|
attributes.positionConstraints.size = .init(width: .intrinsic, height: .intrinsic)
|
attributes.positionConstraints.maxSize = .init(width: .offset(value: 20), height: .constant(value: 200))
|
|
SwiftEntryKit.display(entry: view, using: attributes)
|
}
|
|
static func display(position: EKAttributes.Position,message: String?) {
|
|
guard let message = message else { return }
|
|
let view = MessageView(string: message, textColor: .white)
|
|
var attributes = EKAttributes()
|
attributes = .float
|
attributes.position = position
|
attributes.windowLevel = .alerts
|
attributes.displayDuration = .init(2)
|
|
attributes.entranceAnimation = .init(translate: .init(duration: 0.3, spring: .init(damping: 0.9, initialVelocity: 0)),
|
scale: .init(from: 0.8, to: 1, duration: 0.3, spring: .init(damping: 0.8, initialVelocity: 0)),
|
fade: .init(from: 0.7, to: 1, duration: 0.3))
|
attributes.exitAnimation = .init(translate: .init(duration: 0.3),
|
scale: .init(from: 1, to: 0.8, duration: 0.3),
|
fade: .init(from: 1, to: 0, duration: 0.3))
|
|
attributes.entryInteraction = .dismiss
|
attributes.screenInteraction = .forward
|
|
attributes.entryBackground = .visualEffect(style: .dark)
|
attributes.shadow = .active(with: .init(color: .black, opacity: 0.3, radius: 6))
|
attributes.roundCorners = .all(radius: 5)
|
attributes.positionConstraints.size = .init(width: .intrinsic, height: .intrinsic)
|
attributes.positionConstraints.maxSize = .init(width: .offset(value: 20), height: .constant(value: 200))
|
|
SwiftEntryKit.display(entry: view, using: attributes)
|
}
|
|
static func displayActivity() {
|
|
let view = ActivityView(color: .white)
|
|
var attributes = EKAttributes()
|
attributes = .centerFloat
|
attributes.displayDuration = .infinity
|
attributes.windowLevel = .alerts
|
|
attributes.entranceAnimation = .init(translate: nil,
|
scale: nil,
|
fade: .init(from: 0, to: 1, duration: 0.3))
|
attributes.exitAnimation = .init(translate: nil,
|
scale: nil,
|
fade: .init(from: 1, to: 0, duration: 0.3))
|
attributes.popBehavior = .animated(animation: .init(translate: nil,
|
scale: nil,
|
fade: .init(from: 1, to: 0, duration: 0.3)))
|
|
attributes.screenInteraction = .absorbTouches
|
attributes.entryInteraction = .absorbTouches
|
|
attributes.screenBackground = .color(color: EKColor.init(light: #colorLiteral(red: 0, green: 0, blue: 0, alpha: 0.4), dark: #colorLiteral(red: 0, green: 0, blue: 0, alpha: 0.4)))
|
attributes.entryBackground = .visualEffect(style: .dark)
|
attributes.positionConstraints.size = .init(width: .constant(value: 80), height: .constant(value: 80))
|
|
SwiftEntryKit.display(entry: view, using: attributes)
|
|
}
|
static func displayActivityWithDismiss(dely:CGFloat=1.7) {
|
|
let view = ActivityView(color: .white)
|
|
var attributes = EKAttributes()
|
attributes = .centerFloat
|
attributes.displayDuration = .infinity
|
attributes.windowLevel = .alerts
|
|
attributes.entranceAnimation = .init(translate: nil,
|
scale: nil,
|
fade: .init(from: 0, to: 1, duration: 0.3))
|
attributes.exitAnimation = .init(translate: nil,
|
scale: nil,
|
fade: .init(from: 1, to: 0, duration: 0.3))
|
attributes.popBehavior = .animated(animation: .init(translate: nil,
|
scale: nil,
|
fade: .init(from: 1, to: 0, duration: 0.3)))
|
|
attributes.screenInteraction = .absorbTouches
|
attributes.entryInteraction = .absorbTouches
|
|
attributes.screenBackground = .color(color: EKColor.init(light: #colorLiteral(red: 0, green: 0, blue: 0, alpha: 0.4), dark: #colorLiteral(red: 0, green: 0, blue: 0, alpha: 0.4)))
|
attributes.entryBackground = .visualEffect(style: .dark)
|
attributes.positionConstraints.size = .init(width: .constant(value: 80), height: .constant(value: 80))
|
|
SwiftEntryKit.display(entry: view, using: attributes)
|
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now()+1.7) {
|
self.dismiss()
|
}
|
|
}
|
static func displayActionSheet(actions: [Action]) {
|
|
let vc = ActionSheetViewController(actions: actions)
|
|
var attributes = EKAttributes()
|
attributes = .bottomNote
|
attributes.windowLevel = .alerts
|
attributes.position = .bottom
|
attributes.displayDuration = .infinity
|
|
attributes.entranceAnimation = .init(translate: EKAttributes.Animation.Translate.init(duration: 0.25),
|
scale: nil,
|
fade: nil)
|
attributes.exitAnimation = .init(translate: EKAttributes.Animation.Translate.init(duration: 0.25),
|
scale: nil,
|
fade: nil)
|
|
attributes.entryInteraction = .absorbTouches
|
attributes.screenInteraction = .dismiss
|
|
attributes.screenBackground = .color(color: EKColor.init(light: #colorLiteral(red: 0, green: 0, blue: 0, alpha: 0.4), dark: #colorLiteral(red: 0, green: 0, blue: 0, alpha: 0.4)))
|
attributes.entryBackground = .color(color: EKColor.init(light: #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0), dark: #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)))
|
attributes.shadow = .active(with: .init(color: .black, opacity: 0.3, radius: 6))
|
attributes.roundCorners = .top(radius: 20)
|
attributes.positionConstraints.size = .init(width: .fill, height: .intrinsic)
|
attributes.positionConstraints.maxSize = .init(width: .fill, height: .intrinsic)
|
|
SwiftEntryKit.display(entry: vc, using: attributes)
|
}
|
|
static func displayTopFloat(by vc: UIViewController, backgroundColor: UIColor = .white) {
|
|
var attributes = EKAttributes()
|
attributes = .topFloat
|
attributes.windowLevel = .alerts
|
attributes.position = .top
|
attributes.displayDuration = .infinity
|
|
attributes.entranceAnimation = .init(translate: EKAttributes.Animation.Translate.init(duration: 0.25),
|
scale: nil,
|
fade: nil)
|
attributes.exitAnimation = .init(translate: EKAttributes.Animation.Translate.init(duration: 0.25),
|
scale: nil,
|
fade: nil)
|
|
attributes.entryInteraction = .absorbTouches
|
attributes.screenInteraction = .dismiss
|
|
attributes.screenBackground = .color(color: EKColor.init(light: #colorLiteral(red: 0, green: 0, blue: 0, alpha: 0.4), dark: #colorLiteral(red: 0, green: 0, blue: 0, alpha: 0.4)))
|
attributes.entryBackground = .color(color: EKColor.init(light: backgroundColor, dark: backgroundColor))
|
attributes.shadow = .active(with: .init(color: .black, opacity: 0.3, radius: 6))
|
attributes.roundCorners = .all(radius: 6)
|
attributes.positionConstraints.size = .init(width: .fill, height: .intrinsic)
|
attributes.positionConstraints.maxSize = .init(width: .fill, height: .intrinsic)
|
|
SwiftEntryKit.display(entry: vc, using: attributes)
|
}
|
|
static func displayCenterFloat(by vc: UIViewController, backgroundColor: UIColor = .white) {
|
|
var attributes = EKAttributes()
|
attributes = .centerFloat
|
attributes.windowLevel = .alerts
|
attributes.position = .center
|
attributes.displayDuration = .infinity
|
|
attributes.entranceAnimation = .init(translate: EKAttributes.Animation.Translate.init(duration: 0.25),
|
scale: nil,
|
fade: nil)
|
attributes.exitAnimation = .init(translate: EKAttributes.Animation.Translate.init(duration: 0.25),
|
scale: nil,
|
fade: nil)
|
|
attributes.entryInteraction = .absorbTouches
|
attributes.screenInteraction = .dismiss
|
|
attributes.screenBackground = .color(color: EKColor.init(light: #colorLiteral(red: 0, green: 0, blue: 0, alpha: 0.4), dark: #colorLiteral(red: 0, green: 0, blue: 0, alpha: 0.4)))
|
attributes.entryBackground = .color(color: EKColor.init(light: backgroundColor, dark: backgroundColor))
|
attributes.shadow = .active(with: .init(color: .black, opacity: 0.3, radius: 6))
|
attributes.roundCorners = .all(radius: 20)
|
attributes.positionConstraints.size = .init(width: .fill, height: .intrinsic)
|
attributes.positionConstraints.maxSize = .init(width: .fill, height: .intrinsic)
|
|
SwiftEntryKit.display(entry: vc, using: attributes)
|
}
|
|
static func displayBottomNote(by vc: UIViewController, backgroundColor: UIColor = .white, isEnqueue: Bool = false) {
|
|
var attributes = EKAttributes()
|
attributes = .bottomNote
|
attributes.windowLevel = .alerts
|
attributes.position = .bottom
|
attributes.displayDuration = .infinity
|
attributes.precedence = isEnqueue == true ? .enqueue(priority: .normal) : .override(priority: .normal, dropEnqueuedEntries: false)
|
|
attributes.entranceAnimation = .init(translate: EKAttributes.Animation.Translate.init(duration: 0.25),
|
scale: nil,
|
fade: nil)
|
attributes.exitAnimation = .init(translate: EKAttributes.Animation.Translate.init(duration: 0.25),
|
scale: nil,
|
fade: nil)
|
|
attributes.entryInteraction = .absorbTouches
|
attributes.screenInteraction = .dismiss
|
|
attributes.screenBackground = .color(color: EKColor.init(light: #colorLiteral(red: 0, green: 0, blue: 0, alpha: 0.4), dark: #colorLiteral(red: 0, green: 0, blue: 0, alpha: 0.4)))
|
attributes.entryBackground = .color(color: EKColor.init(light: backgroundColor, dark: backgroundColor))
|
attributes.shadow = .active(with: .init(color: .black, opacity: 0.3, radius: 6))
|
attributes.roundCorners = .top(radius: 20)
|
attributes.positionConstraints.size = .init(width: .fill, height: .intrinsic)
|
attributes.positionConstraints.maxSize = .init(width: .fill, height: .intrinsic)
|
|
SwiftEntryKit.display(entry: vc, using: attributes)
|
}
|
|
// static func displayAlertView(title: String = "提示", message: String, cancelTitle: String = "取消", cancelHandler: @escaping () -> Void = {}, doneTitle: String = "确定", doneHandler: @escaping () -> Void) {
|
//
|
// // Generate textual content
|
// let title = EKProperty.LabelContent(text: title, style: .init(font: UIFont.boldSystemFont(ofSize: 18), color: .black, alignment: .center))
|
// let description = EKProperty.LabelContent(text: message, style: .init(font: UIFont.italicSystemFont(ofSize: 13), color: .black, alignment: .center))
|
// let simpleMessage = EKSimpleMessage(title: title, description: description)
|
//
|
// // Generate buttons content
|
// let buttonFont = UIFont.systemFont(ofSize: 16)
|
//
|
// // Close button
|
// let closeButtonLabelStyle = EKProperty.LabelStyle(font: buttonFont, color: #colorLiteral(red: 0, green: 0, blue: 0, alpha: 0.4))
|
// let closeButtonLabel = EKProperty.LabelContent(text: cancelTitle, style: closeButtonLabelStyle)
|
// let closeButton = EKProperty.ButtonContent(label: closeButtonLabel, backgroundColor: .clear, highlightedBackgroundColor: UIColor.gray.withAlphaComponent(0.05)) {
|
// SwiftEntryKit.dismiss()
|
// cancelHandler()
|
// }
|
//
|
// // Ok Button
|
// let okButtonLabelStyle = EKProperty.LabelStyle(font: buttonFont, color: .red)
|
// let okButtonLabel = EKProperty.LabelContent(text: doneTitle, style: okButtonLabelStyle)
|
// let okButton = EKProperty.ButtonContent(label: okButtonLabel, backgroundColor: .clear, highlightedBackgroundColor: UIColor.red.withAlphaComponent(0.05)) {
|
// SwiftEntryKit.dismiss()
|
// doneHandler()
|
// }
|
//
|
// // Generate the content
|
// let buttonsBarContent = EKProperty.ButtonBarContent(with: closeButton, okButton, separatorColor: .lightGray, expandAnimatedly: false)
|
//
|
// let alertMessage = EKAlertMessage(simpleMessage: simpleMessage, buttonBarContent: buttonsBarContent)
|
//
|
// // Setup the view itself
|
// let contentView = EKAlertMessageView(with: alertMessage)
|
//
|
// var attributes = EKAttributes()
|
// attributes = .centerFloat
|
// attributes.windowLevel = .alerts
|
// attributes.hapticFeedbackType = .success
|
// attributes.screenInteraction = .absorbTouches
|
// attributes.entryInteraction = .absorbTouches
|
// attributes.scroll = .disabled
|
// attributes.screenBackground = .color(color: #colorLiteral(red: 0, green: 0, blue: 0, alpha: 0.4))
|
// attributes.entryBackground = .color(color: .white)
|
// attributes.entranceAnimation = .init(scale: .init(from: 0.9, to: 1, duration: 0.4, spring: .init(damping: 1, initialVelocity: 0)), fade: .init(from: 0, to: 1, duration: 0.3))
|
// attributes.exitAnimation = .init(fade: .init(from: 1, to: 0, duration: 0.2))
|
// attributes.displayDuration = .infinity
|
// attributes.shadow = .active(with: .init(color: .black, opacity: 0.3, radius: 5))
|
// attributes.positionConstraints.maxSize = .init(width: .constant(value: 300), height: .intrinsic)
|
//
|
// SwiftEntryKit.display(entry: contentView, using: attributes)
|
// }
|
//
|
// static func displayTipsAlertView(title: String = "提示", message: String, doneTitle: String = "确定", doneHandler: @escaping () -> Void) {
|
//
|
// // Generate textual content
|
// let title = EKProperty.LabelContent(text: title, style: .init(font: UIFont.boldSystemFont(ofSize: 18), color: .black, alignment: .center))
|
// let description = EKProperty.LabelContent(text: message, style: .init(font: UIFont.italicSystemFont(ofSize: 13), color: .black, alignment: .center))
|
// let simpleMessage = EKSimpleMessage(title: title, description: description)
|
//
|
// // Generate buttons content
|
// let buttonFont = UIFont.systemFont(ofSize: 16)
|
//
|
// // Ok Button
|
// let okButtonLabelStyle = EKProperty.LabelStyle(font: buttonFont, color: #colorLiteral(red: 1, green: 0, blue: 0.2470588235, alpha: 1))
|
// let okButtonLabel = EKProperty.LabelContent(text: doneTitle, style: okButtonLabelStyle)
|
// let okButton = EKProperty.ButtonContent(label: okButtonLabel, backgroundColor: .clear, highlightedBackgroundColor: UIColor.red.withAlphaComponent(0.05)) {
|
// SwiftEntryKit.dismiss()
|
// doneHandler()
|
// }
|
//
|
//
|
//
|
// // Generate the content
|
// let buttonsBarContent = EKProperty.ButtonBarContent(with: okButton, separatorColor: .lightGray, expandAnimatedly: false)
|
//
|
// let alertMessage = EKAlertMessage(simpleMessage: simpleMessage, buttonBarContent: buttonsBarContent)
|
//
|
// // Setup the view itself
|
// let contentView = EKAlertMessageView(with: alertMessage)
|
//
|
// var attributes = EKAttributes()
|
// attributes = .centerFloat
|
// attributes.windowLevel = .alerts
|
// attributes.hapticFeedbackType = .success
|
// attributes.screenInteraction = .absorbTouches
|
// attributes.entryInteraction = .absorbTouches
|
// attributes.scroll = .disabled
|
// attributes.screenBackground = .color(color: #colorLiteral(red: 0, green: 0, blue: 0, alpha: 0.4))
|
// attributes.entryBackground = .color(color: .white)
|
// attributes.entranceAnimation = .init(scale: .init(from: 0.9, to: 1, duration: 0.4, spring: .init(damping: 1, initialVelocity: 0)), fade: .init(from: 0, to: 1, duration: 0.3))
|
// attributes.exitAnimation = .init(fade: .init(from: 1, to: 0, duration: 0.2))
|
// attributes.displayDuration = .infinity
|
// attributes.shadow = .active(with: .init(color: .black, opacity: 0.3, radius: 5))
|
// attributes.positionConstraints.maxSize = .init(width: .constant(value: 300), height: .intrinsic)
|
//
|
// SwiftEntryKit.display(entry: contentView, using: attributes)
|
// }
|
//
|
// static func displayTextFieldAlertView(title: String?, placeholder: String?, doneTitle: String?, text: @escaping (String) -> Void) {
|
//
|
// let title = EKProperty.LabelContent(text: title ?? "", style: .init(font: UIFont.systemFont(ofSize: 16), color: .darkGray, alignment: .center))
|
// let textField = EKProperty.TextFieldContent(keyboardType: .numberPad,
|
// placeholder: .init(text: placeholder ?? "", style: .init(font: UIFont.systemFont(ofSize: 14), color: .lightGray)),
|
// textStyle: EKProperty.LabelStyle.init(font: UIFont.systemFont(ofSize: 16), color: .darkGray),
|
// isSecure: false,
|
// leadingImage: nil,
|
// bottomBorderColor: #colorLiteral(red: 0.9647058824, green: 0.9647058824, blue: 0.9647058824, alpha: 1))
|
// let textFields = [textField]
|
//
|
// let button = EKProperty.ButtonContent(label: .init(text: doneTitle ?? "", style: .init(font: UIFont.systemFont(ofSize: 16), color: .white)), backgroundColor: #colorLiteral(red: 1, green: 0.3215686275, blue: 0.3215686275, alpha: 1), highlightedBackgroundColor: #colorLiteral(red: 1, green: 0.3215686275, blue: 0.3215686275, alpha: 1).withAlphaComponent(0.8)) {
|
//
|
// text(textField.textContent)
|
//
|
// SwiftEntryKit.dismiss()
|
// }
|
//
|
// let contentView = EKFormMessageView(with: title, textFieldsContent: textFields, buttonContent: button)
|
//
|
// var attributes = EKAttributes()
|
// attributes = .float
|
// attributes.windowLevel = .normal
|
// attributes.position = .center
|
// attributes.displayDuration = .infinity
|
//
|
// attributes.entranceAnimation = .init(translate: .init(duration: 0.65, anchorPosition: .bottom, spring: .init(damping: 1, initialVelocity: 0)))
|
// attributes.exitAnimation = .init(translate: .init(duration: 0.65, anchorPosition: .bottom, spring: .init(damping: 1, initialVelocity: 0)))
|
// attributes.popBehavior = .animated(animation: .init(translate: .init(duration: 0.65, spring: .init(damping: 1, initialVelocity: 0))))
|
//
|
// attributes.entryInteraction = .absorbTouches
|
// attributes.screenInteraction = .dismiss
|
//
|
// attributes.entryBackground = .color(color: .white)
|
// attributes.screenBackground = .color(color: #colorLiteral(red: 0, green: 0, blue: 0, alpha: 0.4))
|
//
|
// attributes.border = .value(color: UIColor(white: 0.6, alpha: 1), width: 1)
|
// attributes.shadow = .active(with: .init(color: .black, opacity: 0.3, radius: 3))
|
// attributes.scroll = .enabled(swipeable: false, pullbackAnimation: .jolt)
|
// attributes.statusBar = .light
|
//
|
// attributes.positionConstraints.keyboardRelation = .bind(offset: .init(bottom: 15, screenEdgeResistance: 0))
|
// attributes.positionConstraints.maxSize = .init(width: .constant(value: UIScreen.main.bounds.width), height: .intrinsic)
|
//
|
// attributes.lifecycleEvents.didAppear = {
|
// contentView.becomeFirstResponder(with: 0)
|
// }
|
//
|
// SwiftEntryKit.display(entry: contentView, using: attributes)
|
// }
|
|
}
|
|
extension YYAlertKit {
|
|
struct Action {
|
|
let title: String
|
let style: Style
|
let action: (() -> Void)?
|
|
enum Style {
|
case plain
|
case cancel
|
}
|
}
|
|
}
|