feat(mobile): replace SPIndicator with ToastViewSwift for notifications

Signed-off-by: Innei <tukon479@gmail.com>
This commit is contained in:
Innei
2025-04-09 20:31:50 +08:00
parent a65742e2bf
commit 83e7764cf0
18 changed files with 182 additions and 1495 deletions
+1 -1
View File
@@ -22,7 +22,7 @@ Pod::Spec.new do |s|
s.dependency 'ExpoModulesCore'
s.dependency 'SnapKit', '~> 5.7.0'
s.dependency 'SDWebImage', '~> 5.0'
s.dependency "ToastViewSwift", "~> 2.1.3"
# Swift/Objective-C compatibility
s.pod_target_xcconfig = {
'DEFINES_MODULE' => 'YES',
@@ -71,7 +71,7 @@ public class HelperModule: Module {
}
guard let image = imageView.image else { return }
UIImageWriteToSavedPhotosAlbum(image, self, #selector(HelperModule.image), nil)
SPIndicator.present(title: "Saved to photos", preset: .done, haptic: .success)
Toast.show(options: .init(type: .success, title: "Saved to photos"))
}
}
}
@@ -132,7 +132,8 @@ public class HelperModule: Module {
guard let image = imageView.image else { return }
guard let imageData = image.pngData() else { return }
UIPasteboard.general.setData(imageData, forPasteboardType: "public.png")
SPIndicator.present(title: "Image copied to clipboard", preset: .done, haptic: .success)
Toast.show(options: .init(type: .success, title: "Image copied to clipboard"))
}
}
}
@@ -150,9 +151,9 @@ public class HelperModule: Module {
_ image: UIImage, didFinishSavingWithError error: Error?, contextInfo: UnsafeRawPointer?
) {
if let error = error {
SPIndicator.present(title: "Save image failed", preset: .error, haptic: .error)
Toast.show(options: .init(type: .error, title: "Save image failed"))
} else {
SPIndicator.present(title: "Save image success", preset: .done, haptic: .success)
Toast.show(options: .init(type: .success, title: "Saved to photos"))
}
}
@@ -0,0 +1,76 @@
//
// Toast.swift
// FollowNative
//
// Created by Innei on 2025/4/9.
//
import Foundation
import ToastViewSwift
public enum Toast {
enum ToastType: String {
case error
case info
case warn
case success
}
enum Position: String {
case top
case center
case bottom
}
struct ToastOptions {
var message: String?
var type: ToastType = .info
var duration: Double = 1.5
var title: String
var position: Position?
}
static func show(options: ToastOptions) {
DispatchQueue.main.async {
let config: ToastConfiguration = .init(
direction: options.position?.position() ?? .top,
dismissBy: [.time(time: TimeInterval(options.duration))]
)
ToastViewSwift.Toast.default(image: options.type.image(), title: options.title, subtitle: options.message, config: config)
.show(haptic: options.type.haptic())
}
}
}
extension Toast.Position {
func position() -> ToastViewSwift.Toast.Direction {
switch self {
case .top:
return .top
case .center:
return .center
case .bottom:
return .bottom
}
}
}
extension Toast.ToastType {
func image() -> UIImage {
switch self {
case .error: UIImage(systemName: "xmark.octagon")!.withTintColor(.systemRed)
case .warn: UIImage(systemName: "exclamationmark.triangle")!.withTintColor(.systemOrange)
case .info: UIImage(systemName: "info.circle")!.withTintColor(.systemBlue)
case .success: UIImage(systemName: "checkmark.circle.fill")!.withTintColor(.systemGreen)
}
}
func haptic() -> UINotificationFeedbackGenerator.FeedbackType {
switch self {
case .error: .error
case .info: .success
case .warn: .warning
case .success: .success
}
}
}
@@ -6,79 +6,62 @@
import ExpoModulesCore
enum ToastType: String, Enumerable {
case error
case info
case warn
case success
}
extension ToastType {
func type() -> SPIndicatorIconPreset {
switch self
{
case .error: .error
case .warn: .custom(UIImage(systemName: "exclamationmark.triangle")!.withTintColor(.orange))
case .info: .custom(UIImage(systemName: "info.circle")!.withTintColor(.blue))
case .success: .done
}
}
func haptic() -> SPIndicatorHaptic {
switch self {
case .error: .error
case .info: .none
case .warn: .warning
case .success: .success
}
}
}
enum Position: String, Enumerable {
case top
case center
case bottom
}
extension Position {
func toSPPresentSide() -> SPIndicatorPresentSide {
switch self {
case .top: .top
case .center: .center
case .bottom: .bottom
}
}
}
struct ToastOptions: Record {
@Field
var message: String?
@Field
var type: ToastType = .info
@Field
var duration: Double = 1.5
@Field
var title: String
@Field
var position: Position?
}
public class ToasterModule: Module {
public func definition() -> ModuleDefinition {
Name("Toaster")
Function("toast") { (value: ToastOptions) in
DispatchQueue.main.sync {
let indicatorView = SPIndicatorView(
title: value.title, message: value.message, preset: value.type.type())
if value.position != nil {
indicatorView.presentSide = value.position!.toSPPresentSide()
}
indicatorView.present(duration: value.duration, haptic: value.type.haptic())
}
struct ToastOptions: Record {
@Field
var message: String?
@Field
var type: ToastType = .info
@Field
var duration: Double = 1.5
@Field
var title: String
@Field
var position: Position?
}
enum ToastType: String, Enumerable {
case error
case info
case warn
case success
func toToastType() -> Toast.ToastType {
switch self {
case .error:
return .error
case .info:
return .info
case .warn:
return .warn
case .success:
return .success
}
}
}
enum Position: String, Enumerable {
case top
case center
case bottom
func toPostion() -> Toast.Position {
switch self {
case .top:
return .top
case .center:
return .center
case .bottom:
return .bottom
}
}
}
public func definition() -> ModuleDefinition {
Name("Toaster")
Function("toast") { (value: ToastOptions) in
Toast.show(options: Toast.ToastOptions(message: value.message, type: value.type.toToastType(), duration: value.duration, title: value.title, position: value.position?.toPostion() ?? .top))
}
}
}
}
@@ -3,7 +3,6 @@ import UIKit
class ImageViewerController: UIViewController,
UIGestureRecognizerDelegate
{
var imageView: UIImageView = UIImageView(frame: .zero)
let imageLoader: ImageLoader
private var activityIndicator: UIActivityIndicatorView!
@@ -24,6 +23,7 @@ class ImageViewerController: UIViewController,
}
// MARK: Layout Constraints
private var top: NSLayoutConstraint!
private var leading: NSLayoutConstraint!
private var trailing: NSLayoutConstraint!
@@ -35,9 +35,9 @@ class ImageViewerController: UIViewController,
private var isAnimating: Bool = false
private var maxZoomScale: CGFloat = 1.0
private var sourceView: UIImageView? = nil
private var sourceView: UIImageView?
private var _error: Error? = nil
private var _error: Error?
init(
index: Int,
@@ -45,7 +45,6 @@ class ImageViewerController: UIViewController,
imageLoader: ImageLoader,
sourceView: UIImageView? = nil
) {
self.index = index
self.imageItem = imageItem
self.imageLoader = imageLoader
@@ -94,19 +93,18 @@ class ImageViewerController: UIViewController,
leading.isActive = true
trailing.isActive = true
bottom.isActive = true
}
override func viewDidLoad() {
super.viewDidLoad()
switch imageItem {
case .image(let img):
case let .image(img):
imageView.image = img
imageView.layoutIfNeeded()
case .url(let url, let placeholder):
case let .url(url, placeholder):
activityIndicator.startAnimating()
if let sourceView = self.sourceView {
if let sourceView = sourceView {
imageView.image = sourceView.image
}
@@ -114,11 +112,11 @@ class ImageViewerController: UIViewController,
imageView.image = UIImage(systemName: "photo")?.withTintColor(
.gray, renderingMode: .alwaysOriginal)
imageView.frame = .init(origin: .zero, size: .init(width: 100, height: 100))
self.layout()
layout()
}
self.layout()
layout()
imageLoader.loadImage(url, placeholder: placeholder, imageView: imageView) {
[weak self] (image) in
[weak self] _ in
self?.layout()
self?.activityIndicator.stopAnimating()
} onError: { [weak self] error in
@@ -126,8 +124,14 @@ class ImageViewerController: UIViewController,
.withTintColor(.red, renderingMode: .alwaysOriginal)
self?.imageView.frame = .init(origin: .zero, size: .init(width: 30, height: 30))
self?.layout()
SPIndicator.present(
title: "Image Load Error", message: error.localizedDescription, preset: .error)
Toast.show(
options: .init(
message: error.localizedDescription,
type: .error,
title: "Image Load Error"
))
self?.activityIndicator.stopAnimating()
self?._error = error
}
@@ -136,17 +140,16 @@ class ImageViewerController: UIViewController,
}
addGestureRecognizers()
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
self.navBar?.alpha = 1.0
navBar?.alpha = 1.0
}
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
self.navBar?.alpha = 0.0
navBar?.alpha = 0.0
}
override func viewWillLayoutSubviews() {
@@ -160,8 +163,8 @@ class ImageViewerController: UIViewController,
}
// MARK: Add Gesture Recognizers
func addGestureRecognizers() {
func addGestureRecognizers() {
let panGesture = UIPanGestureRecognizer(
target: self, action: #selector(didPan(_:)))
panGesture.cancelsTouchesInView = false
@@ -230,8 +233,7 @@ class ImageViewerController: UIViewController,
@objc
func didSingleTap(_ recognizer: UITapGestureRecognizer) {
let currentNavAlpha = self.navBar?.alpha ?? 0.0
let currentNavAlpha = navBar?.alpha ?? 0.0
UIView.animate(withDuration: 0.235) {
self.navBar?.alpha = currentNavAlpha > 0.5 ? 0.0 : 1.0
}
@@ -247,11 +249,19 @@ class ImageViewerController: UIViewController,
_ image: UIImage, didFinishSavingWithError error: Error?, contextInfo: UnsafeRawPointer
) {
if let error = error {
SPIndicator.present(
title: "Save Error", message: error.localizedDescription, preset: .error,
haptic: .error)
Toast.show(
options: .init(
message: error.localizedDescription,
type: .error,
title: "Save Error"
))
} else {
SPIndicator.present(title: "Saved", preset: .done, haptic: .success)
Toast.show(
options: .init(
message: "Saved",
type: .success,
title: "Save Success"
))
}
}
@@ -265,14 +275,12 @@ class ImageViewerController: UIViewController,
let velocity = panGesture.velocity(in: scrollView)
return abs(velocity.y) > abs(velocity.x)
}
}
// MARK: Adjusting the dimensions
extension ImageViewerController {
func updateMinMaxZoomScaleForSize(_ size: CGSize) {
let targetSize = imageView.bounds.size
if targetSize.width == 0 || targetSize.height == 0 {
return
@@ -314,14 +322,13 @@ extension ImageViewerController {
trailing.constant = xOffset
view.layoutIfNeeded()
}
}
// MARK: Animation Related stuff
extension ImageViewerController {
extension ImageViewerController {
private func executeCancelAnimation() {
self.isAnimating = true
isAnimating = true
UIView.animate(
withDuration: 0.237,
animations: {
@@ -335,7 +342,6 @@ extension ImageViewerController {
}
extension ImageViewerController: UIScrollViewDelegate {
func viewForZooming(in scrollView: UIScrollView) -> UIView? {
return imageView
}
@@ -346,9 +352,8 @@ extension ImageViewerController: UIScrollViewDelegate {
}
extension ImageViewerController: ImageCarouselViewControllerProtocol {
public func isLoadError() -> Bool {
guard let error = _error else { return false }
guard _error != nil else { return false }
return true
}
@@ -363,7 +368,13 @@ extension ImageViewerController: ImageCarouselViewControllerProtocol {
guard let image = imageView.image else { return }
UIPasteboard.general.image = image
SPIndicator.present(title: "Copied", preset: .done, haptic: .success)
Toast.show(
options: .init(
message: "Copied",
type: .success,
title: "Copy Success"
))
}
public func shareImage() {
@@ -383,5 +394,4 @@ extension ImageViewerController: ImageCarouselViewControllerProtocol {
present(activityViewController, animated: true)
}
}
@@ -1,88 +0,0 @@
// The MIT License (MIT)
// Copyright © 2021 Ivan Vorobei (hello@ivanvorobei.io)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
import SwiftUI
@available(iOSApplicationExtension, unavailable)
extension View {
public func SPIndicator(
isPresent: Binding<Bool>,
indicatorView: SPIndicatorView,
duration: TimeInterval = 2.0,
haptic: SPIndicatorHaptic = .none
) -> some View {
if isPresent.wrappedValue {
let indicatorCompletion = indicatorView.completion
let indicatorDismiss = {
isPresent.wrappedValue = false
indicatorCompletion?()
}
indicatorView.duration = duration
indicatorView.present(haptic: haptic, completion: indicatorDismiss)
}
return self
}
public func SPIndicator(
isPresent: Binding<Bool>,
title: String = "",
message: String? = nil,
duration: TimeInterval = 2.0,
presentSide: SPIndicatorPresentSide = .top,
dismissByDrag: Bool = true,
preset: SPIndicatorIconPreset = .done,
haptic: SPIndicatorHaptic = .none,
layout: SPIndicatorLayout? = nil,
completion: (() -> Void)? = nil
) -> some View {
let indicatorView = SPIndicatorView(title: title, message: message, preset: preset)
indicatorView.presentSide = presentSide
indicatorView.dismissByDrag = dismissByDrag
indicatorView.layout = layout ?? SPIndicatorLayout(for: preset)
indicatorView.completion = completion
return SPIndicator(
isPresent: isPresent, indicatorView: indicatorView, duration: duration, haptic: haptic)
}
public func SPIndicator(
isPresent: Binding<Bool>,
title: String,
duration: TimeInterval = 2.0,
presentSide: SPIndicatorPresentSide = .top,
dismissByDrag: Bool = true,
preset: SPIndicatorIconPreset = .done,
haptic: SPIndicatorHaptic = .none,
completion: (() -> Void)? = nil
) -> some View {
let indicatorView = SPIndicatorView(title: title, preset: preset)
indicatorView.presentSide = presentSide
indicatorView.dismissByDrag = dismissByDrag
indicatorView.completion = completion
return SPIndicator(
isPresent: isPresent, indicatorView: indicatorView, duration: duration, haptic: haptic)
}
}
@@ -1,80 +0,0 @@
// The MIT License (MIT)
// Copyright © 2021 Ivan Vorobei (hello@ivanvorobei.io)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
import UIKit
extension UIColor {
enum Compability {
static var systemBackground: UIColor {
return UIColor.systemBackground
}
static var secondarySystemBackground: UIColor {
return UIColor.secondarySystemBackground
}
static var separator: UIColor {
return UIColor.separator
}
static var label: UIColor {
return UIColor.label
}
static var secondaryLabel: UIColor {
return UIColor.secondaryLabel
}
}
static var buttonArea: UIColor {
return UIColor { (traits) -> UIColor in
return traits.userInterfaceStyle == .dark
? UIColor(red: 61 / 255, green: 62 / 255, blue: 66 / 255, alpha: 1)
: UIColor(red: 238 / 255, green: 238 / 255, blue: 240 / 255, alpha: 1)
}
}
@available(iOSApplicationExtension, unavailable)
static var tint: UIColor {
get {
let value = UIApplication.shared.windows.first?.tintColor
guard let tint = value else { return .systemBlue }
return tint
}
set {
UIApplication.shared.windows.forEach({ $0.tintColor = newValue })
}
}
}
@@ -1,32 +0,0 @@
// The MIT License (MIT)
// Copyright © 2021 Ivan Vorobei (hello@ivanvorobei.io)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
import UIKit
extension UIFont {
static func preferredFont(forTextStyle style: TextStyle, weight: Weight, addPoints: CGFloat = 0) -> UIFont {
let descriptor = UIFontDescriptor.preferredFontDescriptor(withTextStyle: style)
let font = UIFont.systemFont(ofSize: descriptor.pointSize + addPoints, weight: weight)
let metrics = UIFontMetrics(forTextStyle: style)
return metrics.scaledFont(for: font)
}
}
@@ -1,37 +0,0 @@
// The MIT License (MIT)
// Copyright © 2021 Ivan Vorobei (hello@ivanvorobei.io)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
import UIKit
extension UILabel {
func layoutDynamicHeight(width: CGFloat) {
layoutDynamicHeight(x: frame.origin.x, y: frame.origin.y, width: width)
}
func layoutDynamicHeight(x: CGFloat, y: CGFloat, width: CGFloat) {
frame = CGRect.init(x: x, y: y, width: width, height: frame.height)
sizeToFit()
if frame.width != width {
frame = .init(x: x, y: y, width: width, height: frame.height)
}
}
}
@@ -1,84 +0,0 @@
// The MIT License (MIT)
// Copyright © 2021 Ivan Vorobei (hello@ivanvorobei.io)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
import UIKit
public class SPIndicatorIconErrorView: UIView, SPIndicatorIconAnimatable {
public func animate() {
animateTopToBottomLine()
animateBottomToTopLine()
}
private func animateTopToBottomLine() {
let length = frame.width
let topToBottomLine = UIBezierPath()
topToBottomLine.move(to: CGPoint(x: length * 0, y: length * 0))
topToBottomLine.addLine(to: CGPoint(x: length * 1, y: length * 1))
let animatableLayer = CAShapeLayer()
animatableLayer.path = topToBottomLine.cgPath
animatableLayer.fillColor = UIColor.clear.cgColor
animatableLayer.strokeColor = tintColor?.cgColor
animatableLayer.lineWidth = 4
animatableLayer.lineCap = .round
animatableLayer.lineJoin = .round
animatableLayer.strokeEnd = 0
self.layer.addSublayer(animatableLayer)
let animation = CABasicAnimation(keyPath: "strokeEnd")
animation.duration = 0.22
animation.fromValue = 0
animation.toValue = 1
animation.timingFunction = CAMediaTimingFunction(name: .easeInEaseOut)
animatableLayer.strokeEnd = 1
animatableLayer.add(animation, forKey: "animation")
}
private func animateBottomToTopLine() {
let length = frame.width
let bottomToTopLine = UIBezierPath()
bottomToTopLine.move(to: CGPoint(x: length * 0, y: length * 1))
bottomToTopLine.addLine(to: CGPoint(x: length * 1, y: length * 0))
let animatableLayer = CAShapeLayer()
animatableLayer.path = bottomToTopLine.cgPath
animatableLayer.fillColor = UIColor.clear.cgColor
animatableLayer.strokeColor = tintColor?.cgColor
animatableLayer.lineWidth = 4
animatableLayer.lineCap = .round
animatableLayer.lineJoin = .round
animatableLayer.strokeEnd = 0
self.layer.addSublayer(animatableLayer)
let animation = CABasicAnimation(keyPath: "strokeEnd")
animation.duration = 0.22
animation.fromValue = 0
animation.toValue = 1
animation.timingFunction = CAMediaTimingFunction(name: .easeInEaseOut)
animatableLayer.strokeEnd = 1
animatableLayer.add(animation, forKey: "animation")
}
}
@@ -1,51 +0,0 @@
// The MIT License (MIT)
// Copyright © 2021 Ivan Vorobei (hello@ivanvorobei.io)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
import UIKit
public class SPIndicatorIconDoneView: UIView, SPIndicatorIconAnimatable {
public func animate() {
let length = frame.width
let animatablePath = UIBezierPath()
animatablePath.move(to: CGPoint(x: length * 0.196, y: length * 0.527))
animatablePath.addLine(to: CGPoint(x: length * 0.47, y: length * 0.777))
animatablePath.addLine(to: CGPoint(x: length * 0.99, y: length * 0.25))
let animatableLayer = CAShapeLayer()
animatableLayer.path = animatablePath.cgPath
animatableLayer.fillColor = UIColor.clear.cgColor
animatableLayer.strokeColor = tintColor?.cgColor
animatableLayer.lineWidth = 4
animatableLayer.lineCap = .round
animatableLayer.lineJoin = .round
animatableLayer.strokeEnd = 0
layer.addSublayer(animatableLayer)
let animation = CABasicAnimation(keyPath: "strokeEnd")
animation.duration = 0.25
animation.fromValue = 0
animation.toValue = 1
animation.timingFunction = CAMediaTimingFunction(name: .easeInEaseOut)
animatableLayer.strokeEnd = 1
animatableLayer.add(animation, forKey: "animation")
}
}
@@ -1,43 +0,0 @@
// The MIT License (MIT)
// Copyright © 2021 Ivan Vorobei (hello@ivanvorobei.io)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
import UIKit
/**
SPIndicator: Layout model for view.
*/
open class SPIndicatorLayout {
/**
SPIndicator: Icon size.
*/
open var iconSize: CGSize
/**
SPIndicator: Alert margings for each side.
*/
open var margins: UIEdgeInsets
public init(iconSize: CGSize, margins: UIEdgeInsets) {
self.iconSize = iconSize
self.margins = margins
}
}
@@ -1,32 +0,0 @@
// The MIT License (MIT)
// Copyright © 2021 Ivan Vorobei (hello@ivanvorobei.io)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
import Foundation
/**
SPIndicator: Cases from preset styles.
*/
public enum SPIndicatorPresentSide {
case top
case center
case bottom
}
@@ -1,33 +0,0 @@
// The MIT License (MIT)
// Copyright © 2021 Ivan Vorobei (hello@ivanvorobei.io)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
import UIKit
/**
SPIndicator: Protocol for animatable views.
*/
public protocol SPIndicatorIconAnimatable {
/**
SPIndicator: Shoud call when need start animation.
*/
func animate()
}
@@ -1,83 +0,0 @@
// The MIT License (MIT)
// Copyright © 2021 Ivan Vorobei (hello@ivanvorobei.io)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
import UIKit
@available(iOSApplicationExtension, unavailable)
public enum SPIndicator {
/**
SPIndicator: Present alert with preset and custom haptic.
- parameter title: Title text in alert.
- parameter message: Subtitle text in alert. Optional.
- parameter preset: Icon ready-use style or custom image.
- parameter haptic: Haptic response with present. Default is `.success`.
- parameter presentSide: Choose from side appear indicator.
- parameter completion: Will call with dismiss alert.
*/
public static func present(
title: String, message: String? = nil, preset: SPIndicatorIconPreset,
haptic: SPIndicatorHaptic, from presentSide: SPIndicatorPresentSide = .top,
completion: (() -> Void)? = nil
) {
let alertView = SPIndicatorView(title: title, message: message, preset: preset)
alertView.presentSide = presentSide
alertView.present(haptic: haptic, completion: completion)
}
/**
SPIndicator: Present alert with preset and automatically detect type haptic.
- parameter title: Title text in alert.
- parameter message: Subtitle text in alert. Optional.
- parameter preset: Icon ready-use style or custom image.
- parameter presentSide: Choose from side appear indicator. Default is `.top`.
- parameter completion: Will call with dismiss alert.
*/
public static func present(
title: String, message: String? = nil, preset: SPIndicatorIconPreset,
from presentSide: SPIndicatorPresentSide = .top, completion: (() -> Void)? = nil
) {
let alertView = SPIndicatorView(title: title, message: message, preset: preset)
alertView.presentSide = presentSide
let haptic = preset.getHaptic()
alertView.present(haptic: haptic, completion: completion)
}
/**
SPIndicator: Show only message, without title and icon.
- parameter message: Title text.
- parameter haptic: Haptic response with present. Default is `.success`.
- parameter presentSide: Choose from side appear indicator. Default is `.top`.
- parameter completion: Will call with dismiss alert.
*/
public static func present(
title: String, message: String? = nil, haptic: SPIndicatorHaptic,
from presentSide: SPIndicatorPresentSide = .top, completion: (() -> Void)? = nil
) {
let alertView = SPIndicatorView(title: title, message: message)
alertView.presentSide = presentSide
alertView.present(haptic: haptic, completion: completion)
}
}
@@ -1,119 +0,0 @@
// The MIT License (MIT)
// Copyright © 2021 Ivan Vorobei (hello@ivanvorobei.io)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
import UIKit
/**
SPIndicator: Represent icon wrapper.
Included default styles and can be custom image.
*/
public enum SPIndicatorIconPreset {
case done
case error
case spin(_ style: UIActivityIndicatorView.Style)
case custom(_ image: UIImage)
}
// Get view and haptic by Preset.
public extension SPIndicatorIconPreset {
func createView() -> UIView {
switch self {
case .done:
let view = SPIndicatorIconDoneView()
view.tintColor = .systemBlue
return view
case .error:
let view = SPIndicatorIconErrorView()
view.tintColor = UIColor.systemRed
return view
case .spin(let style):
let view = UIActivityIndicatorView(style: style)
view.startAnimating()
return view
case .custom(let image):
let imageView = UIImageView(image: image)
imageView.contentMode = .scaleAspectFit
return imageView
}
}
func getHaptic() -> SPIndicatorHaptic {
switch self {
case .error: return .error
case .done: return .success
case .spin(_): return .none
case .custom(_): return .success
}
}
}
// Get layout by preset.
public extension SPIndicatorLayout {
convenience init() {
self.init(
iconSize: .init(
width: Self.defaultIconSideSize,
height: Self.defaultIconSideSize
),
margins: .init(
top: Self.defaultVerticallInset,
left: Self.defaultHorizontalInset,
bottom: Self.defaultVerticallInset,
right: Self.defaultHorizontalInset
)
)
}
static func message() -> SPIndicatorLayout {
let layout = SPIndicatorLayout()
return layout
}
convenience init(for preset: SPIndicatorIconPreset) {
switch preset {
case .done:
self.init()
iconSize = .init(width: 24, height: 24)
case .error:
self.init()
iconSize = .init(width: 14, height: 14)
margins.left = 19
margins.right = margins.left
case .spin(_):
self.init()
case .custom(_):
self.init()
}
}
// Default values.
private static var defaultIconSideSize: CGFloat { 28 }
private static var defaultSpaceBetweenIconAndTitle: CGFloat { 26 }
private static var defaultVerticallInset: CGFloat { 8 }
private static var defaultHorizontalInset: CGFloat { 15 }
}
@@ -1,654 +0,0 @@
// The MIT License (MIT)
// Copyright © 2021 Ivan Vorobei (hello@ivanvorobei.io)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
import UIKit
/// SPIndicator: Main view. Can be customisable if need.
///
/// For change duration, check method `present` and pass duration and other specific property if need customise.
///
/// Here available set window on which shoud be present.
/// If you have some windows, you shoud configure it. Check property `presentWindow`.
///
/// For disable dismiss by drag, check property `.dismissByDrag`.
///
/// Recomended call `SPIndicator` and choose style func.
@available(iOSApplicationExtension, unavailable)
open class SPIndicatorView: UIView {
// MARK: - UIAppearance
@objc dynamic open var duration: TimeInterval = 1.5
// MARK: - Properties
/**
SPIndicator: Change it for set `top` or `bottom` present side.
Shoud be change before present, instead of no effect.
*/
open var presentSide: SPIndicatorPresentSide = .top
/**
SPIndicator: By default allow drag indicator for hide.
While indicator is dragging, dismiss not work.
This behaviar can be disabled.
*/
open var dismissByDrag: Bool = true {
didSet {
setGesture()
}
}
/**
SPIndicator: Completion call after hide indicator.
*/
open var completion: (() -> Void)? = nil
// MARK: - Views
open var titleLabel: UILabel?
open var subtitleLabel: UILabel?
open var iconView: UIView?
private lazy var backgroundView: UIVisualEffectView = {
let view: UIVisualEffectView = {
if #available(iOS 13.0, *) {
return UIVisualEffectView(effect: UIBlurEffect(style: .systemThickMaterial))
} else {
return UIVisualEffectView(effect: UIBlurEffect(style: .light))
}
}()
view.isUserInteractionEnabled = false
return view
}()
weak open var presentWindow: UIWindow?
// MARK: - Init
public init(title: String, message: String? = nil, preset: SPIndicatorIconPreset) {
super.init(frame: CGRect.zero)
commonInit()
layout = SPIndicatorLayout(for: preset)
setTitle(title)
if let message = message {
setMessage(message)
}
setIcon(for: preset)
}
public init(title: String, message: String?) {
super.init(frame: CGRect.zero)
titleAreaFactor = 1.8
minimumAreaWidth = 100
commonInit()
layout = SPIndicatorLayout.message()
setTitle(title)
if let message = message {
setMessage(message)
}
}
public required init?(coder aDecoder: NSCoder) {
self.presentSide = .top
super.init(coder: aDecoder)
commonInit()
}
private func commonInit() {
preservesSuperviewLayoutMargins = false
insetsLayoutMarginsFromSafeArea = false
backgroundColor = .clear
backgroundView.layer.masksToBounds = true
addSubview(backgroundView)
setShadow()
setGesture()
}
// MARK: - Configure
private func setTitle(_ text: String) {
let label = UILabel()
label.font = UIFont.preferredFont(
forTextStyle: .footnote, weight: .semibold, addPoints: 0)
label.numberOfLines = 1
let style = NSMutableParagraphStyle()
style.lineBreakMode = .byTruncatingTail
style.lineSpacing = 3
label.attributedText = NSAttributedString(
string: text, attributes: [.paragraphStyle: style]
)
label.textAlignment = .center
label.textColor = UIColor.Compability.label
titleLabel = label
addSubview(label)
}
private func setMessage(_ text: String) {
let label = UILabel()
label.font = UIFont.preferredFont(
forTextStyle: .footnote, weight: .semibold, addPoints: 0)
label.numberOfLines = 3
let style = NSMutableParagraphStyle()
style.lineBreakMode = .byTruncatingTail
style.lineSpacing = 2
label.attributedText = NSAttributedString(
string: text, attributes: [.paragraphStyle: style]
)
label.textAlignment = .center
label.textColor = UIColor.Compability.label.withAlphaComponent(0.8)
subtitleLabel = label
addSubview(label)
}
private func setIcon(for preset: SPIndicatorIconPreset) {
let view = preset.createView()
self.iconView = view
addSubview(view)
}
private func setShadow() {
layer.shadowColor = UIColor.black.cgColor
layer.shadowOpacity = 0.22
layer.shadowOffset = .init(width: 0, height: 7)
layer.shadowRadius = 40
// Not use render shadow becouse backgorund is visual effect.
// If turn on it, background will hide.
// layer.shouldRasterize = true
}
private func setGesture() {
if dismissByDrag {
let gestureRecognizer = UIPanGestureRecognizer(
target: self, action: #selector(handlePan))
addGestureRecognizer(gestureRecognizer)
self.gestureRecognizer = gestureRecognizer
} else {
self.gestureRecognizer = nil
}
}
// MARK: - Present
private var presentAndDismissDuration: TimeInterval = 0.6
private var presentWithOpacity: Bool {
if presentSide == .center { return true }
return false
}
open func present(haptic: SPIndicatorHaptic = .success, completion: (() -> Void)? = nil) {
present(duration: self.duration, haptic: haptic, completion: completion)
}
open func present(
duration: TimeInterval, haptic: SPIndicatorHaptic = .success,
completion: (() -> Void)? = nil
) {
if self.presentWindow == nil {
self.presentWindow = UIApplication.shared.keyWindow
}
guard let window = self.presentWindow else { return }
window.addSubview(self)
// Prepare for present
self.whenGestureEndShoudHide = false
self.completion = completion
isHidden = true
sizeToFit()
layoutSubviews()
center.x = window.frame.midX
toPresentPosition(.prepare(presentSide))
self.alpha = presentWithOpacity ? 0 : 1
// Present
isHidden = false
haptic.impact()
UIView.animate(
withDuration: presentAndDismissDuration, delay: 0, usingSpringWithDamping: 1,
initialSpringVelocity: 0, options: [.beginFromCurrentState, .curveEaseOut],
animations: {
self.toPresentPosition(.visible(self.presentSide))
if self.presentWithOpacity { self.alpha = 1 }
},
completion: { finished in
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + duration) {
if self.gestureIsDragging {
self.whenGestureEndShoudHide = true
} else {
self.dismiss()
}
}
})
if let iconView = self.iconView as? SPIndicatorIconAnimatable {
DispatchQueue.main.asyncAfter(
deadline: DispatchTime.now() + presentAndDismissDuration / 3
) {
iconView.animate()
}
}
}
@objc open func dismiss() {
UIView.animate(
withDuration: presentAndDismissDuration, delay: 0, usingSpringWithDamping: 1,
initialSpringVelocity: 0, options: [.beginFromCurrentState, .curveEaseIn],
animations: {
self.toPresentPosition(.prepare(self.presentSide))
if self.presentWithOpacity { self.alpha = 0 }
},
completion: { finished in
self.removeFromSuperview()
self.completion?()
})
}
// MARK: - Internal
private var minimumYTranslationForHideByGesture: CGFloat = -10
private var maximumYTranslationByGesture: CGFloat = 60
private var gestureRecognizer: UIPanGestureRecognizer?
private var gestureIsDragging: Bool = false
private var whenGestureEndShoudHide: Bool = false
@objc func handlePan(_ gestureRecognizer: UIPanGestureRecognizer) {
if gestureRecognizer.state == .began || gestureRecognizer.state == .changed {
self.gestureIsDragging = true
let translation = gestureRecognizer.translation(in: self)
let newTranslation: CGFloat = {
switch presentSide {
case .top:
if translation.y <= 0 {
return translation.y
} else {
return min(maximumYTranslationByGesture, translation.y.squareRoot())
}
case .bottom:
if translation.y >= 0 {
return translation.y
} else {
let absolute = abs(translation.y)
return -min(maximumYTranslationByGesture, absolute.squareRoot())
}
case .center:
let absolute = abs(translation.y).squareRoot()
let newValue = translation.y < 0 ? -absolute : absolute
return min(maximumYTranslationByGesture, newValue)
}
}()
toPresentPosition(.fromVisible(newTranslation, from: (presentSide)))
}
if gestureRecognizer.state == .ended {
gestureIsDragging = false
var shoudDismissWhenEndAnimation: Bool = false
UIView.animate(
withDuration: presentAndDismissDuration, delay: 0, usingSpringWithDamping: 1,
initialSpringVelocity: 0, options: [.beginFromCurrentState, .curveEaseIn],
animations: {
if self.whenGestureEndShoudHide {
self.toPresentPosition(.prepare(self.presentSide))
shoudDismissWhenEndAnimation = true
} else {
let translation = gestureRecognizer.translation(in: self)
if translation.y < self.minimumYTranslationForHideByGesture {
self.toPresentPosition(.prepare(self.presentSide))
shoudDismissWhenEndAnimation = true
} else {
self.toPresentPosition(.visible(self.presentSide))
}
}
},
completion: { _ in
if shoudDismissWhenEndAnimation {
self.dismiss()
}
})
}
}
private func toPresentPosition(_ position: PresentPosition) {
let getPrepareTransform: ((_ side: SPIndicatorPresentSide) -> CGAffineTransform) = {
[weak self] side in
guard let self = self else { return .identity }
guard let window = UIApplication.shared.windows.first else { return .identity }
switch side {
case .top:
let topInset = window.safeAreaInsets.top
let position = -(topInset + 50)
return CGAffineTransform.identity.translatedBy(x: 0, y: position)
case .bottom:
let height = window.frame.height
let bottomInset = window.safeAreaInsets.bottom
let position = height + bottomInset + 50
return CGAffineTransform.identity.translatedBy(x: 0, y: position)
case .center:
return CGAffineTransform.identity.translatedBy(
x: 0, y: window.frame.height / 2 - self.frame.height / 2
).scaledBy(x: 0.9, y: 0.9)
}
}
let getVisibleTransform: ((_ side: SPIndicatorPresentSide) -> CGAffineTransform) = {
[weak self] side in
guard let self = self else { return .identity }
guard let window = UIApplication.shared.windows.first else { return .identity }
switch side {
case .top:
var topSafeAreaInsets = window.safeAreaInsets.top
if topSafeAreaInsets < 20 { topSafeAreaInsets = 20 }
let position = topSafeAreaInsets - 3 + self.offset
return CGAffineTransform.identity.translatedBy(x: 0, y: position)
case .bottom:
let height = window.frame.height
var bottomSafeAreaInsets = window.safeAreaInsets.top
if bottomSafeAreaInsets < 20 { bottomSafeAreaInsets = 20 }
let position =
height - bottomSafeAreaInsets - 3 - self.frame.height - self.offset
return CGAffineTransform.identity.translatedBy(x: 0, y: position)
case .center:
return CGAffineTransform.identity.translatedBy(
x: 0, y: window.frame.height / 2 - self.frame.height / 2)
}
}
switch position {
case .prepare(let presentSide):
transform = getPrepareTransform(presentSide)
case .visible(let presentSide):
transform = getVisibleTransform(presentSide)
case .fromVisible(let translation, let presentSide):
transform = getVisibleTransform(presentSide).translatedBy(x: 0, y: translation)
}
}
// MARK: - Layout
/**
SPIndicator: Wraper of layout values.
*/
open var layout: SPIndicatorLayout = .init()
/**
SPIndicator: Alert offset
*/
open var offset: CGFloat = 0
private var minimumAreaHeight: CGFloat = 50
private var minimumAreaWidth: CGFloat = 196
private var maximumAreaWidth: CGFloat = 260
private var titleAreaFactor: CGFloat = 2.5
private var spaceBetweenTitles: CGFloat = 1
private var spaceBetweenTitlesAndImage: CGFloat = 16
private var titlesCompactWidth: CGFloat {
if let iconView = self.iconView {
let space = iconView.frame.maxY + spaceBetweenTitlesAndImage
return frame.width - space * 2
} else {
return frame.width - layoutMargins.left - layoutMargins.right
}
}
private var titlesFullWidth: CGFloat {
if let iconView = self.iconView {
let space = iconView.frame.maxY + spaceBetweenTitlesAndImage
return frame.width - space - layoutMargins.right - self.spaceBetweenTitlesAndImage
} else {
return frame.width - layoutMargins.left - layoutMargins.right
}
}
open override func sizeThatFits(_ size: CGSize) -> CGSize {
var width = minimumAreaWidth
// First determine the width we'll use
titleLabel?.sizeToFit()
let titleWidth: CGFloat = titleLabel?.frame.width ?? 0
subtitleLabel?.sizeToFit()
let subtitleWidth: CGFloat = subtitleLabel?.frame.width ?? 0
width = (max(titleWidth, subtitleWidth) * titleAreaFactor).rounded()
if width < minimumAreaWidth { width = minimumAreaWidth }
if width > maximumAreaWidth { width = maximumAreaWidth }
// Now calculate heights based on this width
let availableTextWidth = width - layoutMargins.left - layoutMargins.right
var titleHeight: CGFloat = 0
if let titleLabel = titleLabel {
titleLabel.frame.size.width = availableTextWidth
titleLabel.sizeToFit()
titleHeight = titleLabel.frame.height
}
var subtitleHeight: CGFloat = 0
if let subtitleLabel = subtitleLabel {
subtitleLabel.frame.size.width = availableTextWidth
subtitleLabel.sizeToFit()
subtitleHeight = subtitleLabel.frame.height
}
// Calculate dynamic height based on content
var height: CGFloat = 0
if titleLabel != nil && subtitleLabel != nil {
height =
titleHeight + subtitleHeight + spaceBetweenTitles + layoutMargins.top
+ layoutMargins.bottom
} else if titleLabel != nil {
height = titleHeight + layoutMargins.top + layoutMargins.bottom
} else if subtitleLabel != nil {
height = subtitleHeight + layoutMargins.top + layoutMargins.bottom
}
// Ensure minimum height
if height < minimumAreaHeight {
height = minimumAreaHeight
}
return .init(width: width, height: height)
}
open override func layoutSubviews() {
super.layoutSubviews()
layoutMargins = layout.margins
layer.cornerRadius = frame.height / 2
backgroundView.frame = bounds
backgroundView.layer.cornerRadius = layer.cornerRadius
// Flags
let hasIcon = (self.iconView != nil)
let hasTitle = (self.titleLabel != nil)
let hasSubtite = (self.subtitleLabel != nil)
let fitTitleToCompact: Bool = {
guard let titleLabel = self.titleLabel else { return true }
titleLabel.numberOfLines = 1
titleLabel.sizeToFit()
return titleLabel.frame.width < titlesCompactWidth
}()
let fitSubtitleToCompact: Bool = {
guard let subtitleLabel = self.subtitleLabel else { return true }
let originalLines = subtitleLabel.numberOfLines
subtitleLabel.sizeToFit()
subtitleLabel.numberOfLines = originalLines
return subtitleLabel.frame.width < titlesCompactWidth
}()
let notFitAnyLabelToCompact: Bool = {
if !fitTitleToCompact { return true }
if !fitSubtitleToCompact { return true }
return false
}()
var layout: LayoutGrid = .iconTitleCentered
if (hasIcon && hasTitle && hasSubtite) && !notFitAnyLabelToCompact {
layout = .iconTitleMessageCentered
}
if (hasIcon && hasTitle && hasSubtite) && notFitAnyLabelToCompact {
layout = .iconTitleMessageLeading
}
if hasIcon && hasTitle && !hasSubtite {
layout = .iconTitleCentered
}
if !hasIcon && hasTitle && !hasSubtite {
layout = .title
}
if !hasIcon && hasTitle && hasSubtite {
layout = .titleMessage
}
// Actions
let layoutIcon = { [weak self] in
guard let self = self else { return }
guard let iconView = self.iconView else { return }
iconView.frame = .init(
origin: .init(x: self.layoutMargins.left, y: iconView.frame.origin.y),
size: self.layout.iconSize
)
iconView.center.y = self.bounds.midY
}
let layoutTitleCenteredCompact = { [weak self] in
guard let self = self else { return }
guard let titleLabel = self.titleLabel else { return }
titleLabel.textAlignment = .center
titleLabel.layoutDynamicHeight(width: self.titlesCompactWidth)
titleLabel.center.x = self.frame.width / 2
}
let layoutTitleCenteredFullWidth = { [weak self] in
guard let self = self else { return }
guard let titleLabel = self.titleLabel else { return }
titleLabel.textAlignment = .center
titleLabel.layoutDynamicHeight(width: self.titlesFullWidth)
titleLabel.center.x = self.frame.width / 2
}
let layoutTitleLeadingFullWidth = { [weak self] in
guard let self = self else { return }
guard let titleLabel = self.titleLabel else { return }
guard let iconView = self.iconView else { return }
let rtl = self.effectiveUserInterfaceLayoutDirection == .rightToLeft
titleLabel.textAlignment = rtl ? .right : .left
titleLabel.layoutDynamicHeight(width: self.titlesFullWidth)
titleLabel.frame.origin.x =
self.layoutMargins.left + iconView.frame.width + self.spaceBetweenTitlesAndImage
}
let layoutSubtitle = { [weak self] in
guard let self = self else { return }
guard let titleLabel = self.titleLabel else { return }
guard let subtitleLabel = self.subtitleLabel else { return }
subtitleLabel.textAlignment = titleLabel.textAlignment
subtitleLabel.layoutDynamicHeight(width: titleLabel.frame.width)
subtitleLabel.frame.origin.x = titleLabel.frame.origin.x
}
let layoutTitleSubtitleByVertical = { [weak self] in
guard let self = self else { return }
guard let titleLabel = self.titleLabel else { return }
guard let subtitleLabel = self.subtitleLabel else {
titleLabel.center.y = self.bounds.midY
return
}
let allHeight =
titleLabel.frame.height + subtitleLabel.frame.height + self.spaceBetweenTitles
titleLabel.frame.origin.y = (self.frame.height - allHeight) / 2
subtitleLabel.frame.origin.y = titleLabel.frame.maxY + self.spaceBetweenTitles
}
// Apply
switch layout {
case .iconTitleMessageCentered:
layoutIcon()
layoutTitleCenteredCompact()
layoutSubtitle()
case .iconTitleMessageLeading:
layoutIcon()
layoutTitleLeadingFullWidth()
layoutSubtitle()
case .iconTitleCentered:
layoutIcon()
titleLabel?.numberOfLines = 2
layoutTitleCenteredCompact()
case .iconTitleLeading:
layoutIcon()
titleLabel?.numberOfLines = 2
layoutTitleLeadingFullWidth()
case .title:
titleLabel?.numberOfLines = 2
layoutTitleCenteredFullWidth()
case .titleMessage:
layoutTitleCenteredFullWidth()
layoutSubtitle()
}
layoutTitleSubtitleByVertical()
}
// MARK: - Models
enum PresentPosition {
case prepare(_ from: SPIndicatorPresentSide)
case visible(_ from: SPIndicatorPresentSide)
case fromVisible(_ translation: CGFloat, from: SPIndicatorPresentSide)
}
enum LayoutGrid {
case iconTitleMessageCentered
case iconTitleMessageLeading
case iconTitleCentered
case iconTitleLeading
case title
case titleMessage
}
}
@@ -1,47 +0,0 @@
// The MIT License (MIT)
// Copyright © 2021 Ivan Vorobei (hello@ivanvorobei.io)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
import UIKit
/// SPIndicator: Wrapper of haptic styles.
public enum SPIndicatorHaptic {
case success
case warning
case error
case none
func impact() {
let generator = UINotificationFeedbackGenerator()
switch self {
case .success:
generator.notificationOccurred(UINotificationFeedbackGenerator.FeedbackType.success)
case .warning:
generator.notificationOccurred(UINotificationFeedbackGenerator.FeedbackType.warning)
case .error:
generator.notificationOccurred(UINotificationFeedbackGenerator.FeedbackType.error)
case .none:
break
}
}
}