Created
March 7, 2023 16:14
-
-
Save debojyoti452/8a2cdeb615b5b43a117ab2432f60af4a to your computer and use it in GitHub Desktop.
iOS ScreenTime API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
class Helper { | |
static func saveUserDefault(duration: String) { | |
UserDefaults.standard.set(duration, forKey: "key_452") | |
} | |
static func loadUserDefault() -> String { | |
let defaults = UserDefaults.standard | |
return defaults.string(forKey: "key_452") ?? "Not_Found" | |
} | |
static func save(activityReport: ActivityReport, completion: @escaping (Result<Int, Error>)->Void) { | |
DispatchQueue.global(qos: .background).async { | |
do { | |
let data = try JSONEncoder().encode(activityReport) | |
let outfile = try fileURL() | |
try data.write(to: outfile) | |
DispatchQueue.main.async { | |
completion(.success(452)) | |
} | |
} catch { | |
DispatchQueue.main.async { | |
completion(.failure(error)) | |
} | |
} | |
} | |
} | |
static func load(completion: @escaping (Result<ActivityReport?, Error>)->Void) { | |
DispatchQueue.global(qos: .background).async { | |
do { | |
let fileUrl = try fileURL() | |
guard let file = try? Data(contentsOf: fileUrl) else { | |
DispatchQueue.main.async { | |
completion(.success(nil)) | |
} | |
return | |
} | |
let decoded = try JSONDecoder().decode(ActivityReport.self, from: file) | |
DispatchQueue.main.async { | |
completion(.success(decoded)) | |
} | |
} catch { | |
DispatchQueue.main.async { | |
completion(.failure(error)) | |
} | |
} | |
} | |
} | |
private static func fileURL() throws -> URL { | |
let documentDirectory = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.*****.****") | |
let directory = documentDirectory!.appendingPathComponent("encouraged_seconds.json") | |
return directory | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import DeviceActivity | |
import SwiftUI | |
@main | |
struct Report: DeviceActivityReportExtension { | |
var body: some DeviceActivityReportScene { | |
// Create a report for each DeviceActivityReport.Context that your app supports. | |
TotalActivityReport { totalActivity in | |
Helper.saveUserDefault(duration: totalActivity.apps.first?.displayName ?? "NiL") | |
Helper.save(activityReport: totalActivity, completion: { result in | |
switch (result) { | |
case .failure(let error): | |
print("Error on save: \(error)") | |
case .success(let status): | |
print("Data Saved. \(status)") | |
} | |
}) | |
return TotalActivityView(activityReport: totalActivity) | |
} | |
// Add more reports here... | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import DeviceActivity | |
import SwiftUI | |
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: (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 app = AppDeviceActivity(id: bundle, displayName: appName, duration: duration, numberOfPickups: numberOfPickups) | |
list.append(app) | |
} | |
} | |
} | |
} | |
let reponse = ActivityReport(totalDuration: totalActivityDuration, apps: list) | |
Helper.save(activityReport: reponse, completion: { result in | |
switch (result) { | |
case .failure(let error): | |
print("Error on save: \(error)") | |
case .success(let status): | |
print("Data Saved. \(status)") | |
} | |
}) | |
return reponse | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment