Skip to content

Commit

Permalink
Gave example for lazy-loaded views.
Browse files Browse the repository at this point in the history
  • Loading branch information
GeorgeElsham committed Jul 26, 2021
1 parent aa4b77c commit c85ddba
Showing 1 changed file with 42 additions and 1 deletion.
43 changes: 42 additions & 1 deletion README.md
Expand Up @@ -8,7 +8,7 @@ Extract SwiftUI views from ViewBuilder content.
Follow the instructions at [Adding Package Dependencies to Your App][1] 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
## Example #1

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

Expand Down Expand Up @@ -58,4 +58,45 @@ DividedVStack {
<img src="https://user-images.githubusercontent.com/40073010/115965850-f43c5d80-a522-11eb-8113-1f73d07fade0.png" width="25%">


## 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

```swift
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

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

### Result

<img src="https://user-images.githubusercontent.com/40073010/127054749-e5d27295-f179-41bd-9517-3b2d0eb4b970.png" width="25%">


[1]: https://developer.apple.com/documentation/swift_packages/adding_package_dependencies_to_your_app#3234996

0 comments on commit c85ddba

Please sign in to comment.