Skip to content
This repository has been archived by the owner on Jun 24, 2024. It is now read-only.

Commit

Permalink
Merge pull request #456 from rusik/patch-2
Browse files Browse the repository at this point in the history
Unify code indentation across the article
  • Loading branch information
Jeehut authored Nov 29, 2023
2 parents 702381d + d035b23 commit 7072664
Showing 1 changed file with 51 additions and 51 deletions.
102 changes: 51 additions & 51 deletions content/notes/wwdc23/10252.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ A preview is a snippet of code that makes and configures a view. They are writte

```swift
#Preview {
MyView()
MyView()
}
```

Expand Down Expand Up @@ -60,7 +60,7 @@ Every preview has the same basic shape.
// Anatomy of a preview initializer

#Preview {
Content()
Content()
}
```

Expand All @@ -69,7 +69,7 @@ Start with the Preview macro initializer at the top level of a source file. Then
```swift
// Anatomy of a preview initializer
#Preview("Name", configuration) {
Content()
Content()
}
```
There are two main kinds of previews: views and widgets. Views can come from SwiftUI, UIKit, or AppKit.
Expand All @@ -83,9 +83,9 @@ For SwiftUI, we can just return any view that we are working on. or we can place
```swift
// SwiftUI
#Preview {
List {
CollageView(layout: .twoByTwoGrid)
}
List {
CollageView(layout: .twoByTwoGrid)
}
}
```

Expand All @@ -94,42 +94,42 @@ This is also the place to attach modifiers that provide data through the environ
```swift
// SwiftUI
#Preview {
List {
CollageView(layout: .twoByTwoGrid)
}
.environment(CollageLayoutStore.sample)
List {
CollageView(layout: .twoByTwoGrid)
}
.environment(CollageLayoutStore.sample)
}
```

Previews are kind of like the scenes that we define at the top level of the app. Scenes serve as the entry points to the app. When it comes to configuring the preview, we can give it a name. Previews support passing one or more configuration traits as a variadic argument list.

```swift
#Preview(“2x2 Grid”, traits: .landscapeLeft) {
List {
CollageView(layout: .twoByTwoGrid)
}
.environment(CollageLayoutStore.sample)
List {
CollageView(layout: .twoByTwoGrid)
}
.environment(CollageLayoutStore.sample)
}
```

For example, Instead of a SwiftUI view, just make a view controller, and configure it as needed.
```swift
#Preview {
var controller = SavedCollagesController()
controller.dataSource = CollagesDataStore.sample
controller.layoutMode = .grid
return controller
var controller = SavedCollagesController()
controller.dataSource = CollagesDataStore.sample
controller.layoutMode = .grid
return controller
}
```

Beyond view controllers, we can also preview a UIView or NSView directly.

```swift
#Preview(“Filter View”) {
var view = CollageFilterDisplayView()
view.filter = .bloom(amount: 15.0)
view.imageData =
return view
var view = CollageFilterDisplayView()
view.filter = .bloom(amount: 15.0)
view.imageData =
return view
}
```

Expand Down Expand Up @@ -185,27 +185,27 @@ We add a header for each Section, which is provided in a second trailing closure
We’ll switch tab to the view controller that renders a filter using CoreImage, and switch the canvas back to live mode. In the preview macro, we created a view controller and passed it a sample image.
```swift
#Preview {
let viewController = FilterRenderingViewController ()
if let image = UIImage(named: "sample-001")?.cgImage {
viewController.imageData = image
}
return viewController
let viewController = FilterRenderingViewController ()
if let image = UIImage(named: "sample-001")?.cgImage {
viewController.imageData = image
}
return viewController
}
```
But now we add the code to pass a filter to the view controller.
```swift
#Preview {
let viewController = FilterRenderingViewController ()
if let image = UIImage(named: "sample-001")?.cgImage {
viewController.imageData = image
}
viewController.filter = Filter (
bloomAmount: 1.0,
vignetteAmount: 1.0,
saturationAmount: 0.5
}
return viewController
let viewController = FilterRenderingViewController ()
if let image = UIImage(named: "sample-001")?.cgImage {
viewController.imageData = image
}
viewController.filter = Filter (
bloomAmount: 1.0,
vignetteAmount: 1.0,
saturationAmount: 0.5
}
return viewController
}
```

Expand Down Expand Up @@ -238,15 +238,15 @@ This is where a timeline of specific entries comes in handy. We can craft the ex

```swift
#Preview(as: systemSmall) {
FrameWidget()
} timeline: {
let first = CollageLayout<Void>
.preset_2x3_left3.map { _, _ in Color.gray }
.fillSlice(at: 6, with: [.green, .orange, .cyan])
let second = first.fillslice(at: 1, with: [.blue])

ImageGridEntry(layout: first)
ImageGridEntry(layout: second)
FrameWidget()
} timeline: {
let first = CollageLayout<Void>
.preset_2x3_left3.map { _, _ in Color.gray }
.fillSlice(at: 6, with: [.green, .orange, .cyan])
let second = first.fillslice(at: 1, with: [.blue])

ImageGridEntry(layout: first)
ImageGridEntry(layout: second)
}
```

Expand Down Expand Up @@ -276,11 +276,11 @@ Another example. First, we pass in the attributes we want to use in the initiali
// Previewing widgets
// Live activities
#Preview(as: .dynamicIsland(.compact), PizzaDeliveryAttributes()) {
FoodOrderWidget()
FoodOrderWidget()
} contentStates: {
PizzaState.preparing
PizzaState.baking
PizzaState.outForDelivery
PizzaState.preparing
PizzaState.baking
PizzaState.outForDelivery
}
```

Expand Down

0 comments on commit 7072664

Please sign in to comment.