Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: remove PostHogLabelTaggerView from hierarchy if not needed
Browse files Browse the repository at this point in the history
ioannisj committed Dec 5, 2024
1 parent 05fdc10 commit 271693b
Showing 2 changed files with 24 additions and 7 deletions.
27 changes: 22 additions & 5 deletions PostHog/Autocapture/SwiftUI/View+PostHogLabel.swift
Original file line number Diff line number Diff line change
@@ -38,12 +38,19 @@

func body(content: Content) -> some View {
content
.background(PostHogLabelViewTagger(label: label))
.background(viewTagger)
}

@ViewBuilder
private var viewTagger: some View {
if let label {
PostHogLabelViewTagger(label: label)
}
}
}

private struct PostHogLabelViewTagger: UIViewRepresentable {
let label: String?
let label: String

func makeUIView(context _: Context) -> PostHogLabelTaggerView {
PostHogLabelTaggerView(label: label)
@@ -55,16 +62,17 @@
}

private class PostHogLabelTaggerView: UIView {
private let label: String?
private let label: String
weak var taggedView: UIView?

init(label: String?) {
init(label: String) {
self.label = label
super.init(frame: .zero)
}

@available(*, unavailable)
required init?(coder _: NSCoder) {
self.label = nil
label = ""
super.init(frame: .zero)
}

@@ -84,6 +92,7 @@
// L PostHogLabelTaggerView (UIView) <- we are here
//
if let view = findCousinView(of: PostHogSwiftUITaggable.self) {
taggedView = view
view.postHogLabel = label
} else {
// just tag grandparent view
@@ -95,10 +104,18 @@
// L PostHogLabelViewTagger (ViewRepresentable)
// L PostHogLabelTaggerView (UIView) <- we are here
//
taggedView = superview?.superview
superview?.superview?.postHogLabel = label
}
}

override func removeFromSuperview() {
super.removeFromSuperview()
// remove custom label when removed from hierarchy
taggedView?.postHogLabel = nil
taggedView = nil
}

private func findCousinView<T>(of _: T.Type) -> T? {
for sibling in superview?.siblings() ?? [] {
if let match = sibling.child(of: T.self) {
4 changes: 2 additions & 2 deletions PostHog/PostHogMaskViewModifier.swift
Original file line number Diff line number Diff line change
@@ -41,7 +41,7 @@
}

private class PostHogMaskViewTaggerView: UIView {
weak private var maskedView: UIView?
private weak var maskedView: UIView?
override func layoutSubviews() {
super.layoutSubviews()
// ### Why grandparent view?
@@ -53,7 +53,7 @@
maskedView = superview?.superview
superview?.superview?.postHogNoCapture = true
}

override func removeFromSuperview() {
super.removeFromSuperview()
maskedView?.postHogNoCapture = false

0 comments on commit 271693b

Please sign in to comment.