Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,21 @@ 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,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0
) : AppBarLayout(context, attrs, defStyleAttr) {
private var cachedToolbar: MaterialToolbar? = null
private var applyWindowInsets = true

init {
elevation = 0f
Expand All @@ -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
}
}
Expand Down Expand Up @@ -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")
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand Down
8 changes: 7 additions & 1 deletion commons/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="applyWindowInsets" format="boolean" />

<declare-styleable name="BottomActionMenuItem">
<attr name="android:id" />
<attr name="android:title" />
Expand All @@ -9,6 +11,10 @@
</declare-styleable>

<declare-styleable name="MyFloatingActionButton">
<attr name="applyWindowInsets" format="boolean" />
<attr name="applyWindowInsets" />
</declare-styleable>

<declare-styleable name="MyAppBarLayout">
<attr name="applyWindowInsets" />
</declare-styleable>
</resources>
Loading