Skip to content

Commit

Permalink
Merge pull request #359 from ivpn/task/upgrade-floatingpanel
Browse files Browse the repository at this point in the history
Update FloatingPanel package
  • Loading branch information
jurajhilje authored Oct 26, 2023
2 parents 6700fd7 + 1fdaa6f commit fd327d5
Show file tree
Hide file tree
Showing 7 changed files with 180 additions and 99 deletions.
4 changes: 2 additions & 2 deletions IVPNClient.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
archiveVersion = 1;
classes = {
};
objectVersion = 53;
objectVersion = 54;
objects = {

/* Begin PBXBuildFile section */
Expand Down Expand Up @@ -3482,7 +3482,7 @@
repositoryURL = "https://github.com/scenee/FloatingPanel";
requirement = {
kind = exactVersion;
version = 1.7.2;
version = 2.6.3;
};
};
82EC884529A12D510024CC40 /* XCRemoteSwiftPackageReference "SnapKit" */ = {
Expand Down
69 changes: 10 additions & 59 deletions IVPNClient/Scenes/Base.lproj/Main.storyboard

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ import JGProgressHUD
extension ControlPanelViewController {

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
controlPanelView.networkView.isHidden = !UserDefaults.shared.networkProtectionEnabled
controlPanelView.exitServerTableCell.isHidden = !UserDefaults.shared.isMultiHop

if indexPath.row == 0 { return 100 }
if indexPath.row == 1 && Application.shared.settings.connectionProtocol.tunnelType() == .ipsec { return 0 }
if indexPath.row == 1 { return 44 }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ import FloatingPanel
extension FloatingPanelController {

func setup() {
surfaceView.shadowHidden = true
surfaceView.contentInsets = .init(top: 20, left: 0, bottom: 0, right: 0)
surfaceView.contentPadding = .init(top: 20, left: 0, bottom: 0, right: 0)
surfaceView.backgroundColor = UIColor.init(named: Theme.ivpnBackgroundPrimary)

let contentViewController = NavigationManager.getControlPanelViewController()
Expand Down
173 changes: 141 additions & 32 deletions IVPNClient/Scenes/MainScreen/ControlPanel/FloatingPanelMainLayout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,33 @@ import FloatingPanel

class FloatingPanelMainLayout: FloatingPanelLayout {

// MARK: - Override public properties -
var position: FloatingPanelPosition {
if UIDevice.current.userInterfaceIdiom == .pad && UIWindow.isLandscape && !UIApplication.shared.isSplitOrSlideOver {
return .top
}

return .bottom
}

public var initialPosition: FloatingPanelPosition {
var initialState: FloatingPanelState {
if UIDevice.current.userInterfaceIdiom == .pad && UIWindow.isLandscape && !UIApplication.shared.isSplitOrSlideOver {
return .full
}

return .half
}

public var supportedPositions: Set<FloatingPanelPosition> {
var anchors: [FloatingPanelState: FloatingPanelLayoutAnchoring] {
if UIDevice.current.userInterfaceIdiom == .pad && UIWindow.isLandscape && !UIApplication.shared.isSplitOrSlideOver {
return [.full]
return [
.full: FloatingPanelLayoutAnchor(absoluteInset: 0, edge: .bottom, referenceGuide: .superview)
]
}

return [.full, .half]
return [
.full: FloatingPanelLayoutAnchor(absoluteInset: 0, edge: .top, referenceGuide: .safeArea),
.half: FloatingPanelLayoutAnchor(absoluteInset: halfHeight, edge: .bottom, referenceGuide: .safeArea)
]
}

// MARK: - Private properties -
Expand All @@ -60,37 +71,23 @@ class FloatingPanelMainLayout: FloatingPanelLayout {
return 230 - bottomSafeArea
}

// MARK: - Override public methods -

public func insetFor(position: FloatingPanelPosition) -> CGFloat? {
if UIDevice.current.userInterfaceIdiom == .pad && UIWindow.isLandscape && !UIApplication.shared.isSplitOrSlideOver {
switch position {
case .full:
return -20
default:
return nil
}
}

switch position {
case .full:
return 10
case .half:
return halfHeight
default:
return nil
}
}

public func prepareLayout(surfaceView: UIView, in view: UIView) -> [NSLayoutConstraint] {
if let surfaceView = surfaceView as? FloatingPanelSurfaceView {
if let surfaceView = surfaceView as? SurfaceView {
let appearance = SurfaceAppearance()
let shadow = SurfaceAppearance.Shadow()
shadow.color = .clear
appearance.shadows = [shadow]
appearance.backgroundColor = UIColor.init(named: Theme.ivpnBackgroundPrimary)

if UIDevice.current.userInterfaceIdiom == .pad && UIWindow.isLandscape && !UIApplication.shared.isSplitOrSlideOver {
surfaceView.grabberHandle.isHidden = true
surfaceView.cornerRadius = 0
appearance.cornerRadius = 0
} else {
surfaceView.grabberHandle.isHidden = false
surfaceView.cornerRadius = 15
appearance.cornerRadius = 15
}

surfaceView.appearance = appearance
}

if UIDevice.current.userInterfaceIdiom == .pad && UIWindow.isLandscape && !UIApplication.shared.isSplitOrSlideOver {
Expand All @@ -113,12 +110,124 @@ class FloatingPanelMainLayout: FloatingPanelLayout {
]
}

public func backdropAlphaFor(position: FloatingPanelPosition) -> CGFloat {
if position == .full && (UIDevice.current.userInterfaceIdiom == .phone || UIWindow.isPortrait) {
public func backdropAlpha(for state: FloatingPanelState) -> CGFloat {
if state == .full && (UIDevice.current.userInterfaceIdiom == .phone || UIWindow.isPortrait) {
return 0.3
}

return 0
}

}

class MainFloatingPanelBehavior: FloatingPanelBehavior {

func allowsRubberBanding(for edge: UIRectEdge) -> Bool {
return false
}

func shouldProjectMomentum(_ fpc: FloatingPanelController, to proposedTargetPosition: FloatingPanelState) -> Bool {
return false
}

}

//class FloatingPanelMainLayout: FloatingPanelLayout {
//
// // MARK: - Override public properties -
//
// public var initialPosition: FloatingPanelPosition {
// if UIDevice.current.userInterfaceIdiom == .pad && UIWindow.isLandscape && !UIApplication.shared.isSplitOrSlideOver {
// return .full
// }
//
// return .half
// }
//
// public var supportedPositions: Set<FloatingPanelPosition> {
// if UIDevice.current.userInterfaceIdiom == .pad && UIWindow.isLandscape && !UIApplication.shared.isSplitOrSlideOver {
// return [.full]
// }
//
// return [.full, .half]
// }
//
// // MARK: - Private properties -
//
// private let bottomSafeArea = UIWindow.keyWindow?.safeAreaInsets.bottom ?? 0
//
// private var halfHeight: CGFloat {
// if Application.shared.settings.connectionProtocol.tunnelType() != .ipsec && UserDefaults.shared.isMultiHop {
// return 359 - bottomSafeArea
// }
//
// if Application.shared.settings.connectionProtocol.tunnelType() != .ipsec {
// return 274 - bottomSafeArea
// }
//
// return 230 - bottomSafeArea
// }
//
// // MARK: - Override public methods -
//
// public func insetFor(position: FloatingPanelPosition) -> CGFloat? {
// if UIDevice.current.userInterfaceIdiom == .pad && UIWindow.isLandscape && !UIApplication.shared.isSplitOrSlideOver {
// switch position {
// case .full:
// return -20
// default:
// return nil
// }
// }
//
// switch position {
// case .full:
// return 10
// case .half:
// return halfHeight
// default:
// return nil
// }
// }
//
// public func prepareLayout(surfaceView: UIView, in view: UIView) -> [NSLayoutConstraint] {
// if let surfaceView = surfaceView as? FloatingPanelSurfaceView {
// if UIDevice.current.userInterfaceIdiom == .pad && UIWindow.isLandscape && !UIApplication.shared.isSplitOrSlideOver {
// surfaceView.grabberHandle.isHidden = true
// surfaceView.cornerRadius = 0
// } else {
// surfaceView.grabberHandle.isHidden = false
// surfaceView.cornerRadius = 15
// }
// }
//
// if UIDevice.current.userInterfaceIdiom == .pad && UIWindow.isLandscape && !UIApplication.shared.isSplitOrSlideOver {
// return [
// surfaceView.leftAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leftAnchor, constant: 0),
// surfaceView.widthAnchor.constraint(equalToConstant: 375)
// ]
// }
//
// if UIDevice.current.userInterfaceIdiom == .pad && UIWindow.isPortrait && !UIApplication.shared.isSplitOrSlideOver {
// return [
// surfaceView.widthAnchor.constraint(equalToConstant: 520),
// surfaceView.centerXAnchor.constraint(equalTo: view.safeAreaLayoutGuide.centerXAnchor)
// ]
// }
//
// return [
// surfaceView.leftAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leftAnchor, constant: 0),
// surfaceView.rightAnchor.constraint(equalTo: view.safeAreaLayoutGuide.rightAnchor, constant: 0)
// ]
// }
//
// public func backdropAlphaFor(position: FloatingPanelPosition) -> CGFloat {
// if position == .full && (UIDevice.current.userInterfaceIdiom == .phone || UIWindow.isPortrait) {
// return 0.3
// }
//
// return 0
// }
//
//}

13 changes: 10 additions & 3 deletions IVPNClient/Scenes/MainScreen/MainViewController+Ext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import FloatingPanel

extension MainViewController: FloatingPanelControllerDelegate {

func floatingPanel(_ vc: FloatingPanelController, layoutFor newCollection: UITraitCollection) -> FloatingPanelLayout? {
func floatingPanel(_ vc: FloatingPanelController, layoutFor newCollection: UITraitCollection) -> FloatingPanelLayout {
updateAccessibilityLabel(vc: vc)

return FloatingPanelMainLayout()
Expand All @@ -44,22 +44,29 @@ extension MainViewController: FloatingPanelControllerDelegate {

func updateAccessibilityLabel(vc: FloatingPanelController) {
if let controlPanelViewController = floatingPanel.contentViewController, UIDevice.current.userInterfaceIdiom != .pad {
if vc.position == .full {
if vc.state == .full {
controlPanelViewController.view.accessibilityLabel = "Swipe down to collapse control panel"
} else {
controlPanelViewController.view.accessibilityLabel = "Swipe up to expan control panel"
}
}
}

func floatingPanelDidMove(_ fpc: FloatingPanelController) {
let loc = fpc.surfaceLocation
let minY = fpc.surfaceLocation(for: .full).y - 6.0
let maxY = fpc.surfaceLocation(for: .half).y + 6.0
fpc.surfaceLocation = CGPoint(x: loc.x, y: min(max(loc.y, minY), maxY))
}

}

// MARK: - UIAdaptivePresentationControllerDelegate -

extension MainViewController: UIAdaptivePresentationControllerDelegate {

func presentationControllerWillDismiss(_ presentationController: UIPresentationController) {
floatingPanel.updateLayout()
floatingPanel.invalidateLayout()
NotificationCenter.default.post(name: Notification.Name.UpdateControlPanel, object: nil)
}

Expand Down
14 changes: 13 additions & 1 deletion IVPNClient/Scenes/MainScreen/MainViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ class MainViewController: UIViewController {
refreshUI()
initConnectionInfo()
startPingService()
DispatchQueue.async { [self] in
showFloatingPanel()
}
}

deinit {
Expand Down Expand Up @@ -202,7 +205,11 @@ class MainViewController: UIViewController {
// MARK: - Private methods -

@objc private func updateFloatingPanelLayout() {
floatingPanel.updateLayout()
guard floatingPanel != nil else {
return
}

floatingPanel.invalidateLayout()
mainView.setupView(animated: false)
}

Expand Down Expand Up @@ -237,6 +244,11 @@ class MainViewController: UIViewController {
floatingPanel.delegate = self
floatingPanel.addPanel(toParent: self)
floatingPanel.show(animated: true)
floatingPanel.behavior = MainFloatingPanelBehavior()
}

private func showFloatingPanel() {
floatingPanel.show(animated: true)
}

private func startAPIUpdate() {
Expand Down

0 comments on commit fd327d5

Please sign in to comment.