Post

Replies

Boosts

Views

Activity

How to show icons apps when retrieving DeviceActivityReport
Actually, I'm developing a new app based on screen time API, so I implemented my report device extension like this 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: (ActivityReport) -> TotalActivityView func makeConfiguration(representing data: DeviceActivityResults<DeviceActivityData>) async -> ActivityReport { // Reformat the data into a configuration that can be used to create // the report's view. var res = "" var list: [AppDeviceActivity] = [] let totalActivityDuration = await data.flatMap { $0.activitySegments }.reduce(0, { $0 + $1.totalActivityDuration }) for await d in data { res += d.user.appleID!.debugDescription res += d.lastUpdatedDate.description for await a in d.activitySegments{ res += a.totalActivityDuration.formatted() for await c in a.categories { for await ap in c.applications { let appName = (ap.application.localizedDisplayName ?? "nil") let bundle = (ap.application.bundleIdentifier ?? "nil") let duration = ap.totalActivityDuration let numberOfPickups = ap.numberOfPickups let token = ap.application.token let app = AppDeviceActivity( id: bundle, displayName: appName, duration: duration, numberOfPickups: numberOfPickups, token: token ) list.append(app) } } } } return ActivityReport(totalDuration: totalActivityDuration, apps: list) } } Then TotalActivityView to visualize returned data like this // // TotalActivityView.swift // DemoScreenTimeReporter // // Created by Achraf Trabelsi on 17/06/2024. // import SwiftUI import ManagedSettings struct TotalActivityView: View { var activityReport: ActivityReport var body: some View { VStack { Spacer(minLength: 50) Text("Total Screen Time") Spacer(minLength: 10) Text(activityReport.totalDuration.stringFromTimeInterval()) List(activityReport.apps) { app in ListRow(eachApp: app) } } } } struct ListRow: View { var eachApp: AppDeviceActivity var body: some View { HStack { Text(eachApp.displayName) Spacer() Text(eachApp.id) Spacer() Text("\(eachApp.numberOfPickups)") Spacer() Text(String(eachApp.duration.formatted())) if let appToken = eachApp.token { Label(appToken).labelStyle(.iconOnly) } } } } But I got this issue : Cannot convert value of type 'ApplicationToken' (aka 'Token') to expected argument type 'LabelStyleConfiguration' So if anyone have an idea, how can we get application icons please ?
0
0
375
Jun ’24