-1

Following an online tutorial for RevenueCat- Which calls information from an Array. This presents scrollable Pictures as clickable buttons. The problem that I am having is creating a Navigationlink to the different pictures relative to the information from the array/Model information. Any Pointers would be most appreciated

    struct Meditation: Identifiable {
     
     var id = UUID()
     var title:String = ""
     var desc:String = ""
     var imageName = ""
     var productId:String?




     static func getSampleMeditations() -\> \[Meditation\] {
     
     var array = [Meditation]()
     
     var m1 = Meditation()
     m1.title = "GP Calculator"
     m1.desc = "Simple GP Calculator"
     m1.imageName = "ambition"
     m1.productId = "ambition"
     array.append(m1)

    -----------

      struct ContentView: View {

         @EnvironmentObject var userViewModel: UserViewModel
         @State var isPaywallPresented = false

        var body: some View {      
            TabView {            
              NavigationView {
                ScrollView (showsIndicators: false) {
                    VStack (alignment: .leading) {

     ForEach(Meditation.getSampleMeditations()) { meditation in
     
     Button {
     
     if userViewModel.isSubscriptionActive {
     
     NavigationLink(destination: Meditation(array: meditation) )

     }
     else {
     isPaywallPresented = true }
     } label: {
     Card(meditation: meditation)
     .padding(.bottom, 20)
     }
     }
    
    

      // TODO: Show  meditation
     //         NavigationLink(destination: GP_Calculator() )
     //   Meditation.getSampleMeditations()
     //         NavigationLink(destination: Meditation(array: meditation) )
     //     Meditation(meditation: title)
     //    ContentView()
    
  • "This question does not show any research effort; it is unclear or not useful" . Someone downed it minus -1. And gave the above reason. Ok Be gentle Big guy. First time posting on Stackoverflow. And just coming to terms with how to formulate a question. What makes a good question and also what due diligence is required before posting. Cheers for having a look.. – Ben Robertson Nov 25 '22 at 12:00

1 Answers1

0

The root problem is with this part of your code:

Button {
  if userViewModel.isSubscriptionActive {
     NavigationLink(destination: Meditation(array: meditation) )
  } else {
     isPaywallPresented = true 
  }
} label: {
  Card(meditation: meditation)
    .padding(.bottom, 20)
}

Button is taking two different blocks here. The first is the action, which should be Swift code to update your app's state when the button is tapped. The second is the label (aka the visible contents of the button), which shows your card view.

The problem is that NavigationLink is a view - actually, a special kind of Button - but it's placed inside a non-view bit of code.

From looking at that logic, I think the premise is:

  1. If the user's subscription is active, clicking on a link should send them to a meditation view.
  2. If their subscription is not active, then clicking will instead set a state variable that will show a paywall view (e.g., with a fullScreenCover or similar).

Your simplest approach will be to move the if statement to the outside and create two alternative presentations of Card.

if userViewModel.isSubscriptionActive {
  NavigationLink(destination: Meditation(array: meditation)) {
    Card(meditation: meditation)
      .padding(.bottom, 20)
  }
} else {
  Button {
    isPaywallPresented = true
  } label: {
    Card(meditation: meditation)
      .padding(.bottom, 20)
  }
}

Sure, Card gets specified twice, but now it's a lot clearer what your code is doing.

ScottM
  • 7,108
  • 1
  • 25
  • 42
  • Hi ScottM Thanks very much for taking the time to help me out in this puzzle of logic. As I am fresh to Coding, I'm following a lot of tutorials which allows one to ad-lib the code about to certain degree to achieve an outcome. But when the problem solving comes into play then that's the time when I feel the most Vulnerable with my Knowledge, or lack of. The way that you have explained the solution has helped me appreciate that my intuition about the problem was pointing in the right direction and also as to where my attention of study needs direction. So I wanted to say thanks very much... – Ben Robertson Nov 25 '22 at 15:48