diff options
Diffstat (limited to 'core/src/main/kotlin/ca/allanwang/kau/utils/ViewUtils.kt')
-rw-r--r-- | core/src/main/kotlin/ca/allanwang/kau/utils/ViewUtils.kt | 124 |
1 files changed, 99 insertions, 25 deletions
diff --git a/core/src/main/kotlin/ca/allanwang/kau/utils/ViewUtils.kt b/core/src/main/kotlin/ca/allanwang/kau/utils/ViewUtils.kt index 59ae204..ead2cb7 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/utils/ViewUtils.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/utils/ViewUtils.kt @@ -5,10 +5,8 @@ package ca.allanwang.kau.utils import android.animation.ValueAnimator import android.content.Context import android.graphics.Color -import android.support.annotation.ColorInt -import android.support.annotation.ColorRes -import android.support.annotation.StringRes -import android.support.annotation.TransitionRes +import android.os.Build +import android.support.annotation.* import android.support.design.widget.FloatingActionButton import android.support.design.widget.Snackbar import android.support.design.widget.TextInputEditText @@ -93,30 +91,98 @@ fun FloatingActionButton.hideIf(hide: Boolean) = if (hide) hide() else show() /** * Set left margin to a value in px */ -@KauUtils fun View.updateLeftMargin(margin: Int) = updateMargins(margin, KAU_LEFT) +@KauUtils fun View.setMarginLeft(margin: Int) = setMargins(margin, KAU_LEFT) /** * Set top margin to a value in px */ -@KauUtils fun View.updateTopMargin(margin: Int) = updateMargins(margin, KAU_TOP) +@KauUtils fun View.setMarginTop(margin: Int) = setMargins(margin, KAU_TOP) /** * Set right margin to a value in px */ -@KauUtils fun View.updateRightMargin(margin: Int) = updateMargins(margin, KAU_RIGHT) +@KauUtils fun View.setMarginRight(margin: Int) = setMargins(margin, KAU_RIGHT) /** * Set bottom margin to a value in px */ -@KauUtils fun View.updateBottomMargin(margin: Int) = updateMargins(margin, KAU_BOTTOM) +@KauUtils fun View.setMarginBottom(margin: Int) = setMargins(margin, KAU_BOTTOM) -@KauUtils private fun View.updateMargins(margin: Int, flag: Int) { - val p = (layoutParams as? ViewGroup.MarginLayoutParams) ?: return +/** + * Set left and right margins to a value in px + */ +@KauUtils fun View.setMarginHorizontal(margin: Int) = setMargins(margin, KAU_HORIZONTAL) + +/** + * Set top and bottom margins to a value in px + */ +@KauUtils fun View.setMarginVertical(margin: Int) = setMargins(margin, KAU_VERTICAL) + +/** + * Set all margins to a value in px + */ +@KauUtils fun View.setMargin(margin: Int) = setMargins(margin, KAU_ALL) + +/** + * Base margin setter + * returns true if setting is successful, false otherwise + */ +@KauUtils private fun View.setMargins(margin: Int, flag: Int): Boolean { + val p = (layoutParams as? ViewGroup.MarginLayoutParams) ?: return false p.setMargins( - if (flag == KAU_LEFT) margin else p.leftMargin, - if (flag == KAU_TOP) margin else p.topMargin, - if (flag == KAU_RIGHT) margin else p.rightMargin, - if (flag == KAU_BOTTOM) margin else p.bottomMargin + if (flag and KAU_LEFT > 0) margin else p.leftMargin, + if (flag and KAU_TOP > 0) margin else p.topMargin, + if (flag and KAU_RIGHT > 0) margin else p.rightMargin, + if (flag and KAU_BOTTOM > 0) margin else p.bottomMargin + ) + requestLayout() + return true +} + +/** + * Set left padding to a value in px + */ +@KauUtils fun View.setPaddingLeft(padding: Int) = setPadding(padding, KAU_LEFT) + +/** + * Set top padding to a value in px + */ +@KauUtils fun View.setPaddingTop(padding: Int) = setPadding(padding, KAU_TOP) + +/** + * Set right padding to a value in px + */ +@KauUtils fun View.setPaddingRight(padding: Int) = setPadding(padding, KAU_RIGHT) + +/** + * Set bottom padding to a value in px + */ +@KauUtils fun View.setPaddingBottom(padding: Int) = setPadding(padding, KAU_BOTTOM) + +/** + * Set left and right padding to a value in px + */ +@KauUtils fun View.setPaddingHorizontal(padding: Int) = setPadding(padding, KAU_HORIZONTAL) + +/** + * Set top and bottom padding to a value in px + */ +@KauUtils fun View.setPaddingVertical(padding: Int) = setPadding(padding, KAU_VERTICAL) + +/** + * Set all padding to a value in px + */ +@KauUtils fun View.setPadding(padding: Int) = setPadding(padding, KAU_ALL) + +/** + * Base padding setter + */ +@KauUtils private fun View.setPadding(padding: Int, flag: Int) { + setPadding( + if (flag and KAU_LEFT > 0) padding else paddingLeft, + if (flag and KAU_TOP > 0) padding else paddingTop, + if (flag and KAU_RIGHT > 0) padding else paddingRight, + if (flag and KAU_BOTTOM > 0) padding else paddingBottom ) requestLayout() } @@ -131,18 +197,8 @@ fun FloatingActionButton.hideIf(hide: Boolean) = if (hide) hide() else show() (context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager).showSoftInput(this, InputMethodManager.SHOW_IMPLICIT) } -@KauUtils fun ViewGroup.transitionAuto(builder: AutoTransition.() -> Unit = {}) { - val transition = AutoTransition() - transition.builder() - TransitionManager.beginDelayedTransition(this, transition) -} - -@KauUtils fun ViewGroup.transitionDelayed(@TransitionRes id: Int, builder: Transition.() -> Unit = {}) { - val transition = TransitionInflater.from(context).inflateTransition(id) - transition.builder() - TransitionManager.beginDelayedTransition(this, transition) -} +@RequiresApi(Build.VERSION_CODES.LOLLIPOP) @KauUtils fun View.setRippleBackground(@ColorInt foregroundColor: Int, @ColorInt backgroundColor: Int) { background = createSimpleRippleDrawable(foregroundColor, backgroundColor) } @@ -192,4 +248,22 @@ inline fun FloatingActionButton.transition(crossinline action: FloatingActionBut start() } } +} + +/** + * Attaches a listener to the recyclerview to hide the fab when it is scrolling downwards + * The fab will reappear when scrolling has stopped or if the user scrolls up + */ +fun FloatingActionButton.hideOnDownwardsScroll(recycler: RecyclerView) { + recycler.addOnScrollListener(object : RecyclerView.OnScrollListener() { + + override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) { + if (newState == android.support.v7.widget.RecyclerView.SCROLL_STATE_IDLE && !isShown) show(); + } + + override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) { + if (dy > 0 && isShown) hide() + else if (dy < 0 && isHidden) show() + } + }) }
\ No newline at end of file |