Skip to content
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

feat(replay): skip snapshot during view controller transitions #265

Merged
merged 4 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## Next

- fix: skip capturing a snapshot during view controller transitions ([#265](https://github.com/PostHog/posthog-ios/pull/265))

## 3.15.7 - 2024-11-25

- fix: detect and mask out system photo library and user photos ([#261](https://github.com/PostHog/posthog-ios/pull/261))
Expand Down
21 changes: 21 additions & 0 deletions PostHog/Replay/PostHogReplayIntegration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,11 @@
return nil
}

// this will bail on interactive animations (e.g edge slide)
if isAnimatingTransition(window) {
return nil
}
marandaneto marked this conversation as resolved.
Show resolved Hide resolved

var maskableWidgets: [CGRect] = []
var maskChildren = false
findMaskableWidgets(window, window, &maskableWidgets, &maskChildren)
Expand All @@ -386,6 +391,22 @@
return wireframe
}

/// Check if window's root view controller is animating a transition
private func isAnimatingTransition(_ window: UIWindow) -> Bool {
guard let viewController = window.rootViewController else { return false }

return isAnimatingTransition(viewController)
}

// Recursively check if a view controller (or any of its children) is currently animating a transition
// During a transition, a UIViewControllerTransitionCoordinator is assigned to a UIViewController which controls the overall transition
private func isAnimatingTransition(_ viewController: UIViewController) -> Bool {
if let isAnimated = viewController.transitionCoordinator?.isAnimated {
return isAnimated
}
return viewController.children.contains(where: isAnimatingTransition)
}
ioannisj marked this conversation as resolved.
Show resolved Hide resolved

private func isAssetsImage(_ image: UIImage) -> Bool {
// https://github.com/daydreamboy/lldb_scripts#9-pimage
// do not mask if its an asset image, likely not PII anyway
Expand Down
6 changes: 5 additions & 1 deletion PostHog/Replay/UIView+Util.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,11 @@
let renderer = UIGraphicsImageRenderer(size: size, format: rendererFormat)

let image = renderer.image { _ in
drawHierarchy(in: bounds, afterScreenUpdates: false)
// true for afterScreenUpdates so that we capture view *after* any pending animations are committed
// As a side effect, we need to pause view tracker temporarily to avoid recursive calls since this option will cause subviews to layout, which will then trigger another capture
ViewLayoutTracker.paused = true
drawHierarchy(in: bounds, afterScreenUpdates: true)
ViewLayoutTracker.paused = false
}

return image
Expand Down
5 changes: 4 additions & 1 deletion PostHog/Replay/ViewLayoutTracker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@
import UIKit

enum ViewLayoutTracker {
static var hasChanges = false
static var paused = false
private(set) static var hasChanges = false
private static var hasSwizzled = false

static func viewDidLayout(view _: UIView) {
guard !paused else { return }
hasChanges = true
}

static func clear() {
guard !paused else { return }
hasChanges = false
}

Expand Down
Loading