Update / End live activity

Hi! I'm working on a timer app. In the app I want that when you start a timer, there also starts a live activity showcasing the remaining time.

So for I have got so far that the live activity starts when the timer start, but I don't know how to do the next parts.

So I got a couple of questions:

  1. How do I stop the live activity when the timer ends?
  2. How do I update the live activity (for example if I want to change the title while the timer is counting down?)

-> I've found two functions online but they don't work for me. I've included them below.

.

My code so far that works:

The file of the live activity

import SwiftUI
import WidgetKit
import ActivityKit

struct TimersWidgetLiveActivitiesView: View {
    let context: ActivityViewContext<TimerAttributes>
    
    private func ColorTheme() -> Int {
        let defaults = UserDefaults(suiteName: "group.MeesAkveld.ShareDefaults")
        return defaults?.integer(forKey: "ColorTheme") ?? 1
    }
    
    var body: some View {
            
        ZStack{
            
            Rectangle()
                .foregroundColor(.blue)
            
            HStack(){
                Image(systemName: context.state.icon)
                Text(context.state.title == "" ? "Timer" : context.state.title)
                
                Spacer()

                Text(context.state.time , style: .timer)
            }
            .padding()
            .foregroundColor(.white)
        }
    }
    
}


struct TimersWidgetLiveActivities: Widget {
    
    let kind: String = "TimersWidgetLiveActivities"
    
    var body: some WidgetConfiguration {
        
        ActivityConfiguration(for: TimerAttributes.self) { context in
            TimersWidgetLiveActivitiesView(context: context)
        } dynamicIsland: { context in
            DynamicIsland {
               
                DynamicIslandExpandedRegion(.leading) {
                    Text("Leading")
                }
                DynamicIslandExpandedRegion(.trailing) {
                    Text("Trailing")
                }
                DynamicIslandExpandedRegion(.bottom) {
                    Text("Bottom")
                }
            } compactLeading: {
                Text("L")
            } compactTrailing: {
                Text("T")
            } minimal: {
                Text("Min")
            }
        
        }
        
    }
    
}

.

The file of the TimerAttributes

import SwiftUI
import ActivityKit

struct TimerAttributes: ActivityAttributes {
    
    public typealias TimerStatus = ContentState
    
    public struct ContentState: Codable, Hashable {
        var time = Date()
        var title = String()
        var icon = String()
        
    }
    
}

.

The function that I use to start the Live Activity:

func startActivityT1() {
        let attributes = TimerAttributes()
        let state = TimerAttributes.TimerStatus(time: Date().addingTimeInterval(Double(vm.totalSecondsFunc() + 3600)), title: "Timer", icon: "timer")
        
        activity = try? Activity<TimerAttributes>.request(attributes: attributes, contentState: state, pushType: nil)
        
    }

.

The functions that I found online to update / end the live activity that don't work:

Stop Live Activity

func stopActivityT1() {
        
        let state = TimerAttributes.TimerStatus(time: .now, title: "Timer", icon:  "timer")

        Task {
            await activity?.end(using: state, dismissalPolicy: .immediate)
        }

    }

.

Update Live Activity

func updateActivityT1() {
        
        let state = TimerAttributes.TimerStatus(time: Date().addingTimeInterval(Double(vm.totalSecondsFunc() + 3600)), title: oD.titleT1, icon: oD.iconT1)
        
        Task {
            await activity?.update(using: state)
        }
        
    }

I also found this in the apple developer documentation, but I'm not sure how to implement this in the code:

.

func end(
         using contentState: Activity<Attributes>.ContentState? = nil,
         dismissalPolicy: ActivityUIDismissalPolicy = .default
        ) async

.

Link to the documentation: https://developer.apple.com/documentation/activitykit/activity/end(_:dismissalpolicy:)

Update / End live activity
 
 
Q