From 573761dcfa0b17d5cd171f485d538906e1c2321b Mon Sep 17 00:00:00 2001 From: John Estropia Date: Thu, 5 Dec 2024 16:36:47 +0900 Subject: [PATCH] handle more edge cases --- .../Preview/PreviewRegistryWrapper.swift | 30 +++++++++++++++---- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/Sources/StorybookKit/Internals/Preview/PreviewRegistryWrapper.swift b/Sources/StorybookKit/Internals/Preview/PreviewRegistryWrapper.swift index b40983b..c94af2a 100644 --- a/Sources/StorybookKit/Internals/Preview/PreviewRegistryWrapper.swift +++ b/Sources/StorybookKit/Internals/Preview/PreviewRegistryWrapper.swift @@ -20,7 +20,10 @@ struct PreviewRegistryWrapper: Comparable { @MainActor var makeView: (@MainActor () -> any View) { - let preview: FieldReader = .init(try! previewType.makePreview()) + guard let rawPreview = try? previewType.makePreview() else { + return { EmptyView() } + } + let preview: FieldReader = .init(rawPreview) let title: String? = preview["displayName"] let source: FieldReader = preview["source"] switch source.typeName { @@ -48,11 +51,28 @@ struct PreviewRegistryWrapper: Comparable { Text(title) .font(.system(size: 17, weight: .semibold)) } + Text("UIView Preview not supported on iOS 17") + .foregroundStyle(Color.red) + .font(.caption.monospacedDigit()) Text("\(fileID):\(line)") .font(.caption.monospacedDigit()) - Text("UIView Preview not supported (UIKit.UIViewPreviewSource)") + BookSpacer(height: 16) + } + } + + case "UIKit.UIViewControllerPreviewSource": // iOS 17 + // Unsupported due to iOS 17 not supporting casting between non-sendable closure types + return { + VStack { + if let title, !title.isEmpty { + Text(title) + .font(.system(size: 17, weight: .semibold)) + } + Text("UIViewController Preview not supported on iOS 17") .foregroundStyle(Color.red) .font(.caption.monospacedDigit()) + Text("\(fileID):\(line)") + .font(.caption.monospacedDigit()) BookSpacer(height: 16) } } @@ -103,11 +123,11 @@ struct PreviewRegistryWrapper: Comparable { Text(title) .font(.system(size: 17, weight: .semibold)) } - Text("\(fileID):\(line)") - .font(.caption.monospacedDigit()) Text("Failed to load preview (\(sourceTypeName))") .foregroundStyle(Color.red) .font(.caption.monospacedDigit()) + Text("\(fileID):\(line)") + .font(.caption.monospacedDigit()) BookSpacer(height: 16) } } @@ -152,7 +172,7 @@ struct PreviewRegistryWrapper: Comparable { subscript(_ key: String, _ nextKeys: String...) -> T { if nextKeys.isEmpty { - return fields[key] as! T + return fields[key]! as! T } else { return Self.traverse(from: fields[key]!, nextKeys: nextKeys) as! T