I am new to CarPlay development. I am working on an app that has four tabs. The first tab has a POI template. I make an API call, once the data is received, I need to refresh the POI template to display the data received. Below is a sample code. Please guide me on this. Right now I am pushing a new template once data is obtained which is not the right behaviour
func templateApplicationScene(_ templateApplicationScene: CPTemplateApplicationScene, didConnect interfaceController: CPInterfaceController) {
self.interfaceController = interfaceController
let tabs = CPTabBarTemplate(templates: [firstTab(), secondTab(), thirdTab(), fourthTab()])
interfaceController.setRootTemplate(tabs,
animated: true,
completion: nil)
}
func firstTab() -> CPTemplate {
// Placeholder template while data is loading
let placeholderPOI = CPPointOfInterest(location: MKMapItem(placemark: MKPlacemark(coordinate: CLLocationCoordinate2D(latitude: 0.0, longitude: 0.0))),
title: "Loading...",
subtitle: "",
summary: "Data is currently loading",
detailTitle: "",
detailSubtitle: "",
detailSummary: "",
pinImage: nil)
let placeholderTemplate = CPPointOfInterestTemplate(title: "EV Stations", pointsOfInterest: [placeholderPOI], selectedIndex: 0)
// Asynchronously update the template once data is loaded
Task {
await loadAndUpdateTimeConsumingData(for: placeholderTemplate)
}
return placeholderTemplate
}
func loadAndUpdateTimeConsumingData(for template: CPPointOfInterestTemplate) async {
do {
// Simulate a 1-minute delay
try await Task.sleep(nanoseconds: 60 * 1_000_000_000)
// Mock data
let stations = [Station1(name: "Station 1", city: "City A", address: "123 Lane", coordinates: (latitude: 12.3456, longitude: 76.7890))]
let pointsOfInterest = stations.map { station -> CPPointOfInterest in
let mapItem = MKMapItem(placemark: MKPlacemark(coordinate: CLLocationCoordinate2D(latitude: station.coordinates.latitude, longitude: station.coordinates.longitude)))
return CPPointOfInterest(location: mapItem,
title: station.name,
subtitle: station.city,
summary: station.address,
detailTitle: "",
detailSubtitle: "",
detailSummary: "",
pinImage: nil)
}
// Update the template
let poiTemplate = CPPointOfInterestTemplate(title: "EV Stations", pointsOfInterest: pointsOfInterest, selectedIndex: 0)
await MainActor.run {
self.interfaceController?.pushTemplate(poiTemplate, animated: true, completion: nil)
}
} catch {
}
}