Skip to content

Commit

Permalink
Rethink naming
Browse files Browse the repository at this point in the history
  • Loading branch information
Supereg committed Jan 30, 2024
1 parent 4cfad4d commit e4aba92
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 22 deletions.
28 changes: 14 additions & 14 deletions Sources/SpeziViews/Views/Layout/DynamicHStack.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
import SwiftUI


/// Access the alignment of a dynamic layout component.
/// Access the dynamic layout of a child view.
///
/// Refer to the documentation of ``DynamicHStack`` on how to retrieve the current layout.
public enum Alignment {
public enum DynamicLayout {
/// The layout is horizontal.
case horizontal
/// The layout is vertical.
Expand All @@ -31,12 +31,12 @@ public enum Alignment {
/// of the view. This check won't apply if the [`horizontalSizeClass`](https://developer.apple.com/documentation/swiftui/environmentvalues/horizontalsizeclass)
/// is `regular` (not `compact`) or if the device is in landscape orientation (refer to [`UIDeviceOrientation`](https://developer.apple.com/documentation/uikit/uideviceorientation)).
///
/// ### Checking the current alignment
/// ### Checking the current layout
///
/// You can retrieve the current `DynamicHStack` alignment using the ``Alignment`` preference key.
/// You can retrieve the current `DynamicHStack` layout using the ``DynamicLayout`` preference key.
/// This is useful if you want to conditionally change the layout of your View depending on the current
/// layout of your content (e.g., only render a `Spacer` if the view is horizontally aligned).
/// You can retrieve the current alignment using the [`onPreferenceChange(_:perform:)`](https://developer.apple.com/documentation/swiftui/view/onpreferencechange(_:perform:))
/// You can retrieve the current layout using the [`onPreferenceChange(_:perform:)`](https://developer.apple.com/documentation/swiftui/view/onpreferencechange(_:perform:))
/// modifier.
///
/// Below is a short code example that demonstrates this capability.
Expand All @@ -51,30 +51,30 @@ public enum Alignment {
/// private let city: LocalizedStringResource
/// private let temperature: Int
///
/// @State var currentAlignment: Alignment?
/// @State var currentLayout: DynamicLayout?
///
/// var body: some View {
/// DynamicHStack {
/// Text(city)
///
/// if alignment == .horizontal {
/// if currentLayout == .horizontal {
/// Spacer()
/// }
///
/// Text(verbatim: "\(temperature) °C")
/// .foregroundColor(.secondary)
/// }
/// .onPreferenceChange(Alignment.self) { alignment in
/// currentAlignment = alignment
/// .onPreferenceChange(DynamicLayout.self) { layout in
/// currentLayout = layout
/// }
/// }
/// }
/// ```
///
/// ## Topics
///
/// ### Checking the current alignment
/// - ``Alignment``
/// ### Checking the current layout
/// - ``DynamicLayout``
public struct DynamicHStack<Content: View>: View {
private let realignAfter: DynamicTypeSize
private let horizontalAlignment: VerticalAlignment
Expand All @@ -96,12 +96,12 @@ public struct DynamicHStack<Content: View>: View {
HStack(alignment: horizontalAlignment, spacing: spacing) {
content
}
.preference(key: Alignment.self, value: .horizontal)
.preference(key: DynamicLayout.self, value: .horizontal)
} else {
VStack(alignment: verticalAlignment, spacing: spacing) {
content
}
.preference(key: Alignment.self, value: .vertical)
. preference(key: DynamicLayout.self, value: .vertical)
}
}
.observeOrientationChanges($orientation)
Expand Down Expand Up @@ -131,7 +131,7 @@ public struct DynamicHStack<Content: View>: View {
}


extension Alignment: PreferenceKey {
extension DynamicLayout: PreferenceKey {
public typealias Value = Self?

public static func reduce(value: inout Self?, nextValue: () -> Self?) {
Expand Down
15 changes: 7 additions & 8 deletions Sources/SpeziViews/Views/List/ListRow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,33 +37,32 @@ public struct ListRow<Label: View, Content: View>: View {
private let label: Label
private let content: Content

@State private var alignment: Alignment?
@State private var layout: DynamicLayout?


public var body: some View {
HStack {
DynamicHStack {
label
.foregroundColor(.primary)
.lineLimit(alignment == .horizontal ? 1 : nil)
.lineLimit(layout == .horizontal ? 1 : nil)

if alignment == .horizontal {
if layout == .horizontal {
Spacer()
}

content
.lineLimit(alignment == .horizontal ? 1 : nil)
.lineLimit(layout == .horizontal ? 1 : nil)
.layoutPriority(1)
.foregroundColor(.secondary)
}

if alignment == .vertical {
if layout == .vertical {
Spacer()
}
}
// .accessibilityElement(children: .combine)
.onPreferenceChange(Alignment.self) { value in
alignment = value
.onPreferenceChange(DynamicLayout.self) { value in
layout = value
}
}

Expand Down

0 comments on commit e4aba92

Please sign in to comment.