Feb-08-2023, 07:23 AM
Hi everyone,
I'm looking for API calls to fetch information for my EV Charging App and how to merge them into the app code.
My code has dummy entries and I need to swap them to relevant API
Thank you so much
I'm looking for API calls to fetch information for my EV Charging App and how to merge them into the app code.
My code has dummy entries and I need to swap them to relevant API
Thank you so much
import UIKit import CoreLocation class ViewController: UIViewController { let locationManager = CLLocationManager() let networkRequest = NetworkRequest() let chargerDataSource = ChargerDataSource() override func viewDidLoad() { super.viewDidLoad() // Request location authorization from the user locationManager.requestWhenInUseAuthorization() locationManager.delegate = self // Start updating the user's location locationManager.startUpdatingLocation() } } extension ViewController: CLLocationManagerDelegate { func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { // Get the latest user location let userLocation = locations.last // Use the user's location to find EV charger locations near them networkRequest.fetchChargerLocations(near: userLocation) { [weak self] result in switch result { case .success(let chargerLocations): self?.chargerDataSource.update(with: chargerLocations) case .failure(let error): print("Failed to fetch charger locations: \(error)") } } } } class ChargerDataSource { private var chargerLocations: [ChargerLocation] = [] func update(with chargerLocations: [ChargerLocation]) { self.chargerLocations = chargerLocations } func charger(at index: Int) -> ChargerLocation { return chargerLocations[index] } } struct ChargerLocation { let location: CLLocation let type: ChargerType let isInUse: Bool } enum ChargerType { case level1 case level2 case level3 } class NetworkRequest { func fetchChargerLocations(near location: CLLocation?, completion: @escaping (Result<[ChargerLocation], Error>) -> Void) { // Replace this with a network request to an API that returns EV charger locations let chargerLocations = [ ChargerLocation(location: CLLocation(), type: .level2, isInUse: false), ChargerLocation(location: CLLocation(), type: .level3, isInUse: true), ] completion(.success(chargerLocations)) } }