-
Notifications
You must be signed in to change notification settings - Fork 0
Add Buffering, Configurable Endpoint, and Expensive Geometric Appearance Calculation #3
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -43,7 +43,14 @@ public struct AnalyticsContextProvider<Content: View>: View { | |
| } | ||
|
|
||
| extension View { | ||
| @available(*, deprecated, message: "Switch to analytics(_:logViewAppearances:)") | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are we using this anywhere? I can't seem to find any uses in Penn Mobile, so if we aren't then we may as well remove this entirely There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's not it just felt cool to deprecate my own code |
||
| @ViewBuilder public func analytics(_ subkey: String?, logViewAppearances: Bool) -> some View { | ||
| AnalyticsView(subkey: subkey, logViewAppearances: logViewAppearances ? .enabled : .disabled) { | ||
| self | ||
| } | ||
| } | ||
|
|
||
| @ViewBuilder public func analytics(_ subkey: String?, logViewAppearances: ViewAppearanceLoggingMode) -> some View { | ||
| AnalyticsView(subkey: subkey, logViewAppearances: logViewAppearances) { | ||
| self | ||
| } | ||
|
|
@@ -53,30 +60,39 @@ extension View { | |
| private struct AnalyticsView<Content: View>: View { | ||
| @Environment(\.labsAnalyticsPath) var path: String | ||
| let content: Content | ||
| let logViewAppearances: Bool | ||
| let logViewAppearances: ViewAppearanceLoggingMode | ||
| let subkey: String? | ||
| @State var key: String = "" | ||
| @State var onScreen: Bool = false | ||
| let platform = LabsPlatform.shared | ||
| init(subkey: String?, logViewAppearances: Bool, @ViewBuilder _ content: () -> Content) { | ||
| init(subkey: String?, logViewAppearances: ViewAppearanceLoggingMode, @ViewBuilder _ content: () -> Content) { | ||
| self.content = content() | ||
| self.subkey = subkey | ||
| self.logViewAppearances = logViewAppearances | ||
| } | ||
|
|
||
| var body: some View { | ||
| Group { | ||
|
Comment on lines
74
to
75
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think if you annotate the |
||
| if logViewAppearances { | ||
| if case .enabled = logViewAppearances { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we use a switch statement here instead |
||
| content | ||
| .onAppear { | ||
| guard let platform else { return } | ||
| Task { | ||
| await platform.analytics.record(AnalyticsValue(key: "\(key).appear", value: "1", timestamp: Date.now)) | ||
| } | ||
| onScreen = true | ||
| } | ||
| .onDisappear { | ||
| guard let platform else { return } | ||
| Task { | ||
| await platform.analytics.record(AnalyticsValue(key: "\(key).disappear", value: "1", timestamp: Date.now)) | ||
| onScreen = false | ||
| } | ||
| } else if case .enabledExpensive = logViewAppearances { | ||
| content | ||
| .background { | ||
| GeometryReader { proxy in | ||
| Color.clear | ||
| .onChange(of: proxy.frame(in: .global)) { | ||
| let frame = proxy.frame(in: .global) | ||
| let intersects = frame.intersects(.init(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)) | ||
| if intersects != onScreen { | ||
| onScreen = intersects | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } else { | ||
|
|
@@ -87,5 +103,16 @@ private struct AnalyticsView<Content: View>: View { | |
| .onAppear { | ||
| self.key = subkey == nil ? path : "\(path).\(subkey!)" | ||
| } | ||
| .onChange(of: onScreen) { | ||
| Task { | ||
| await platform?.analytics?.record(AnalyticsValue(key: "\(key).\(onScreen ? "appear" : "disappear")", value: "1", timestamp: Date.now)) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public enum ViewAppearanceLoggingMode { | ||
| case disabled, enabled, enabledExpensive | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should probably make |
||
|
|
||
| // enabledExpensive for more accurate processing, such as for portal posts where we actually want to be sure that it was on screen, instead of just in the VStack | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Have we ever considered just using DocC instead at this point?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did DocC commenting for a lot of stuff that's public facing, README was easier to maintain though.