Giter Site home page Giter Site logo

presenting's Introduction

Presenting

Swift Package Manager GitHub stars GitHub forks GitHub contributors Pull Requests Badge Issues Badge

Description

Presenting is a lightweight SwiftUI library for abstracting logic from views.

  • Handle presenting sheet, fullScreenCover, alert, toast, and confirmationDialog.
  • 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


Requirements

Platform Minimum Version
iOS 15.0
macOS 12.0
tvOS 15.0
watchOS 8.0

Installation

You can install Presenting using the Swift Package Manager.

  1. In Xcode, select File > Add Package Dependencies.
  2. Copy & paste the following into the Search or Enter Package URL search bar.
https://github.com/JamesSedlacek/Presenting.git
  1. Xcode will fetch the repository & the Presenting library will be added to your project.

Getting Started

  1. Create a Route enum that conforms to the Presentable protocol if you need views presented in a sheet or fullScreenCover.
import Presenting
import SwiftUI

enum ExampleRoute: Presentable {
    case detail
    case settings
    
    var body: some View {
        switch self {
        case .detail:
            DetailView()
        case .settings:
            SettingsView()
        }
    }
}
  1. Wrap your RootView with a PresentingView.
import Presenting
import SwiftUI

struct ContentView: View {
    var body: some View {
        PresentingView(ExampleRoute.self) { presenter in
            Button("Go to Settings") {
                presenter.presentSheet(.settings)
            }
        }
    }
}
  1. 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()
  1. 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
        }
    }
}

Author

James Sedlacek, find me on X/Twitter or LinkedIn

presenting's People

Contributors

jamessedlacek avatar achi113s avatar ibrahimhamed avatar jorgeagomez avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.