Added extension functions to apply insets

This commit is contained in:
Prathamesh More 2021-10-03 19:28:05 +05:30
parent 9f5b9d32ad
commit 03b9e36014
25 changed files with 116 additions and 66 deletions

View file

@ -14,15 +14,13 @@
*/
package code.name.monkey.retromusic.extensions
import android.app.Activity
import android.content.Context
import android.content.res.Configuration
import android.graphics.drawable.Drawable
import android.os.PowerManager
import android.widget.Toast
import androidx.annotation.DrawableRes
import androidx.annotation.IdRes
import androidx.annotation.IntegerRes
import androidx.annotation.StringRes
import androidx.annotation.*
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.content.res.AppCompatResources
import androidx.fragment.app.Fragment
@ -101,4 +99,8 @@ fun Fragment.getDrawableCompat(@DrawableRes drawableRes: Int): Drawable {
fun Fragment.applyToolbar(toolbar: MaterialToolbar) {
(requireActivity() as AppCompatActivity).applyToolbar(toolbar)
}
fun Fragment.dip(@DimenRes id: Int): Int {
return resources.getDimensionPixelSize(id)
}

View file

@ -19,15 +19,20 @@ import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.ViewGroup.MarginLayoutParams
import android.view.ViewTreeObserver
import android.view.inputmethod.InputMethodManager
import android.widget.EditText
import androidx.annotation.LayoutRes
import androidx.core.animation.doOnEnd
import androidx.core.animation.doOnStart
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.updateLayoutParams
import code.name.monkey.appthemehelper.ThemeStore
import code.name.monkey.appthemehelper.util.TintHelper
import code.name.monkey.retromusic.util.PreferenceUtil
import com.afollestad.materialdialogs.utils.MDUtil.updatePadding
import com.google.android.material.bottomsheet.BottomSheetBehavior
import com.google.android.material.imageview.ShapeableImageView
import com.google.android.material.shape.ShapeAppearanceModel
@ -124,4 +129,52 @@ fun ShapeableImageView.setCircleShape(boolean: Boolean) {
val radius = width / 2f
shapeAppearanceModel = ShapeAppearanceModel().withCornerSize(radius)
}
}
/**
* This will draw our view above the navigation bar instead of behind it by adding margins.
*/
fun View.drawAboveNavBar() {
ViewCompat.setOnApplyWindowInsetsListener(
(this)
) { v: View, insets: WindowInsetsCompat ->
v.updateLayoutParams<MarginLayoutParams> {
bottomMargin =
insets.getInsets(WindowInsetsCompat.Type.navigationBars()).bottom
}
insets
}
}
/**
* This will draw our view above the navigation bar instead of behind it by adding padding.
*/
fun View.drawAboveNavBarWithPadding() {
ViewCompat.setOnApplyWindowInsetsListener(
(this)
) { v: View, insets: WindowInsetsCompat ->
val navBarHeight = insets.getInsets(WindowInsetsCompat.Type.navigationBars()).bottom
v.updatePadding(bottom = navBarHeight)
insets
}
requestApplyInsetsWhenAttached()
}
fun View.requestApplyInsetsWhenAttached() {
if (isAttachedToWindow) {
// We're already attached, just request as normal
requestApplyInsets()
} else {
// We're not attached to the hierarchy, add a listener to
// request when we are
addOnAttachStateChangeListener(object : View.OnAttachStateChangeListener {
override fun onViewAttachedToWindow(v: View) {
v.removeOnAttachStateChangeListener(this)
v.requestApplyInsets()
}
override fun onViewDetachedFromWindow(v: View) = Unit
})
}
}