In my SwiftUI iOS 17 app, I am displaying a timer in my ActivityWidget so that the user can see a countdown timer when the phone screen is locked.
In order to display the widget, I am only passing the end Date to the Activity Widget and using the Text date style to display the timer countdown:
import ActivityKit
import WidgetKit
import SwiftUI
struct TimerAttributes: ActivityAttributes {
let endDate: Date
public struct ContentState: Codable, Hashable {
}
}
struct TimerWidgetLiveActivity: Widget {
var body: some WidgetConfiguration {
ActivityConfiguration(for: TimerAttributes.self) { context in
Text(context.attributes.endDate, style: .timer)
} dynamicIsland: { context in
DynamicIsland {
DynamicIslandExpandedRegion(.leading) {
}
DynamicIslandExpandedRegion(.center) {
}
DynamicIslandExpandedRegion(.trailing) {
}
DynamicIslandExpandedRegion(.bottom) {
}
} compactLeading: {
} compactTrailing: {
} minimal: {
}
}
}
}
The issue I am having is with the inconsistent format of the text. What I would like is for it to display like 03:32:44. It does display like this when the screen has just been locked. But when the screen goes to a deeper sleep in the new iPhone it displays ‘3 hours, 32 minutes’. I understand dropping the seconds to save battery but I want 3:32:– which is what the native timer app does. Is there anyway to change this formatting?
I have tried firing a timer and updating every second but that seems also like using too much battery and I prefer dropping seconds when it goes to a deep sleep which it seems to manage well itself using the Text styling. Just wonder what the best approach is to show timer with seconds if just sleep and timer replacing seconds with — in deep sleep instead of writing out ‘hours and minutes’ which takes too much space and ruins the limited UI.