I have a view, in which I want the user to enter data. Then I want the user, to be able to press a button which directs the user to another view. And depending on the value, which the user entered previously I want a link to be changed. The link is at top-level because I need it more than once. So for example if the user enters "89", I want the link to be "https://www.example.com/89".
this is the code of the first view:
class lClass: ObservableObject {
@Published var longitude: String = ""
}
struct Data: View {
@State var showGraph = false
@ObservedObject var longitude2 = lClass()
var body: some View {
Button(action: {
self.showGraph.toggle()
}) {
Text("Show Graph")
}.sheet(isPresented: $showGraph){
GraphView()
}
if showGraph {
GraphView()
}
else {
HStack {
TextField("Longitude:", text: $longitude2.longitude)
}.frame(width: 400, height: 100, alignment: .center)
}
}
}
And this is the shortened code of the second view:
var month = 1
var day = 1
var year = 2020
var latitude = 59.911232
var longitude = 10.757933
var offset = "1.0"
class test {
@ObservedObject var longitude2 = lClass()
}
class test2 {
var car = test.init().longitude2
}
private let url = URL(string: "https://midcdmz.nrel.gov/apps/spa.pl?syear=\(year)&smonth=\(month)&sday=\(day)&eyear=\(year)&emonth=\(month)&eday=\(day)&otype=0&step=60&stepunit=1&hr=12&min=0&sec=0&latitude=\(latitude)&longitude=\(longitude)&timezone=\(offset)&elev=53&press=835&temp=10&dut1=0.0&deltat=64.797&azmrot=180&slope=0&refract=0.5667&field=0")
private func loadData(from url: URL?) -> String {
guard let url = url else {
return "nil"
}
let html = try! String(contentsOf: url, encoding: String.Encoding.utf8)
return html
}
let html = loadData(from: url)
private func dbl1() -> Double {
let leftSideOfTheValue = "0:00:00,"
let rightSideOfTheValue = "\(month)/\(day)/\(year),1:00:00,"
guard let leftRange = html.range(of: leftSideOfTheValue) else {
print("cant find left range")
return 0
}
guard let rightRange = html.range(of: rightSideOfTheValue) else {
print("cant find right range")
return 0
}
let rangeOfTheValue = leftRange.upperBound..<rightRange.lowerBound
return Double(html[rangeOfTheValue].dropLast()) ?? 90
}
struct elevationGraph: View {
var body: some View {
Text(String(dbl1())
}
}
In the end, I want the user to be able to select the vars, month, day, year, longitude, latitude, offset in the first view. And then manipulate the url so that it uses the data entered by the user.