Git Product home page Git Product logo

presenting's Introduction

Presenting

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

Description

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

  • Simplifies code by removing responsibilities from views.
  • Leads to cleaner, more manageable code.
  • Promotes better separation of concerns.
  • Ridiculously lightweight.
  • Type-safe routing using enums and associated values.
  • Unit Tested protocol implementations.
  • Zero 3rd party dependencies.

Requirements

  • iOS: Requires iOS 15.0 or later.
  • macOS: Requires macOS 12.0 or later.

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 if you need views presented in a sheet or full screen cover.
import SwiftUI
import Presenting

enum Route: ViewDisplayable {
    case detail
    case settings
    
    @ViewBuilder
    var viewToDisplay: some View {
        switch self {
        case .detail:
            DetailView()
        case .settings:
            SettingsView()
        }
    }
}
  1. The PresentingView will be used to inject the presenter object into the view model. Pass in the Route enumeration, so that the PresenterView can use them.
import SwiftUI
import Presenting

struct ContentView: View {
    var body: some View {
        PresentingView(Route.self) { presenter in
            ExampleView(viewModel: .init(presenter: presenter))
        }
    }
}

struct ExampleView: View {
    @ObservedObject var viewModel: ExampleViewModel
    
    var body: some View {
        // The app's main content goes here
    }
}

final class ExampleViewModel: ObservableObject {
    private let presenter: Presenter<Route>
    
    init(presenter: Presenter<Route>) {
        self.presenter = presenter
    }
}
  1. Handle presenting sheets, fullScreenCovers, alerts, toasts, etc. by using the Presenter functions:
// Sheet
func presentSheet(_ destination: Destination)
func dismissSheet()

// FullScreenCover
func presentFullScreenCover(_ destination: Destination)
func dismissFullScreenCover()

// Alert
func presentAlert(_ alert: Alert)
func dismissAlert()

// Toast
func presentToast(on edge: VerticalEdge = .top,
                  _ toast: Toast,
                  isAutoDismissed: Bool = true)
func dismissToast()
  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 over a view.
import SwiftUI
import Presenting

struct ContentView: View {
    var body: some View {
        BasicPresentingView { presenter in
            ExampleView(viewModel: .init(presenter: presenter))
        }
    }
}

Settings sheet example

import SwiftUI
import Presenting

enum ExampleRoute: ViewDisplayable {
    case settings
    
    @ViewBuilder
    var viewToDisplay: some View {
        switch self {
        case .settings:
            SettingsView()
        }
    }
}

struct ContentView: View {
    var body: some View {
        PresentingView(Route.self) { presenter in
            ExampleView(viewModel: .init(presenter: presenter))
        }
    }
}

struct ExampleView: View {
    @ObservedObject var viewModel: ExampleViewModel
    
    var body: some View {
        VStack {
            Button("Settings", action: viewModel.didTapSettings)
        }
    }
}

final class ExampleViewModel: ObservableObject {
    private let presenter: Presenter<Route>
    
    init(presenter: Presenter<Route>) {
        self.presenter = presenter
    }
    
    func didTapSettings() {
        presenter.presentSheet(.settings)
    }
}

Author

James Sedlacek, find me on X/Twitter or LinkedIn

License

Presenting is available under the MIT license.

Copyright (c) 2023 James Sedlacek

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

presenting's People

Contributors

jamessedlacek 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.