在我的健身应用程序中,用户会看到可用训练的列表,该列表由以下结构实现
ForEach(self.mainData.journeys) { journeyDraft in
NavigationLink(destination:
VStack { [data] }
)
}
(见下面的完整代码)
[数据]包含一个长if { } else { } if { } else { }块,包括多个场景 - SetView/WeekView/BuildWeekView/GenerateJourney(我们将它们都称为 GoToView),具体取决于训练设置和状态(新训练或已经开始一)。
问题来了。用户点击列表中的一些训练,然后在从 NavigationLink 将用户发送到适当的视图 ( GoToView ) 之前,我检查他是否完成了他的健康水平调查:
self.mainData.customerData?.fitnessLevel != nil
如果他还没有,我需要先给他看一个调查视图。只有在他完成调查之后 - 他才会被进一步发送到GoToView ..(如果他已经填写了调查,他会被直接发送到GoToView,而不是显示调查视图)
你会如何推荐实现这个中间视图,以便当他回答我的问题时,他会继续 GoToView ?
我在 if/else 块中有 4 个不同的场景(SetView、WeekView、BuildWeekView、GenerateJourney),我不想在每个场景中包含调查视图。我想在发送用户之前找到一些方法来显示视图到 NavigationLink 的目的地 ( GoToView )。
任何帮助表示赞赏!
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("")
}
}