This is the whole code:
import SwiftUI
struct ContentView: View {
@State private var selectedCity = 0
@State private var prayerTimes = [String: String]()
@State private var date = Date()
var cities = ["copenhagen","arhus", "odense" , "malmo"]
var body: some View {
NavigationView {
Form {
Section {
Picker("Choose a city", selection: $selectedCity) {
ForEach(0 ..< cities.count) {
Text("\(self.cities[$0].capitalizingFirstLetter())")
}
}.pickerStyle(SegmentedPickerStyle())
//Text(" \(cities[selectedCity].capitalizingFirstLetter())" )
``
}
Section {
VStack(alignment: .leading) {
List(prayerTimes.sorted(by: {$0.1 < $1.1 }), id: \.key) { prayerName, time in
VStack(alignment: .trailing) {
switch prayerName {
case PrayerSymbols.fajr.rawValue:
PrayerIconView(prayerIconName: "sun.haze.fill", backgroundColor: Color(#colorLiteral(red: 0.1764705926, green: 0.01176470611, blue: 0.5607843399, alpha: 1)))
case PrayerSymbols.shuruk.rawValue:
PrayerIconView(prayerIconName: "sunrise.fill", backgroundColor: Color(#colorLiteral(red: 0.2588235438, green: 0.7568627596, blue: 0.9686274529, alpha: 1)))
case PrayerSymbols.dhuhr.rawValue:
PrayerIconView(prayerIconName: "sun.max.fill", backgroundColor: Color(#colorLiteral(red: 0.2588235438, green: 0.7568627596, blue: 0.9686274529, alpha: 1)))
case PrayerSymbols.asr.rawValue:
PrayerIconView(prayerIconName: "cloud.sun.fill", backgroundColor: Color(#colorLiteral(red: 0.7254902124, green: 0.4784313738, blue: 0.09803921729, alpha: 1)))
case PrayerSymbols.hanafi.rawValue:
PrayerIconView(prayerIconName: "cloud.sun.fill", backgroundColor: Color(#colorLiteral(red: 0.7254902124, green: 0.4784313738, blue: 0.09803921729, alpha: 1)))
case PrayerSymbols.maghrib.rawValue:
PrayerIconView(prayerIconName: "sunset.fill", backgroundColor: Color(#colorLiteral(red: 0.2196078449, green: 0.007843137719, blue: 0.8549019694, alpha: 1)))
case PrayerSymbols.isha.rawValue:
PrayerIconView(prayerIconName: "moon.stars.fill", backgroundColor: Color(#colorLiteral(red: 0.09019608051, green: 0, blue: 0.3019607961, alpha: 1)))
default:
PrayerIconView(prayerIconName: "", backgroundColor: Color.black)
}
Spacer()
}
Spacer()
VStack(alignment: .center) {
Text(prayerName.capitalizingFirstLetter())
}
Spacer()
VStack(alignment: .trailing) {
Text(time)
}
}
}
}
}
.navigationBarTitle("Salahtider")
.onAppear(perform: loadData)
}
}
func loadData() {
guard let url = URL(string: "http://salah.dk/ws.php?city=\(cities[selectedCity])") else {
print("Invalid URL")
return
}
let request = URLRequest(url: url)
let config = URLSessionConfiguration.default
config.waitsForConnectivity = true
URLSession(configuration: config).dataTask(with: request) { data, response, error in
if let data = data {
if let decodedResponse = try? JSONDecoder().decode(Response.self, from: data) {
// we have good data - go back to the main thread
DispatchQueue.main.async {
self.prayerTimes = decodedResponse.prayerTimes
//self.date = decodedResponse.date
}
// everything is good, so we can exit
return
}
print("Fetch failed: \(error?.localizedDescription ?? "Unknown error")")
}
config.timeoutIntervalForResource = 60
}.resume()
}
}
enum PrayerSymbols: String {
case fajr = "fajr"
case shuruk = "shuruk"
case dhuhr = "dhuhr"
case asr = "asr"
case hanafi = "hanafi asr"
case maghrib = "maghrib"
case isha = "isha"
}
extension String {
func capitalizingFirstLetter() -> String {
return prefix(1).capitalized + dropFirst()
}
mutating func capitalizeFirstLetter() {
self = self.capitalizingFirstLetter()
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
struct PrayerIconView: View {
var prayerIconName: String
var backgroundColor: Color
var body: some View {
VStack(alignment: .leading) {
Image(systemName: prayerIconName)
.renderingMode(.original)
.font(.system(size: 15))
.padding(.all)
.background(backgroundColor)
.clipShape(Circle())
}
}
}