Skip to content

Commit

Permalink
Full height option and more documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
PimCoumans committed Oct 24, 2022
1 parent 1e32b81 commit aec95ea
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
23 changes: 18 additions & 5 deletions Sources/PanelPresenter/PanelPresentable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,32 +28,45 @@ import UIKit
/// }

public protocol PanelPresentable: UIViewController {

/// Return an instance of `PanelPresenter` that manages the view controller‘s presentation
var panelPresenter: PanelPresenter { get }

/// Override to provide your own scroll view to use for dismissing logic
/// Override to provide your own scroll view to use for dismissing logic.
var panelScrollView: UIScrollView { get }

/// Set an additional top inset from the screen‘s top
/// Set an additional top inset from the screen‘s top.
/// Default value is `10`
var panelTopInset: CGFloat { get }

/// Whether the view controller allows the panel to be dismissed, return `false` to (temporarily) disable panel dismissing
/// Whether the view controller allows the panel to be dismissed, return `false` to (temporarily) disable panel dismissing.
/// Default value is `true`
var panelCanBeDismissed: Bool { get }

/// Whether the presenting view controller's view should
/// Returning `true` disables auto-resizing and keeps the panel‘s top below the safe area insets and ``panelTopInset``.
/// Default value is `false`
///
/// When changing the value returned here, make sure to call `panelPresenter.updatePanelHeight(animated:)`
/// or call `panelPresenter.layoutIfNeeded()` from your own animation logic
var panelExtendsToFullHeight: Bool { get }

/// Whether the tint mode of the presenting view controller‘s view should be changed when presented,
/// Default value is `true`
var shouldAdjustPresenterTintMode: Bool { get }
}

extension PanelPresentable {
public var panelScrollView: UIScrollView { panelPresenter.panelScrollView }
public var panelTopInset: CGFloat { 10 }
public var panelCanBeDismissed: Bool { true }
public var panelExtendsToFullHeight: Bool { false }
public var shouldAdjustPresenterTintMode: Bool { true }

public var headerContentView: UIView { panelPresenter.headerContentView }
}

public extension UIViewController {
/// The panel presenter that presented this view controller, nil if not presented by panel presenter
/// The panel presenter that presented this view controller, `nil` if not presented by any panel presenter
var presentingPanelPresenter: PanelPresenter? {
transitioningDelegate as? PanelPresenter
}
Expand Down
14 changes: 10 additions & 4 deletions Sources/PanelPresenter/PanelPresenter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,10 @@ public class PanelPresenter: NSObject {
panelPresentable?.panelCanBeDismissed ?? true
}

private var extendsToFullHeight: Bool {
panelPresentable?.panelExtendsToFullHeight ?? false
}

private var shouldAdjustPresenterTintMode: Bool {
panelPresentable?.shouldAdjustPresenterTintMode ?? true
}
Expand All @@ -178,6 +182,7 @@ public class PanelPresenter: NSObject {
private lazy var containerView: UIView = {
let view = PanelContainerView()
view.insetsLayoutMarginsFromSafeArea = false
view.directionalLayoutMargins = .zero
return view
}()

Expand Down Expand Up @@ -255,7 +260,7 @@ private extension PanelPresenter {
if let viewController = panelPresentable {
panelTopInset = viewController.panelTopInset
}
containerView.layoutMargins.top = headerViewHeight + panelTopInset
containerView.directionalLayoutMargins.top = headerViewHeight + panelTopInset
if isScrollViewCustom {
panelScrollView.removeFromSuperview()
prepareCustomScrollView(scrollView)
Expand Down Expand Up @@ -283,7 +288,7 @@ private extension PanelPresenter {
private extension PanelPresenter {

func setupViews() {
containerView.layoutMargins.top = headerViewHeight + panelTopInset
containerView.directionalLayoutMargins.top = headerViewHeight + panelTopInset

scrollView.addSubview(scrollContentView)

Expand Down Expand Up @@ -389,7 +394,7 @@ private extension PanelPresenter {
}
scrollView.contentInset.bottom = bottomInset
let scrollViewHeight = scrollView.frame.inset(by: scrollView.safeAreaInsets).height - bottomInset
let contentHeight = scrollView.contentSize.height
let contentHeight = extendsToFullHeight ? scrollViewHeight : scrollView.contentSize.height
// Set top inset so content is always aligned to bottom
scrollView.contentInset.top = max(0, scrollViewHeight - contentHeight)

Expand Down Expand Up @@ -647,7 +652,8 @@ extension PanelPresenter: UIViewControllerAnimatedTransitioning {
}

let visibleContainerViewFrame = containerView.bounds.inset(by: containerView.layoutMargins)
let fullOffset = min(visibleContainerViewFrame.height, scrollView.contentSize.height + headerViewHeight) + containerView.safeAreaInsets.bottom
let contentHeight = extendsToFullHeight ? scrollView.frame.height : scrollView.contentSize.height
let fullOffset = min(visibleContainerViewFrame.height, contentHeight + headerViewHeight) + headerViewHeight
let fullDuration = transitionDuration(using: context)
let duration = max(0.15, fullDuration * ((fullOffset / containerView.bounds.height) * 0.75))

Expand Down

0 comments on commit aec95ea

Please sign in to comment.