Skip to content

Commit

Permalink
Leif/preview improvements (#98)
Browse files Browse the repository at this point in the history
* Add improvements for SwiftUI previews

* Improve preview API
  • Loading branch information
0xLeif authored Mar 8, 2024
1 parent eeab0d2 commit fbffe58
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
29 changes: 29 additions & 0 deletions Sources/AppState/Application/Application+public.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import Foundation
#if !os(Linux) && !os(Windows)
import SwiftUI
#endif

// MARK: Application Functions

Expand Down Expand Up @@ -293,6 +296,32 @@ public extension Application {
}
}

#if !os(Linux) && !os(Windows)
// MARK: - SwiftUI Preview Dependency Functions

public extension Application {
/**
Use in SwiftUI previews to inject mock dependencies into the content view.
- Parameters:
- dependencyOverrides: An array of `Application.override(_, with:)` outputs that you want to use for the preview.
- content: A closure that returns the View you want to preview.
- Returns: A View with the overridden dependencies applied.
*/
@ViewBuilder
static func preview<Content: View>(
_ dependencyOverrides: DependencyOverride...,
content: @escaping () -> Content
) -> some View {
ApplicationPreview(
dependencyOverrides: dependencyOverrides,
content: content
)
}
}
#endif

// MARK: - State Functions

public extension Application {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#if !os(Linux) && !os(Windows)
import SwiftUI

extension Application {
struct ApplicationPreview<Content: View>: View {
let dependencyOverrides: [DependencyOverride]
let content: () -> Content

init(
dependencyOverrides: [DependencyOverride],
content: @escaping () -> Content
) {
self.dependencyOverrides = dependencyOverrides
self.content = content
}

var body: some View {
content()
}
}
}
#endif

0 comments on commit fbffe58

Please sign in to comment.