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 @@ -20,6 +20,7 @@ import org.fossify.commons.extensions.getContrastColor
import org.fossify.commons.extensions.getProperBackgroundColor
import org.fossify.commons.extensions.onApplyWindowInsets
import org.fossify.commons.extensions.setSystemBarsAppearance
import org.fossify.commons.extensions.updateMarginWithBase
import org.fossify.commons.extensions.updatePaddingWithBase
import org.fossify.commons.views.MyAppBarLayout

Expand Down Expand Up @@ -47,6 +48,7 @@ abstract class EdgeToEdgeActivity : AppCompatActivity() {
padTopSystem: List<View> = emptyList(),
padBottomSystem: List<View> = emptyList(),
padBottomImeAndSystem: List<View> = emptyList(),
moveBottomSystem: List<View> = emptyList()
) {
onApplyWindowInsets { insets ->
val system = insets.getInsetsIgnoringVisibility(Type.systemBars())
Expand All @@ -55,6 +57,7 @@ abstract class EdgeToEdgeActivity : AppCompatActivity() {
padTopSystem.forEach { it.updatePaddingWithBase(top = system.top) }
padBottomSystem.forEach { it.updatePaddingWithBase(bottom = system.bottom) }
padBottomImeAndSystem.forEach { it.updatePaddingWithBase(bottom = imeAndSystem.bottom) }
moveBottomSystem.forEach { it.updateMarginWithBase(bottom = system.bottom) }

if (padCutout) {
val cutout = insets.getInsets(Type.displayCutout())
Expand Down
35 changes: 34 additions & 1 deletion commons/src/main/kotlin/org/fossify/commons/extensions/View.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,14 @@ package org.fossify.commons.extensions
import android.content.Context
import android.view.HapticFeedbackConstants
import android.view.View
import android.view.ViewGroup
import android.view.ViewTreeObserver
import androidx.annotation.Px
import androidx.core.view.marginBottom
import androidx.core.view.marginLeft
import androidx.core.view.marginRight
import androidx.core.view.marginTop
import androidx.core.view.updateLayoutParams
import androidx.core.view.updatePadding
import androidx.core.view.updatePaddingRelative
import org.fossify.commons.R
Expand Down Expand Up @@ -100,6 +106,33 @@ fun View.updatePaddingWithBase(
left = base[0] + left,
top = base[1] + top,
right = base[2] + right,
bottom = base[3] + bottom
bottom = base.last() + bottom
)
}

fun View.ensureBaseMargin(): IntArray {
val key = R.id.tag_base_margin
val base = getTag(key) as? IntArray
if (base != null) return base
val arr = intArrayOf(marginLeft, marginTop, marginRight, marginBottom)
setTag(key, arr)
return arr
}

fun View.updateMarginWithBase(
@Px left: Int = 0,
@Px top: Int = 0,
@Px right: Int = 0,
@Px bottom: Int = 0,
) {
val base = ensureBaseMargin()
try {
updateLayoutParams<ViewGroup.MarginLayoutParams> {
leftMargin = base[0] + left
topMargin = base[1] + top
rightMargin = base[2] + right
bottomMargin = base.last() + bottom
}
} catch (ignored: ClassCastException) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,43 @@ import android.util.AttributeSet
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.google.android.material.floatingactionbutton.FloatingActionButton
import org.fossify.commons.R
import org.fossify.commons.extensions.applyColorFilter
import org.fossify.commons.extensions.getContrastColor
import org.fossify.commons.extensions.updateMarginWithBase

open class MyFloatingActionButton : FloatingActionButton {
private var applyWindowInsets = false

constructor(context: Context) : super(context)

constructor(context: Context, attrs: AttributeSet) : super(context, attrs)
constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
init(context, attrs)
}

constructor(context: Context, attrs: AttributeSet, defStyle: Int) : super(
context,
attrs,
defStyle
)
) {
init(context, attrs)
}

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()
}
}

init {
ViewCompat.setOnApplyWindowInsetsListener(this) { _, insets ->
val system = insets.getInsetsIgnoringVisibility(WindowInsetsCompat.Type.systemBars())
translationY = -system.bottom.toFloat()
insets
if (applyWindowInsets) {
ViewCompat.setOnApplyWindowInsetsListener(this) { _, insets ->
val system = insets.getInsetsIgnoringVisibility(WindowInsetsCompat.Type.systemBars())
updateMarginWithBase(bottom = system.bottom)
insets
}
}
}

Expand Down
4 changes: 4 additions & 0 deletions commons/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,8 @@
<attr name="android:visible" />
<attr name="showAsAction" />
</declare-styleable>

<declare-styleable name="MyFloatingActionButton">
<attr name="applyWindowInsets" format="boolean" />
</declare-styleable>
</resources>
1 change: 1 addition & 0 deletions commons/src/main/res/values/ids.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@
<item name="properties_sha1" type="id" />
<item name="properties_sha256" type="id" />
<item name="tag_base_padding" type="id" />
<item name="tag_base_margin" type="id" />
</resources>
Loading