Swiftpack.co - alexdremov/PathPresenter as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by alexdremov.
alexdremov/PathPresenter 1.1.3
Pure SwiftUI state-driven library to present view sequences and hierarchies.
⭐️ 72
🕓 49 weeks ago
iOS macOS watchOS tvOS
.package(url: "https://github.com/alexdremov/PathPresenter.git", from: "1.1.3")

PathPresenter

GitHub release (latest by date) GitHub top language Codacy Badge

https://user-images.githubusercontent.com/25539425/181922855-3ff3aa77-757b-4091-b682-63456fe963a1.mp4

Pure SwiftUI routing with transitions, animations, and .sheet() support.

In SwiftUI, View is a function of the state. Routing is not an exception. Reasoning and under the hood explanation can be found on my blog:

http://alexdremov.me/swiftui-navigation-is-a-mess-heres-what-you-can-do/

Why

  • .sheet() usages are usually messy, deviate from app architecture, and require additional business-logic
  • Creating view sequences in SwiftUI is not elegant
  • MVVM is gorgeous, but what about one level above? Routing between MVVM modules is cluttered.

Advantages

  • Purely state-driven. No ObservableObjects, no EnvironmentObjects, no Singletons.
  • Pure SwiftUI.
  • SwiftUI transitions and animations.
  • Structured .sheet() support. No need to remaster the whole codebase to present the view with .sheet(). It just works.

Example

Example from the video: PathPresenterExample

The view hierarchy is managed through the PathPresenter.Path() structure. You can push new views into it using .append methods and delete views from the top using .removeLatest.

Internally, the view's layout is managed by ZStack, so all views history is visible.

Possible presentation ways:

enum PathType {
  /**
   * Just show a view. No animation, no transition.
   * Show view above all other views
   */
  case plain
  
  /**
   * Show view with in and out transitions.
   * Transition animation also can be specified.
   */
  case animated(transition: AnyTransition, animation: Animation)
  
  /**
   * Show view in .sheet()
   * - Note: If you want to present several views in sheet,
   * you can create a second RoutingView and use it in sheet!
   */
  case sheet(onDismiss: Action)
}

Complete example:


struct RootViewGitHub: View {
    @State var path = PathPresenter.Path()

    var body: some View {
        PathPresenter.RoutingView(path: $path) {
            // Root view. Always presented
            VStack {
                Button("Push") {
                    path.append(
                        VStack {
                            Text("Hello from plain push")
                            backButton
                        }.frame(width: 300, height: 300)
                         .background(.white)
                         .border(.red),
                        type: .plain
                    )
                }
                Button("Sheet") {
                    path.append(
                        VStack {
                            Text("Hello from sheet")
                            backButton
                        }.frame(width: 300, height: 300)
                         .background(.white)
                         .border(.red),
                        type: .sheet(onDismiss: {print("dismissed")})
                    )
                }
                Button("Left animation") {
                    path.append(
                        VStack {
                            Text("Hello from left animation")
                            backButton
                        }.frame(width: 300, height: 300)
                         .background(.white)
                         .border(.red),
                        type: .animated(transition: .move(edge: .leading),
                                        animation: .easeIn)
                    )
                }
            }
            .frame(width: 300, height: 300)
        }
    }
    
    var backButton: some View {
        Button("Back") {
            if !path.isEmpty {
                path.removeLast()
            }
        }
    }
}

Transitions and animation example

Documentation

Code is mostly commented and simply structured. Check it out!

TODO

  • Path-based routing. Define view hierarchy with URL-like structures for better views switching architecture

GitHub

link
Stars: 72
Last commit: 49 weeks ago
Advertisement: IndiePitcher.com - Cold Email Software for Startups

Release Notes

Feature: Show only the last view
49 weeks ago

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics