Created
July 29, 2022 05:25
-
-
Save 1998code/f32848acf22dc776b168f82cd68e8c61 to your computer and use it in GitHub Desktop.
Pizza Order Function (iOS 16 Live Activities)
This file contains 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
func startDeliveryPizza() { | |
let pizzaDeliveryAttributes = PizzaDeliveryAttributes(numberOfPizzas: 1, totalAmount:"$99") | |
let initialContentState = PizzaDeliveryAttributes.PizzaDeliveryStatus(driverName: "TIM 👨🏻🍳", estimatedDeliveryTime: Date().addingTimeInterval(15 * 60)) | |
do { | |
let deliveryActivity = try Activity<PizzaDeliveryAttributes>.request( | |
attributes: pizzaDeliveryAttributes, | |
contentState: initialContentState, | |
pushType: nil) | |
print("Requested a pizza delivery Live Activity \(deliveryActivity.id)") | |
} catch (let error) { | |
print("Error requesting pizza delivery Live Activity \(error.localizedDescription)") | |
} | |
} | |
func updateDeliveryPizza() { | |
Task { | |
let updatedDeliveryStatus = PizzaDeliveryAttributes.PizzaDeliveryStatus(driverName: "TIM 👨🏻🍳", estimatedDeliveryTime: Date().addingTimeInterval(60 * 60)) | |
for activity in Activity<PizzaDeliveryAttributes>.activities{ | |
await activity.update(using: updatedDeliveryStatus) | |
} | |
} | |
} | |
func stopDeliveryPizza() { | |
Task { | |
for activity in Activity<PizzaDeliveryAttributes>.activities{ | |
await activity.end(dismissalPolicy: .immediate) | |
} | |
} | |
} | |
func showAllDeliveries() { | |
Task { | |
for activity in Activity<PizzaDeliveryAttributes>.activities { | |
print("Pizza delivery details: \(activity.id) -> \(activity.attributes)") | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment