Skip to content

Add device activity report - almost working #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions example/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -216,9 +216,8 @@ export default function App() {

<ReactNativeDeviceActivity.DeviceActivitySelectionView
style={{
width: 200,
height: 40,
alignSelf: "center",
width: 300,
height: 400,
borderRadius: 20,
borderWidth: 10,
borderColor: "rgb(213,85,37)",
Expand Down Expand Up @@ -247,13 +246,19 @@ export default function App() {
pointerEvents="none"
style={{
backgroundColor: "rgb(213,85,37)",
flex: 1,
alignItems: "center",
justifyContent: "center",
}}
>
<Text style={{ color: "white" }}>Select apps</Text>
</View>
<ReactNativeDeviceActivity.DeviceActivityReportView
style={{
width: 300,
height: 400,
}}
familyActivitySelection={familyActivitySelection}
/>
</ReactNativeDeviceActivity.DeviceActivitySelectionView>
<Text>{JSON.stringify(events, null, 2)}</Text>
<Text>{JSON.stringify(activities, null, 2)}</Text>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>com.apple.developer.family-controls</key>
<true/>
</dict>
</plist>
20 changes: 20 additions & 0 deletions example/ios/DeviceActivityReport/DeviceActivityReport.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//
// DeviceActivityReport.swift
// DeviceActivityReport
//
// Created by Robert Herber on 2024-11-10.
//

import DeviceActivity
import SwiftUI

@main
struct DeviceActivityReportUI: 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...
}
}
11 changes: 11 additions & 0 deletions example/ios/DeviceActivityReport/Info.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>EXAppExtensionAttributes</key>
<dict>
<key>EXExtensionPointIdentifier</key>
<string>com.apple.deviceactivityui.report-extension</string>
</dict>
</dict>
</plist>
46 changes: 46 additions & 0 deletions example/ios/DeviceActivityReport/TotalActivityReport.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//
// TotalActivityReport.swift
// DeviceActivityReport
//
// Created by Robert Herber on 2024-11-10.
//

import DeviceActivity
import SwiftUI

extension DeviceActivityReport.Context {
static let totalActivity = DeviceActivityReport.Context("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, .second]
formatter.unitsStyle = .abbreviated
formatter.zeroFormattingBehavior = .dropAll

let totalActivityDuration = await data.flatMap { $0.activitySegments }.reduce(0, {
$0 + $1.totalActivityDuration
})

/* let names = data.flatMap { point in
point.activitySegments.flatMap { segment in
segment.categories.flatMap { category in
category.applications.map { application in
application.application.localizedDisplayName
}
}
}
} */

return "\(String(describing: formatter.string(from: totalActivityDuration)))"
}
}
23 changes: 23 additions & 0 deletions example/ios/DeviceActivityReport/TotalActivityView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//
// TotalActivityView.swift
// DeviceActivityReport
//
// Created by Robert Herber on 2024-11-10.
//

import SwiftUI

struct TotalActivityView: View {
let totalActivity: String

var body: some View {
Text(totalActivity)
}
}

// In order to support previews for your extension's custom views, make sure its source files are
// members of your app's Xcode target as well as members of your extension's target. You can use
// Xcode's File Inspector to modify a file's Target Membership.
#Preview {
TotalActivityView(totalActivity: "1h 23m")
}
Loading
Loading