Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ViewBuilder buildIf fix #162

Merged
merged 2 commits into from
Jul 6, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 28 additions & 8 deletions Sources/TokamakCore/Views/ViewBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,33 @@ 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
}
}
}

extension Optional: View where Wrapped: View {
public var body: AnyView {
carson-katri marked this conversation as resolved.
Show resolved Hide resolved
if let view = self {
return AnyView(view)
} else {
return AnyView(EmptyView())
}
}
}

Expand All @@ -51,13 +71,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