How to detect always-on display in swiftui in a Live Activity?

595 Views Asked by At

According to apple, a Live Activity should be able to detect always on display

https://developer.apple.com/documentation/activitykit/displaying-live-data-with-live-activities

On devices that include an Always-On display, the system dims the screen to preserve battery life and renders Live Activities on the Lock Screen as if in Dark Mode. Use SwiftUI’s isLuminanceReduced environment value to detect reduced luminance on devices with an Always-On display and use images that look great for reduced luminance.

But it seems to have no effect in live activities when displayed in the lock screen with AOD

  @Environment(\.isLuminanceReduced) var isLuminanceReduced
    Image("background1")
                            .resizable()
                            .aspectRatio(contentMode: .fit)
                            .frame(height: 400)
                            .offset(y:-40)
                            .brightness(isLuminanceReduced ? -0.5 : 0)

There's also nothing else in the context that seems to be able to tell as well https://developer.apple.com/documentation/widgetkit/activityviewcontext

1

There are 1 best solutions below

1
On

It isn't explicitly documented but context.cadence let you detect when the updates will happen slower than requested.

The system might use a cadence that’s slower than the schedule’s update rate. For example, a view on watchOS might remain visible when the user lowers their wrist, but update less frequently, and thus require less detail.

TimelineView(.periodic(from: startDate, by: 1.0)) { context in
    AnalogTimerView(
        date: context.date,
        showSeconds: context.cadence <= .seconds)
}

https://developer.apple.com/documentation/swiftui/timelineview

https://developer.apple.com/documentation/watchos-apps/updating-watchos-apps-with-timelines

https://developer.apple.com/documentation/watchos-apps/designing-your-app-for-the-always-on-state

Apple demonstrates its use in Build a workout app for Apple Watch