eugene_prg
eugene_prg

Reputation: 1168

NavigationLink: show intermediate view with a survey and after it is closed - proceed to "destination" view

In my fitness application a user sees a list of available trainings, this list is implemented by the following structure

ForEach(self.mainData.journeys) { journeyDraft in
                        NavigationLink(destination:
                            VStack { [data] }
                        )
}

(see full code below)

[data] contains a long if { } else { } if { } else { } block including multiple scenarios - SetView/WeekView/BuildWeekView/GenerateJourney (lets call them all GoToView), depending on training settings and state (new training or already started one).

Here comes a problem.. The user clicks on some training from the list and before sending the user to appropriate view (GoToView) from NavigationLink I check if he has completed his fitness level survey:

self.mainData.customerData?.fitnessLevel != nil

And if he has not yet, I need to show him a survey view first. And only after he completes the survey - he is sent further to GoToView.. (If he has already filled the survey, he is sent straight to GoToView, not survey view is shown)

How would you recommend implementing this intermediate View so that when he answers my question, he proceeds to GoToView ?

I have 4 different scenarios (SetView, WeekView, BuildWeekView, GenerateJourney) inside that if/else block and I don't wanna include the survey view inside each of them.. I want to find some way to show the view BEFORE sending the user to NavigationLink's destination (GoToView).

Any help is appreciated!

var body: some View {
        NavigationView {
            ZStack {
                ProfileView()
                .opacity(self.showProfile ? 1 : 0)
            VStack {
                Image(customImage: ((self.thisSession.profile.photo != nil) ? self.thisSession.profile.photo : Constants.defaultProfileImage)!)!
                        .resizable()
                        .aspectRatio(contentMode: .fit)
                        .frame(width: 50, height: 50)
                        .clipShape(Circle())
                        .overlay(Circle().stroke(Color.blue, lineWidth: 3))
                    .onTapGesture {
                        self.showProfile.toggle()
                    }
                List {
                    ForEach(self.mainData.journeys) { journeyDraft in
                        NavigationLink(destination:
                            VStack {
                                
                                // if fitness survey IS COMPLETED but journey was not yet created
                                if (self.mainData.customerData?.fitnessLevel != nil && self.mainData.userJourneys[journeyDraft.journeyId!] == nil) {
                                    
                                    // if it's NOT a single day training, no need to return to it? why..?
                                    if journeyDraft.type != Constants.singleDay {
                                        VStack {
                                            //Text("Section 1").foregroundColor(.blue)
                                            DeferView { BuildWeekView(journeyDraftId: journeyDraft.journeyId, customerData: self.mainData.customerData)
                                            .navigationBarHidden(true)
                                            .navigationBarTitle("")
                                            }
                                        }
                                    } else {
                                        
                                        VStack {
                                            DeferView { GenerateJourney(schedule: [:], tools: [:], journeyDraft: journeyDraft,  thisSession: self.thisSession, goto: Constants.goToDay)
                                                .navigationBarHidden(true)
                                                .navigationBarTitle("")
                                            }
                                        }
                                    }
                                    
                                // if fitnessLevel is set and this is not a first launch
                                } else if (self.mainData.customerData?.fitnessLevel != nil && self.mainData.userJourneys[journeyDraft.journeyId!] != nil) {
                                    // multiple day training
                                    if (self.mainData.userJourneys[journeyDraft.journeyId!]!.type! == Constants.singleDay) {
                                                                        VStack {
                                                                            WeekView(journey: self.mainData.userJourneys[journeyDraft.journeyId!]!)
                                                                            .navigationBarHidden(true)
                                                                            .navigationBarTitle("")
                                                                        }
                                                                    } else {
                                                                            // it's a single day training
                                                                        VStack {
                                                                            SetView(journey:self.mainData.userJourneys[journeyDraft.journeyId!]!,day:self.mainData.userJourneys[journeyDraft.journeyId!]!.days.keys.first!)
                                                                            .navigationBarHidden(true)
                                                                            .navigationBarTitle("")
                                                                        }
                                                                    }
                                        
                                // if survey is not completed and this is a first launch (but we need to run GenerateJourney then
                                } else if (self.mainData.customerData?.fitnessLevel == nil) {
                                   // HERE WE NEED TO SHOW THE SURVEY AND AFTER IT IS COMPLETED - DO THE SAME AS ABOVE
                                }

                            }
                        ) {
                            Image(customImage: journeyDraft.image!)!
                                .resizable()
                                .renderingMode(.original)
                                .frame(height: 150, alignment: .leading)
                                .opacity( (self.mainData.userJourneys[journeyDraft.journeyId!] == nil) ? 1 : 0.5 )
                                .overlay(
                                    Text("\(journeyDraft.name!) : \(journeyDraft.type!)")
                                        .font(.largeTitle)
                                        .fontWeight(.semibold)
                                        .foregroundColor((self.mainData.userJourneys[journeyDraft.journeyId!] == nil) ? .white : .red)
                            )
                        }
                    }
                }
            }
            .opacity(self.showProfile ? 0 : 0.6)
            
            }
        }
        .navigationBarHidden(true)
        .navigationBarTitle("")
    }
}

Upvotes: 0

Views: 17

Answers (1)

eugene_prg
eugene_prg

Reputation: 1168

I solved the problem by using tags in NavigatingLink

Upvotes: 0

Related Questions