Skip to content
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

KOA-4949 Update FloatingPanel from 1.6.6 to 2.5.2 #1324

Merged
merged 14 commits into from
Jun 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Backpack.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ Pod::Spec.new do |s|

s.dependency 'FSCalendar', '~> 2.8.2'
s.dependency 'TTTAttributedLabel', '~> 2.0.0'
s.dependency 'FloatingPanel', '1.6.6'
s.dependency 'FloatingPanel', '2.5.2'
s.dependency 'Backpack-Common'
s.dependency 'MBProgressHUD', '~> 1.2.0'
s.frameworks = 'UIKit', 'Foundation', 'CoreText'
Expand Down
10 changes: 6 additions & 4 deletions Backpack/BottomSheet/Classes/BPKFloatingPanelPosition.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ public enum BPKFloatingPanelPosition: Int {
}

extension BPKFloatingPanelPosition {
// create a BPKFloatingPanelPositon from a 3rd party FloatingPanelPosition
init(floatinPanelPosition: FloatingPanelPosition) {
switch floatinPanelPosition {
// create a BPKFloatingPanelPosition from a 3rd party FloatingPanelPosition
init(floatingPanelPosition: FloatingPanelState) {
switch floatingPanelPosition {
case .full:
self = .full
case .half:
Expand All @@ -39,10 +39,12 @@ extension BPKFloatingPanelPosition {
self = .tip
case .hidden:
self = .hidden
default:
self = .half
}
}

var asFloatingPanelPosition: FloatingPanelPosition {
var asFloatingPanelPosition: FloatingPanelState {
switch self {
case .full:
return .full
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ private extension BPKFloatingPanelController {

outsideSafeAreaView.topAnchor.constraint(
greaterThanOrEqualTo: content.view.topAnchor,
constant: layout.insetFor(position: .half) ?? 0
gert-janvercauteren marked this conversation as resolved.
Show resolved Hide resolved
constant: BottomSheetInsets.Constants.bottomSheetHeightInHalfPosition
)
])
}
Expand Down Expand Up @@ -130,12 +130,11 @@ private extension BPKFloatingPanelController {
}

func updateScrollViewInsetsForBottomSection() {
guard let scrollView = scrollView, let bottomSection = bottomSectionViewController else { return }
guard let scrollView = trackingScrollView, let bottomSection = bottomSectionViewController else { return }
let insets = UIEdgeInsets(top: 0, left: 0, bottom: bottomSection.view.frame.size.height, right: 0)
scrollView.contentInset = insets
scrollView.scrollIndicatorInsets = insets
}

}

private extension BPKFloatingPanelController {
Expand Down
46 changes: 15 additions & 31 deletions Backpack/BottomSheet/Classes/BottomSheet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,19 +75,23 @@ public final class BPKBottomSheet: NSObject {

private lazy var floatingPanelController: BPKFloatingPanelController = {
var panel = BPKFloatingPanelController(delegate: self)

let appearance = SurfaceAppearance()
appearance.cornerRadius = BPKCornerRadiusLg
panel.surfaceView.appearance = appearance

panel.surfaceView.backgroundColor = BPKColor.backgroundTertiaryColor
panel.surfaceView.cornerRadius = BPKCornerRadiusLg
panel.surfaceView.grabberTopPadding = BPKSpacingMd
panel.surfaceView.grabberHandleHeight = BPKSpacingSm
panel.surfaceView.grabberHandleWidth = Constants.grabberHandleWidth
panel.surfaceView.grabberHandlePadding = BPKSpacingMd
panel.surfaceView.grabberHandle.barColor = BPKColor.skyGrayTint06
panel.surfaceView.grabberHandleSize = .init(width: Constants.grabberHandleWidth, height: BPKSpacingSm)

switch presentationStyle {
case .modal:
panel.isRemovalInteractionEnabled = true
panel.backdropView.dismissalTapGestureRecognizer.isEnabled = true
case .persistent:
panel.isRemovalInteractionEnabled = false
panel.backdropView.dismissalTapGestureRecognizer.isEnabled = false
panel.backdropView.backgroundColor = .clear
}

Expand Down Expand Up @@ -179,7 +183,8 @@ public final class BPKBottomSheet: NSObject {
assertionFailure("present(_:animated:completion:) not compatible with persistent presentation style")
return
}
if let scrollView = floatingPanelController.scrollView {

if let scrollView = floatingPanelController.trackingScrollView {
gert-janvercauteren marked this conversation as resolved.
Show resolved Hide resolved
scrollView.setContentOffset(.init(x: 0, y: -scrollView.adjustedContentInset.top), animated: animated)
}

Expand All @@ -200,7 +205,7 @@ public final class BPKBottomSheet: NSObject {
assertionFailure("present(in:animated:completion:) not compatible with modal presentation style")
return
}
floatingPanelController.addPanel(toParent: parent, belowView: nil, animated: true)
floatingPanelController.addPanel(toParent: parent)
}

/// This method removes the panel from the parent view
Expand All @@ -213,7 +218,7 @@ public final class BPKBottomSheet: NSObject {
/// It can be useful, for example, when changing the inner constraints of the `contentViewController`
/// and bottom sheet needs to be resized to fit the content.
public func updateLayout() {
floatingPanelController.updateLayout()
floatingPanelController.invalidateLayout()
}
gert-janvercauteren marked this conversation as resolved.
Show resolved Hide resolved

/// This method allows change the presentation mode of the BPKBottomSheet.
Expand All @@ -225,45 +230,24 @@ public final class BPKBottomSheet: NSObject {
}

extension BPKBottomSheet: FloatingPanelControllerDelegate {
final class IntrinsicLayout: FloatingPanelIntrinsicLayout {
func backdropAlphaFor(position: FloatingPanelPosition) -> CGFloat {
return Constants.backdropAlpha
}
}

public func floatingPanel(
_ viewController: FloatingPanelController,
layoutFor newCollection: UITraitCollection
) -> FloatingPanelLayout? {
) -> FloatingPanelLayout {
gert-janvercauteren marked this conversation as resolved.
Show resolved Hide resolved
switch self.presentationStyle {
case .modal:
return scrollView == nil ? IntrinsicLayout() : ModalBottomSheetLayout(insets: insets)
return scrollView == nil ? IntrinsicBottomSheetLayout() : ModalBottomSheetLayout(insets: insets)
case .persistent:
return PersistentBottomSheetLayout(insets: insets)
}
}

public func floatingPanelDidChangePosition(_ viewController: FloatingPanelController) {
delegate?.bottomSheetDidChangePosition(.init(floatinPanelPosition: viewController.position))
delegate?.bottomSheetDidChangePosition(.init(floatingPanelPosition: viewController.state))
}
}

private enum Constants {
static let backdropAlpha: CGFloat = 0.3
static let grabberHandleWidth: CGFloat = 60.0
}

extension FloatingPanelPosition {
var asBPKFloatingPanelPosition: BPKFloatingPanelPosition {
switch self {
case .full:
return .full
case .half:
return .half
case .tip:
return .tip
case .hidden:
return .hidden
}
}
}
51 changes: 50 additions & 1 deletion Backpack/BottomSheet/Classes/BottomSheetInsets.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,25 @@
* limitations under the License.
*/

import FloatingPanel

public struct BottomSheetInsets {
public let full: CGFloat?
public let half: CGFloat?
public let tip: CGFloat?

public enum Constants {
static let bottomSheetHeightInHalfPosition: CGFloat = 386.0
static let bottomSheetHeightInTipPosition: CGFloat = 120.0
}

/// Default insets for the bottom sheet.
public init() {
self.init(full: nil, half: 386.0, tip: 120.0)
self.init(
full: nil,
half: Constants.bottomSheetHeightInHalfPosition,
tip: Constants.bottomSheetHeightInTipPosition
)
}

/// Use this initializer to have full control over the heights and spacing of your bottom sheet
Expand All @@ -37,3 +48,41 @@ public struct BottomSheetInsets {
self.tip = tip
}
}

extension BottomSheetInsets {
var fullAnchor: FloatingPanelLayoutAnchoring {
guard let full = full else {
return FloatingPanelLayoutAnchor(
absoluteInset: BPKSpacingNone,
edge: .top,
referenceGuide: .safeArea
)
}

return FloatingPanelLayoutAnchor(absoluteInset: full, edge: .top, referenceGuide: .safeArea)
}

var halfAnchor: FloatingPanelLayoutAnchoring {
guard let half = half else {
return FloatingPanelLayoutAnchor(
absoluteInset: Constants.bottomSheetHeightInHalfPosition,
edge: .bottom,
referenceGuide: .safeArea
)
}

return FloatingPanelLayoutAnchor(absoluteInset: half, edge: .bottom, referenceGuide: .safeArea)
}

var tipAnchor: FloatingPanelLayoutAnchoring {
guard let tip = tip else {
return FloatingPanelLayoutAnchor(
absoluteInset: Constants.bottomSheetHeightInTipPosition,
edge: .bottom,
referenceGuide: .safeArea
)
}

return FloatingPanelLayoutAnchor(absoluteInset: tip, edge: .bottom, referenceGuide: .safeArea)
}
}
34 changes: 34 additions & 0 deletions Backpack/BottomSheet/Classes/IntrinsicBottomSheetLayout.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Backpack - Skyscanner's Design System
*
* Copyright 2018 Skyscanner Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import FloatingPanel

class IntrinsicBottomSheetLayout: FloatingPanelLayout {
let position: FloatingPanelPosition = .bottom
let initialState: FloatingPanelState = .full

var anchors: [FloatingPanelState: FloatingPanelLayoutAnchoring] {
return [
.full: FloatingPanelIntrinsicLayoutAnchor(absoluteOffset: 0, referenceGuide: .safeArea)
]
}

func backdropAlpha(for state: FloatingPanelState) -> CGFloat {
return 0.3
}
}
38 changes: 16 additions & 22 deletions Backpack/BottomSheet/Classes/ModalBottomSheetLayout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,40 +22,34 @@ import FloatingPanel
/// will not be interactable, there will be an overlay in the Parent ViewController,
/// and the BPKBottomShet can be dismissed
public final class ModalBottomSheetLayout: FloatingPanelLayout {
public let position: FloatingPanelPosition = .bottom

/// Initial position of BPKBottomSheet
public var initialPosition: FloatingPanelPosition {
return .half
}
public let initialState: FloatingPanelState = .half

/// All supported positions of BPKBottomSheet
public var supportedPositions: Set<FloatingPanelPosition> {
return [.full, .half]
public var anchors: [FloatingPanelState: FloatingPanelLayoutAnchoring] {
return [
.full: insets.fullAnchor,
.half: insets.halfAnchor
]
}

/// Insets that will be used for the modal presentation
private let insets: BottomSheetInsets

public init(insets: BottomSheetInsets) {
self.insets = insets
}

/// Method to describe the height of BPKBottomSheet in each position
public func insetFor(position: FloatingPanelPosition) -> CGFloat? {
switch position {
case .half: return insets.half
default: return nil
}
}

/// Method to define the overlay in the parent view controller
public func backdropAlphaFor(position: FloatingPanelPosition) -> CGFloat {
switch position {
public func backdropAlpha(for state: FloatingPanelState) -> CGFloat {
switch state {
case .full, .half:
return Constants.backdropAlpha
default:
return 0.0
}
}

private let insets: BottomSheetInsets

public init(insets: BottomSheetInsets) {
self.insets = insets
}
}

private enum Constants {
Expand Down
44 changes: 16 additions & 28 deletions Backpack/BottomSheet/Classes/PersistentBottomSheetLayout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,41 +21,29 @@ import FloatingPanel
/// Layout implementation where the BPKBottomSheet will remain persistent in the parent
/// ViewController. There is no overlay and the parent ViewController remains interactable
public final class PersistentBottomSheetLayout: FloatingPanelLayout {
/// The position of the floating panel
public let position: FloatingPanelPosition = .bottom

/// Initial position of BPKBottomSheet
public var initialPosition: FloatingPanelPosition {
return .half
}

public let initialState: FloatingPanelState = .half

/// All supported positions of BPKBottomSheet
public var supportedPositions: Set<FloatingPanelPosition> {
return [.half, .full, .tip]
public var anchors: [FloatingPanelState: FloatingPanelLayoutAnchoring] {
return [
.full: insets.fullAnchor,
.half: insets.halfAnchor,
.tip: insets.tipAnchor
]
}

/// Method to define the overlay in the parent view controller
public func backdropAlpha(for state: FloatingPanelState) -> CGFloat {
return 0
}

private let insets: BottomSheetInsets

public init(insets: BottomSheetInsets) {
self.insets = insets
}

/// Method to describe the height of BPKBottomSheet in each position
public func insetFor(position: FloatingPanelPosition) -> CGFloat? {
switch position {
case .half:
return insets.half
case .tip:
return insets.tip
default:
return insets.full
}
}

/// Method to define the overlay in the parent view controller
public func backdropAlphaFor(position: FloatingPanelPosition) -> CGFloat {
return 0.0
}
}

private enum Constants {
static let bottomSheetHeightInHalfPosition: CGFloat = 386.0
static let bottomSheetHeightInTipPosition: CGFloat = 120.0
}
Loading