Created
June 18, 2019 09:56
-
-
Save bpisano/62118b31bcc7aad5bd136e0c2e781cf6 to your computer and use it in GitHub Desktop.
Embedded code in my Weather article on Medium
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| struct NewCityView : View { | |
| @Binding var isAddingCity: Bool | |
| @State private var search: String = "" | |
| @ObjectBinding var cityFinder: CityFinder = CityFinder() | |
| @EnvironmentObject var cityStore: CityStore | |
| var body: some View { | |
| NavigationView { | |
| List { | |
| Section { | |
| TextField($search, placeholder: Text("Search City")) { | |
| self.cityFinder.search(self.search) | |
| } | |
| } | |
| Section { | |
| ForEach(cityFinder.results.identified(by: \.self)) { result in | |
| Button(action: { | |
| self.addCity(from: result) | |
| self.isAddingCity = false | |
| }) { | |
| Text(result) | |
| } | |
| .foregroundColor(.black) | |
| } | |
| } | |
| } | |
| .navigationBarTitle(Text("Add City")) | |
| .navigationBarItems(leading: cancelButton) | |
| .listStyle(.grouped) | |
| } | |
| } | |
| private var cancelButton: some View { | |
| Button(action: { | |
| self.isAddingCity = false | |
| }) { | |
| Text("Cancel") | |
| } | |
| } | |
| private func addCity(from result: String) { | |
| let cityName = result.split(separator: ",").first ?? "" | |
| let city = City(name: String(cityName)) | |
| cityStore.cities.append(city) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment