Skip to content

Commit

Permalink
RUM-7024: Add ImageViewMapper internal constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanmos committed Nov 13, 2024
1 parent ee99938 commit 224eb7c
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import com.datadog.android.sessionreplay.internal.resources.ResourceDataStoreMan
import com.datadog.android.sessionreplay.internal.storage.RecordWriter
import com.datadog.android.sessionreplay.internal.storage.ResourcesWriter
import com.datadog.android.sessionreplay.internal.time.SessionReplayTimeProvider
import com.datadog.android.sessionreplay.internal.utils.ImageViewUtils
import com.datadog.android.sessionreplay.recorder.OptionSelectorDetector
import com.datadog.android.sessionreplay.recorder.mapper.EditTextMapper
import com.datadog.android.sessionreplay.recorder.mapper.ImageViewMapper
Expand Down Expand Up @@ -96,10 +97,11 @@ internal class DefaultRecorderProvider(
val viewBoundsResolver: ViewBoundsResolver = DefaultViewBoundsResolver
val drawableToColorMapper: DrawableToColorMapper = DrawableToColorMapper.getDefault()
val imageViewMapper = ImageViewMapper(
viewIdentifierResolver,
colorStringFormatter,
viewBoundsResolver,
drawableToColorMapper
viewIdentifierResolver = viewIdentifierResolver,
colorStringFormatter = colorStringFormatter,
viewBoundsResolver = viewBoundsResolver,
drawableToColorMapper = drawableToColorMapper,
imageViewUtils = ImageViewUtils
)
val textViewMapper = TextViewMapper<TextView>(
viewIdentifierResolver,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,13 @@ internal object ImageViewUtils {
}

internal fun resolveContentRectWithScaling(
view: ImageView,
imageView: ImageView,
drawable: Drawable
): Rect {
val drawableWidthPx = drawable.intrinsicWidth
val drawableHeightPx = drawable.intrinsicHeight

val parentRect = resolveParentRectAbsPosition(view)
val parentRect = resolveParentRectAbsPosition(imageView)

val childRect = Rect(
0,
Expand All @@ -79,7 +79,7 @@ internal object ImageViewUtils {

val resultRect: Rect

when (view.scaleType) {
when (imageView.scaleType) {
ImageView.ScaleType.FIT_START -> {
val contentRect = scaleRectToFitParent(parentRect, childRect)
resultRect = positionRectAtStart(parentRect, contentRect)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,38 @@ import com.datadog.android.sessionreplay.utils.ViewIdentifierResolver
/**
* A [WireframeMapper] implementation to map an [ImageView] component.
*/
open class ImageViewMapper(
viewIdentifierResolver: ViewIdentifierResolver,
colorStringFormatter: ColorStringFormatter,
viewBoundsResolver: ViewBoundsResolver,
drawableToColorMapper: DrawableToColorMapper
) : BaseAsyncBackgroundWireframeMapper<ImageView>(
viewIdentifierResolver,
colorStringFormatter,
viewBoundsResolver,
drawableToColorMapper
) {
open class ImageViewMapper : BaseAsyncBackgroundWireframeMapper<ImageView> {
private val imageViewUtils: ImageViewUtils

@Suppress("Unused") // used by external mappers
constructor(
viewIdentifierResolver: ViewIdentifierResolver,
colorStringFormatter: ColorStringFormatter,
viewBoundsResolver: ViewBoundsResolver,
drawableToColorMapper: DrawableToColorMapper
) : this(
viewIdentifierResolver,
colorStringFormatter,
viewBoundsResolver,
drawableToColorMapper,
ImageViewUtils
)

internal constructor(
viewIdentifierResolver: ViewIdentifierResolver,
colorStringFormatter: ColorStringFormatter,
viewBoundsResolver: ViewBoundsResolver,
drawableToColorMapper: DrawableToColorMapper,
imageViewUtils: ImageViewUtils
) : super(
viewIdentifierResolver,
colorStringFormatter,
viewBoundsResolver,
drawableToColorMapper
) {
this.imageViewUtils = imageViewUtils
}

@UiThread
override fun map(
view: ImageView,
Expand All @@ -48,17 +69,18 @@ open class ImageViewMapper(

val drawable = view.drawable?.current ?: return wireframes

val parentRect = ImageViewUtils.resolveParentRectAbsPosition(view)
val contentRect = ImageViewUtils.resolveContentRectWithScaling(view, drawable)
val parentRect = imageViewUtils.resolveParentRectAbsPosition(view)
val contentRect = imageViewUtils.resolveContentRectWithScaling(view, drawable)

val resources = view.resources
val density = resources.displayMetrics.density

val clipping = if (view.cropToPadding) {
ImageViewUtils.calculateClipping(parentRect, contentRect, density)
imageViewUtils.calculateClipping(parentRect, contentRect, density)
} else {
null
}

val contentXPosInDp = contentRect.left.densityNormalized(density).toLong()
val contentYPosInDp = contentRect.top.densityNormalized(density).toLong()
val contentWidthPx = contentRect.width()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ internal class ImageViewUtilsTest {

// When
val result = testedImageViewUtils.resolveContentRectWithScaling(
view = mockImageView,
imageView = mockImageView,
drawable = mockDrawable
)

Expand Down Expand Up @@ -326,7 +326,7 @@ internal class ImageViewUtilsTest {

// When
val result = testedImageViewUtils.resolveContentRectWithScaling(
view = mockImageView,
imageView = mockImageView,
drawable = mockDrawable
)

Expand Down Expand Up @@ -374,7 +374,7 @@ internal class ImageViewUtilsTest {

// When
val result = testedImageViewUtils.resolveContentRectWithScaling(
view = mockImageView,
imageView = mockImageView,
drawable = mockDrawable
)

Expand Down Expand Up @@ -422,7 +422,7 @@ internal class ImageViewUtilsTest {

// When
val result = testedImageViewUtils.resolveContentRectWithScaling(
view = mockImageView,
imageView = mockImageView,
drawable = mockDrawable
)

Expand Down Expand Up @@ -477,7 +477,7 @@ internal class ImageViewUtilsTest {

// When
val result = testedImageViewUtils.resolveContentRectWithScaling(
view = mockImageView,
imageView = mockImageView,
drawable = mockDrawable
)

Expand Down Expand Up @@ -525,7 +525,7 @@ internal class ImageViewUtilsTest {

// When
val result = testedImageViewUtils.resolveContentRectWithScaling(
view = mockImageView,
imageView = mockImageView,
drawable = mockDrawable
)

Expand Down Expand Up @@ -577,7 +577,7 @@ internal class ImageViewUtilsTest {

// When
val result = testedImageViewUtils.resolveContentRectWithScaling(
view = mockImageView,
imageView = mockImageView,
drawable = mockDrawable
)

Expand Down Expand Up @@ -627,7 +627,7 @@ internal class ImageViewUtilsTest {

// When
val result = testedImageViewUtils.resolveContentRectWithScaling(
view = mockImageView,
imageView = mockImageView,
drawable = mockDrawable
)

Expand Down Expand Up @@ -673,7 +673,7 @@ internal class ImageViewUtilsTest {

// When
val result = testedImageViewUtils.resolveContentRectWithScaling(
view = mockImageView,
imageView = mockImageView,
drawable = mockDrawable
)

Expand Down Expand Up @@ -718,7 +718,7 @@ internal class ImageViewUtilsTest {

// When
val result = testedImageViewUtils.resolveContentRectWithScaling(
view = mockImageView,
imageView = mockImageView,
drawable = mockDrawable
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,10 +193,11 @@ internal class ImageViewMapperTest {
)

testedMapper = ImageViewMapper(
mockViewIdentifierResolver,
mockColorStringFormatter,
mockViewBoundsResolver,
mockDrawableToColorMapper
viewIdentifierResolver = mockViewIdentifierResolver,
colorStringFormatter = mockColorStringFormatter,
viewBoundsResolver = mockViewBoundsResolver,
drawableToColorMapper = mockDrawableToColorMapper,
imageViewUtils = stubImageViewUtils
)
}

Expand Down

0 comments on commit 224eb7c

Please sign in to comment.