Skip to content

Commit

Permalink
Add support for custom startInteraction start input events
Browse files Browse the repository at this point in the history
  • Loading branch information
pyricau committed Jul 27, 2024
1 parent 2cfaf47 commit cec58e1
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
5 changes: 4 additions & 1 deletion papa/src/main/java/papa/InputTracker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class DeliveredInput<InputEventType : InputEvent>(
val eventUptime: Duration,
val framesSinceDelivery: Int,
private var endTrace: (() -> Unit)?
) {
) : InteractionStartInput {

fun takeOverTraceEnd(): (() -> Unit)? {
val transferedEndTrace = endTrace
Expand All @@ -65,6 +65,9 @@ class DeliveredInput<InputEventType : InputEvent>(
return copy
}

override val inputUptime: Duration
get() = eventUptime

override fun toString(): String {
return "DeliveredInput(" +
"deliveryUptime=${deliveryUptime.toString(MILLISECONDS)}, " +
Expand Down
20 changes: 13 additions & 7 deletions papa/src/main/java/papa/InteractionRuleClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ private class InteractionEngine<ParentEventType : Any>(
private var eventInScope: SentEvent<ParentEventType>? = null

inner class RealRunningInteraction(
override val interactionInput: DeliveredInput<out InputEvent>?,
override val interactionInput: InteractionStartInput?,
private val trace: InteractionTrace,
cancelTimeout: Duration
) : RunningInteraction<ParentEventType>, FinishingInteraction<ParentEventType>, FrameCallback {
Expand Down Expand Up @@ -224,13 +224,17 @@ private class InteractionEngine<ParentEventType : Any>(
override val interactionInput = interactionInput

override fun startInteraction(
interactionStartInput: InteractionStartInput?,
trace: InteractionTrace,
cancelTimeout: Duration,
): RunningInteraction<ParentEventType> {
// If the interaction input trace end isn't taken over yet, end it.
interactionInput?.takeOverTraceEnd()?.invoke()

val runningInteraction = RealRunningInteraction(
interactionInput = interactionInput,
// Default to custom interactionStartInput if passed in, otherwise default to delivered
// input (key or tap)
interactionInput = interactionStartInput ?: interactionInput,
trace = trace,
cancelTimeout
)
Expand Down Expand Up @@ -265,6 +269,7 @@ interface OnEventScope<ParentEventType : Any, EventType : ParentEventType> {
val event: EventType

fun startInteraction(
interactionStartInput: InteractionStartInput? = null,
trace: InteractionTrace = InteractionTrace.fromInputDelivered(event, interactionInput),
cancelTimeout: Duration = 1.minutes,
): RunningInteraction<ParentEventType>
Expand All @@ -277,7 +282,7 @@ interface OnEventScope<ParentEventType : Any, EventType : ParentEventType> {
fun recordSingleFrameInteraction(
trace: InteractionTrace = InteractionTrace.fromInputDelivered(event, interactionInput),
): FinishingInteraction<ParentEventType> {
return startInteraction(trace).finish()
return startInteraction(trace = trace).finish()
}

/**
Expand Down Expand Up @@ -334,7 +339,7 @@ fun interface InteractionTrace {

interface TrackedInteraction<EventType : Any> {
val sentEvents: List<SentEvent<EventType>>
val interactionInput: DeliveredInput<out InputEvent>?
val interactionInput: InteractionStartInput?

/**
* Adds the current event instance to the list of events (if not already added).
Expand All @@ -356,7 +361,7 @@ interface InteractionResultData<EventType : Any> {
* Interaction input that was automatically detected when the interaction started to be tracked,
* if any.
*/
val interactionInput: DeliveredInput<out InputEvent>?
val interactionInput: InteractionStartInput?

/**
* The number of frames that were rendered between the first and the last event in [sentEvents]
Expand All @@ -372,7 +377,7 @@ class SentEvent<EventType : Any>(
)

class InteractionResultDataPayload<EventType : Any>(
override val interactionInput: DeliveredInput<out InputEvent>?,
override val interactionInput: InteractionStartInput?,
override val runningFrameCount: Int,
override val sentEvents: List<SentEvent<EventType>>,
) : InteractionResultData<EventType>
Expand Down Expand Up @@ -414,6 +419,7 @@ sealed class InteractionResult<EventType : Any>(
is Canceled<*> -> "cancelReason=\"$cancelReason\", startToCancel=${
startToCancel.toString(MILLISECONDS)
}, "

is Finished<*> -> "startToEndFrameRendered=${
startToEndFrameRendered.toString(MILLISECONDS)
}, "
Expand All @@ -424,7 +430,7 @@ sealed class InteractionResult<EventType : Any>(
interactionInput?.let {
append(
"inputToStart=${
(sentEvents.first().uptime - it.eventUptime).toString(
(sentEvents.first().uptime - it.inputUptime).toString(
MILLISECONDS
)
}, "
Expand Down
7 changes: 7 additions & 0 deletions papa/src/main/java/papa/InteractionStartInput.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package papa

import kotlin.time.Duration

interface InteractionStartInput {
val inputUptime: Duration
}

0 comments on commit cec58e1

Please sign in to comment.