Hey, I wrote a function to draw custom borders, do...
# compose
c
Hey, I wrote a function to draw custom borders, does anyone has any suggestion on how I can improve this? In 🧵
Copy code
object Decoration {
    const val sideImageId = de.cicerohellmann.viking.R.drawable.side2
    const val cornerImageId = de.cicerohellmann.viking.R.drawable.corner2
}

@Composable
fun Modifier.addDecoration(
    sideImageId: Int = Decoration.sideImageId,
    cornerImageId: Int = Decoration.cornerImageId
) =
    then(object : CustomDrawModifier {
        val sideImage = ImageBitmap.imageResource(id = sideImageId)
        val cornerImage = ImageBitmap.imageResource(id = cornerImageId)
        val cornerTileSize = 40
        val sideTileSize = 20
        val combinationOfCorners = cornerTileSize * 2

        override fun ContentDrawScope.draw() {
            drawContent()
            val width = drawContext.size.width.toInt()
            val height = drawContext.size.height.toInt()

            fun positionOperation(position: Int) =
                cornerTileSize + position * sideTileSize

            val spaceBetweenHorizontalCorners = width - combinationOfCorners
            val spaceBetweenVerticalCorners = height - combinationOfCorners
            val horizontalAmount = (spaceBetweenHorizontalCorners / sideTileSize) + 1
            val verticalAmount = (spaceBetweenVerticalCorners / sideTileSize) + 1

            val sidesHorizontal = listOf(
                Pair("poop", 0),
                Pair("poop", height - sideTileSize),
                Pair(0, "poop"),
                Pair(width - sideTileSize, "poop"),
            )

            fun drawImage(x: Int, y: Int){
                drawImage(
                    image = sideImage,
                    dstOffset = IntOffset(x, y),
                    dstSize = IntSize(sideTileSize, sideTileSize)
                )
            }
            fun drawSides(sides: List<Pair<Any, Any>>) {
                sides.forEach { newPosition ->
                    when (newPosition.first) {
                        is String -> {
                            repeat(horizontalAmount) {
                                drawImage(x = positionOperation(it), y = newPosition.second as Int)
                            }
                        }
                        else -> {
                            repeat(verticalAmount) {
                                drawImage(x = newPosition.first as Int, y = positionOperation(it))
                            }
                        }
                    }
                }
            }

            drawSides(sidesHorizontal)

            drawCorners(width, cornerTileSize, height, cornerImage)
        }


    })

private fun ContentDrawScope.drawCorners(
    width: Int,
    cornerTileSize: Int,
    height: Int,
    cornerImage: ImageBitmap
) {
    val corners = listOf(
        Pair(0, 0),
        Pair((width - cornerTileSize), 0),
        Pair(0, (height - cornerTileSize)),
        Pair((width - cornerTileSize), (height - cornerTileSize))
    )

    corners.forEach {
        drawImage(
            image = cornerImage,
            dstOffset = IntOffset(it.first, it.second),
            dstSize = IntSize(cornerTileSize, cornerTileSize)
        )
    }
}