fix SDKView logic, hide the view when the content is empty

This commit is contained in:
Duong Thai 2023-10-14 15:01:00 +07:00
parent 2f7d45c67a
commit 2fdc06031c

View file

@ -10,19 +10,28 @@ import SwiftUI
import struct XCModel.SDKs import struct XCModel.SDKs
struct SDKsView: View { struct SDKsView: View {
let sdks: SDKs? let content: String
var body: some View { var body: some View {
if let sdks = sdks { if content.isEmpty {
EmptyView()
} else {
VStack(alignment: .leading) { VStack(alignment: .leading) {
Text("SDKs").font(.headline) Text("SDKs").font(.headline)
Text(Self.content(from: sdks)).font(.subheadline) Text(content).font(.subheadline)
} }
} else {
EmptyView()
} }
} }
init(sdks: SDKs?) {
guard let sdks = sdks else {
self.content = ""
return
}
let content = Self.content(from: sdks)
self.content = content
}
static private func content(from sdks: SDKs) -> String { static private func content(from sdks: SDKs) -> String {
let content: String = [ let content: String = [
("macOS", sdks.macOS), ("macOS", sdks.macOS),
@ -41,6 +50,7 @@ struct SDKsView: View {
// description for each type of compilers // description for each type of compilers
return "\($0.0): \(numbers.joined(separator: ", "))" return "\($0.0): \(numbers.joined(separator: ", "))"
}.joined(separator: "\n") }.joined(separator: "\n")
.trimmingCharacters(in: .whitespaces)
return content return content
} }
@ -68,7 +78,7 @@ private struct WrapperView: View {
SDKsView(sdks: sdks).border(.red) SDKsView(sdks: sdks).border(.red)
Spacer() Spacer()
Toggle(isOn: $isNil) { Toggle(isOn: $isNil) {
Text("Is Nil?") Text("Empty Content?")
} }
} }
.frame(width: 200, height: 100) .frame(width: 200, height: 100)