Post not yet marked as solved
Post marked as unsolved with 0 replies, 232 views
I am creating a Medical App using SwiftUI. I am trying to make a treatment view that provides a different set of answers (fetched from a .json) based on the answer to a certain set of questions.
I have tried this (see below) but I am experiencing errors with it. I figured it would be best to get some other opinions before diving down this rabbithole.
Full code is on GitHub: https://github.com/e-kendall/ReGen-Responder/
//
// BrokenBonesQ1.swift
// ReGen Responder
//
// Created by Eamon Kendall on 3/8/2023.
//
import SwiftUI
struct BrokenBonesQ1: View {
@State private var BrokenBonesQ1YES: Bool = false
@State private var BrokenBonesQ1NO: Bool = false
@Binding var BrokenBonesQ2YES: Bool
@Binding var BrokenBonesQ2NO: Bool
@Binding var stepIDS: [String]
@Binding var currentStepIndex: Int
var body: some View {
NavigationStack {
VStack {
Spacer()
Text("You've called for help for a Broken Limb.")
.font(.title)
Text("Please answer the following questions")
.font(.title2)
Spacer()
Text("Are there any protruding bones?")
.font(.title3)
ZStack {
RoundedRectangle(cornerRadius: 16)
.frame(width: 451, height: 58)
.foregroundColor(.gray)
NavigationLink("Yes", destination: BrokenBonesQ2(), isActive: $BrokenBonesQ1YES)
.foregroundColor(.white)
}
NavigationLink(destination: TreatmentView(), isActive: $BrokenBonesQ1NO) {
ZStack {
RoundedRectangle(cornerRadius: 16)
.frame(width: 451, height: 58)
.foregroundColor(.gray)
Text("No")
.foregroundColor(.white)
}
.simultaneousGesture(TapGesture().onEnded {
BrokenBonesQ1NO = true
if BrokenBonesQ1NO {
stepIDS = ["4", "5", "6"]
currentStepIndex += 1
}
})
}
Spacer()
}
.navigationBarBackButtonHidden()
}
}
}
struct BrokenBonesQ1_Previews: PreviewProvider {
@State static var brokenBonesQ2YESExamples = false
@State static var brokenBonesQ2NOExamples = false // Set it to false initially
@State static var stepIDExamples = ["5", "6"]
@State static var currentStepIndex = 0 // Set it to the initial step index
static var previews: some View {
BrokenBonesQ1(
BrokenBonesQ2YES: $brokenBonesQ2YESExamples,
BrokenBonesQ2NO: $brokenBonesQ2NOExamples,
stepIDS: $stepIDExamples,
currentStepIndex: $currentStepIndex // Pass the currentStepIndex binding
)
.previewInterfaceOrientation(.landscapeRight)
}
}
//
// TreatmentView.swift
// ReGen Responder
//
// Created by Eamon Kendall on 6/8/2023.
//
import SwiftUI
struct TreatmentView: View {
@EnvironmentObject var modelData: ModelData
@State private var currentStepIndex: Int = 0
@State private var stepIDS: [String] = ["1", "6", "9"]
var body: some View {
NavigationStack {
HStack {
VStack(alignment: .leading) {
Text(modelData.treatmentSteps[currentStepIndex].direction)
.font(.system(size: 36, weight: .semibold))
.multilineTextAlignment(.leading)
Text(modelData.treatmentSteps[currentStepIndex].description)
.font(.system(size: 24, weight: .medium))
if let materialLocation = modelData.treatmentSteps[currentStepIndex].materialLocation {
HStack {
ZStack {
Circle()
.foregroundColor(.red)
.frame(width: 224, height: 224)
Text(materialLocation)
.foregroundColor(.white)
.font(.system(size: 84, weight: .medium))
}
.padding()
}
.frame(width: 490)
}
Spacer()
ZStack {
RoundedRectangle(cornerRadius: 16)
.frame(width: 451, height: 58)
.foregroundColor(.gray)
Button("Next") {
showNextStep(withStepIDs: stepIDS)
}
.foregroundColor(.white)
}
.padding()
Text("If the patients condition worsens or you are ever in doubt, Dial 000.")
.font(.headline)
}
.padding()
Image(modelData.treatmentSteps[currentStepIndex].image)
.resizable()
.aspectRatio(contentMode: .fit)
.cornerRadius(10)
.padding()
.navigationBarBackButtonHidden()
}
.padding()
}
}
func showNextStep(withStepIDs stepIDs: [String]) {
guard let currentIndex = modelData.treatmentSteps.firstIndex(where: { $0.id == stepIDS[currentStepIndex] }) else {
return
}
let validSteps = modelData.treatmentSteps.enumerated().filter { index, step in
stepIDs.contains(step.id) && index > currentIndex
}
if let nextStep = validSteps.first {
currentStepIndex = nextStep.offset
}
}
}
struct TreatmentView_Previews: PreviewProvider {
static var previews: some View {
TreatmentView()
.environmentObject(ModelData())
.previewInterfaceOrientation(.landscapeRight)
}
}