Skip to content

Commit a08c0b1

Browse files
committed
feat: make insets optional in FABs
1 parent 6504e2e commit a08c0b1

File tree

3 files changed

+30
-8
lines changed

3 files changed

+30
-8
lines changed

commons/src/main/kotlin/org/fossify/commons/extensions/View.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,6 @@ fun View.updateMarginWithBase(
133133
rightMargin = base[2] + right
134134
bottomMargin = base.last() + bottom
135135
}
136-
} catch (e: ClassCastException) {
136+
} catch (ignored: ClassCastException) {
137137
}
138138
}

commons/src/main/kotlin/org/fossify/commons/views/MyFloatingActionButton.kt

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,43 @@ import android.util.AttributeSet
66
import androidx.core.view.ViewCompat
77
import androidx.core.view.WindowInsetsCompat
88
import com.google.android.material.floatingactionbutton.FloatingActionButton
9+
import org.fossify.commons.R
910
import org.fossify.commons.extensions.applyColorFilter
1011
import org.fossify.commons.extensions.getContrastColor
12+
import org.fossify.commons.extensions.updateMarginWithBase
1113

1214
open class MyFloatingActionButton : FloatingActionButton {
15+
private var applyWindowInsets = false
16+
1317
constructor(context: Context) : super(context)
1418

15-
constructor(context: Context, attrs: AttributeSet) : super(context, attrs)
19+
constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
20+
init(context, attrs)
21+
}
1622

1723
constructor(context: Context, attrs: AttributeSet, defStyle: Int) : super(
1824
context,
1925
attrs,
2026
defStyle
21-
)
27+
) {
28+
init(context, attrs)
29+
}
30+
31+
private fun init(context: Context, attrs: AttributeSet) {
32+
context.theme.obtainStyledAttributes(attrs, R.styleable.MyFloatingActionButton, 0, 0).apply {
33+
try {
34+
applyWindowInsets = getBoolean(R.styleable.MyFloatingActionButton_applyWindowInsets, false)
35+
} finally {
36+
recycle()
37+
}
38+
}
2239

23-
init {
24-
ViewCompat.setOnApplyWindowInsetsListener(this) { _, insets ->
25-
val system = insets.getInsetsIgnoringVisibility(WindowInsetsCompat.Type.systemBars())
26-
translationY = -system.bottom.toFloat()
27-
insets
40+
if (applyWindowInsets) {
41+
ViewCompat.setOnApplyWindowInsetsListener(this) { _, insets ->
42+
val system = insets.getInsetsIgnoringVisibility(WindowInsetsCompat.Type.systemBars())
43+
updateMarginWithBase(bottom = system.bottom)
44+
insets
45+
}
2846
}
2947
}
3048

commons/src/main/res/values/attrs.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,8 @@
77
<attr name="android:visible" />
88
<attr name="showAsAction" />
99
</declare-styleable>
10+
11+
<declare-styleable name="MyFloatingActionButton">
12+
<attr name="applyWindowInsets" format="boolean" />
13+
</declare-styleable>
1014
</resources>

0 commit comments

Comments
 (0)