Skip to content

Commit 43c0b3f

Browse files
Abbondanzometa-codesync[bot]
authored andcommitted
Replace !! operators with idiomatic Kotlin null handling
Summary: Replace all non-null assertion operators (`!!`) with idiomatic Kotlin alternatives following code review feedback. Use `checkNotNull()` for validation, safe call operators (`?.`) with early returns, and local variables to eliminate repeated null assertions. Also adopt Kotlin stdlib helpers like `isNullOrEmpty()` and AndroidX KTX extensions (`isNotEmpty()`, `isEmpty()`, `toDrawable()`) for cleaner, more type-safe code. Changelog: [Internal] Differential Revision: D107525522
1 parent 43fe8c1 commit 43c0b3f

1 file changed

Lines changed: 31 additions & 22 deletions

File tree

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/scroll/ReactHorizontalScrollView.kt

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import android.graphics.Canvas
1414
import android.graphics.Color
1515
import android.graphics.Point
1616
import android.graphics.Rect
17-
import android.graphics.drawable.ColorDrawable
1817
import android.graphics.drawable.Drawable
1918
import android.os.Build
2019
import android.view.FocusFinder
@@ -26,8 +25,11 @@ import android.view.ViewParent
2625
import android.view.accessibility.AccessibilityNodeInfo
2726
import android.widget.HorizontalScrollView
2827
import android.widget.OverScroller
28+
import androidx.core.graphics.drawable.toDrawable
2929
import androidx.core.view.ViewCompat
3030
import androidx.core.view.ViewCompat.FocusDirection
31+
import androidx.core.view.isEmpty
32+
import androidx.core.view.isNotEmpty
3133
import com.facebook.common.logging.FLog
3234
import com.facebook.react.R
3335
import com.facebook.react.common.ReactConstants
@@ -426,10 +428,11 @@ constructor(context: Context, private val fpsListener: FpsListener? = null) :
426428
config: MaintainVisibleScrollPositionHelper.Config?
427429
) {
428430
if (config != null && maintainVisibleContentPositionHelper == null) {
429-
maintainVisibleContentPositionHelper = MaintainVisibleScrollPositionHelper(this, true)
430-
maintainVisibleContentPositionHelper!!.start()
431-
} else if (config == null && maintainVisibleContentPositionHelper != null) {
432-
maintainVisibleContentPositionHelper!!.stop()
431+
val helper = MaintainVisibleScrollPositionHelper(this, true)
432+
maintainVisibleContentPositionHelper = helper
433+
helper.start()
434+
} else if (config == null) {
435+
maintainVisibleContentPositionHelper?.stop()
433436
maintainVisibleContentPositionHelper = null
434437
}
435438
maintainVisibleContentPositionHelper?.let { it.config = config }
@@ -691,7 +694,7 @@ constructor(context: Context, private val fpsListener: FpsListener? = null) :
691694
if (pagingEnabled) {
692695
pagedArrowScrolling = true
693696

694-
if (childCount > 0) {
697+
if (isNotEmpty()) {
695698
val currentFocused = findFocus()
696699
val nextFocused = FocusFinder.getInstance().findNextFocus(this, currentFocused, direction)
697700
val rootChild = getContentView()
@@ -916,7 +919,7 @@ constructor(context: Context, private val fpsListener: FpsListener? = null) :
916919

917920
Systrace.beginSection(Systrace.TRACE_TAG_REACT, "ReactHorizontalScrollView.updateClippingRect")
918921
try {
919-
val rect = clippingRect!!
922+
val rect = checkNotNull(clippingRect)
920923
ReactClippingViewGroupHelper.calculateClippingRect(this, rect)
921924
val cv = getContentView()
922925
if (cv is ReactClippingViewGroup) {
@@ -928,7 +931,7 @@ constructor(context: Context, private val fpsListener: FpsListener? = null) :
928931
}
929932

930933
override fun getClippingRect(outClippingRect: Rect) {
931-
outClippingRect.set(clippingRect!!)
934+
outClippingRect.set(checkNotNull(clippingRect))
932935
}
933936

934937
override fun getChildVisibleRect(child: View, r: Rect, offset: android.graphics.Point?): Boolean {
@@ -942,7 +945,7 @@ constructor(context: Context, private val fpsListener: FpsListener? = null) :
942945
public open fun setEndFillColor(color: Int) {
943946
if (color != endFillColor) {
944947
endFillColor = color
945-
endBackground = ColorDrawable(endFillColor)
948+
endBackground = endFillColor.toDrawable()
946949
}
947950
}
948951

@@ -1002,26 +1005,31 @@ constructor(context: Context, private val fpsListener: FpsListener? = null) :
10021005

10031006
private fun enableFpsListener() {
10041007
if (isScrollPerfLoggingEnabled()) {
1005-
fpsListener!!.enable(scrollPerfTag!!)
1008+
val listener = fpsListener ?: return
1009+
val perfTag = scrollPerfTag.takeUnless { it.isNullOrEmpty() } ?: return
1010+
listener.enable(perfTag)
10061011
}
10071012
}
10081013

10091014
private fun disableFpsListener() {
10101015
if (isScrollPerfLoggingEnabled()) {
1011-
fpsListener!!.disable(scrollPerfTag!!)
1016+
val listener = fpsListener ?: return
1017+
val perfTag = scrollPerfTag.takeUnless { it.isNullOrEmpty() } ?: return
1018+
listener.disable(perfTag)
10121019
}
10131020
}
10141021

10151022
private fun isScrollPerfLoggingEnabled(): Boolean {
1016-
return fpsListener != null && scrollPerfTag != null && scrollPerfTag!!.isNotEmpty()
1023+
return fpsListener != null && !scrollPerfTag.isNullOrEmpty()
10171024
}
10181025

10191026
override fun draw(canvas: Canvas) {
10201027
if (endFillColor != Color.TRANSPARENT) {
10211028
val content = getContentView()
1022-
if (endBackground != null && content != null && content.right < width) {
1023-
endBackground!!.setBounds(content.right, 0, width, height)
1024-
endBackground!!.draw(canvas)
1029+
val bg = endBackground
1030+
if (bg != null && content != null && content.right < width) {
1031+
bg.setBounds(content.right, 0, width, height)
1032+
bg.draw(canvas)
10251033
}
10261034
}
10271035
super.draw(canvas)
@@ -1183,7 +1191,7 @@ constructor(context: Context, private val fpsListener: FpsListener? = null) :
11831191
FLog.i(TAG, "smoothScrollAndSnap[%d] velocityX %d", id, velocityX)
11841192
}
11851193

1186-
if (childCount <= 0) return
1194+
if (isEmpty()) return
11871195

11881196
// pagingEnabled only allows snapping one interval at a time
11891197
if (snapInterval == 0 && snapOffsets == null && snapToAlignment == SNAP_ALIGNMENT_DISABLED) {
@@ -1212,12 +1220,13 @@ constructor(context: Context, private val fpsListener: FpsListener? = null) :
12121220
}
12131221

12141222
// get the nearest snap points to the target offset
1215-
if (snapOffsets != null && snapOffsets!!.isNotEmpty()) {
1216-
firstOffset = snapOffsets!![0]
1217-
lastOffset = snapOffsets!![snapOffsets!!.size - 1]
1223+
val offsets = snapOffsets
1224+
if (!offsets.isNullOrEmpty()) {
1225+
firstOffset = offsets[0]
1226+
lastOffset = offsets[offsets.size - 1]
12181227

1219-
for (i in snapOffsets!!.indices) {
1220-
val offset = snapOffsets!![i]
1228+
for (i in offsets.indices) {
1229+
val offset = offsets[i]
12211230
if (offset <= targetOffset) {
12221231
if (targetOffset - offset < targetOffset - smallerOffset) {
12231232
smallerOffset = offset
@@ -1253,7 +1262,7 @@ constructor(context: Context, private val fpsListener: FpsListener? = null) :
12531262
maximumOffset,
12541263
)
12551264
} else {
1256-
val cv = getContentView() as ViewGroup
1265+
val cv = getContentView() as? ViewGroup ?: return
12571266
var smallerChildOffset = largerOffset
12581267
var largerChildOffset = smallerOffset
12591268
for (i in 0 until cv.childCount) {

0 commit comments

Comments
 (0)