diff --git a/commons/src/main/kotlin/org/fossify/commons/views/MyAppBarLayout.kt b/commons/src/main/kotlin/org/fossify/commons/views/MyAppBarLayout.kt index 40af25985..a9664b915 100644 --- a/commons/src/main/kotlin/org/fossify/commons/views/MyAppBarLayout.kt +++ b/commons/src/main/kotlin/org/fossify/commons/views/MyAppBarLayout.kt @@ -3,11 +3,13 @@ package org.fossify.commons.views import android.content.Context import android.util.AttributeSet import android.view.View +import androidx.core.content.withStyledAttributes import androidx.core.view.ViewCompat import androidx.core.view.WindowInsetsCompat.Type -import androidx.core.view.updatePadding import com.google.android.material.appbar.AppBarLayout import com.google.android.material.appbar.MaterialToolbar +import org.fossify.commons.R +import org.fossify.commons.extensions.updatePaddingWithBase open class MyAppBarLayout @JvmOverloads constructor( context: Context, @@ -15,6 +17,7 @@ open class MyAppBarLayout @JvmOverloads constructor( defStyleAttr: Int = 0 ) : AppBarLayout(context, attrs, defStyleAttr) { private var cachedToolbar: MaterialToolbar? = null + private var applyWindowInsets = true init { elevation = 0f @@ -23,9 +26,19 @@ open class MyAppBarLayout @JvmOverloads constructor( isLiftOnScroll = false isLifted = false + context.withStyledAttributes(attrs, R.styleable.MyAppBarLayout) { + applyWindowInsets = getBoolean(R.styleable.MyAppBarLayout_applyWindowInsets, true) + } + ViewCompat.setOnApplyWindowInsetsListener(this) { view, insets -> - val system = insets.getInsetsIgnoringVisibility(Type.systemBars()) - view.updatePadding(top = system.top, left = system.left, right = system.right) + if (applyWindowInsets) { + val system = insets.getInsetsIgnoringVisibility(Type.systemBars()) + view.updatePaddingWithBase( + top = system.top, + left = system.left, + right = system.right + ) + } insets } } @@ -57,6 +70,17 @@ open class MyAppBarLayout @JvmOverloads constructor( cachedToolbar = null } + fun setApplyWindowInsets(apply: Boolean) { + applyWindowInsets = apply + if (apply) { + ViewCompat.requestApplyInsets(this) + } else { + updatePaddingWithBase(top = 0, left = 0, right = 0) + } + } + + fun isApplyWindowInsetsEnabled(): Boolean = applyWindowInsets + fun requireToolbar(): MaterialToolbar = toolbar ?: error("MyAppBarLayout requires a Toolbar/MaterialToolbar child") } diff --git a/commons/src/main/kotlin/org/fossify/commons/views/MyFloatingActionButton.kt b/commons/src/main/kotlin/org/fossify/commons/views/MyFloatingActionButton.kt index 20e84fd68..f720197fc 100644 --- a/commons/src/main/kotlin/org/fossify/commons/views/MyFloatingActionButton.kt +++ b/commons/src/main/kotlin/org/fossify/commons/views/MyFloatingActionButton.kt @@ -3,6 +3,7 @@ package org.fossify.commons.views import android.content.Context import android.content.res.ColorStateList import android.util.AttributeSet +import androidx.core.content.withStyledAttributes import androidx.core.view.ViewCompat import androidx.core.view.WindowInsetsCompat import com.google.android.material.floatingactionbutton.FloatingActionButton @@ -29,12 +30,8 @@ open class MyFloatingActionButton : FloatingActionButton { } private fun init(context: Context, attrs: AttributeSet) { - context.theme.obtainStyledAttributes(attrs, R.styleable.MyFloatingActionButton, 0, 0).apply { - try { - applyWindowInsets = getBoolean(R.styleable.MyFloatingActionButton_applyWindowInsets, false) - } finally { - recycle() - } + context.withStyledAttributes(attrs, R.styleable.MyFloatingActionButton) { + applyWindowInsets = getBoolean(R.styleable.MyFloatingActionButton_applyWindowInsets, false) } if (applyWindowInsets) { diff --git a/commons/src/main/res/values/attrs.xml b/commons/src/main/res/values/attrs.xml index 187fd3a56..b70cac6b7 100644 --- a/commons/src/main/res/values/attrs.xml +++ b/commons/src/main/res/values/attrs.xml @@ -1,5 +1,7 @@ + + @@ -9,6 +11,10 @@ - + + + + +