Skip to content

Commit

Permalink
Merge pull request #633 from kiwicom/632-improve-emptyview-detection
Browse files Browse the repository at this point in the history
Improve `EmptyView` detection
  • Loading branch information
PavelHolec authored Jul 19, 2023
2 parents d6131c4 + 8971e10 commit 7f7de37
Show file tree
Hide file tree
Showing 15 changed files with 77 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Sources/Orbit/Components/Badge.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import SwiftUI
/// They can be updated when a status changes, but they should not be actionable.
///
/// - Note: [Orbit definition](https://orbit.kiwi/components/badge/)
public struct Badge<LeadingIcon: View, TrailingIcon: View>: View {
public struct Badge<LeadingIcon: View, TrailingIcon: View>: View, PotentiallyEmptyView {

@Environment(\.status) private var status
@Environment(\.sizeCategory) private var sizeCategory
Expand Down
2 changes: 1 addition & 1 deletion Sources/Orbit/Components/BadgeList.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import SwiftUI
/// The items in the list should all be static information, *not* actionable.
///
/// - Note: [Orbit definition](https://orbit.kiwi/components/information/badgelist/)
public struct BadgeList<Icon: View, Content: View>: View {
public struct BadgeList<Icon: View, Content: View>: View, PotentiallyEmptyView {

@Environment(\.status) private var status
@Environment(\.textAccentColor) private var textAccentColor
Expand Down
2 changes: 1 addition & 1 deletion Sources/Orbit/Components/Button.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import SwiftUI
///
/// - Note: [Orbit definition](https://orbit.kiwi/components/button/)
/// - Important: Component expands horizontally unless prevented by `fixedSize` or `idealSize` modifier.
public struct Button<LeadingIcon: View, TrailingIcon: View>: View {
public struct Button<LeadingIcon: View, TrailingIcon: View>: View, PotentiallyEmptyView {

@Environment(\.suppressButtonStyle) private var suppressButtonStyle

Expand Down
2 changes: 1 addition & 1 deletion Sources/Orbit/Components/ButtonLink.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import SwiftUI
/// Displays a single, less important action a user can take.
///
/// - Note: [Orbit definition](https://orbit.kiwi/components/buttonlink/)
public struct ButtonLink<LeadingIcon: View, TrailingIcon: View>: View {
public struct ButtonLink<LeadingIcon: View, TrailingIcon: View>: View, PotentiallyEmptyView {

@Environment(\.suppressButtonStyle) var suppressButtonStyle

Expand Down
6 changes: 5 additions & 1 deletion Sources/Orbit/Components/CountryFlag.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import SwiftUI
/// ```
///
/// - Note: [Orbit definition](https://orbit.kiwi/components/countryflag/)
public struct CountryFlag: View {
public struct CountryFlag: View, PotentiallyEmptyView {

@Environment(\.textSize) var textSize
@Environment(\.iconSize) var iconSize
Expand Down Expand Up @@ -51,6 +51,10 @@ public struct CountryFlag: View {
var size: CGFloat {
(iconSize ?? textSize.map(Icon.Size.fromTextSize(size:)) ?? Icon.Size.normal.value) * sizeCategory.ratio
}

var isEmpty: Bool {
countryCode == nil
}
}

// MARK: - Inits
Expand Down
2 changes: 1 addition & 1 deletion Sources/Orbit/Components/Icon.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import SwiftUI
/// ```
///
/// - Note: [Orbit definition](https://orbit.kiwi/components/icon/)
public struct Icon: View, TextBuildable {
public struct Icon: View, TextBuildable, PotentiallyEmptyView {

/// Approximate size ratio between SF Symbol and Orbit icon symbol.
public static let sfSymbolToOrbitSymbolSizeRatio: CGFloat = 0.75
Expand Down
4 changes: 2 additions & 2 deletions Sources/Orbit/Components/ListChoice.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import SwiftUI
///
/// - Note: [Orbit definition](https://orbit.kiwi/components/listchoice/)
/// - Important: Component expands horizontally unless prevented by `fixedSize` or `idealSize` modifier.
public struct ListChoice<Header: View, Icon: View, Content: View>: View {
public struct ListChoice<Header: View, Icon: View, Content: View>: View, PotentiallyEmptyView {

public let verticalPadding: CGFloat = .small // = 45 height @ normal size

Expand Down Expand Up @@ -148,7 +148,7 @@ public struct ListChoice<Header: View, Icon: View, Content: View>: View {
}

var isEmpty: Bool {
isHeaderEmpty && header is EmptyView && content is EmptyView && disclosure == .none
isHeaderEmpty && header.isEmpty && content.isEmpty && disclosure == .none
}

var isHeaderEmpty: Bool {
Expand Down
41 changes: 41 additions & 0 deletions Sources/Orbit/Components/Select.swift
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ struct SelectPreviews: PreviewProvider {
static var previews: some View {
PreviewWrapper {
standalone
customContent
idealSize
sizing
styles
Expand All @@ -197,6 +198,9 @@ struct SelectPreviews: PreviewProvider {

static var standalone: some View {
VStack(spacing: .medium) {
Select(InputFieldPreviews.label, value: InputFieldPreviews.value) {
// No action
}
Select(InputFieldPreviews.label, prefix: .grid, value: InputFieldPreviews.value) {
// No action
}
Expand All @@ -214,6 +218,43 @@ struct SelectPreviews: PreviewProvider {
.previewDisplayName()
}

static var customContent: some View {
VStack(spacing: .medium) {
Select(value: "Value with a very very very very very long value..") {
// No action
} prefix: {
EmptyView()
} suffix: {
EmptyView()
}

Select(value: "Value with a very very very very very long value..") {
// No action
} prefix: {
CountryFlag("")
} suffix: {
CountryFlag("")
}

// FIXME: conditional content EmptyView
StateWrapper(false) { state in
Select(value: "Value with a very very very very very long value..") {
state.wrappedValue.toggle()
} prefix: {
if state.wrappedValue {
CountryFlag("us")
}
} suffix: {
if state.wrappedValue {
CountryFlag("us")
}
}
}
}
.padding(.medium)
.previewDisplayName()
}

static var idealSize: some View {
VStack(spacing: .medium) {
Select("Ideal size", prefix: .grid, value: InputFieldPreviews.value, action: {})
Expand Down
2 changes: 1 addition & 1 deletion Sources/Orbit/Components/Tag.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import SwiftUI
///
/// - Note: [Orbit definition](https://orbit.kiwi/components/tag/)
/// - Important: Component can expand horizontally by using `idealSize` modifier.
public struct Tag<Icon: View>: View {
public struct Tag<Icon: View>: View, PotentiallyEmptyView {

@Environment(\.idealSize) private var idealSize
@Environment(\.textColor) private var textColor
Expand Down
2 changes: 1 addition & 1 deletion Sources/Orbit/Components/Text.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import SwiftUI
///
/// - Note: [Orbit definition](https://orbit.kiwi/components/text/)
/// - Important: Component has fixed vertical size. When the content is empty, the component results in `EmptyView`.
public struct Text: View, FormattedTextBuildable {
public struct Text: View, FormattedTextBuildable, PotentiallyEmptyView {

@Environment(\.multilineTextAlignment) private var multilineTextAlignment
@Environment(\.lineSpacing) private var lineSpacing
Expand Down
4 changes: 2 additions & 2 deletions Sources/Orbit/Components/TileGroup.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import SwiftUI
/// ```
///
/// - Note: [Orbit definition](https://orbit.kiwi/components/tilegroup/)
public struct TileGroup<Content: View>: View {
public struct TileGroup<Content: View>: View, PotentiallyEmptyView {

@ViewBuilder let content: Content

Expand All @@ -38,7 +38,7 @@ public struct TileGroup<Content: View>: View {
}

var isEmpty: Bool {
content is EmptyView
content.isEmpty
}

/// Creates Orbit TileGroup component as a wrapper for Tile content.
Expand Down
4 changes: 2 additions & 2 deletions Sources/Orbit/Components/Timeline.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import SwiftUI
/// - ``TimelineItem``
///
/// - Note: [Orbit definition](https://orbit.kiwi/components/progress-indicators/timeline/)
public struct Timeline<Content: View>: View {
public struct Timeline<Content: View>: View, PotentiallyEmptyView {

@Environment(\.sizeCategory) var sizeCategory
@ViewBuilder let content: Content
Expand Down Expand Up @@ -35,7 +35,7 @@ public struct Timeline<Content: View>: View {
}

var isEmpty: Bool {
content is EmptyView
content.isEmpty
}

func progressLineHeight(
Expand Down
8 changes: 8 additions & 0 deletions Sources/Orbit/Support/Forms/InputContent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,14 @@ struct InputContentPreviews: PreviewProvider {
// No action
}
}

InputContent(state: .default) {
headerPlaceholder
} prefix: {
CountryFlag("")
} suffix: {
EmptyView()
}
}
.padding(.medium)
.previewDisplayName()
Expand Down
6 changes: 5 additions & 1 deletion Sources/Orbit/Support/Layout/IsEmpty.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import SwiftUI
extension View {

var isEmpty: Bool {
self is EmptyView || (self as? Orbit.Icon)?.isEmpty == true || (self as? Orbit.Text)?.isEmpty == true
switch self {
case let view as PotentiallyEmptyView: return view.isEmpty
case is EmptyView: return true
default: return false
}
}
}
5 changes: 5 additions & 0 deletions Sources/Orbit/Support/Layout/PotentiallyEmptyView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

/// A type that represents views that can optionally result in `EmptyView`.
protocol PotentiallyEmptyView {
var isEmpty: Bool { get }
}

0 comments on commit 7f7de37

Please sign in to comment.