How can I re-create a CLKRelativeDateTextProvider in a WidgetKit complication?

38 views Asked by At

I'm in the process of migrating one of my app's Apple Watch complications from ClockKit to WidgetKit.

In my ClockKit complications, there are some cases where a show the relative time since the last event in the app (for example, "25MIN").

With ClockKit, this was quite straightforward using a CLKTextProvider designed for this exact purpose:

CLKRelativeDateTextProvider(date: date, style: .offsetShort, units: [.hour, .minute])

This has always worked great, since it lets me specify that hours and minutes should be used, but not seconds, since they're not relevant here.

How can I accomplish the same thing with WidgetKit and SwiftUI?

The closest thing I've found is this:

Text(date, style: .relative)

It shows, for example: 14MIN 8SEC

This is not what I want, since it shows seconds. It makes the complication look messy with all the extra information, and is distracting because it updates every second until an hour has passed.

I suppose I could write my own logic to create a new timeline entry every minute to show the number of hours and minutes since the last update, but since CLKTextProvider works so nicely I wanted to check whether there is some way to accomplish the same thing without creating a new entry for every minute.

There seem to be some more customizable options with date formatters, but I haven't found a way to combine this with the SwiftUI version that updates automatically on the Apple Watch face: https://developer.apple.com/documentation/foundation/date/relativeformatstyle/3766585-presentation

0

There are 0 answers