generated from StanfordBDHG/SwiftPackageTemplate
-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add views to presenting row-based layouts (#27)
# Add views to presenting row-based layouts ## ♻️ Current situation & Problem We identified a use case providing a default component to present `ListRow`s that automatically adapt the the current dynamic type size, device orientation and size classes. This PR adds a new `DynamicHStack` that automatically adapts row-based, horizontal content to the current dynamic type size. ## ⚙️ Release Notes * Added `DynamicHStack` for presenting row-based content that adapts to the current dynamic type size. * Added the `ListRow` view to present key-value-based list rows, based on the `DynamicHStack`. ## 📚 Documentation Documentation and code examples were provided. I restructured the landing page a bit to accommodate for those new elements. ## ✅ Testing This PR adds basic support for snapshot testing using baseline images. These allow to unit test view layouts across builds. We rely on the [SnapshotTesting](https://github.com/pointfreeco/swift-snapshot-testing) framework for that. ## 📝 Code of Conduct & Contributing Guidelines By submitting creating this pull request, you agree to follow our [Code of Conduct](https://github.com/StanfordSpezi/.github/blob/main/CODE_OF_CONDUCT.md) and [Contributing Guidelines](https://github.com/StanfordSpezi/.github/blob/main/CONTRIBUTING.md): - [x] I agree to follow the [Code of Conduct](https://github.com/StanfordSpezi/.github/blob/main/CODE_OF_CONDUCT.md) and [Contributing Guidelines](https://github.com/StanfordSpezi/.github/blob/main/CONTRIBUTING.md).
- Loading branch information
Showing
14 changed files
with
419 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
Format: https://www.debian.org/doc/packaging-manuals/copyright-format/1.0/ | ||
|
||
Files: Tests/SpeziViewsTests/__Snapshots__/* | ||
Copyright: 2024 Stanford University and the project authors (see CONTRIBUTORS.md) | ||
License: MIT |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
Sources/SpeziViews/ViewModifier/DeviceOrientationModifier.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// | ||
// This source file is part of the Stanford Spezi open-source project | ||
// | ||
// SPDX-FileCopyrightText: 2024 Stanford University and the project authors (see CONTRIBUTORS.md) | ||
// | ||
// SPDX-License-Identifier: MIT | ||
// | ||
|
||
import SwiftUI | ||
|
||
|
||
struct DeviceOrientationModifier: ViewModifier { | ||
@Binding private var orientation: UIDeviceOrientation | ||
|
||
|
||
init(orientation: Binding<UIDeviceOrientation>) { | ||
self._orientation = orientation | ||
} | ||
|
||
|
||
func body(content: Content) -> some View { | ||
content | ||
.onAppear { | ||
orientation = UIDevice.current.orientation | ||
} | ||
.onReceive(NotificationCenter.default.publisher(for: UIDevice.orientationDidChangeNotification)) { _ in | ||
orientation = UIDevice.current.orientation | ||
} | ||
} | ||
} | ||
|
||
|
||
extension View { | ||
/// Observe changes to the device orientation. | ||
/// | ||
/// Use this modifier to observe changes to the current device orientation. | ||
/// | ||
/// ```swift | ||
/// struct MyView: View { | ||
/// @State private var orientation = UIDevice.current.orientation | ||
/// | ||
/// var body: some View { | ||
/// List { | ||
/// // ... | ||
/// } | ||
/// .observeOrientationChanges($orientation) | ||
/// } | ||
/// } | ||
/// ``` | ||
/// | ||
/// | ||
/// - Parameter orientation: The Binding to your `UIDeviceOrientation` state. | ||
/// - Returns: The modified view that observes device orientation. | ||
public func observeOrientationChanges(_ orientation: Binding<UIDeviceOrientation>) -> some View { | ||
modifier(DeviceOrientationModifier(orientation: orientation)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.