Swiftpack.co -  GeorgeElsham/ViewExtractor as Swift Package
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
GeorgeElsham/ViewExtractor
Extract SwiftUI views from ViewBuilder content.
.package(url: "https://github.com/GeorgeElsham/ViewExtractor.git", from: "1.1.0")

ViewExtractor

Extract SwiftUI views from ViewBuilder content.

Installation

Follow the instructions at Adding Package Dependencies to Your App to find out how to install the Swift package. Use the link of this GitHub repo as the URL (https://github.com/GeorgeElsham/ViewExtractor).

Example #1

Here is an example which creates a VStack with Divider()s in between.

View code

struct DividedVStack: View {
    private let views: [AnyView]
    
    // For 2 or more views
    init<Views>(@ViewBuilder content: TupleContent<Views>) {
        views = ViewExtractor.getViews(from: content)
    }
    
    // For 0 or 1 view
    init<Content: View>(@ViewBuilder content: NormalContent<Content>) {
        views = ViewExtractor.getViews(from: content)
    }
    
    var body: some View {
        VStack(spacing: 0) {
            ForEach(views.indices) { index in
                if index != 0 {
                    Divider()
                }
                
                views[index]
            }
        }
    }
}

Usage

DividedVStack {
    Text("View 1")
    Text("View 2")
    Text("View 3")
    Image(systemName: "circle")
}

Result

Example #2

Here is an example which creates a VStack of views, for every multiple of 2. The views are loaded lazily, so they aren't all computed at once which would waste resources if half of them aren't used.

View code

struct IntervalVStack: View {
    private let extractor: ViewExtractor

    init<Content: View & DynamicViewContentProvider>(content: ForEachContent<Content>) {
        extractor = ViewExtractor(content: content)
    }

    var body: some View {
        VStack(spacing: 0) {
            ForEach(extractor.forEachRange!) { index in
                if index.isMultiple(of: 2) {
                    extractor.uncheckedView(at: index)
                }
            }
        }
    }
}

Usage

IntervalVStack {
    ForEach(0 ..< 10) { index in
        Text("Index: \(index)")
    }
}

Result

GitHub

link
Stars: 8
Last commit: 1 week ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Release Notes

Lazily get view by index
1 week ago

You can now lazily get a view, based on its index in a ForEach.

Bug also fixed where conditional views were always creating a view.

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