fun applySystemBars()

in app/src/main/java/com/example/android/trackr/ui/utils/EdgeToEdge.kt [109:155]


fun applySystemBars(
    view: View,
    padLeft: Boolean,
    padTop: Boolean,
    padRight: Boolean,
    padBottom: Boolean,
    marginLeft: Boolean,
    marginTop: Boolean,
    marginRight: Boolean,
    marginBottom: Boolean
) {
    val adjustPadding = padLeft || padTop || padRight || padBottom
    val adjustMargins = marginLeft || marginTop || marginRight || marginBottom
    if (!(adjustPadding || adjustMargins)) {
        return
    }

    view.doOnApplyWindowInsets { v, insets, padding, margins ->
        val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
        if (adjustPadding) {
            val systemLeft = if (padLeft) systemBars.left else 0
            val systemTop = if (padTop) systemBars.top else 0
            val systemRight = if (padRight) systemBars.right else 0
            val systemBottom = if (padBottom) systemBars.bottom else 0
            v.updatePadding(
                left = padding.left + systemLeft,
                top = padding.top + systemTop,
                right = padding.right + systemRight,
                bottom = padding.bottom + systemBottom,
            )
        }
        if (adjustMargins) {
            val systemLeft = if (marginLeft) systemBars.left else 0
            val systemTop = if (marginTop) systemBars.top else 0
            val systemRight = if (marginRight) systemBars.right else 0
            val systemBottom = if (marginBottom) systemBars.bottom else 0
            v.updateLayoutParams<MarginLayoutParams> {
                leftMargin = margins.left + systemLeft
                topMargin = margins.top + systemTop
                rightMargin = margins.right + systemRight
                bottomMargin = margins.bottom + systemBottom
            }

        }
        insets // Always return the insets, so that children can also use them.
    }
}