Skip to content

Commit

Permalink
ViewBuilder buildIf fix (#162)
Browse files Browse the repository at this point in the history
* ViewBuilder buildIf fix

* Add FIXME
  • Loading branch information
carson-katri authored Jul 6, 2020
1 parent e2966fe commit aedab8c
Showing 1 changed file with 30 additions and 8 deletions.
38 changes: 30 additions & 8 deletions Sources/TokamakCore/Views/ViewBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,35 @@ public struct EmptyView: View {
}

// swiftlint:disable:next type_name
public enum _ConditionalContent<TrueBranch, FalseBranch>: View
where TrueBranch: View, FalseBranch: View {
case trueBranch(TrueBranch)
case falseBranch(FalseBranch)
public struct _ConditionalContent<TrueContent, FalseContent>: View
where TrueContent: View, FalseContent: View {
enum Storage {
case trueContent(TrueContent)
case falseContent(FalseContent)
}

public var body: Never {
neverBody("_ConditionalContent")
let storage: Storage

@ViewBuilder
public var body: some View {
switch storage {
case let .trueContent(view):
view
case let .falseContent(view):
view
}
}
}

// FIXME: Remove type erasure when https://github.com/swiftwasm/swift/issues/1379
// is resolved
extension Optional: View where Wrapped: View {
public var body: AnyView {
if let view = self {
return AnyView(view)
} else {
return AnyView(EmptyView())
}
}
}

Expand All @@ -51,13 +73,13 @@ public enum _ConditionalContent<TrueBranch, FalseBranch>: View
public static func buildEither<TrueContent, FalseContent>(
first: TrueContent
) -> _ConditionalContent<TrueContent, FalseContent> where TrueContent: View, FalseContent: View {
.trueBranch(first)
.init(storage: .trueContent(first))
}

public static func buildEither<TrueContent, FalseContent>(
second: FalseContent
) -> _ConditionalContent<TrueContent, FalseContent> where TrueContent: View, FalseContent: View {
.falseBranch(second)
.init(storage: .falseContent(second))
}
}

Expand Down

0 comments on commit aedab8c

Please sign in to comment.