Skip to content

Commit

Permalink
1.5.3
Browse files Browse the repository at this point in the history
  • Loading branch information
nathantannar4 committed Oct 23, 2024
1 parent f15ba9e commit 806844a
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 46 deletions.
20 changes: 10 additions & 10 deletions Example/Example/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -340,33 +340,33 @@ struct ContentView: View {
PresentationLink {
TransitionReader { proxy in
Color.blue.opacity(proxy.progress)
.overlay {
Text(proxy.progress.description)
.foregroundStyle(.white)
}
.ignoresSafeArea()
.onChange(of: proxy.progress) { newValue in
progress = newValue
}
}
} label: {
HStack {
Text("PresentationLink")

Text(progress.description)
}
Text("PresentationLink")
}

NavigationLink {
TransitionReader { proxy in
Color.blue.opacity(proxy.progress)
.overlay {
Text(proxy.progress.description)
.foregroundStyle(.white)
}
.ignoresSafeArea()
.onChange(of: proxy.progress) { newValue in
progress = newValue
}
}
} label: {
HStack {
Text("NavigationLink")

Text(progress.description)
}
Text("NavigationLink")
}

} label: {
Expand Down
16 changes: 11 additions & 5 deletions Sources/Transmission/Sources/PresentationLinkModifier.swift
Original file line number Diff line number Diff line change
Expand Up @@ -406,14 +406,20 @@ private struct PresentationLinkModifierBody<
}

let present: () -> Void = {
guard let viewController = adapter.viewController else { return }
presentingViewController.present(
adapter.viewController,
viewController,
animated: isAnimated
) {
adapter.viewController
.setNeedsStatusBarAppearanceUpdate(animated: isAnimated)
if isTransitioningPresentationController {
adapter.viewController.presentationDelegate = context.coordinator
if !isPresented.wrappedValue {
// Handle `isPresented` changing mid presentation
viewController.dismiss(animated: isAnimated)
} else {
viewController
.setNeedsStatusBarAppearanceUpdate(animated: isAnimated)
if isTransitioningPresentationController {
viewController.presentationDelegate = context.coordinator
}
}
}
}
Expand Down
74 changes: 43 additions & 31 deletions Sources/Transmission/Sources/TransitionReader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,19 +77,42 @@ private struct TransitionReaderAdapter: UIViewRepresentable {
}
}

private var trackedViewControllers = NSHashTable<UIViewController>.weakObjects()
private weak var transitionCoordinator: UIViewControllerTransitionCoordinator?
private weak var displayLink: CADisplayLink?

init(progress: Binding<CGFloat>) {
self.progress = progress
}

deinit {
presentingViewController = nil
}

private func reset() {
for viewController in trackedViewControllers.allObjects {
viewController.swizzle_beginAppearanceTransition(nil)
viewController.swizzle_endAppearanceTransition(nil)
}
trackedViewControllers.removeAllObjects()
}

private func presentingViewControllerDidChange() {
if let presentingViewController = presentingViewController {
presentingViewController.swizzle_beginAppearanceTransition { [unowned self] in
reset()

if let presentingViewController {
trackedViewControllers.add(presentingViewController)

if let parent = presentingViewController.parent {
trackedViewControllers.add(parent)
}
}

for viewController in trackedViewControllers.allObjects {
viewController.swizzle_beginAppearanceTransition { [unowned self] in
self.transitionCoordinatorDidChange()
}
presentingViewController.swizzle_endAppearanceTransition { [unowned self] in
viewController.swizzle_endAppearanceTransition { [unowned self] in
self.transitionCoordinatorDidChange()
}
}
Expand All @@ -108,8 +131,8 @@ private struct TransitionReaderAdapter: UIViewRepresentable {
transitionDidChange(transitionCoordinator)
}

transitionCoordinator.notifyWhenInteractionChanges { [unowned self] ctx in
self.transitionDidChange(ctx)
transitionCoordinator.notifyWhenInteractionChanges { [weak self] ctx in
self?.transitionDidChange(ctx)
}
} else if presentingViewController.isBeingPresented || presentingViewController.isBeingDismissed {
let isPresented = presentingViewController.isBeingPresented
Expand Down Expand Up @@ -137,7 +160,7 @@ private struct TransitionReaderAdapter: UIViewRepresentable {
_ transitionCoordinator: UIViewControllerTransitionCoordinatorContext
) {
let from = transitionCoordinator.viewController(forKey: .from)
let isPresenting = from !== presentingViewController
let isPresenting = !trackedViewControllers.contains(from)

if transitionCoordinator.isInteractive {
let percentComplete = isPresenting ? transitionCoordinator.percentComplete : 1 - transitionCoordinator.percentComplete
Expand All @@ -160,25 +183,6 @@ private struct TransitionReaderAdapter: UIViewRepresentable {
}
}
}

private func isDismissing(_ vc: UIViewController?) -> Bool {
guard let vc = vc else {
return false
}
if vc.isBeingDismissed == true || vc.parent == nil {
return true
} else if let navigationController = vc.navigationController {
var current: UIViewController? = vc
while let c = current {
let parent = c.parent
if parent == navigationController {
return !navigationController.viewControllers.contains(c)
}
current = parent
}
}
return false
}
}
}

Expand All @@ -190,7 +194,7 @@ extension UIViewController {
var value: () -> Void
}

func swizzle_beginAppearanceTransition(_ transition: @escaping () -> Void) {
func swizzle_beginAppearanceTransition(_ transition: (() -> Void)?) {
let original = #selector(UIViewController.beginAppearanceTransition(_:animated:))
let swizzled = #selector(UIViewController.swizzled_beginAppearanceTransition(_:animated:))

Expand All @@ -204,8 +208,12 @@ extension UIViewController {
}
}

let box = ObjCBox(value: BeginAppearanceTransition(value: transition))
objc_setAssociatedObject(self, &Self.beginAppearanceTransitionKey, box, .OBJC_ASSOCIATION_RETAIN)
if let transition {
let box = ObjCBox(value: BeginAppearanceTransition(value: transition))
objc_setAssociatedObject(self, &Self.beginAppearanceTransitionKey, box, .OBJC_ASSOCIATION_RETAIN)
} else {
objc_setAssociatedObject(self, &Self.beginAppearanceTransitionKey, nil, .OBJC_ASSOCIATION_RETAIN)
}
}

@objc
Expand All @@ -225,7 +233,7 @@ extension UIViewController {
var value: () -> Void
}

func swizzle_endAppearanceTransition(_ transition: @escaping () -> Void) {
func swizzle_endAppearanceTransition(_ transition: (() -> Void)?) {
let original = #selector(UIViewController.endAppearanceTransition)
let swizzled = #selector(UIViewController.swizzled_endAppearanceTransition)

Expand All @@ -239,8 +247,12 @@ extension UIViewController {
}
}

let box = ObjCBox(value: EndAppearanceTransition(value: transition))
objc_setAssociatedObject(self, &Self.endAppearanceTransitionKey, box, .OBJC_ASSOCIATION_RETAIN)
if let transition {
let box = ObjCBox(value: EndAppearanceTransition(value: transition))
objc_setAssociatedObject(self, &Self.endAppearanceTransitionKey, box, .OBJC_ASSOCIATION_RETAIN)
} else {
objc_setAssociatedObject(self, &Self.endAppearanceTransitionKey, nil, .OBJC_ASSOCIATION_RETAIN)
}
}

@objc
Expand Down

0 comments on commit 806844a

Please sign in to comment.