Skip to content

Commit defec05

Browse files
authored
fix: canvas nullability in ScreenStack for Android SDK 34 (#1795)
## Description Due to [Android docs](https://developer.android.com/reference/android/view/ViewGroup#drawChild(android.graphics.Canvas,%20android.view.View,%20long)) the canvas parameter in `ViewGroup.drawChild(canvas, ...)` method must not be null. This restriction was enforced on type level in sdk 34 thus our code was no longer valid. Closes #1787 ## Changes Removed type nullability using `!!` operator. I decided to go with this solution, because: 1. With current implementation `DrawingOp.canvas` has to be nullable (as after performing the draw we do not drop the object, but invalidate it instead and we do not want to hold no longer requried reference to canvas). 2. In case we actually pass null there -- we will crash anyway. ## Test code and steps to reproduce Set `compileSdkVersion` & `targetSdkVersion` to 34 in `TestExample` & see that the issue is resolved (& app works fine). ## Checklist - [ ] Included code example that can be used to test this change - [ ] Updated TS types - [ ] Updated documentation: <!-- For adding new props to native-stack --> - [ ] https://github.com/software-mansion/react-native-screens/blob/main/guides/GUIDE_FOR_LIBRARY_AUTHORS.md - [ ] https://github.com/software-mansion/react-native-screens/blob/main/native-stack/README.md - [ ] https://github.com/software-mansion/react-native-screens/blob/main/src/types.tsx - [ ] https://github.com/software-mansion/react-native-screens/blob/main/src/native-stack/types.tsx - [ ] Ensured that CI passes
1 parent cf20f8c commit defec05

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

android/src/main/java/com/swmansion/rnscreens/ScreenStack.kt

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -292,29 +292,30 @@ class ScreenStack(context: Context?) : ScreenContainer<ScreenStackFragment>(cont
292292
}
293293

294294
override fun drawChild(canvas: Canvas, child: View, drawingTime: Long): Boolean {
295-
drawingOps.add(obtainDrawingOp().set(canvas, child, drawingTime))
295+
drawingOps.add(
296+
obtainDrawingOp().apply {
297+
this.canvas = canvas
298+
this.child = child
299+
this.drawingTime = drawingTime
300+
}
301+
)
296302
return true
297303
}
298304

299305
private fun performDraw(op: DrawingOp) {
300-
super.drawChild(op.canvas, op.child, op.drawingTime)
306+
// Canvas parameter can not be null here https://developer.android.com/reference/android/view/ViewGroup#drawChild(android.graphics.Canvas,%20android.view.View,%20long)
307+
// So if we are passing null here, we would crash anyway
308+
super.drawChild(op.canvas!!, op.child, op.drawingTime)
301309
}
302310

303311
private fun obtainDrawingOp(): DrawingOp =
304-
if (drawingOpPool.isEmpty()) DrawingOp() else drawingOpPool.removeAt(drawingOpPool.size - 1)
312+
if (drawingOpPool.isEmpty()) DrawingOp() else drawingOpPool.removeLast()
305313

306314
private inner class DrawingOp {
307315
var canvas: Canvas? = null
308316
var child: View? = null
309317
var drawingTime: Long = 0
310318

311-
operator fun set(canvas: Canvas?, child: View?, drawingTime: Long): DrawingOp {
312-
this.canvas = canvas
313-
this.child = child
314-
this.drawingTime = drawingTime
315-
return this
316-
}
317-
318319
fun draw() {
319320
performDraw(this)
320321
canvas = null

0 commit comments

Comments
 (0)