diff --git a/Example/Example/VariadicViewExamples.swift b/Example/Example/VariadicViewExamples.swift index dda3959..3e3b91a 100644 --- a/Example/Example/VariadicViewExamples.swift +++ b/Example/Example/VariadicViewExamples.swift @@ -21,7 +21,9 @@ struct BulletList: View { @ViewBuilder var content: Content var body: some View { - VariadicViewAdapter { content in + VariadicViewAdapter { + content + } content: { source in ForEachSubview(content) { index, subview in HStack(alignment: .firstTextBaseline) { Text("\(index + 1).") @@ -29,8 +31,6 @@ struct BulletList: View { subview } } - } source: { - content } } } @@ -59,9 +59,12 @@ struct PickerView: View { @ViewBuilder var content: Content var body: some View { - VariadicViewAdapter { content in - ForEachSubview(content) { index, subview in + VariadicViewAdapter { + content + } content: { source in + ForEachSubview(source) { index, subview in HStack { + // This works since the ForEach ID is the Fruit (ie Selection) type let isSelected: Bool = selection == subview.id(as: Selection.self) if isSelected { Image(systemName: "checkmark") @@ -74,8 +77,6 @@ struct PickerView: View { } } } - } source: { - content } } } diff --git a/README.md b/README.md index 183155d..ad9a41b 100644 --- a/README.md +++ b/README.md @@ -208,8 +208,8 @@ public struct VariadicViewAdapter: View { @inlinable public init( - @ViewBuilder content: @escaping (VariadicView) -> Content, - @ViewBuilder source: () -> Source + @ViewBuilder source: () -> Source, + @ViewBuilder content: @escaping (VariadicView) -> Content ) } ``` @@ -247,8 +247,10 @@ struct PickerView: View { @ViewBuilder var content: Content var body: some View { - VariadicViewAdapter { content in - ForEachSubview(content) { index, subview in + VariadicViewAdapter { + content + } content: { source in + ForEachSubview(source) { index, subview in HStack { // This works since the ForEach ID is the Fruit (ie Selection) type let isSelected: Bool = selection == subview.id(as: Selection.self) @@ -263,8 +265,6 @@ struct PickerView: View { } } } - } source: { - content } } } diff --git a/Sources/Engine/Sources/VariadicView.swift b/Sources/Engine/Sources/VariadicView.swift index 6cbceee..d006570 100644 --- a/Sources/Engine/Sources/VariadicView.swift +++ b/Sources/Engine/Sources/VariadicView.swift @@ -176,8 +176,25 @@ public struct VariadicViewAdapter: View { @usableFromInline var content: (VariadicView) -> Content + /* + FB12082130: Xcode 14.3 RC regression, type inference no longer works unless + the `Source` parameter is before the `content` + + @inlinable + public init( + @ViewBuilder content: @escaping (VariadicView) -> Content, + @ViewBuilder source: () -> Source + ) { + self.source = source() + self.content = content + } + */ + @inlinable - public init(@ViewBuilder content: @escaping (VariadicView) -> Content, @ViewBuilder source: () -> Source) { + public init( + @ViewBuilder source: () -> Source, + @ViewBuilder content: @escaping (VariadicView) -> Content + ) { self.source = source() self.content = content } @@ -208,70 +225,70 @@ enum PreviewCases: Int, Hashable, CaseIterable { struct VariadicView_Previews: PreviewProvider { static var previews: some View { Group { - VariadicViewAdapter { content in + VariadicViewAdapter { + Text("Line 1").id("1") + Text("Line 2").id("2") + } content: { source in VStack { - ForEachSubview(content) { index, subview in + ForEachSubview(source) { index, subview in Text(subview.id(as: String.self) ?? "nil") } } - } source: { - Text("Line 1").id("1") - Text("Line 2").id("2") } - VariadicViewAdapter { content in + VariadicViewAdapter { + ForEach(PreviewCases.allCases, id: \.self) { + Text($0.rawValue.description) + } + } content: { source in VStack { - ForEachSubview(content) { index, subview in + ForEachSubview(source) { index, subview in Text("\(subview.id(as: PreviewCases.self)?.rawValue ?? -1)") Text(String("\(subview.id)")) } } - } source: { - ForEach(PreviewCases.allCases, id: \.self) { - Text($0.rawValue.description) - } } if #available(iOS 14.0, macOS 11.0, tvOS 14.0, watchOS 7.0, *) { - VariadicViewAdapter { content in + VariadicViewAdapter { + Text("Line 1").tag("1") + Text("Line 2").tag("2") + } content: { source in VStack { - ForEachSubview(content) { index, subview in + ForEachSubview(source) { index, subview in Text(subview.tag(as: String.self) ?? "nil") } } - } source: { - Text("Line 1").tag("1") - Text("Line 2").tag("2") } } - VariadicViewAdapter { content in - VStack { - content - } - } source: { + VariadicViewAdapter { Text("Line 1") Text("Line 2") + } content: { source in + VStack { + source + } } - VariadicViewAdapter { content in - Text(content.children.count.description) - } source: { + VariadicViewAdapter { EmptyView() + } content: { source in + Text(source.children.count.description) } - VariadicViewAdapter { content in - Text(content.children.count.description) - } source: { + VariadicViewAdapter { Text("Line 1") + } content: { source in + Text(source.children.count.description) } - VariadicViewAdapter { content in - Text(content.children.count.description) - } source: { + VariadicViewAdapter { Text("Line 1") Text("Line 2") + } content: { source in + Text(source.children.count.description) } } .padding()