Adarsh
Adarsh

Reputation: 125

How to stop Text timer in ios swiftUI widget

I have a live activity and dynamic island widget with text component timer. Widget is using ActivityConfiguration and how can i stop the timer or close the widget once it reaches specified date?

This is how the timer is implemented

ActivityConfiguration(for: ExtensionAttributes.self) { context in
    Text(context.attributes.date, style: .timer)
}

And when the date reaches date + 2 hours, i need to stop this timer or remove the widget. How can i achieve this?

Upvotes: 0

Views: 340

Answers (1)

Krati Mittal
Krati Mittal

Reputation: 11

You can display a timer in the live activity by using the Text initialiser that takes in a date range like so;

struct TimerAttributes: ActivityAttributes {
public typealias TimerStatus = ContentState

public struct ContentState: Codable, Hashable {
    var startDate: Date
}

var timeRange: ClosedRange<Date>
var timerName: String

}

struct TimerActivityView : View {
let context: ActivityViewContext<TimerAttributes>

var body: some View {
    VStack {
        Text(timerInterval: context.attributes.timeRange,
                 countsDown: false)
    }
}

}

Upvotes: 1

Related Questions