chrisbanes / insetter

Insetter is a library to help apps handle WindowInsets more easily
https://chrisbanes.github.io/insetter
Apache License 2.0
1.13k stars 42 forks source link

Request: add alternative to data-binding, in code #30

Closed AndroidDeveloperLB closed 3 years ago

AndroidDeveloperLB commented 5 years ago

For each of those : https://github.com/chrisbanes/insetter/blob/master/dbx/README.md

For example, we have paddingBottomSystemWindowInsets as attribute of data-binding. I suggest we have it in code too.

Maybe like this in this case:

view.setPaddingSystemWindowInsets(bottom=true)

Or even better: handle both padding and margins together:

view.setPaddingAndMarginsSystemWindowInsets(Type.MARGIN_BOTTOM,Type.PADDING_LEFT,TYPE.PADDING_RIGHT...)

This will add the insets to the original padding and margins that were set before.

This way, in a single line we could handle an entire view's insets related stuff...

AndroidDeveloperLB commented 5 years ago

Maybe something like that (hopefully doesn't have bugs) :


enum class InsetsType {
    MARGIN_LEFT, MARGIN_RIGHT, MARGIN_TOP, MARGIN_BOTTOM,
    PADDING_LEFT, PADDING_RIGHT, PADDING_TOP, PADDING_BOTTOM
}

fun View.updateMargins(@Px left: Int = marginLeft, @Px top: Int = marginTop, @Px right: Int = marginRight, @Px bottom: Int = marginBottom) {
    with(layoutParams as ViewGroup.MarginLayoutParams) {
        bottomMargin = bottom
        topMargin = top
        leftMargin = left
        rightMargin = right
    }
}

fun View.useInsets(vararg insetsTypes: InsetsType) {
    val enumSet = EnumSet.noneOf(InsetsType::class.java)
    enumSet.addAll(insetsTypes)
    doOnApplyWindowInsets { _, insets, initialState ->
        val originalMargins = initialState.margins
        val originalPadding = initialState.paddings
        val newPadding = Rect(originalPadding.left, originalPadding.top, originalPadding.right, originalPadding.bottom)
        val newMargins = Rect(originalMargins.left, originalMargins.top, originalMargins.right, originalMargins.bottom)
        for (insetsType in enumSet) {
            when (insetsType!!) {
                InsetsType.MARGIN_LEFT -> newMargins.left += insets.systemWindowInsetLeft
                InsetsType.MARGIN_RIGHT -> newMargins.right += insets.systemWindowInsetRight
                InsetsType.MARGIN_TOP -> newMargins.top += insets.systemWindowInsetTop
                InsetsType.MARGIN_BOTTOM -> newMargins.bottom += insets.systemWindowInsetBottom
                InsetsType.PADDING_LEFT -> newPadding.left += insets.systemWindowInsetLeft
                InsetsType.PADDING_RIGHT -> newPadding.right += insets.systemWindowInsetRight
                InsetsType.PADDING_TOP -> newPadding.top += insets.systemWindowInsetTop
                InsetsType.PADDING_BOTTOM -> newPadding.bottom += insets.systemWindowInsetBottom
            }
        }
        updatePadding(newPadding.left, newPadding.top, newPadding.right, newPadding.bottom)
        updateMargins(newMargins.left, newMargins.top, newMargins.right, newMargins.bottom)
    }
}
chrisbanes commented 5 years ago

Hi, it's an interesting idea and we're already 80% of the way there with Insetter.applyInsetsToView():

Insetter.setOnApplyInsetsListener(view) { view, insets, initialState ->
    Insetter.applyInsetsToView(
        view,
        insets,
        initialState,
        // Apply bottom padding using system window insets
        EnumSet.of(InsetDimension.BOTTOM),
        null,
        null,
        null
    )
}

We can make the ktx version of this much nicer though.

AndroidDeveloperLB commented 5 years ago

I've updated my code. I don't know what are all the "null" you have, but mine is much shorter to call. Examples:

recyclerView.useInsets(InsetsType.PADDING_BOTTOM, InsetsType.MARGIN_LEFT, InsetsType.MARGIN_RIGHT)
appBarLayout.useInsets(InsetsType.PADDING_LEFT,InsetsType.PADDING_RIGHT,InsetsType.PADDING_TOP)

Not sure about the naming of the function though. I don't like the name I've set to it ... :(

chrisbanes commented 3 years ago

I think the DSL from last year should satisfy this hopefully. Closing.