StanfordSpezi / SpeziOnboarding

Spezi Onboarding module to inform a user or retrieve consent for a study participation
https://swiftpackageindex.com/StanfordSpezi/SpeziOnboarding/documentation/
MIT License
11 stars 5 forks source link

Customize identifier of View within the OnboardingStack #43

Closed philippzagar closed 2 months ago

philippzagar commented 3 months ago

Problem

The OnboardingStack identifies the individual views via the type name of the respective view (required for ordered navigation operations). For trivial use cases, this identification method is sufficient. However, for more advanced onboarding flows that contain multiple instances of the same View, this mechanism begins to fall apart.

Solution

We envision a custom ViewModifier in combination with a protocol that can be applied to Views within the OnboardingStack. This enables to customize the identifier of the onboarding Views, enabling more complex onboarding flows. The default identifier should still be inferred from the type of the View. The protocol enforces a get computed property (e.g., var viewIdentifier) that is able to fetch the identifier of a View set via a custom ViewModifier (e.g. .onboardingIdentifier(_: String) on a View used in the OnboardingStack.

The public API would then look similar to this:

struct OnboardingFlow: View {
    var view: some View {
         OnboardingStack {
               TestView1()
                    .onboardingIdentifier("...")
               TestView2()
               TestView1()
                    .onboardingIdentifier("...")
         }
    }
}

struct TestView1: OnboardingView {
   var viewIdentifier: ID = "..."

    var view: some View {
           // ...
    }
}

Additional context

It might be a good idea to utilize the Identifiable protocol from Swift for the custom protocol enabling to get the View identifier.

Sadly, there seems to be no way to utilize existing SwiftUI infrastructure like https://developer.apple.com/documentation/swiftui/view/id(_:) or https://developer.apple.com/documentation/swiftui/view/tag(_:), as one cannot access these data points on a View. The only option might be something like https://github.com/siteline/swiftui-introspect, but this would require significant further investigation.

Code of Conduct

PSchmiedmayer commented 3 months ago

Thank you for summarizing this @philippzagar 🚀