Twelve: Insets extension goodies
Change-Id: I9428f407ed8eaf10e2d3b16ca262fe4eb207c855
diff --git a/app/src/main/java/org/lineageos/twelve/ext/Configuration.kt b/app/src/main/java/org/lineageos/twelve/ext/Configuration.kt
new file mode 100644
index 0000000..e820d97
--- /dev/null
+++ b/app/src/main/java/org/lineageos/twelve/ext/Configuration.kt
@@ -0,0 +1,14 @@
+/*
+ * SPDX-FileCopyrightText: 2024 The LineageOS Project
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+package org.lineageos.twelve.ext
+
+import android.content.res.Configuration
+
+/**
+ * Return whether the orientation is [Configuration.ORIENTATION_LANDSCAPE].
+ */
+val Configuration.isLandscape
+ get() = orientation == Configuration.ORIENTATION_LANDSCAPE
diff --git a/app/src/main/java/org/lineageos/twelve/ext/View.kt b/app/src/main/java/org/lineageos/twelve/ext/View.kt
index ed0d69f..843cd6c 100644
--- a/app/src/main/java/org/lineageos/twelve/ext/View.kt
+++ b/app/src/main/java/org/lineageos/twelve/ext/View.kt
@@ -6,10 +6,13 @@
package org.lineageos.twelve.ext
import android.view.View
+import android.view.ViewGroup
import android.view.animation.AlphaAnimation
import android.view.animation.AnimationSet
import android.view.animation.TranslateAnimation
+import androidx.core.graphics.Insets
import androidx.core.view.isVisible
+import androidx.core.view.updateLayoutParams
fun View.slide() {
if (isVisible) {
@@ -68,3 +71,69 @@
}
)
}
+
+/**
+ * Return whether [View.getLayoutDirection] is [View.LAYOUT_DIRECTION_RTL].
+ */
+val View.isRtl: Boolean
+ get() = layoutDirection == View.LAYOUT_DIRECTION_RTL
+
+/**
+ * Updates the padding of the view based on the insets.
+ * Layout direction is taken into account.
+ *
+ * @param insets The insets to apply
+ * @param start Whether the start padding should be applied
+ * @param top Whether the top padding should be applied
+ * @param end Whether the end padding should be applied
+ * @param bottom Whether the bottom padding should be applied
+ */
+fun View.updatePadding(
+ insets: Insets,
+ start: Boolean = false,
+ top: Boolean = false,
+ end: Boolean = false,
+ bottom: Boolean = false
+) {
+ val (left, right) = when (isRtl) {
+ true -> end to start
+ false -> start to end
+ }
+
+ setPadding(
+ insets.left.takeIf { left } ?: paddingLeft,
+ insets.top.takeIf { top } ?: paddingTop,
+ insets.right.takeIf { right } ?: paddingRight,
+ insets.bottom.takeIf { bottom } ?: paddingBottom
+ )
+}
+
+/**
+ * Updates the margin of the view based on the insets.
+ * Layout direction is taken into account.
+ *
+ * @param insets The insets to apply
+ * @param start Whether the start padding should be applied
+ * @param top Whether the top padding should be applied
+ * @param end Whether the end padding should be applied
+ * @param bottom Whether the bottom padding should be applied
+ */
+fun View.updateMargin(
+ insets: Insets,
+ start: Boolean = false,
+ top: Boolean = false,
+ end: Boolean = false,
+ bottom: Boolean = false
+) {
+ val (left, right) = when (isRtl) {
+ true -> end to start
+ false -> start to end
+ }
+
+ updateLayoutParams<ViewGroup.MarginLayoutParams> {
+ leftMargin = insets.left.takeIf { left } ?: leftMargin
+ topMargin = insets.top.takeIf { top } ?: topMargin
+ rightMargin = insets.right.takeIf { right } ?: rightMargin
+ bottomMargin = insets.bottom.takeIf { bottom } ?: bottomMargin
+ }
+}