Skip to content

Commit

Permalink
Add initializer variant (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
muukii committed Apr 16, 2024
1 parent 0cce1f1 commit fcb306f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 11 deletions.
4 changes: 0 additions & 4 deletions Sources/StorybookKit/Primitives/BookProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ public protocol BookProvider {
static var bookBody: BookPage { get }
}

public protocol BookType: View {

}

private enum BookContextKey: EnvironmentKey {
static var defaultValue: BookStore?
}
Expand Down
41 changes: 34 additions & 7 deletions Sources/StorybookKit/StorybookDisplayRootView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,34 @@ public struct StorybookDisplayRootView: View {
public var body: some View {

_ViewControllerHost {
let controller = _ViewController(book: book)
let controller = _ViewController(content: book)
return controller
}
.ignoresSafeArea()

}
}

struct BookContainer: BookType {
public struct BookActionHosting<Content: View>: View {

private let content: Content

public init(_ content: Content) {
self.content = content
}

public var body: some View {

_ViewControllerHost {
let controller = _ViewController(content: content)
return controller
}
.ignoresSafeArea()

}
}

struct BookContainer: View {

@ObservedObject var store: BookStore
@State var isSearching: Bool = false
Expand Down Expand Up @@ -125,12 +144,12 @@ struct BookContainer: BookType {

}

final class _ViewController<Book: BookType>: UIViewController {
final class _ViewController<Content: View>: UIViewController {

private let book: Book
private let content: Content

init(book: Book) {
self.book = book
init(content: Content) {
self.content = content
super.init(nibName: nil, bundle: nil)
}

Expand All @@ -143,7 +162,7 @@ final class _ViewController<Book: BookType>: UIViewController {

let hosting = UIHostingController(
rootView:
book
content
.environment(\._targetViewController, self)
)

Expand All @@ -163,3 +182,11 @@ final class _ViewController<Book: BookType>: UIViewController {
}

}

#if DEBUG

#Preview {
BookActionHosting(BookAction(title: "Hello", action: { vc in }))
}

#endif

0 comments on commit fcb306f

Please sign in to comment.