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
| /// Fetch API data for `Endpoint` | |
| struct ApiService { | |
| let businesses: (YelpEndpoint) async throws -> [Business] | |
| } | |
| extension ApiService { | |
| /// Live service used in App | |
| static let live = ApiService { endpoint in | |
| let (data, error) = try await URLSession.shared.data(for: endpoint.request) | |
| let decoder = JSONDecoder() |
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
| extension YelpEndpoint { | |
| /// Endpoint URL Path | |
| var path: String { | |
| switch self { | |
| case .search: | |
| return "/v3/businesses/search" | |
| case let .detail(id): | |
| return "/v3/businesses/\(id)" | |
| } | |
| } |
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 CoreLocation | |
| /// Endpoints for Yelp | |
| enum YelpEndpoint { | |
| case search(term: String, location: CLLocation) | |
| case detail(id: String) | |
| } |
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
| Task { | |
| let (data, _) = try! await session.data(for: request) | |
| let result = try! JSONDecoder().decode(SearchResult.self, from: data) | |
| self.businesses = result.businesses | |
| } |
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
| NavigationView { | |
| List { | |
| ForEach(businesses) { business in | |
| Text(business.name) | |
| } | |
| } | |
| .listStyle(.plain) | |
| .navigationTitle(Text("Boston")) | |
| .refreshable { | |
| // do something |
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
| NavigationView { | |
| List { | |
| ForEach(businesses) { business in | |
| Text(business.name) | |
| } | |
| // Swipe actions | |
| .swipeActions( | |
| edge: .trailing, | |
| allowsFullSwipe: true, | |
| content: { |
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
| @State var businesses = [Bussiness]() | |
| NavigationView { | |
| List { | |
| ForEach(businesses) { business in | |
| Text(business.name ?? "no name") | |
| } | |
| } | |
| .listStyle(.plain) | |
| .navigationTitle(Text("Boston")) |
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
| NavigationView { | |
| List { | |
| ForEach(businesses) { business in | |
| Text(business.name) | |
| } | |
| } | |
| .listStyle(.plain) | |
| .navigationTitle(Text("Boston")) | |
| .searchable($searchText) | |
| // Receive an update here when the user selects the done button on the keyboard |
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
| NavigationView { | |
| List { | |
| ForEach(viewModel.businesses) { business in | |
| Text(business.name) | |
| } | |
| } | |
| .listStyle(.plain) | |
| .navigationTitle(Text("Boston")) | |
| // Position a gradient at the bottom edge of a list | |
| .safeAreaInset(edge: .bottom) { |
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
| NavigationView { | |
| List { | |
| ForEach(businesses, id: \.id) { business in | |
| Text(business.name) | |
| } | |
| } | |
| .listStyle(.plain) | |
| .navigationTitle(Text("Boston")) | |
| // New tool bar modifier and tool bar item | |
| .toolbar { |