Hello, I am trying to display basic screen time data on my main screen. On the initial load of the screen, the DeviceActivityReport renders correctly and visible, but after being in the background and coming back to the app, the whole view is just blank. I don't think I'm doing anything special. Is this a known bug?
@main
struct MyActivityReportExtension: DeviceActivityReportExtension {
var body: some DeviceActivityReportScene {
// Create a report for each DeviceActivityReport.Context that your app supports.
TotalActivityReport { totalActivity in
TotalActivityView(totalActivity: totalActivity)
}
// Add more reports here...
}
}
extension DeviceActivityReport.Context {
// If your app initializes a DeviceActivityReport with this context, then the system will use
// your extension's corresponding DeviceActivityReportScene to render the contents of the
// report.
static let totalActivity = Self("Total Activity")
}
struct TotalActivityReport: DeviceActivityReportScene {
// Define which context your scene will represent.
let context: DeviceActivityReport.Context = .totalActivity
// Define the custom configuration and the resulting view for this report.
let content: (String) -> TotalActivityView
func makeConfiguration(representing data: DeviceActivityResults<DeviceActivityData>) async -> String {
// Reformat the data into a configuration that can be used to create
// the report's view.
let formatter = DateComponentsFormatter()
formatter.allowedUnits = [.day, .hour, .minute]
formatter.unitsStyle = .abbreviated
formatter.zeroFormattingBehavior = .dropAll
let totalActivityDuration = await data.flatMap { $0.activitySegments }.reduce(0, {
$0 + $1.totalActivityDuration
})
return formatter.string(from: totalActivityDuration) ?? "No activity data"
}
}
struct TotalActivityView: View {
let totalActivity: String
var body: some View {
VStack(alignment: .center, spacing: 4) {
Text("Screen Time")
.font(.system(size: 14, weight: .regular))
.foregroundColor(.secondary)
.frame(maxWidth: .infinity, // stretch to the full cell width
alignment: .center)
Text(totalActivity)
.font(.system(size: 18, weight: .medium))
.foregroundColor(.primary)
}
}
}
And I am using it in my main view:
private var analyticsSection: some View {
HStack(spacing: 24) {
// Some View
DeviceActivityReport(DeviceActivityReport.Context(rawValue: "Total Activity"), filter: DeviceActivityFilter(
segment: .weekly(
during: Calendar.current.dateInterval(
of: .weekOfYear, for: .now
)!
),
users: .all,
devices: .init([.iPhone, .iPad]),
))
.frame(maxWidth: .infinity)
// another view
}
.frame(maxWidth: .infinity, maxHeight: showAnalytics ? 58 : 0)
.padding(.horizontal, showAnalytics ? 24 : 0)
.opacity(showAnalytics ? 1.0 : 0.0)
.clipped()
}
Selecting any option will automatically load the page