Transmission

repository·main·Indexed 22 days ago

https://github.com/nathantannar4/transmission

A library that bridges UIKit's presentation and transition APIs into a SwiftUI-native interface. It enables advanced features such as presentation controllers, interactive transitions, and UIWindow overlays. Key components include PresentationLink, DestinationLink, WindowLink, ShareSheetLink, and QuickLookPreviewLink, as well as TransitionReader for accessing transition progress and coordinators for programmatic dismissal.

Tokens
2.1K
Snippets
9
Records
12
Agent score
28%

What's inside Transmission

  1. Overview of Transmission

    main
    Transmission is a library designed to improve SwiftUI view presentations and transitions. It bridges UIKit presentation APIs to a SwiftUI-friendly API, enabling the use of presentation controllers, interactive transitions, and other advanced UIKit presentation features within SwiftUI applications.
  2. Access transition progress with TransitionReader

    main

    Use TransitionReader to build interactive presentation and dismissal transitions. It provides a container view that exposes the UIViewControllerTransitionCoordinator progress.

    Inside the TransitionReader closure, you receive a Proxy object containing:

    • progress: A CGFloat from 0 to 1 representing the transition state.
    • isPresented: A Bool indicating if the view is currently presented.
    TransitionReader {
        Proxy(progress: progress, isPresented: isPresented) in
        // Use progress to drive animations or opacity
        MyView()
            .opacity(progress)
    }
  3. Install Transmission in Swift Package Manager Projects

    main

    Add Transmission as a dependency in your Package.swift file by adding the URL to your dependencies array and referencing the Transmission product in your target dependencies.

    let package = Package(
        //...
        dependencies: [
            .package(url: "https://github.com/nathantannar4/Transmission"),
        ],
        targets: [
            .target(
                name: "YourPackageTarget",
                dependencies: [
                    .product(name: "Transmission", package: "Transmission"),
                ],
                //...
            ),
            //...
        ],
        //...
    )
  4. Configure Status Bar style and visibility

    main

    Transmission provides view modifiers to control the status bar of the hosting UIViewController:

    • preferredStatusBarStyle(_:): Sets the UIStatusBarStyle.
    • prefersStatusBarHidden(_:): Sets whether the status bar is hidden.
    MyView()
        .preferredStatusBarStyle(.light)
        .prefersStatusBarHidden(true)
  5. Programmatically dismiss views with Coordinators

    main

    Transmission provides coordinators to programmatically control dismissal from within the view hierarchy via the Environment.

    PresentationCoordinator (for PresentationLink):

    • dismiss(count:animation:): Dismisses a specified number of presented views.
    • dismiss(count:transaction:): Dismisses with a specific transaction.
    • Access via @Environment(\.presentationCoordinator).

    DestinationCoordinator (for DestinationLink):

    • pop(count:animation:): Pops a specified number of views from the navigation stack.
    • pop(count:transaction:): Pops with a specific transaction.
    • Access via @Environment(\.destinationCoordinator).
    struct MySubView: View {
        @Environment(\.presentationCoordinator) var coordinator
    
        var body: some View {
            Button("Dismiss") {
                coordinator.dismiss(count: 1)
            }
        }
    }
  6. Preview files with QuickLookPreviewLink

    main

    Use QuickLookPreviewLink to present a QLPreviewController for quick look previews.

    QuickLookPreviewItem:

    • Requires a URL and an optional Text label.

    Usage:

    • Pass items: [QuickLookPreviewItem] to the link.
    • Pass a url: URL directly to the link.
    • Use the .quickLookPreview(...) view modifier.
    // Using a URL directly
    QuickLookPreviewLink(url: myFileURL)
        label: { Text("Preview") }
    
    // Using the .quickLookPreview modifier
    MyView()
        .quickLookPreview(url: myFileURL, isPresented: $isShowing)
  7. Present views in a new UIWindow with WindowLink

    main

    Use WindowLink to present a destination view in a completely new UIWindow. This is useful for overlays or alerts that need to exist outside the main view hierarchy.

    WindowLinkLevel:

    • .default
    • .overlay
    • .background
    • .alert
    • .custom(CGFloat)

    WindowLinkTransition:

    • .identity
    • .opacity
    • .move(edge: Edge)
    • .scale(CGFloat)
    • .combined(with: WindowLinkTransition)

    You can also use the .window(...) view modifier.

    // Using the WindowLink view
    WindowLink(level: .alert, transition: .opacity) {
        MyOverlayView()
    } label: {
        Text("Show Alert Window")
    }
    
    // Using the .window modifier
    MyView()
        .window(isPresented: $isShowing, level: .overlay, transition: .scale(1.2)) {
        MyOverlayView()
    }
  8. Push views with DestinationLink

    main

    Use DestinationLink to create a button that pushes a destination view onto a navigation stack (using UINavigationController). You can specify a transition style using DestinationLinkTransition.

    Available transitions include:

    • .default
    • .zoom (iOS 18.0+)
    • .custom(_:) for implementing DestinationLinkTransitionRepresentable.

    You can also use the .destination(...) view modifier to push views based on a binding.

    // Using the DestinationLink view
    DestinationLink(transition: .default) {
        MyDestinationView()
    } label: {
        Text("Push View")
    }
    
    // Using the .destination modifier
    MyView()
        .destination(isPresented: $isShowing) {
        MyDestinationView()
    }
  9. Show Share Sheets with ShareSheetLink

    main

    Use ShareSheetLink to present a UIActivityViewController. To provide items to the share sheet, implement the ShareSheetItemProvider protocol.

    Supported Item Providers:

    • String (via extension)
    • URL (via extension)
    • SnapshotItemProvider<Content> (for sharing a view as an image).

    You can also use the .share(...) view modifier or ShareSheetLinkModifier.

    // Sharing a URL
    ShareSheetLink(items: [myURL]) {
        Text("Share Link")
    }
    
    // Using the .share modifier
    MyView()
        .share(items: [myString], isPresented: $isShowing)
  10. Present views with PresentationLink

    main

    Use PresentationLink to create a button that presents a destination view in a new UIViewController. You can specify a transition style using PresentationLinkTransition.

    Available transitions include:

    • .default
    • .sheet
    • .currentContext
    • .fullscreen
    • .popover
    • .slide
    • .card
    • .zoom (iOS 18.0+)
    • .matchedGeometry
    • .toast
    • .custom(_:) for implementing PresentationLinkTransitionRepresentable.

    You can also use the .presentation(...) view modifier to present views based on a binding.

    // Using the PresentationLink view
    PresentationLink(transition: .sheet) {
        MyDestinationView()
    } label: {
        Text("Open Sheet")
    }
    
    // Using the .presentation modifier
    MyView()
        .presentation(isPresented: $isShowing, transition: .card) {
        MyDestinationView()
    }