Struggling with game

Struggling with game
| import SwiftUI | |
| structContentView:View{ | |
| varbody:someView{ | |
| Home() | |
| } | |
| } | |
| structContentView_Previews:PreviewProvider{ | |
| staticvarpreviews:someView{ | |
| ContentView() | |
| } | |
| } | |
| structHome:View{ | |
| @Statevarindex=0 | |
| @Statevarmain:MainData! | |
| @Statevardaily:[Daily]=[] | |
| @Statevarlast:Int=0 | |
| @Statevarcountry="usa" | |
| @Statevaralert=false | |
| varbody:someView{ | |
| VStack{ | |
| ifself.main !=nil && !self.daily.isEmpty{ | |
| VStack{ | |
| VStack(spacing:18){ | |
| HStack{ | |
| Text("Statistics") | |
| .font(.title) | |
| .fontWeight(.bold) | |
| .foregroundColor(.white) | |
| Spacer() | |
| Button(action:{ | |
| self.Dialog() | |
| }){ | |
| Text(self.country.uppercased()) | |
| .foregroundColor(.white) | |
| .fontWeight(.bold) | |
| } | |
| } | |
| .padding(.top,(UIApplication.shared.windows.first?.safeAreaInsets.top)! +15) | |
| HStack{ | |
| Button(action:{ | |
| self.index =0 | |
| self.main =nil | |
| self.daily.removeAll() | |
| self.getData() | |
| }){ | |
| Text("My Country") | |
| .foregroundColor(self.index ==0?.black :.white) | |
| .padding(.vertical,12) | |
| .frame(width:(UIScreen.main.bounds.width /2)-30) | |
| } | |
| .background(self.index ==0?Color.white :Color.clear) | |
| .clipShape(Capsule()) | |
| Button(action:{ | |
| self.index =1 | |
| self.main =nil | |
| self.daily.removeAll() | |
| self.getData() | |
| }){ | |
| Text("Global") | |
| .foregroundColor(self.index ==1?.black :.white) | |
| .padding(.vertical,12) | |
| .frame(width:(UIScreen.main.bounds.width /2)-30) | |
| } | |
| .background(self.index ==1?Color.white :Color.clear) | |
| .clipShape(Capsule()) | |
| } | |
| .background(Color.black.opacity(0.25)) | |
| .clipShape(Capsule()) | |
| .padding(.top,10) | |
| HStack(spacing:15){ | |
| VStack(spacing:12){ | |
| Text("Affected") | |
| .fontWeight(.bold) | |
| Text("\(self.main.cases)") | |
| .fontWeight(.bold) | |
| .font(.title) | |
| } | |
| .padding(.vertical) | |
| .frame(width:(UIScreen.main.bounds.width /2)-30) | |
| .background(Color("affected")) | |
| .cornerRadius(12) | |
| VStack(spacing:12){ | |
| Text("Deaths") | |
| .fontWeight(.bold) | |
| Text("\(self.main.deaths)") | |
| .fontWeight(.bold) | |
| .font(.title) | |
| } | |
| .padding(.vertical) | |
| .frame(width:(UIScreen.main.bounds.width /2)-30) | |
| .background(Color("death")) | |
| .cornerRadius(12) | |
| } | |
| .foregroundColor(.white) | |
| .padding(.top,10) | |
| HStack(spacing:15){ | |
| VStack(spacing:12){ | |
| Text("Recovered") | |
| .fontWeight(.bold) | |
| Text("\(self.main.recovered)") | |
| .fontWeight(.bold) | |
| } | |
| .padding(.vertical) | |
| .frame(width:(UIScreen.main.bounds.width /3)-30) | |
| .background(Color("recovered")) | |
| .cornerRadius(12) | |
| VStack(spacing:12){ | |
| Text("Active") | |
| .fontWeight(.bold) | |
| Text("\(self.main.active)") | |
| .fontWeight(.bold) | |
| } | |
| .padding(.vertical) | |
| .frame(width:(UIScreen.main.bounds.width /3)-30) | |
| .background(Color("active")) | |
| .cornerRadius(12) | |
| VStack(spacing:12){ | |
| Text("Serious") | |
| .fontWeight(.bold) | |
| Text("\(self.main.critical)") | |
| .fontWeight(.bold) | |
| } | |
| .padding(.vertical) | |
| .frame(width:(UIScreen.main.bounds.width /3)-30) | |
| .background(Color("serious")) | |
| .cornerRadius(12) | |
| } | |
| .foregroundColor(.white) | |
| .padding(.top,10) | |
| } | |
| .padding(.horizontal) | |
| .padding(.bottom,45) | |
| .background(Color("bg")) | |
| VStack(spacing:15){ | |
| HStack{ | |
| Text("Last 7 Days") | |
| .font(.title) | |
| .foregroundColor(.black) | |
| Spacer() | |
| } | |
| .padding(.top) | |
| HStack{ | |
| ForEach(self.daily){i in | |
| VStack(spacing:10){ | |
| Text("\(i.cases /1000)K") | |
| .lineLimit(1) | |
| .font(.caption) | |
| .foregroundColor(.gray) | |
| GeometryReader{g in | |
| VStack{ | |
| Spacer(minLength:0) | |
| Rectangle() | |
| .fill(Color("death")) | |
| .frame(width:15,height:self.getHeight(value: i.cases, height: g.frame(in:.global).height)) | |
| } | |
| } | |
| Text(i.day) | |
| .lineLimit(1) | |
| .font(.caption) | |
| .foregroundColor(.gray) | |
| } | |
| } | |
| } | |
| } | |
| .padding(.horizontal) | |
| .background(Color.white) | |
| .cornerRadius(20) | |
| .padding(.bottom,-30) | |
| .offset(y:-30) | |
| } | |
| } | |
| else{ | |
| Indicator() | |
| } | |
| } | |
| .edgesIgnoringSafeArea(.top) | |
| .alert(isPresented:self.$alert, content:{ | |
| Alert(title:Text("Error"), message:Text("Invalid Country Name"), dismissButton:.destructive(Text("Ok"))) | |
| }) | |
| .onAppear{ | |
| self.getData() | |
| } | |
| } | |
| func getData(){ | |
| varurl="" | |
| ifself.index ==0{ | |
| url ="https://corona.lmao.ninja/v2/countries/\(self.country)?yesterday=false" | |
| } | |
| else{ | |
| url ="https://corona.lmao.ninja/v2/all?today" | |
| } | |
| letsession=URLSession(configuration:.default) | |
| session.dataTask(with:URL(string: url)!){(data, _, err)in | |
| if err !=nil{ | |
| print((err?.localizedDescription)!) | |
| return | |
| } | |
| letjson=try!JSONDecoder().decode(MainData.self, from: data!) | |
| self.main = json | |
| } | |
| .resume() | |
| varurl1="" | |
| ifself.index ==0{ | |
| url1 ="https://corona.lmao.ninja/v2/historical/\(self.country)?lastdays=7" | |
| } | |
| else{ | |
| url1 ="https://corona.lmao.ninja/v2/historical/all?lastdays=7" | |
| } | |
| letsession1=URLSession(configuration:.default) | |
| session1.dataTask(with:URL(string: url1)!){(data, _, err)in | |
| if err !=nil{ | |
| print((err?.localizedDescription)!) | |
| return | |
| } | |
| varcount=0 | |
| varcases:[String:Int]=[:] | |
| ifself.index ==0{ | |
| letjson=try!JSONDecoder().decode(MyCountry.self, from: data!) | |
| cases = json.timeline["cases"]! | |
| } | |
| else{ | |
| letjson=try!JSONDecoder().decode(Global.self, from: data!) | |
| cases = json.cases | |
| } | |
| foriin cases{ | |
| self.daily.append(Daily(id: count, day: i.key, cases: i.value)) | |
| count +=1 | |
| } | |
| self.daily.sort{(t, t1)->Boolin | |
| if t.day < t1.day{ | |
| returntrue | |
| } | |
| else{ | |
| returnfalse | |
| } | |
| } | |
| self.last =self.daily.last!.cases | |
| } | |
| .resume() | |
| } | |
| func getHeight(value :Int,height:CGFloat)->CGFloat{ | |
| // safer Side... | |
| ifself.last !=0{ | |
| letconverted=CGFloat(value)/ CGFloat(self.last) | |
| return converted * height | |
| } | |
| else{ | |
| return0 | |
| } | |
| } | |
| func Dialog(){ | |
| letalert=UIAlertController(title:"Country", message:"Type A Country", preferredStyle:.alert) | |
| alert.addTextField{(_)in | |
| } | |
| alert.addAction(UIAlertAction(title:"Ok", style:.default, handler:{(_)in | |
| foriin countryList{ | |
| if i.lowercased()== alert.textFields![0].text!.lowercased(){ | |
| self.country = alert.textFields![0].text!.lowercased() | |
| self.main =nil | |
| self.daily.removeAll() | |
| self.getData() | |
| return | |
| } | |
| } | |
| self.alert.toggle() | |
| })) | |
| alert.addAction(UIAlertAction(title:"Cancel", style:.destructive, handler:nil)) | |
| UIApplication.shared.windows.first?.rootViewController?.present(alert, animated:true, completion:nil) | |
| } | |
| } | |
| classHost:UIHostingController<ContentView>{ | |
| overridevarpreferredStatusBarStyle:UIStatusBarStyle{ | |
| return.lightContent | |
| } | |
| } | |
| // Data Models For JSON Parsing.... | |
| structDaily:Identifiable{ | |
| varid:Int | |
| varday:String | |
| varcases:Int | |
| } | |
| structMainData:Decodable{ | |
| vardeaths:Int | |
| varrecovered:Int | |
| varactive:Int | |
| varcritical:Int | |
| varcases:Int | |
| } | |
| structMyCountry:Decodable{ | |
| vartimeline:[String:[String:Int]] | |
| } | |
| structGlobal:Decodable{ | |
| varcases:[String:Int] | |
| } | |
| structIndicator:UIViewRepresentable{ | |
| func makeUIView(context:Context)->UIActivityIndicatorView{ | |
| letview=UIActivityIndicatorView(style:.large) | |
| view.startAnimating() | |
| return view | |
| } | |
| func updateUIView(_ uiView:UIActivityIndicatorView, context:Context){ | |
| } | |
| } | |
| import Foundation | |
| varcountryList=[ | |
| "Afghanistan", | |
| "Albania", | |
| "Algeria", | |
| "Andorra", | |
| "Angola", | |
| "Anguilla", | |
| "Antigua and Barbuda", | |
| "Argentina", | |
| "Armenia", | |
| "Aruba", | |
| "Australia", | |
| "Austria", | |
| "Azerbaijan", | |
| "Bahamas", | |
| "Bahrain", | |
| "Bangladesh", | |
| "Belarus", | |
| "Belgium", | |
| "Belize", | |
| "Benin", | |
| "Bermuda", | |
| "Bhutan", | |
| "Bolivia", | |
| "Botswana", | |
| "Bouvet Island", | |
| "Brazil", | |
| "British Indian Ocean Territory", | |
| "Brunei", | |
| "Bulgaria", | |
| "Burkina Faso", | |
| "Burundi", | |
| "Cabo Verde", | |
| "Cambodia", | |
| "Cameroon", | |
| "Canada", | |
| "Cayman Islands", | |
| "Central African Republic", | |
| "Chad", | |
| "Chile", | |
| "China", | |
| "Christmas Island", | |
| "Colombia", | |
| "Congo", | |
| "Croatia", | |
| "Cuba", | |
| "Curaçao", | |
| "Cyprus", | |
| "Czechia", | |
| "Côte d'Ivoire", | |
| "Denmark", | |
| "Djibouti", | |
| "Dominica", | |
| "Dominican Republic", | |
| "Ecuador", | |
| "Egypt", | |
| "El Salvador", | |
| "Equatorial Guinea", | |
| "Eritrea", | |
| "Estonia", | |
| "Eswatini", | |
| "Ethiopia", | |
| "Falkland Islands", | |
| "Faroe Islands", | |
| "Fiji", | |
| "Finland", | |
| "France", | |
| "French Guiana", | |
| "French Polynesia", | |
| "Gabon", | |
| "Gambia (the)", | |
| "Georgia", | |
| "Germany", | |
| "Ghana", | |
| "Gibraltar", | |
| "Greece", | |
| "Greenland", | |
| "Grenada", | |
| "Guadeloupe", | |
| "Guam", | |
| "Guatemala", | |
| "Guernsey", | |
| "Guinea", | |
| "Guinea-Bissau", | |
| "Guyana", | |
| "Haiti", | |
| "Heard Island and McDonald Islands", | |
| "Holy See", | |
| "Honduras", | |
| "Hong Kong", | |
| "Hungary", | |
| "Iceland", | |
| "India", | |
| "Indonesia", | |
| "Iran", | |
| "Iraq", | |
| "Ireland", | |
| "Isle of Man", | |
| "Israel", | |
| "Italy", | |
| "Jamaica", | |
| "Japan", | |
| "Jersey", | |
| "Jordan", | |
| "Kazakhstan", | |
| "Kenya", | |
| "Kiribati", | |
| "Korea", | |
| "Korea", | |
| "Kuwait", | |
| "Kyrgyzstan", | |
| "Lao People's Democratic Republic", | |
| "Latvia", | |
| "Lebanon", | |
| "Lesotho", | |
| "Liberia", | |
| "Libya", | |
| "Liechtenstein", | |
| "Lithuania", | |
| "Luxembourg", | |
| "Macao", | |
| "Madagascar", | |
| "Malawi", | |
| "Malaysia", | |
| "Maldives", | |
| "Mali", | |
| "Malta", | |
| "Marshall Islands", | |
| "Martinique", | |
| "Mauritania", | |
| "Mauritius", | |
| "Mayotte", | |
| "Mexico", | |
| "Micronesia", | |
| "Moldova", | |
| "Monaco", | |
| "Mongolia", | |
| "Montenegro", | |
| "Montserrat", | |
| "Morocco", | |
| "Mozambique", | |
| "Myanmar", | |
| "Namibia", | |
| "Nauru", | |
| "Nepal", | |
| "Netherlands", | |
| "New Caledonia", | |
| "New Zealand", | |
| "Nicaragua", | |
| "Niger (the)", | |
| "Nigeria", | |
| "Niue", | |
| "Norfolk Island", | |
| "Northern Mariana Islands", | |
| "Norway", | |
| "Oman", | |
| "Pakistan", | |
| "Palau", | |
| "Palestine, State of", | |
| "Panama", | |
| "Papua New Guinea", | |
| "Paraguay", | |
| "Peru", | |
| "Philippines", | |
| "Pitcairn", | |
| "Poland", | |
| "Portugal", | |
| "Puerto Rico", | |
| "Qatar", | |
| "Republic of North Macedonia", | |
| "Romania", | |
| "Russian Federation", | |
| "Rwanda", | |
| "Réunion", | |
| "Saint Barthélemy", | |
| "Saint Helena, Ascension and Tristan da Cunha", | |
| "Saint Kitts and Nevis", | |
| "Saint Lucia", | |
| "Saint Martin (French part)", | |
| "Saint Pierre and Miquelon", | |
| "Saint Vincent and the Grenadines", | |
| "Samoa", | |
| "San Marino", | |
| "Sao Tome and Principe", | |
| "Saudi Arabia", | |
| "Senegal", | |
| "Serbia", | |
| "Seychelles", | |
| "Sierra Leone", | |
| "Singapore", | |
| "Sint Maarten", | |
| "Slovakia", | |
| "Slovenia", | |
| "Solomon Islands", | |
| "Somalia", | |
| "South Africa", | |
| "South Georgia and the South Sandwich Islands", | |
| "South Sudan", | |
| "Spain", | |
| "Sri Lanka", | |
| "Sudan", | |
| "Suriname", | |
| "Svalbard and Jan Mayen", | |
| "Sweden", | |
| "Switzerland", | |
| "Syrian Arab Republic", | |
| "Taiwan", | |
| "Tajikistan", | |
| "Tanzania, United Republic of", | |
| "Thailand", | |
| "Timor-Leste", | |
| "Togo", | |
| "Tokelau", | |
| "Tonga", | |
| "Trinidad and Tobago", | |
| "Tunisia", | |
| "Turkey", | |
| "Turkmenistan", | |
| "Turks and Caicos Islands (the)", | |
| "Tuvalu", | |
| "Uganda", | |
| "Ukraine", | |
| "UAE", | |
| "UK", | |
| "USA", | |
| "Uruguay", | |
| "Uzbekistan", | |
| "Vanuatu", | |
| "Venezuela", | |
| "Viet Nam", | |
| "Wallis and Futuna", | |
| "Western Sahara", | |
| "Yemen", | |
| "Zambia", | |
| "Zimbabwe", | |
| "Åland Islands" | |
| ] | |