Skip to content

Commit

Permalink
Rename Source to Sources for consistency
Browse files Browse the repository at this point in the history
  • Loading branch information
aheze committed Dec 25, 2021
1 parent de8c7e3 commit 4a0e3cb
Show file tree
Hide file tree
Showing 14 changed files with 77 additions and 92 deletions.
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ let package = Package(
.target(
name: "Popovers",
dependencies: [],
path: "Source"
path: "Sources"
)
]
)
2 changes: 1 addition & 1 deletion Popovers.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Pod::Spec.new do |s|
s.name = 'Popovers'
s.version = '1.0.1'
s.version = '1.0.2'
s.summary = 'A library to present popovers.'

s.description = <<-DESC
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public extension UIView {
return self.convert(self.bounds, to: nil)
}
}

public extension Optional where Wrapped: UIView {
/// Convert a view's frame to global coordinates, which are needed for `sourceFrame` and `excludedFrames.` This is a convenience overload for optional `UIView`s.
func windowFrame() -> CGRect {
Expand All @@ -29,7 +30,6 @@ public extension Optional where Wrapped: UIView {

public extension View {


/// Read a view's frame. From https://stackoverflow.com/a/66822461/14351818
func frameReader(rect: @escaping (CGRect) -> Void) -> some View {
return self
Expand Down
163 changes: 74 additions & 89 deletions Source/Popovers.swift → Sources/Popovers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,107 +60,35 @@ public struct Popovers {
}
}

/**
Get the current window.
*/
public static func getWindow() -> PopoverContainerWindow {
if let currentScene = UIApplication.shared.currentWindowScene {

/// Get the window for the current scene.
if let window = windows[currentScene] {
return window
} else {

/// No popover window exists yet, make one.
let window = PopoverContainerWindow(
popoverModel: model,
scene: currentScene
)

windows[currentScene] = window
return window
}
} else {

/// No popover window exists yet and there is no scene active, make one.
/// This is highly unlikely to be called.
let window = PopoverContainerWindow(
popoverModel: model,
scene: nil
)

return window
}
/// Make sure that a window exists, so that the popover's presentation animation doesn't stutter..
public static func prepare() {
_ = getWindow()
}

/**
Present a popover.
*/
public static func present(_ popover: Popover) {

/// Make sure the view model exists.
_ = model

/// Make sure a window exists.
_ = getWindow()

/// Configure and present the popover.
func presentPopover() {
/// Create a transaction for the presentation animation.
let transaction = Transaction(animation: popover.attributes.presentation.animation)

/// Inject the transaction into the popover, so following frame calculations are animated smoothly.
popover.context.transaction = transaction

withTransaction(transaction) {

/// Add the popover to the container view.
current.append(popover)
}
}
/// Create a transaction for the presentation animation.
let transaction = Transaction(animation: popover.attributes.presentation.animation)

/// Directly present the popover if an existing window was reused.
// if reused {
presentPopover()
// } else {
//
// /// Otherwise, make sure the window is set up the first time and ready for SwiftUI.
// /// Without a delay, SwiftUI won't apply the animation.
// DispatchQueue.main.asyncAfter(deadline: .now() + 0.01) {
// presentPopover()
// }
// }
}

/**
Dismiss a popover.
Provide `transaction` to override the default dismissal transition.
*/
public static func dismiss(_ popover: Popover, transaction: Transaction? = nil) {
if let popoverIndex = index(of: popover) {
popover.context.dismissed?()
popover.attributes.onDismiss?()
/// Inject the transaction into the popover, so following frame calculations are animated smoothly.
popover.context.transaction = transaction

withTransaction(transaction) {

let dismissalTransaction = transaction ?? Transaction(animation: popover.attributes.dismissal.animation)
withTransaction(dismissalTransaction) {
_ = current.remove(at: popoverIndex)
}
/// Add the popover to the container view.
current.append(popover)
}
}

/// Dismiss all popovers.
public static func dismissAll() {
for popover in current.reversed() {
dismiss(popover)
}
}

/// Remove all saved frames for `.popover(selection:tag:attributes:view:)`. Call this method when you present another view where the frames don't apply.
public static func clearSavedFrames() {
model.selectionFrameTags.removeAll()
}

/**
Replace a popover with another popover smoothly.
Expand Down Expand Up @@ -195,6 +123,30 @@ public struct Popovers {
}
}

/**
Dismiss a popover.
Provide `transaction` to override the default dismissal transition.
*/
public static func dismiss(_ popover: Popover, transaction: Transaction? = nil) {
if let popoverIndex = index(of: popover) {
popover.context.dismissed?()
popover.attributes.onDismiss?()

let dismissalTransaction = transaction ?? Transaction(animation: popover.attributes.dismissal.animation)
withTransaction(dismissalTransaction) {
_ = current.remove(at: popoverIndex)
}
}
}

/// Dismiss all popovers.
public static func dismissAll() {
for popover in current.reversed() {
dismiss(popover)
}
}

/**
Refresh the popovers with a new transaction.
Expand All @@ -211,11 +163,6 @@ public struct Popovers {
model.refresh()
}

/// Make sure that a window exists, so that the popover's presentation animation doesn't stutter..
public static func prepare() {
_ = getWindow()
}

/**
Update all popover frames.
Expand All @@ -242,6 +189,11 @@ public struct Popovers {
model.refresh()
}

/// Remove all saved frames for `.popover(selection:tag:attributes:view:)`. Call this method when you present another view where the frames don't apply.
public static func clearSavedFrames() {
model.selectionFrameTags.removeAll()
}

/// Get a currently-presented popover with a tag. Returns `nil` if no popover with the tag was found.
public static func popover(tagged tag: String) -> Popover? {
return current.first(where: { $0.attributes.tag == tag })
Expand All @@ -251,4 +203,37 @@ public struct Popovers {
public static func index(of popover: Popover) -> Int? {
return current.indices.first(where: { current[$0] == popover })
}

/**
Get the current window.
*/
public static func getWindow() -> PopoverContainerWindow {
if let currentScene = UIApplication.shared.currentWindowScene {

/// Get the window for the current scene.
if let window = windows[currentScene] {
return window
} else {

/// No popover window exists yet, make one.
let window = PopoverContainerWindow(
popoverModel: model,
scene: currentScene
)

windows[currentScene] = window
return window
}
} else {

/// No popover window exists yet and there is no scene active, make one.
/// This is highly unlikely to be called.
let window = PopoverContainerWindow(
popoverModel: model,
scene: nil
)

return window
}
}
}

0 comments on commit 4a0e3cb

Please sign in to comment.