generated from StanfordBDHG/SwiftPackageTemplate
-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce handling for identifiable onboarding views (#45)
# Introduce handling for identifiable onboarding views ## ♻️ Current situation & Problem Closes #43. Previously, the `OnboardingStack` identified views by their type name for ordered navigation operations. By introducing the protocol `OnboardingIdentifiableView` we require views to set an `id: String` allowing for multiple views of the same type to be part of an `OnboardingStack`. ## ⚙️ Release Notes * `OnboardingIdentifiableView` (new): * new `protocol` `OnboardingIdentifiableView` conforming to [`View`](https://developer.apple.com/documentation/swiftui/view) and [`Identifiable`](https://developer.apple.com/documentation/swift/identifiable) * add default implementation for `id` that uses the view's type name as its identifier * `OnboardingNavigationPath` (changed): * add new `func append(identifiableView: any OnboardingIdentifiableView)` that allows navigating to any view that conforms to the new `OnboardingIdentifiableView` protocol Example Usage: ```swift struct TestView1: OnboardingIdentifiableView { // id = "TestView1" if not explicitly specified var body: some View {...} } struct TestView2: OnboardingIdentifiableView { var id: String = "my-custom-identifier" var body: some View {...} } struct OnboardingFlow: View { var view: some View { OnboardingStack { TestView2() TestView1() TestView2(id: "second-test-view-identifier") } } } ``` ## 📚 Documentation Documentation added to all new public interface members. ## ✅ Testing * add new UI Test `testIdentifiableViews` that clicks through a set of views with both the default view id and custom view identifiers ## 📝 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). --------- Co-authored-by: Paul Schmiedmayer <[email protected]>
- Loading branch information
1 parent
e699133
commit 8d6dda3
Showing
11 changed files
with
381 additions
and
133 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
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
54 changes: 54 additions & 0 deletions
54
Sources/SpeziOnboarding/OnboardingIdentifiableViewModifier.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,54 @@ | ||
// | ||
// 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 | ||
|
||
|
||
private struct OnboardingIdentifiableViewModifier<ID>: ViewModifier, Identifiable where ID: Hashable { | ||
let id: ID | ||
|
||
func body(content: Content) -> some View { content } | ||
} | ||
|
||
|
||
extension View { | ||
/// Assign a unique identifier to a ``SwiftUI/View`` appearing in an ``OnboardingStack``. | ||
/// | ||
/// A `ViewModifier` assigning an identifier to the `View` it is applied to. | ||
/// When applying this modifier repeatedly, the outermost ``SwiftUI/View/onboardingIdentifier(_:)`` counts. | ||
/// | ||
/// - Note: This `ViewModifier` should only be used to identify `View`s of the same type within an ``OnboardingStack``. | ||
/// | ||
/// - Parameters: | ||
/// - identifier: The `Hashable` identifier given to the view. | ||
/// | ||
/// ```swift | ||
/// struct Onboarding: View { | ||
/// @AppStorage(StorageKeys.onboardingFlowComplete) var completedOnboardingFlow = false | ||
/// | ||
/// var body: some View { | ||
/// OnboardingStack(onboardingFlowComplete: $completedOnboardingFlow) { | ||
/// MyOwnView() | ||
/// .onboardingIdentifier("my-own-view-1") | ||
/// MyOwnView() | ||
/// .onboardingIdentifier("my-own-view-2") | ||
/// } | ||
/// } | ||
/// } | ||
/// ``` | ||
public func onboardingIdentifier<ID>(_ identifier: ID) -> some View where ID: Hashable { | ||
modifier(OnboardingIdentifiableViewModifier(id: identifier)) | ||
} | ||
} | ||
|
||
|
||
extension ModifiedContent: Identifiable where Modifier: Identifiable { | ||
public var id: Modifier.ID { | ||
self.modifier.id | ||
} | ||
} |
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
45 changes: 45 additions & 0 deletions
45
Tests/UITests/TestApp/Views/OnboardingIdentifiableTestViewCustom.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,45 @@ | ||
// | ||
// 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 SpeziOnboarding | ||
import SwiftUI | ||
|
||
struct OnboardingIdentifiableTestViewCustom: View, Identifiable { | ||
var id: String | ||
|
||
@Environment(OnboardingNavigationPath.self) private var path | ||
|
||
|
||
var body: some View { | ||
VStack(spacing: 12) { | ||
Text(self.id) | ||
|
||
Button { | ||
if self.id == "ID: 1" { | ||
path.append(customView: OnboardingIdentifiableTestViewCustom(id: "ID: 2")) | ||
} else { | ||
path.nextStep() | ||
} | ||
} label: { | ||
Text("Next") | ||
} | ||
} | ||
} | ||
} | ||
|
||
#if DEBUG | ||
struct OnboardingIdentifiableTestViewCustomView_Previews: PreviewProvider { | ||
static var previews: some View { | ||
OnboardingStack(startAtStep: OnboardingIdentifiableTestViewCustom.self) { | ||
for onboardingView in OnboardingFlow.previewSimulatorViews { | ||
onboardingView | ||
} | ||
} | ||
} | ||
} | ||
#endif |
Oops, something went wrong.