Skip to content

Commit

Permalink
fix: Check keyboard status during snapshot instead of using WindowIns…
Browse files Browse the repository at this point in the history
…ets listener (#77)
  • Loading branch information
marandaneto authored Jan 11, 2024
1 parent b507e70 commit 7735aca
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 28 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## Next

- fix: Check keyboard status during snapshot instead of using WindowInsets listener ([#77](https://github.com/PostHog/posthog-android/pull/77))

## 3.1.0 - 2024-01-08

- chore: Add mutations support to Session Recording ([#72](https://github.com/PostHog/posthog-android/pull/72))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,34 +111,6 @@ public class PostHogReplayIntegration(
}
}

// keyboard events
var open = false
ViewCompat.setOnApplyWindowInsetsListener(view) { _, insets ->
val imeVisible = insets.isVisible(WindowInsetsCompat.Type.ime())
if (open == imeVisible) {
return@setOnApplyWindowInsetsListener insets
}
open = imeVisible

val payload = mutableMapOf<String, Any>()
if (imeVisible) {
val imeHeight = insets.getInsets(WindowInsetsCompat.Type.ime()).bottom
payload["open"] = true
payload["height"] = imeHeight.densityValue(displayMetrics.density)
} else {
payload["open"] = false
}

val event = RRCustomEvent(
tag = "keyboard",
payload = payload,
config.dateProvider.currentTimeMillis(),
)
listOf(event).capture()

insets
}

val status = ViewTreeSnapshotStatus(listener)
decorViews[decorView] = status
}
Expand All @@ -159,6 +131,31 @@ public class PostHogReplayIntegration(
}
}

private fun detectKeyboardVisibility(view: View, visible: Boolean): Pair<Boolean, RRCustomEvent?> {
val insets = ViewCompat.getRootWindowInsets(view) ?: return Pair(visible, null)
val imeVisible = insets.isVisible(WindowInsetsCompat.Type.ime())
if (visible == imeVisible) {
return Pair(visible, null)
}

val payload = mutableMapOf<String, Any>()
if (imeVisible) {
val imeHeight = insets.getInsets(WindowInsetsCompat.Type.ime()).bottom
payload["open"] = true
payload["height"] = imeHeight.densityValue(displayMetrics.density)
} else {
payload["open"] = false
}

val event = RRCustomEvent(
tag = "keyboard",
payload = payload,
config.dateProvider.currentTimeMillis(),
)

return Pair(imeVisible, event)
}

private val onTouchEventListener = OnTouchEventListener { motionEvent ->
if (!isSessionReplayEnabled) {
return@OnTouchEventListener
Expand Down Expand Up @@ -341,6 +338,13 @@ public class PostHogReplayIntegration(
}
}

// detect keyboard visibility
val (visible, event) = detectKeyboardVisibility(view, status.keyboardVisible)
status.keyboardVisible = visible
event?.let {
events.add(it)
}

if (events.isNotEmpty()) {
events.capture()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ internal class ViewTreeSnapshotStatus(
val listener: NextDrawListener,
var sentFullSnapshot: Boolean = false,
var sentMetaEvent: Boolean = false,
var keyboardVisible: Boolean = false,
var lastSnapshot: RRWireframe? = null,
)

0 comments on commit 7735aca

Please sign in to comment.