import WidgetKit
import SwiftUI
import Foundation
struct dynamicIsland : Widget {
var body: some WidgetConfiguration{
ActivityConfiguration(for: PizzaDeliveryAttributes.self) { context in
LockScreenLiveActivityView(context: context)
} dynamicIsland: { context in
DynamicIsland{
DynamicIslandExpandedRegion(.leading){
Label("\(context.attributes.numberOfPizzas) Pizzas", systemImage: "bag")
.foregroundColor(.indigo)
.font(.title2)
}
DynamicIslandExpandedRegion(.trailing) {
Label {
Text(timerInterval: context.state.deliveryTimer , countsDown: true)
.multilineTextAlignment(.trailing)
.frame(width: 50)
.monospacedDigit()
} icon: {
Image(systemName: "timer")
.foregroundColor(.indigo)
}
.font(.title2)
}
DynamicIslandExpandedRegion(.center) {
Text("\(context.state.driverName) is on their way!")
.lineLimit(1)
.font(.caption)
}
DynamicIslandExpandedRegion(.bottom) {
Button {
// Deep link into your app.
} label: {
Label("Call driver", systemImage: "phone")
}
.foregroundColor(.indigo)
}
} compactLeading: {
// Create the compact leading presentation.
// ...
} compactTrailing: {
// Create the compact trailing presentation.
// ...
} minimal: {
// Create the minimal presentation.
// ...
}
.contentMargins(.trailing, 8, for: .expanded)
.keylineTint(.yellow)
}
}
}
struct LockScreenLiveActivityView: View {
let context: ActivityViewContext<PizzaDeliveryAttributes>
var body: some View {
VStack {
Spacer()
Text("\(context.state.driverName) is on their way with your pizza!")
Spacer()
HStack {
Spacer()
Label {
Text("\(context.attributes.numberOfPizzas) Pizzas")
} icon: {
Image(systemName: "bag")
.foregroundColor(.indigo)
}
.font(.title2)
Spacer()
Label {
Text(timerInterval: context.state.deliveryTimer, countsDown: true)
.multilineTextAlignment(.center)
.frame(width: 50)
.monospacedDigit()
} icon: {
Image(systemName: "timer")
.foregroundColor(.indigo)
}
.font(.title2)
Spacer()
}
Spacer()
}
.activitySystemActionForegroundColor(.indigo)
.activityBackgroundTint(.cyan)
}
}
struct PizzaDeliveryAttributes: ActivityAttributes {
public typealias PizzaDeliveryStatus = ContentState
public struct ContentState: Codable, Hashable {
var driverName: String
var deliveryTimer: ClosedRange<Date>
}
//var id = UUID()
var numberOfPizzas: Int
var totalAmount: String
var orderNumber: String
}
now check the code above
also I have this in my contentview
// ContentView.swift
// Widget_and_LiveActivities
//
// Created by Makwin Santhosh K on 20/11/22.
//
import SwiftUI
import WidgetKit
import ActivityKit
struct ContentView: View {
var body: some View {
ZStack {
VStack{
Button("Start Activity"){
AddLiveACtivity()
}
}
}
.navigationTitle("Live Activities")
}
}
func AddLiveACtivity() {
var future = Calendar.current.date(byAdding: .minute, value: (Int(minutes) ), to: Date())!
future = Calendar.current.date(byAdding: .second, value: (Int(seconds) ), to: future)!
let date = Date.now...future
let activityAttributes = PizzaDeliveryAttributes(numberOfPizzas: 10, totalAmount: "$100", orderNumber: "101")
let initialContentState = PizzaDeliveryAttributes.ContentState(driverName: "Makwin", deliveryTimer: date )
do {
let deliveryActivity = try Activity<PizzaDeliveryAttributes>.request(attributes: activityAttributes, contentState: initialContentState,pushType: nil)
print("Requested a pizza delivery Live Activity \(String(describing: deliveryActivity.id)).")
} catch (let error) {
print("Error requesting pizza delivery Live Activity \(error.localizedDescription).")
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}