Skip to content

Commit

Permalink
Patch 2.3.1
Browse files Browse the repository at this point in the history
fix:
- Fixed a bug that caused the dismissAfter function to close an incorrect popup (#86)
- Improved animations (#88)
  • Loading branch information
FulcrumOne committed Apr 22, 2024
1 parent 890da6d commit 828066a
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 20 deletions.
2 changes: 1 addition & 1 deletion MijickPopupView.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Pod::Spec.new do |s|
PopupView is a free and open-source library dedicated for SwiftUI that makes the process of presenting popups easier and much cleaner.
DESC

s.version = '2.3.0'
s.version = '2.3.1'
s.ios.deployment_target = '14.0'
s.osx.deployment_target = '12.0'
s.swift_version = '5.0'
Expand Down
22 changes: 22 additions & 0 deletions Sources/Internal/Extensions/DispatchSource++.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// DispatchSource++.swift of PopupView
//
// Created by Tomasz Kurylik
// - Twitter: https://twitter.com/tkurylik
// - Mail: [email protected]
// - GitHub: https://github.com/FulcrumOne
//
// Copyright ©2024 Mijick. Licensed under MIT License.


import Foundation

extension DispatchSource {
static func createAction(deadline seconds: Double, event: @escaping () -> ()) -> DispatchSourceTimer {
let action = DispatchSource.makeTimerSource(queue: .main)
action.schedule(deadline: .now() + max(0.6, seconds))
action.setEventHandler(handler: event)
action.resume()
return action
}
}
9 changes: 9 additions & 0 deletions Sources/Internal/Managers/PopupManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class PopupManager: ObservableObject {
@Published private(set) var views: [any Popup] = []
private(set) var presenting: Bool = true
private(set) var popupsWithoutOverlay: [String] = []
private(set) var popupsToBeDismissed: [String: DispatchSourceTimer] = [:]

static let shared: PopupManager = .init()
private init() {}
Expand All @@ -26,12 +27,20 @@ enum StackOperation {
}
extension PopupManager {
static func performOperation(_ operation: StackOperation) { DispatchQueue.main.async {
removePopupFromStackToBeDismissed(operation)
updateOperationType(operation)
shared.views.perform(operation)
}}
static func dismissPopupAfter(_ popup: any Popup, _ seconds: Double) { shared.popupsToBeDismissed[popup.id] = DispatchSource.createAction(deadline: seconds) { performOperation(.remove(id: popup.id)) } }
static func hideOverlay(_ popup: any Popup) { shared.popupsWithoutOverlay.append(popup.id) }
}
private extension PopupManager {
static func removePopupFromStackToBeDismissed(_ operation: StackOperation) { switch operation {
case .removeLast: shared.popupsToBeDismissed.removeValue(forKey: shared.views.last?.id ?? "")
case .remove(let id): shared.popupsToBeDismissed.removeValue(forKey: id)
case .removeAllUpTo, .removeAll: shared.popupsToBeDismissed.removeAll()
default: break
}}
static func updateOperationType(_ operation: StackOperation) { switch operation {
case .insertAndReplace, .insertAndStack: shared.presenting = true
case .removeLast, .remove, .removeAllUpTo, .removeAll: shared.presenting = false
Expand Down
25 changes: 11 additions & 14 deletions Sources/Internal/Other/AnimationType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,15 @@ import SwiftUI

public enum AnimationType { case spring, linear, easeInOut }
extension AnimationType {
var entry: Animation {
switch self {
case .spring: return .spring(response: 0.4, dampingFraction: 1, blendDuration: 0.1)
case .linear: return .linear(duration: 0.4)
case .easeInOut: return .easeInOut(duration: 0.4)
}
}
var removal: Animation {
switch self {
case .spring: return .interactiveSpring(response: 0.14, dampingFraction: 1, blendDuration: 1)
case .linear: return .linear(duration: 0.3)
case .easeInOut: return .easeInOut(duration: 0.3)
}
}
var entry: Animation { switch self {
case .spring: return .spring(duration: 0.36, bounce: 0, blendDuration: 0.1)
case .linear: return .linear(duration: 0.4)
case .easeInOut: return .easeInOut(duration: 0.4)
}}
var removal: Animation { switch self {
case .spring: return .spring(duration: 0.32, bounce: 0, blendDuration: 0.1)
case .linear: return .linear(duration: 0.3)
case .easeInOut: return .easeInOut(duration: 0.3)
}}
var dragGesture: Animation { .linear(duration: 0.05) }
}
10 changes: 8 additions & 2 deletions Sources/Internal/Protocols/PopupStack.swift
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,18 @@ extension PopupStack {
// MARK: - Initial Height
extension PopupStack {
func getInitialHeight() -> CGFloat {
guard let previousView = items.nextToLast else { return 0 }
guard let previousView = items.nextToLast else { return 30 }

let height = heights.filter { $0.key == previousView.id }.first?.value ?? 0
let height = heights.filter { $0.key == previousView.id }.first?.value ?? 30
return height
}
}

// MARK: - Item ZIndex
extension PopupStack {
func getZIndex(_ item: AnyPopup<Config>) -> Double { .init(items.firstIndex(of: item) ?? 2137) }
}


// MARK: - Animations
extension PopupStack {
Expand All @@ -122,6 +127,7 @@ extension PopupStack {
extension PopupStack {
var transitionEntryAnimation: Animation { globalConfig.common.animation.entry }
var transitionRemovalAnimation: Animation { globalConfig.common.animation.removal }
var dragGestureAnimation: Animation { globalConfig.common.animation.dragGesture }
}

// MARK: - Configurables
Expand Down
3 changes: 2 additions & 1 deletion Sources/Internal/Views/PopupBottomStackView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ struct PopupBottomStackView: PopupStack {
ZStack(alignment: .top, content: createPopupStack)
.background(createTapArea())
.animation(getHeightAnimation(isAnimationDisabled: screenManager.animationsDisabled), value: heights)
.animation(transitionRemovalAnimation, value: gestureTranslation)
.animation(isGestureActive ? dragGestureAnimation : transitionRemovalAnimation, value: gestureTranslation)
.onDragGesture($isGestureActive, onChanged: onPopupDragGestureChanged, onEnded: onPopupDragGestureEnded)
}
}
Expand Down Expand Up @@ -54,6 +54,7 @@ private extension PopupBottomStackView {
.focusSectionIfAvailable()
.align(to: .bottom, lastPopupConfig.contentFillsEntireScreen ? 0 : popupBottomPadding)
.transition(transition)
.zIndex(getZIndex(item))
}
}

Expand Down
3 changes: 2 additions & 1 deletion Sources/Internal/Views/PopupTopStackView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ struct PopupTopStackView: PopupStack {
ZStack(alignment: .bottom, content: createPopupStack)
.background(createTapArea())
.animation(getHeightAnimation(isAnimationDisabled: screenManager.animationsDisabled), value: heights)
.animation(transitionRemovalAnimation, value: gestureTranslation)
.animation(isGestureActive ? dragGestureAnimation : transitionRemovalAnimation, value: gestureTranslation)
.onDragGesture($isGestureActive, onChanged: onPopupDragGestureChanged, onEnded: onPopupDragGestureEnded)
}
}
Expand Down Expand Up @@ -51,6 +51,7 @@ private extension PopupTopStackView {
.focusSectionIfAvailable()
.align(to: .top, popupTopPadding)
.transition(transition)
.zIndex(getZIndex(item))
}
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/Public/Extensions/Public+Popup.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public extension Popup {
// MARK: - Modifiers
public extension Popup {
/// Closes popup after n seconds
@discardableResult func dismissAfter(_ seconds: Double) -> some Popup { DispatchQueue.main.asyncAfter(deadline: .now() + max(0.5, seconds)) { PopupManager.dismiss(Self.self) }; return self }
@discardableResult func dismissAfter(_ seconds: Double) -> some Popup { PopupManager.dismissPopupAfter(self, seconds); return self }

/// Hides the overlay for the selected popup
@discardableResult func hideOverlay() -> some Popup { PopupManager.hideOverlay(self); return self }
Expand Down

0 comments on commit 828066a

Please sign in to comment.