Skip to content

Commit

Permalink
Customize modal presentation style via modal-style
Browse files Browse the repository at this point in the history
In the path configuration, set "modal-style" to:

* "medium" to present a small, half-screen modal.
* "large" to present a normal sized modal, filling the screen.
* "full" to present a modal that can't be swiped to dismiss.

If the property is missing or doesn't exactly match one of these three
options then `large` is used.
  • Loading branch information
joemasilotti committed Dec 11, 2023
1 parent d35d443 commit 7071966
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,20 @@ extension UINavigationController {
let viewControllers = viewControllers.dropLast()
setViewControllers(viewControllers + [viewController], animated: false)
}

func setModalPresentationStyle(via proposal: VisitProposal) {
switch proposal.modalStyle {
case .medium:
modalPresentationStyle = .automatic
if #available(iOS 15.0, *) {
if let sheet = sheetPresentationController {
sheet.detents = [.medium(), .large()]
}
}
case .large:
modalPresentationStyle = .automatic
case .full:
modalPresentationStyle = .fullScreen
}
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import UIKit

public extension VisitProposal {
var context: TurboNavigation.Context {
if let rawValue = properties["context"] as? String {
Expand All @@ -13,6 +15,13 @@ public extension VisitProposal {
return .default
}

var modalStyle: TurboNavigation.ModalStyle {
if let rawValue = properties["modal-style"] as? String {
return TurboNavigation.ModalStyle(rawValue: rawValue) ?? .large
}
return .large
}

/// Used to identify a custom native view controller if provided in the path configuration properties of a given pattern.
///
/// For example, given the following configuration file:
Expand Down
6 changes: 6 additions & 0 deletions Source/Turbo Navigator/Helpers/TurboNavigation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,10 @@ public enum TurboNavigation {
case replaceRoot = "replace_root"
case none
}

public enum ModalStyle: String {
case medium
case large
case full
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ class TurboNavigationHierarchyController {

func navController(for navigationType: NavigationStackType) -> UINavigationController {
switch navigationType {
case .main: navigationController
case .modal: modalNavigationController
case .main: navigationController
case .modal: modalNavigationController
}
}

Expand Down Expand Up @@ -82,6 +82,7 @@ class TurboNavigationHierarchyController {
pushOrReplace(on: modalNavigationController, with: controller, via: proposal)
} else {
modalNavigationController.setViewControllers([controller], animated: true)
modalNavigationController.setModalPresentationStyle(via: proposal)
navigationController.present(modalNavigationController, animated: true)
}
if let visitable = controller as? Visitable {
Expand Down Expand Up @@ -146,6 +147,7 @@ class TurboNavigationHierarchyController {
modalNavigationController.replaceLastViewController(with: controller)
} else {
modalNavigationController.setViewControllers([controller], animated: false)
modalNavigationController.setModalPresentationStyle(via: proposal)
navigationController.present(modalNavigationController, animated: true)
}
if let visitable = controller as? Visitable {
Expand Down

0 comments on commit 7071966

Please sign in to comment.