Created
June 18, 2019 09:22
-
-
Save bpisano/573440907f6482aacb216ffafbdbdb70 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
| import SwiftUI | |
| struct CityListView : View { | |
| @EnvironmentObject var cityStore: CityStore | |
| @State var isAddingCity: Bool = false | |
| @State private var isEditing: Bool = false | |
| var body: some View { | |
| NavigationView { | |
| List { | |
| Section(header: Text("Your Cities")) { | |
| ForEach(cityStore.cities) { city in | |
| CityRow(city: city) | |
| } | |
| .onDelete(perform: delete) | |
| .onMove(perform: move) | |
| } | |
| } | |
| .navigationBarItems(leading: EditButton(), trailing: addButton) | |
| .navigationBarTitle(Text("Weather")) | |
| } | |
| } | |
| private var addButton: some View { | |
| Button(action: { | |
| self.isAddingCity = true | |
| self.isEditing = false | |
| }) { | |
| Image(systemName: "plus.circle.fill") | |
| .font(.title) | |
| } | |
| .presentation(isAddingCity ? newCityView : nil) | |
| } | |
| private func delete(at offsets: IndexSet) { | |
| for index in offsets { | |
| cityStore.cities.remove(at: index) | |
| } | |
| } | |
| private func move(from source: IndexSet, to destination: Int) { | |
| var removeCities: [City] = [] | |
| for index in source { | |
| removeCities.append(cityStore.cities[index]) | |
| cityStore.cities.remove(at: index) | |
| } | |
| cityStore.cities.insert(contentsOf: removeCities, at: destination) | |
| } | |
| private var newCityView: Modal { | |
| Modal(NewCityView(isAddingCity: $isAddingCity).environmentObject(cityStore)) { | |
| self.isAddingCity = false | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment