Skip to content

Commit 166f5fa

Browse files
committed
refactor(mobile): keep word ranges with diff drawing
1 parent abbdf62 commit 166f5fa

2 files changed

Lines changed: 17 additions & 26 deletions

File tree

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import kotlin.math.min
1515
internal class ReviewDiffCanvasDrawing(context: Context) {
1616
private val density = context.resources.displayMetrics.density
1717
var theme: DiffTheme = DiffTheme.fallback("light")
18+
private val wordDiffRangesByRowId = mutableMapOf<String, List<DiffWordDiffRange>>()
1819

1920
val backgroundPaint = Paint()
2021
val borderPaint = Paint(Paint.ANTI_ALIAS_FLAG)
@@ -41,6 +42,14 @@ internal class ReviewDiffCanvasDrawing(context: Context) {
4142
uiPaint.typeface = Typeface.DEFAULT_BOLD
4243
}
4344

45+
fun mergeWordDiffRangesByRowId(patch: Map<String, List<DiffWordDiffRange>>) {
46+
wordDiffRangesByRowId.putAll(patch)
47+
}
48+
49+
fun clearWordDiffRanges() {
50+
wordDiffRangesByRowId.clear()
51+
}
52+
4453
fun fileHeaderChevronRect(top: Int, bottom: Int, style: DiffStyle): RectF {
4554
val centerY = (top + bottom) / 2f
4655
val left = style.fileHeaderHorizontalPaddingPx
@@ -203,9 +212,9 @@ internal class ReviewDiffCanvasDrawing(context: Context) {
203212
row: DiffRow,
204213
codeX: Float,
205214
top: Int,
206-
bottom: Int,
207-
ranges: List<DiffWordDiffRange>
215+
bottom: Int
208216
) {
217+
val ranges = wordDiffRangesByRowId[row.id] ?: row.wordDiffRanges
209218
if (ranges.isEmpty() || (row.change != "add" && row.change != "delete")) return
210219
val color = if (row.change == "add") theme.addBar else theme.deleteBar
211220
backgroundPaint.color = withAlpha(color, 71)

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

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,15 @@ class T3ReviewDiffView(context: Context, appContext: AppContext) : ExpoView(cont
7878
if (tokensResetKey == value) return
7979
tokensResetKey = value
8080
canvasView.tokensByRowId = emptyMap()
81-
canvasView.clearWordDiffRanges()
81+
canvasView.drawing.clearWordDiffRanges()
8282
}
8383

8484
fun setContentResetKey(value: String) {
8585
if (contentResetKey == value) return
8686
contentResetKey = value
8787
tokensDecodeGeneration += 1
8888
canvasView.tokensByRowId = emptyMap()
89-
canvasView.clearWordDiffRanges()
89+
canvasView.drawing.clearWordDiffRanges()
9090
lastVisibleFileId = null
9191
pendingInitialScroll = true
9292
canvasView.setVerticalOffset(0)
@@ -187,7 +187,8 @@ class T3ReviewDiffView(context: Context, appContext: AppContext) : ExpoView(cont
187187
canvasView.tokensByRowId = canvasView.tokensByRowId + decodedTokens
188188
}
189189
if (decodedWordDiffRanges.isNotEmpty()) {
190-
canvasView.mergeWordDiffRangesByRowId(decodedWordDiffRanges)
190+
canvasView.drawing.mergeWordDiffRangesByRowId(decodedWordDiffRanges)
191+
canvasView.invalidate()
191192
}
192193
}
193194
} catch (_: Exception) {
@@ -658,7 +659,7 @@ internal data class DiffStyle(
658659

659660
private class DiffCanvasView(context: Context) : View(context) {
660661
private val density = resources.displayMetrics.density
661-
private val drawing = ReviewDiffCanvasDrawing(context)
662+
val drawing = ReviewDiffCanvasDrawing(context)
662663
private val backgroundPaint = drawing.backgroundPaint
663664
private val borderPaint = drawing.borderPaint
664665
private val textPaint = drawing.textPaint
@@ -713,18 +714,6 @@ private class DiffCanvasView(context: Context) : View(context) {
713714
field = value
714715
invalidate()
715716
}
716-
private val wordDiffRangesByRowId = mutableMapOf<String, List<DiffWordDiffRange>>()
717-
718-
fun mergeWordDiffRangesByRowId(patch: Map<String, List<DiffWordDiffRange>>) {
719-
wordDiffRangesByRowId.putAll(patch)
720-
invalidate()
721-
}
722-
723-
fun clearWordDiffRanges() {
724-
wordDiffRangesByRowId.clear()
725-
invalidate()
726-
}
727-
728717
var viewedFileIds: Set<String> = emptySet()
729718
set(value) {
730719
field = value
@@ -1218,14 +1207,7 @@ private class DiffCanvasView(context: Context) : View(context) {
12181207
val tokens = tokensByRowId[row.id]
12191208
drawScrollableCode(canvas, top, bottom) { codeX ->
12201209
drawing.configureCodePaint(theme.text, 0, style)
1221-
drawing.drawWordDiffRanges(
1222-
canvas,
1223-
row,
1224-
codeX,
1225-
top,
1226-
bottom,
1227-
wordDiffRangesByRowId[row.id] ?: row.wordDiffRanges,
1228-
)
1210+
drawing.drawWordDiffRanges(canvas, row, codeX, top, bottom)
12291211
if (tokens.isNullOrEmpty()) {
12301212
canvas.drawText(row.content, codeX, centeredBaseline(top, bottom, textPaint), textPaint)
12311213
} else {

0 commit comments

Comments
 (0)