Presenting
is a lightweight SwiftUI library for abstracting logic from views.
- Handle presenting
sheet
,fullScreenCover
,alert
,toast
, andconfirmationDialog
. - Unit Tested protocol implementations.
- Zero 3rd party dependencies.
Note - This library is for Presenting only.
If you need to abstract NavigationStack
then use my other library Routing
Platform | Minimum Version |
---|---|
iOS | 15.0 |
macOS | 12.0 |
tvOS | 15.0 |
watchOS | 8.0 |
You can install Presenting
using the Swift Package Manager.
- In Xcode, select
File
>Add Package Dependencies
. - Copy & paste the following into the
Search or Enter Package URL
search bar.
https://github.com/JamesSedlacek/Presenting.git
- Xcode will fetch the repository & the
Presenting
library will be added to your project.
- Create a
Route
enum that conforms to thePresentable
protocol if you need views presented in asheet
orfullScreenCover
.
import Presenting
import SwiftUI
enum ExampleRoute: Presentable {
case detail
case settings
var body: some View {
switch self {
case .detail:
DetailView()
case .settings:
SettingsView()
}
}
}
- Wrap your
RootView
with aPresentingView
.
import Presenting
import SwiftUI
struct ContentView: View {
var body: some View {
PresentingView(ExampleRoute.self) { presenter in
Button("Go to Settings") {
presenter.presentSheet(.settings)
}
}
}
}
- Handle presenting sheets, fullScreenCovers, alerts, toasts,
and confirmation dialogs by using the
Presenter
functions:
// MARK: Sheet
/// Presents a new sheet view.
/// - Parameter destination: The view to be presented as a sheet.
/// - Parameter onDismiss: The action to be triggered after the view is dismissed.
func presentSheet(_ destination: Destination, onDismiss: @escaping () -> Void)
/// Dismisses the currently presented sheet view.
func dismissSheet()
// MARK: Full Screen Cover
/// Presents a full screen cover with the specified destination.
/// - Parameter destination: The destination to present as a full screen cover.
/// - Parameter onDismiss: The action to be triggered after the view is dismissed.
func presentFullScreenCover(_ destination: Destination, onDismiss: @escaping () -> Void)
/// Dismisses the currently presented full screen cover.
func dismissFullScreenCover()
// MARK: Alert
/// Presents the specified alert.
func presentAlert(_ alert: Alert)
/// Dismisses the currently presented alert.
func dismissAlert()
// MARK: Toast
/// Presents a toast notification.
///
/// - Parameters:
/// - edge: The vertical edge on which to present the toast.
/// - toast: The toast to present.
/// - isAutoDismissed: A Boolean value indicating whether the toast is automatically dismissed.
func presentToast(on edge: VerticalEdge, _ toast: Toast, isAutoDismissed: Bool)
/// Dismisses the currently presented toast notification.
func dismissToast()
// MARK: Confirmation Dialog
/// Presents the specified confirmation dialog.
func presentConfirmationDialog(_ confirmationDialog: ConfirmationDialog)
/// Dismisses the currently presented confirmation dialog.
func dismissConfirmationDialog()
- If you don't need to present views in a sheet or full screen cover, use the
BasicPresentingView
instead. This will allow you to present alerts, toasts, and confirmation dialogs over a view.
import SwiftUI
import Presenting
struct ContentView: View {
var body: some View {
BasicPresentingView { presenter in
// code goes here
}
}
}