~ knotless Search/
↑↓ move↵ open⇧↵ all results

geesawra.industries/old-fella

Old people's social media app

0
1
main
45 lines · 1.5 KB Raw History
import OSLog
import SwiftUI

@MainActor
enum ViewUpdateMetrics {
    private static let enabled = ProcessInfo.processInfo.arguments.contains("-profile-view-updates")
    private static let log = OSLog(subsystem: "com.oldfella.app", category: "View updates")
    private static var counts: [String: Int] = [:]

    static func record(_ viewName: @autoclosure () -> String) {
        guard enabled else { return }
        let name = viewName()
        counts[name, default: 0] += 1
        os_signpost(.event, log: log, name: "View body", "%{public}@", name as NSString)
    }

    static func reset() { counts.removeAll() }

    static var snapshot: String {
        counts.keys.sorted().map { "\($0)=\(counts[$0, default: 0])" }.joined(separator: ";")
    }
}

struct ViewUpdateMetricsPanel: View {
    @State private var snapshot = ""

    var body: some View {
        if ProcessInfo.processInfo.arguments.contains("-ui-testing"),
            ProcessInfo.processInfo.arguments.contains("-profile-view-updates") {
            VStack {
                Button("Reset view update counts") {
                    ViewUpdateMetrics.reset()
                    snapshot = ""
                }
                Button("Read view update counts") { snapshot = ViewUpdateMetrics.snapshot }
                Text("View updates")
                    .accessibilityIdentifier("viewUpdateCounts")
                    .accessibilityValue(snapshot)
            }
            .font(.caption2)
            .padding(4)
            .background(.background)
        }
    }
}