Skip to content

Commit 20b9c56

Browse files
fix(mobile): Android diff no longer stalls when comment cards scroll into view
1 parent 36d4d71 commit 20b9c56

2 files changed

Lines changed: 26 additions & 4 deletions

File tree

‎apps/mobile/modules/t3-review-diff/android/src/main/java/expo/modules/t3reviewdiff/T3ReviewDiffView.kt‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1423,12 +1423,12 @@ private fun centeredBaseline(top: Int, bottom: Int, paint: Paint): Float {
14231423
return (top + bottom) / 2f - (metrics.ascent + metrics.descent) / 2f
14241424
}
14251425

1426-
private fun ellipsize(value: String, paint: Paint, width: Float): String {
1427-
if (paint.measureText(value) <= width) return value
1426+
internal fun ellipsize(value: String, paint: Paint, width: Float): String {
1427+
if (paint.breakText(value, true, width, null) == value.length) return value
14281428
val suffix = "..."
14291429
val available = max(0f, width - paint.measureText(suffix))
1430-
var end = value.length
1431-
while (end > 0 && paint.measureText(value, 0, end) > available) end -= 1
1430+
var end = paint.breakText(value, true, available, null)
1431+
if (end > 0 && Character.isHighSurrogate(value[end - 1])) end -= 1
14321432
return value.substring(0, end) + suffix
14331433
}
14341434

‎apps/mobile/modules/t3-review-diff/tests/android/src/test/kotlin/expo/modules/t3reviewdiff/ReviewDiffGeometryTest.kt‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package expo.modules.t3reviewdiff
22

33
import android.graphics.Color
4+
import android.graphics.Paint
45
import android.view.View
56
import kotlin.math.ceil
67
import expo.modules.kotlin.AppContext
@@ -219,6 +220,15 @@ class ReviewDiffGeometryTest {
219220
}
220221
}
221222

223+
@Test
224+
fun ellipsizingALongCommentDoesNotRemeasureItPerCharacter() {
225+
val paint = CountingPaint().apply { textSize = 24f }
226+
val result = ellipsize("A long pending review comment. ".repeat(200), paint, 300f)
227+
assertTrue("measured ${paint.measurements} times", paint.measurements <= 2)
228+
assertTrue(result.endsWith("..."))
229+
assertTrue(paint.measureText(result) <= 300f)
230+
}
231+
222232
private fun call(target: Any, name: String, vararg args: Any): Any? = target.javaClass
223233
.declaredMethods.single { it.name == name && it.parameterCount == args.size }
224234
.apply { isAccessible = true }.invoke(target, *args)
@@ -241,4 +251,16 @@ class ReviewDiffGeometryTest {
241251
return rows[index]
242252
}
243253
}
254+
255+
private class CountingPaint : Paint() {
256+
var measurements = 0
257+
override fun measureText(text: String): Float {
258+
measurements += 1
259+
return super.measureText(text)
260+
}
261+
override fun measureText(text: String, start: Int, end: Int): Float {
262+
measurements += 1
263+
return super.measureText(text, start, end)
264+
}
265+
}
244266
}

0 commit comments

Comments
 (0)