All Downloads are FREE. Search and download functionalities are using the official Maven repository.

main.com.sceyt.chatuikit.presentation.components.media.ViewExt.kt Maven / Gradle / Ivy

package com.sceyt.chatuikit.presentation.components.media

import android.view.View
import android.view.ViewGroup
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.updateLayoutParams

fun View.applySystemWindowInsetsPadding(
        applyLeft: Boolean = false,
        applyTop: Boolean = false,
        applyRight: Boolean = false,
        applyBottom: Boolean = false,
) {
    doOnApplyWindowInsets { view, insets, padding, _ ->
        val left = if (applyLeft) insets.getInsetsIgnoringVisibility(WindowInsetsCompat.Type.systemBars()).left else 0
        val top = if (applyTop) insets.getInsetsIgnoringVisibility(WindowInsetsCompat.Type.systemBars()).top else 0
        val right = if (applyRight) insets.getInsetsIgnoringVisibility(WindowInsetsCompat.Type.systemBars()).right else 0
        val bottom = if (applyBottom) insets.getInsetsIgnoringVisibility(WindowInsetsCompat.Type.systemBars()).bottom else 0

        view.setPadding(
            padding.left + left,
            padding.top + top,
            padding.right + right,
            padding.bottom + bottom
        )
    }
}

fun View.applySystemWindowInsetsMargin(
        applyLeft: Boolean = false,
        applyTop: Boolean = false,
        applyRight: Boolean = false,
        applyBottom: Boolean = false,
        userDefaultMargins: Boolean = true
) {
    doOnApplyWindowInsets { view, insets, _, margin ->
        val left = if (applyLeft) insets.getInsets(WindowInsetsCompat.Type.systemBars()).left else 0
        val top = if (applyTop) insets.getInsets(WindowInsetsCompat.Type.systemBars()).top else 0
        val right = if (applyRight) insets.getInsets(WindowInsetsCompat.Type.systemBars()).right else 0
        val bottom = if (applyBottom) insets.getInsets(WindowInsetsCompat.Type.systemBars()).bottom else 0

        view.updateLayoutParams {
            leftMargin = if (userDefaultMargins) margin.left else 0 + left
            topMargin = if (userDefaultMargins) margin.top else 0 + top
            rightMargin = if (userDefaultMargins) margin.right else 0 + right
            bottomMargin = if (userDefaultMargins) margin.bottom else 0 + bottom
        }
    }
}

fun View.doOnApplyWindowInsets(
        block: (View, WindowInsetsCompat, InitialPadding, InitialMargin) -> Unit,
) {
    // Create a snapshot of the view's padding & margin states
    val initialPadding = recordInitialPaddingForView(this)
    val initialMargin = recordInitialMarginForView(this)
    // Set an actual OnApplyWindowInsetsListener which proxies to the given
    // lambda, also passing in the original padding & margin states
    setOnApplyWindowInsetsListener { v, insets ->
        block(v, WindowInsetsCompat.toWindowInsetsCompat(insets), initialPadding, initialMargin)
        // Always return the insets, so that children can also use them
        insets
    }
    // request some insets
    requestApplyInsetsWhenAttached()
}

class InitialPadding(val left: Int, val top: Int, val right: Int, val bottom: Int)

class InitialMargin(val left: Int, val top: Int, val right: Int, val bottom: Int)

private fun recordInitialPaddingForView(view: View) = InitialPadding(
    view.paddingLeft, view.paddingTop, view.paddingRight, view.paddingBottom
)

private fun recordInitialMarginForView(view: View): InitialMargin {
    val lp = view.layoutParams as? ViewGroup.MarginLayoutParams
            ?: throw IllegalArgumentException("Invalid view layout params")
    return InitialMargin(lp.leftMargin, lp.topMargin, lp.rightMargin, lp.bottomMargin)
}

fun View.requestApplyInsetsWhenAttached() {
    if (isAttachedToWindow) {
        // We're already attached, just request as normal
        requestApplyInsets()
    } else {
        // We're not attached to the hierarchy, add a listener to
        // request when we are
        addOnAttachStateChangeListener(object : View.OnAttachStateChangeListener {
            override fun onViewAttachedToWindow(v: View) {
                v.removeOnAttachStateChangeListener(this)
                v.requestApplyInsets()
            }

            override fun onViewDetachedFromWindow(v: View) = Unit
        })
    }
}







© 2015 - 2024 Weber Informatics LLC | Privacy Policy