Skip to content

Commit

Permalink
Refactored nav controller .attachView to not return constraints
Browse files Browse the repository at this point in the history
  • Loading branch information
charliescheer committed Jul 26, 2024
1 parent 95a74e7 commit e9d8905
Showing 1 changed file with 14 additions and 19 deletions.
33 changes: 14 additions & 19 deletions Simplenote/SPNavigationController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,24 +91,18 @@ class SPNavigationController: NSViewController {

// MARK: - Add a View to the stack
//
func push(_ viewController: NSViewController, animated: Bool = false) {
func push(_ viewController: NSViewController, animated: Bool = true) {
let currentView = topViewController?.view

attach(child: viewController)

guard let (leadingAnchor, trailingAnchor) = attachView(subview: viewController.view, below: currentView, animated: animated) else {
return
}
attachView(subview: viewController.view, below: currentView, animated: animated)

guard animated else {
currentView?.removeFromSuperview()
backButton.animator().isHidden = hideBackButton
return
}

leadingAnchor.constant = view.frame.width
trailingAnchor.constant = view.frame.width

animateTransition(slidingView: viewController.view, fadingView: currentView, direction: .trailingToLeading) {
currentView?.removeFromSuperview()
}
Expand All @@ -119,34 +113,30 @@ class SPNavigationController: NSViewController {
viewStack.append(child)
}

@discardableResult
private func attachView(subview: NSView, below siblingView: NSView?, animated: Bool) -> (leading: NSLayoutConstraint, trailing: NSLayoutConstraint)? {
private func attachView(subview: NSView, below siblingView: NSView?, animated: Bool) {

let padding = Constants.buttonViewLeadingPadding + Constants.buttonViewHeight
let finalHeight = subview.fittingSize.height + padding

if let siblingView,
animated {
heightConstraint?.constant = finalHeight
heightConstraint?.constant = siblingView.fittingSize.height + padding
view.addSubview(subview, positioned: .below, relativeTo: siblingView)
} else {
view.addSubview(subview)
}

subview.translatesAutoresizingMaskIntoConstraints = false

let leadingAnchor = subview.leadingAnchor.constraint(equalTo: view.leadingAnchor)
let trailingAnchor = subview.trailingAnchor.constraint(equalTo: view.trailingAnchor)

NSLayoutConstraint.activate([
leadingAnchor,
trailingAnchor,
subview.leadingAnchor.constraint(equalTo: view.leadingAnchor),
subview.trailingAnchor.constraint(equalTo: view.trailingAnchor),
subview.topAnchor.constraint(equalTo: backButton.bottomAnchor)
])

guard animated else {
heightConstraint?.constant = finalHeight
return (leading: leadingAnchor, trailing: trailingAnchor)
return
}

NSAnimationContext.runAnimationGroup { context in
Expand All @@ -156,11 +146,11 @@ class SPNavigationController: NSViewController {
heightConstraint?.animator().constant = finalHeight
}

return (leading: leadingAnchor, trailing: trailingAnchor)
return
}

// MARK: - Remove view from stack
func popViewController(animated: Bool = false) {
func popViewController(animated: Bool = true) {
guard viewStack.count > 1, let currentViewController = viewStack.popLast(), let nextViewController = viewStack.last else {
return
}
Expand Down Expand Up @@ -196,6 +186,11 @@ class SPNavigationController: NSViewController {
return
}

if direction == .trailingToLeading {
leadingConstraint.constant = view.frame.width
trailingConstraint.constant = view.frame.width
}

let multiplier: CGFloat = direction == .leadingToTrailing ? 1 : -1
let alpha: CGFloat = direction == .leadingToTrailing ? 1 : 0

Expand Down

0 comments on commit e9d8905

Please sign in to comment.