Skip to content

Commit

Permalink
Merge branch 'main' into john/previewRevamp
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnEstropia committed Dec 2, 2024
2 parents 3aa94b1 + b0538f7 commit 1dcaad5
Show file tree
Hide file tree
Showing 13 changed files with 34 additions and 45 deletions.
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,5 @@ let package = Package(
]
),
],
swiftLanguageVersions: [.v5]
swiftLanguageModes: [.v6, .v5]
)
19 changes: 1 addition & 18 deletions Sources/StorybookKit/Internals/TargetViewControllerKey.swift
Original file line number Diff line number Diff line change
@@ -1,22 +1,5 @@
import SwiftUI

public enum _ViewControllerKey: EnvironmentKey {
public static var defaultValue: UIViewController?
}

enum TestKey: EnvironmentKey {
static var defaultValue: String { "default" }
}


extension EnvironmentValues {
var _targetViewController: UIViewController? {
get { self[_ViewControllerKey.self] }
set { self[_ViewControllerKey.self] = newValue }
}

var test: String {
get { self[TestKey.self] }
set { self[TestKey.self] = newValue }
}
@Entry public var storybook_targetViewController: UIViewController?
}
1 change: 1 addition & 0 deletions Sources/StorybookKit/Primitives/Book.swift
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ public struct FolderBuilder {

public typealias Element = Book.Node

@MainActor
public static func buildExpression<Provider: BookProvider>(_ expression: Provider.Type) -> [FolderBuilder.Element] {
return [.page(expression.bookBody)]
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/StorybookKit/Primitives/BookAction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import SwiftUI

public struct BookAction: BookView {

@Environment(\._targetViewController) var targetViewController
@Environment(\.storybook_targetViewController) var targetViewController

public let declarationIdentifier: DeclarationIdentifier

Expand Down
10 changes: 5 additions & 5 deletions Sources/StorybookKit/Primitives/BookPage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import Foundation
import SwiftUI
import ResultBuilderKit

public struct DeclarationIdentifier: Hashable, Codable {
public struct DeclarationIdentifier: Hashable, Codable, Sendable {

public let index: Int

Expand All @@ -37,7 +37,7 @@ public struct DeclarationIdentifier: Hashable, Codable {
}

private let _lock = NSLock()
private var _counter: Int = 0
private nonisolated(unsafe) var _counter: Int = 0
private func issueUniqueNumber() -> Int {
_lock.lock()
defer {
Expand All @@ -52,21 +52,21 @@ public struct BookPage: BookView, Identifiable {

@Environment(\.bookContext) var context

public var id: DeclarationIdentifier {
public nonisolated var id: DeclarationIdentifier {
declarationIdentifier
}

public let title: String
public let destination: AnyView
public let declarationIdentifier: DeclarationIdentifier
public nonisolated let declarationIdentifier: DeclarationIdentifier
private let file: StaticString
private let line: UInt

public init<Destination: View>(
_ file: StaticString = #fileID,
_ line: UInt = #line,
title: String,
@ViewBuilder destination: () -> Destination
@ViewBuilder destination: @MainActor () -> Destination
) {
self.file = file
self.line = line
Expand Down
5 changes: 1 addition & 4 deletions Sources/StorybookKit/Primitives/BookPresent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ import SwiftUI
/// A component descriptor that just displays UI-Component
public struct BookPresent: BookView {

@Environment(\._targetViewController) private var targetViewController
@Environment(\.test) var test
@Environment(\.storybook_targetViewController) private var targetViewController

public let declarationIdentifier: DeclarationIdentifier
public let presentedViewControllerBlock: @MainActor () -> UIViewController
Expand All @@ -44,8 +43,6 @@ public struct BookPresent: BookView {
public var body: some View {
Button(title) {
let viewController = presentedViewControllerBlock()
print(test)
print(targetViewController)
targetViewController?.present(viewController, animated: true)
}
}
Expand Down
16 changes: 3 additions & 13 deletions Sources/StorybookKit/Primitives/BookProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,12 @@ import SwiftUI

@_alwaysEmitConformanceMetadata
public protocol BookProvider {
@MainActor
static var bookBody: BookPage { get }
}

private enum BookContextKey: EnvironmentKey {
static var defaultValue: BookStore?
}

extension EnvironmentValues {

public var bookContext: BookStore? {
get {
self[BookContextKey.self]
}
set {
self[BookContextKey.self] = newValue
}
}

@Entry public var bookContext: BookStore?

}
4 changes: 2 additions & 2 deletions Sources/StorybookKit/Primitives/BookPush.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import SwiftUI
/// A component descriptor that previewing with push presentation.
public struct BookPush: BookView {

@Environment(\._targetViewController) var targetViewController
@Environment(\.storybook_targetViewController) var targetViewController

public let pushingViewControllerBlock: @MainActor () -> UIViewController
public let declarationIdentifier: DeclarationIdentifier
Expand All @@ -44,7 +44,7 @@ public struct BookPush: BookView {

NavigationLink(title, destination: {
_ViewControllerHost(instantiate: pushingViewControllerBlock)
.environment(\._targetViewController, targetViewController)
.environment(\.storybook_targetViewController, targetViewController)
})

}
Expand Down
2 changes: 1 addition & 1 deletion Sources/StorybookKit/StorybookDisplayRootView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ final class _ViewController<Content: View>: UIViewController {
let hosting = UIHostingController(
rootView:
content
.environment(\._targetViewController, self)
.environment(\.storybook_targetViewController, self)
)

addChild(hosting)
Expand Down
1 change: 1 addition & 0 deletions Sources/StorybookMacrosPlugin/StorybookPageMacro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public struct StorybookPageMacro: DeclarationMacro {
.init(
stringLiteral: """
enum \(enumName): BookProvider {
@MainActor
static var bookBody: BookPage {
.init(
title: \(title),
Expand Down
1 change: 1 addition & 0 deletions Sources/StorybookMacrosPlugin/StorybookPreviewMacro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public struct StorybookPreviewMacro: ExpressionMacro {
stringLiteral: """
{
enum \(enumName): BookProvider {
@MainActor
static var bookBody: BookPage {
.init(
title: \(title),
Expand Down
8 changes: 8 additions & 0 deletions Sources/StorybookMacrosTests/StorybookPageTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ final class StorybookPageTests: XCTestCase {
} expansion: {
"""
enum __macro_local_20__🤖🛠️_StorybookMagic_fMu_: BookProvider {
@MainActor
static var bookBody: BookPage {
.init(
title: _typeName(UIView.self),
Expand Down Expand Up @@ -74,6 +75,7 @@ final class StorybookPageTests: XCTestCase {
} expansion: {
"""
enum __macro_local_20__🤖🛠️_StorybookMagic_fMu_: BookProvider {
@MainActor
static var bookBody: BookPage {
.init(
title: "Path1.Path2.Title",
Expand Down Expand Up @@ -105,6 +107,7 @@ final class StorybookPageTests: XCTestCase {
"""
enum Namespace1 { enum Namespace2 { enum Namespace3 { class TestableView: UIView {} } } }
enum __macro_local_20__🤖🛠️_StorybookMagic_fMu_: BookProvider {
@MainActor
static var bookBody: BookPage {
.init(
title: _typeName(target: Namespace1.Namespace2.Namespace3.TestableView.self),
Expand Down Expand Up @@ -132,6 +135,7 @@ final class StorybookPageTests: XCTestCase {
} expansion: {
"""
enum __macro_local_20__🤖🛠️_StorybookMagic_fMu_: BookProvider {
@MainActor
static var bookBody: BookPage {
.init(
title: "Path1.Path2.Title",
Expand Down Expand Up @@ -163,6 +167,7 @@ final class StorybookPageTests: XCTestCase {
} expansion: {
"""
enum __macro_local_20__🤖🛠️_StorybookMagic_fMu_: BookProvider {
@MainActor
static var bookBody: BookPage {
.init(
title: _typeName(UIView.self),
Expand Down Expand Up @@ -193,6 +198,7 @@ final class StorybookPageTests: XCTestCase {
} expansion: {
"""
enum __macro_local_20__🤖🛠️_StorybookMagic_fMu_: BookProvider {
@MainActor
static var bookBody: BookPage {
.init(
title: "Path1.Path2.Title",
Expand Down Expand Up @@ -227,6 +233,7 @@ final class StorybookPageTests: XCTestCase {
"""
enum Namespace1 { enum Namespace2 { enum Namespace3 { class TestableView: UIView {} } } }
enum __macro_local_20__🤖🛠️_StorybookMagic_fMu_: BookProvider {
@MainActor
static var bookBody: BookPage {
.init(
title: _typeName(
Expand Down Expand Up @@ -258,6 +265,7 @@ final class StorybookPageTests: XCTestCase {
} expansion: {
"""
enum __macro_local_20__🤖🛠️_StorybookMagic_fMu_: BookProvider {
@MainActor
static var bookBody: BookPage {
.init(
title: "Path1.Path2.Title",
Expand Down
8 changes: 8 additions & 0 deletions Sources/StorybookMacrosTests/StorybookPreviewTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ final class StorybookPreviewTests: XCTestCase {
#Preview("Some title") {
{
enum __macro_local_20__🤖🛠️_StorybookMagic_fMu_: BookProvider {
@MainActor
static var bookBody: BookPage {
.init(
title: _typeName(UIView.self),
Expand Down Expand Up @@ -85,6 +86,7 @@ final class StorybookPreviewTests: XCTestCase {
#Preview {
{
enum __macro_local_20__🤖🛠️_StorybookMagic_fMu_: BookProvider {
@MainActor
static var bookBody: BookPage {
.init(
title: "Path1.Path2.Title",
Expand Down Expand Up @@ -123,6 +125,7 @@ final class StorybookPreviewTests: XCTestCase {
#Preview {
{
enum __macro_local_20__🤖🛠️_StorybookMagic_fMu_: BookProvider {
@MainActor
static var bookBody: BookPage {
.init(
title: _typeName(target: Namespace1.Namespace2.Namespace3.TestableView.self),
Expand Down Expand Up @@ -157,6 +160,7 @@ final class StorybookPreviewTests: XCTestCase {
#Preview {
{
enum __macro_local_20__🤖🛠️_StorybookMagic_fMu_: BookProvider {
@MainActor
static var bookBody: BookPage {
.init(
title: "Path1.Path2.Title",
Expand Down Expand Up @@ -195,6 +199,7 @@ final class StorybookPreviewTests: XCTestCase {
#Preview {
{
enum __macro_local_20__🤖🛠️_StorybookMagic_fMu_: BookProvider {
@MainActor
static var bookBody: BookPage {
.init(
title: _typeName(UIView.self),
Expand Down Expand Up @@ -232,6 +237,7 @@ final class StorybookPreviewTests: XCTestCase {
#Preview {
{
enum __macro_local_20__🤖🛠️_StorybookMagic_fMu_: BookProvider {
@MainActor
static var bookBody: BookPage {
.init(
title: "Path1.Path2.Title",
Expand Down Expand Up @@ -273,6 +279,7 @@ final class StorybookPreviewTests: XCTestCase {
#Preview {
{
enum __macro_local_20__🤖🛠️_StorybookMagic_fMu_: BookProvider {
@MainActor
static var bookBody: BookPage {
.init(
title: _typeName(
Expand Down Expand Up @@ -311,6 +318,7 @@ final class StorybookPreviewTests: XCTestCase {
#Preview {
{
enum __macro_local_20__🤖🛠️_StorybookMagic_fMu_: BookProvider {
@MainActor
static var bookBody: BookPage {
.init(
title: "Path1.Path2.Title",
Expand Down

0 comments on commit 1dcaad5

Please sign in to comment.