forked from slackhq/PanModal
-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
APP-10298 : PanModalNavigationController 추가 #10
Open
Raymond-Sik
wants to merge
1
commit into
master
Choose a base branch
from
feature/APP-10298-talk-lounge
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
import UIKit | ||
|
||
public class PanModalNavigationController: UINavigationController, PanModalPresentable { | ||
public var panScrollable: UIScrollView? { | ||
return mTopPanModalPresentable?.panScrollable | ||
} | ||
public var longFormHeight: PanModalHeight { | ||
return mTopPanModalPresentable?.longFormHeight ?? .maxHeight | ||
} | ||
public var shortFormHeight: PanModalHeight { | ||
return mTopPanModalPresentable?.shortFormHeight ?? longFormHeight | ||
} | ||
public var panCustomTopView: PanCustomTopView? { | ||
return mTopPanModalPresentable?.panCustomTopView | ||
} | ||
public var allowsTapToDismiss: Bool { | ||
return mTopPanModalPresentable?.allowsTapToDismiss ?? true | ||
} | ||
public var allowsDragToDismiss: Bool { | ||
return mTopPanModalPresentable?.allowsDragToDismiss ?? true | ||
} | ||
public var indicatorColor: UIColor { | ||
return mTopPanModalPresentable?.indicatorColor ?? UIColor(red: 241/255.0, green: 243/255.0, blue: 245/255.0, alpha: 1) | ||
} | ||
public var panModalBackgroundColor: UIColor { | ||
UIColor.black.withAlphaComponent(0.7) | ||
} | ||
|
||
public var dragIndicatorBackgroundColor: UIColor { .Palette.surface } | ||
|
||
public var allowsExtendedPanScrolling = true | ||
// 16: IndicatorView Size, 56: IndicatorView를 제외한 상단 마진 | ||
// IndicatorView는 TopOffset에 포함되어 있지 않음 | ||
// 만약 IndicatorView를 상단에 붙이고 싶으면 topOffset값은 16이 되어야 한다. | ||
public var topOffset = CGFloat(56 + 16) | ||
|
||
private var mIsInteractiveBack = false | ||
private var mPushedControllerBeforeViewLoaded = [UIViewController]() | ||
private var mLastStateStore = [Int: PanModalPresentationState]() | ||
private var mTopPanModalPresentable: PanModalPresentable? { | ||
return topViewController as? PanModalPresentable | ||
} | ||
private var borderCoordinator: BorderCoordinator? | ||
|
||
public override func viewDidLoad() { | ||
super.viewDidLoad() | ||
|
||
delegate = self | ||
interactivePopGestureRecognizer?.delegate = self | ||
view.backgroundColor = .Palette.surface | ||
navigationBar.isTranslucent = false | ||
navigationBar.backgroundColor = .Palette.surface | ||
navigationBar.hideNavigationBarBorder() | ||
|
||
mPushedControllerBeforeViewLoaded.forEach { pushViewController($0, animated: false) } | ||
mPushedControllerBeforeViewLoaded.removeAll() | ||
borderCoordinator = BorderCoordinator(borderCoordinatorAdaptable: self) | ||
} | ||
|
||
public override func popViewController(animated: Bool) -> UIViewController? { | ||
let vc = super.popViewController(animated: animated) | ||
|
||
mIsInteractiveBack = vc?.transitionCoordinator?.isInteractive ?? false | ||
if !mIsInteractiveBack { | ||
panModalSetNeedsLayoutUpdate() | ||
} | ||
return vc | ||
} | ||
|
||
public override func pushViewController(_ viewController: UIViewController, animated: Bool) { | ||
if !isViewLoaded { | ||
mPushedControllerBeforeViewLoaded.append(viewController) | ||
} else { | ||
super.pushViewController(viewController, animated: animated) | ||
panModalSetNeedsLayoutUpdate() | ||
} | ||
} | ||
|
||
public func willTransition(to state: PanModalPresentationState) { | ||
if let topControllerHashValue = topViewController?.hashValue { | ||
mLastStateStore[topControllerHashValue] = state | ||
} | ||
} | ||
|
||
public func shouldRespond(to panModalGestureRecognizer: UIPanGestureRecognizer) -> Bool { | ||
return mTopPanModalPresentable?.shouldRespond(to: panModalGestureRecognizer) ?? true | ||
} | ||
|
||
func panModalSetNeedsLayoutUpdate() { | ||
presentedVC?.setNeedsLayoutUpdate() | ||
adjustPanModalState() | ||
} | ||
|
||
fileprivate func adjustPanModalState() { | ||
panModalTransition(to: mLastStateStore[(topViewController ?? self).hashValue] ?? .shortForm) | ||
} | ||
|
||
public func willRespond(to panModalGestureRecognizer: UIPanGestureRecognizer) { | ||
if let presentable = topViewController as? PanModalPresentable { | ||
presentable.willRespond(to: panModalGestureRecognizer) | ||
} | ||
} | ||
|
||
public func panModalWillDismiss() { | ||
if let presentable = topViewController as? PanModalPresentable { | ||
presentable.panModalWillDismiss() | ||
} | ||
} | ||
} | ||
|
||
extension PanModalNavigationController: UINavigationControllerDelegate { | ||
public func navigationController(_ navigationController: UINavigationController, didShow viewController: UIViewController, animated: Bool) { | ||
if mIsInteractiveBack { | ||
panModalSetNeedsLayoutUpdate() | ||
} | ||
} | ||
} | ||
|
||
extension PanModalNavigationController: UIGestureRecognizerDelegate { | ||
public func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool { | ||
return viewControllers.count > 1 | ||
} | ||
} | ||
|
||
extension PanModalNavigationController: BorderCoordinatorAdaptable { | ||
public var scrollView: UIScrollView { | ||
return panScrollable ?? UIScrollView() | ||
} | ||
|
||
public var targetView: UIView { | ||
return navigationBar | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. PanModal에서 사용하는 디자인 시스템 컬러값들이 있어서 별도로 정의해주었습니다. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 컬러값을 외부에서 주입받을 수 있도록 변경 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import UIKit | ||
|
||
extension UIColor { | ||
enum Palette { | ||
static var surface: UIColor { | ||
return UIColor { traitCollection in | ||
switch traitCollection.userInterfaceStyle { | ||
case .dark: | ||
return UIColor(red: 25/255, green: 25/255, blue: 25/255, alpha: 1.0) | ||
default: | ||
return UIColor.white | ||
} | ||
} | ||
} | ||
|
||
static var gray100: UIColor { | ||
return UIColor { traitCollection in | ||
switch traitCollection.userInterfaceStyle { | ||
case .dark: | ||
return UIColor(red: 33/255, green: 36/255, blue: 37/255, alpha: 1.0) | ||
default: | ||
return UIColor(red: 245/255, green: 246/255, blue: 248/255, alpha: 1.0) | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import UIKit | ||
import Combine | ||
|
||
class BorderCoordinator { | ||
private var cancellables = Set<AnyCancellable>() | ||
private var borderView: UIView? | ||
private weak var borderCoordinatorAdaptable: BorderCoordinatorAdaptable? | ||
|
||
init(borderCoordinatorAdaptable: BorderCoordinatorAdaptable) { | ||
self.borderCoordinatorAdaptable = borderCoordinatorAdaptable | ||
|
||
let scrollView = borderCoordinatorAdaptable.scrollView | ||
let yOffset = borderCoordinatorAdaptable.yOffset | ||
|
||
scrollView.publisher(for: \.contentOffset) | ||
.removeDuplicates() | ||
.map { $0.y >= yOffset } | ||
.removeDuplicates() | ||
.sink { [weak self] isShowingBorder in | ||
if isShowingBorder { | ||
self?.showBorder() | ||
} else { | ||
self?.hideBorder() | ||
} | ||
} | ||
.store(in: &cancellables) | ||
} | ||
|
||
private func showBorder() { | ||
if let navigationBar = borderCoordinatorAdaptable?.targetView as? UINavigationBar { | ||
navigationBar.showNavigationBarBorder() | ||
} else { | ||
appendBorder() | ||
} | ||
} | ||
|
||
private func hideBorder() { | ||
if let navigationBar = borderCoordinatorAdaptable?.targetView as? UINavigationBar { | ||
navigationBar.hideNavigationBarBorder() | ||
} else { | ||
borderView?.removeFromSuperview() | ||
} | ||
} | ||
|
||
private func appendBorder() { | ||
if borderView == nil { | ||
borderView = UIView() | ||
borderView?.backgroundColor = .Palette.gray100 | ||
borderView?.translatesAutoresizingMaskIntoConstraints = false | ||
} | ||
|
||
guard let borderView = borderView, | ||
let targetView = borderCoordinatorAdaptable?.targetView else { return } | ||
|
||
borderView.removeFromSuperview() | ||
targetView.addSubview(borderView) | ||
targetView.sendSubviewToBack(borderView) | ||
borderView.heightAnchor.constraint(equalToConstant: 1).isActive = true | ||
borderView.leadingAnchor.constraint(equalTo: targetView.leadingAnchor).isActive = true | ||
borderView.trailingAnchor.constraint(equalTo: targetView.trailingAnchor).isActive = true | ||
borderView.bottomAnchor.constraint(equalTo: targetView.bottomAnchor).isActive = true | ||
} | ||
} | ||
|
||
protocol BorderCoordinatorAdaptable: AnyObject { | ||
var scrollView: UIScrollView { get } | ||
var yOffset: CGFloat { get } | ||
var targetView: UIView { get } | ||
} | ||
|
||
extension BorderCoordinatorAdaptable { | ||
var yOffset: CGFloat { | ||
return 1 | ||
} | ||
} | ||
|
||
extension UINavigationBar { | ||
func hideNavigationBarBorder() { | ||
if #available(iOS 15.0, *) { | ||
let appearance = UINavigationBarAppearance() | ||
appearance.configureWithTransparentBackground() | ||
appearance.shadowColor = .clear | ||
appearance.backgroundColor = barTintColor | ||
standardAppearance = appearance | ||
scrollEdgeAppearance = appearance | ||
} else { | ||
standardAppearance.shadowColor = .clear | ||
standardAppearance.backgroundColor = barTintColor | ||
} | ||
} | ||
|
||
func showNavigationBarBorder() { | ||
if #available(iOS 15.0, *) { | ||
let appearance = UINavigationBarAppearance() | ||
appearance.configureWithTransparentBackground() | ||
appearance.shadowColor = .Palette.gray100 | ||
appearance.backgroundColor = barTintColor | ||
standardAppearance = appearance | ||
scrollEdgeAppearance = appearance | ||
} else { | ||
standardAppearance.shadowColor = .Palette.gray100 | ||
standardAppearance.backgroundColor = barTintColor | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PanModalNavigationController를 PanModal 안으로 옮기게된 이유입니다.