Skip to content

Commit

Permalink
Convert Dribbble ShotActivity insets to data binding.
Browse files Browse the repository at this point in the history
  • Loading branch information
nickbutcher committed Jun 14, 2019
1 parent 1d47e7f commit d21ef86
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 19 deletions.
55 changes: 48 additions & 7 deletions core/src/main/java/io/plaidapp/core/util/BindingAdapters.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ import android.graphics.drawable.Drawable
import android.view.View
import android.view.View.GONE
import android.view.View.VISIBLE
import android.view.ViewGroup
import android.view.WindowInsets
import android.widget.ImageView
import androidx.core.view.updateLayoutParams
import androidx.databinding.BindingAdapter
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions
import com.bumptech.glide.request.RequestListener
Expand Down Expand Up @@ -101,14 +103,14 @@ fun bindLayoutFullscreen(view: View, fullscreen: Boolean) {
"paddingBottomSystemWindowInsets",
requireAll = false
)
fun applySystemWindowInsets(
fun applySystemWindowInsetsPadding(
view: View,
applyLeft: Boolean,
applyTop: Boolean,
applyRight: Boolean,
applyBottom: Boolean
) {
view.doOnApplyWindowInsets { view, insets, padding ->
view.doOnApplyWindowInsets { view, insets, padding, _ ->
val left = if (applyLeft) insets.systemWindowInsetLeft else 0
val top = if (applyTop) insets.systemWindowInsetTop else 0
val right = if (applyRight) insets.systemWindowInsetRight else 0
Expand All @@ -123,13 +125,43 @@ fun applySystemWindowInsets(
}
}

fun View.doOnApplyWindowInsets(f: (View, WindowInsets, InitialPadding) -> Unit) {
// Create a snapshot of the view's padding state
@BindingAdapter(
"marginLeftSystemWindowInsets",
"marginTopSystemWindowInsets",
"marginRightSystemWindowInsets",
"marginBottomSystemWindowInsets",
requireAll = false
)
fun applySystemWindowInsetsMargin(
view: View,
applyLeft: Boolean,
applyTop: Boolean,
applyRight: Boolean,
applyBottom: Boolean
) {
view.doOnApplyWindowInsets { view, insets, _, margin ->
val left = if (applyLeft) insets.systemWindowInsetLeft else 0
val top = if (applyTop) insets.systemWindowInsetTop else 0
val right = if (applyRight) insets.systemWindowInsetRight else 0
val bottom = if (applyBottom) insets.systemWindowInsetBottom else 0

view.updateLayoutParams<ViewGroup.MarginLayoutParams> {
leftMargin = margin.left + left
topMargin = margin.top + top
rightMargin = margin.right + right
bottomMargin = margin.bottom + bottom
}
}
}

fun View.doOnApplyWindowInsets(f: (View, WindowInsets, InitialPadding, InitialMargin) -> Unit) {
// Create a snapshot of the view's padding & margin states
val initialPadding = recordInitialPaddingForView(this)
val initialMargin = recordInitialMarginForView(this)
// Set an actual OnApplyWindowInsetsListener which proxies to the given
// lambda, also passing in the original padding state
// lambda, also passing in the original padding & margin states
setOnApplyWindowInsetsListener { v, insets ->
f(v, insets, initialPadding)
f(v, insets, initialPadding, initialMargin)
// Always return the insets, so that children can also use them
insets
}
Expand All @@ -139,8 +171,17 @@ fun View.doOnApplyWindowInsets(f: (View, WindowInsets, InitialPadding) -> Unit)

class InitialPadding(val left: Int, val top: Int, val right: Int, val bottom: Int)

class InitialMargin(val left: Int, val top: Int, val right: Int, val bottom: Int)

private fun recordInitialPaddingForView(view: View) = InitialPadding(
view.paddingLeft, view.paddingTop, view.paddingRight, view.paddingBottom)
view.paddingLeft, view.paddingTop, view.paddingRight, view.paddingBottom
)

private fun recordInitialMarginForView(view: View): InitialMargin {
val lp = view.layoutParams as? ViewGroup.MarginLayoutParams
?: throw IllegalArgumentException("Invalid view layout params")
return InitialMargin(lp.leftMargin, lp.topMargin, lp.rightMargin, lp.bottomMargin)
}

fun View.requestApplyInsetsWhenAttached() {
if (isAttachedToWindow) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,17 +137,6 @@ class ShotActivity : AppCompatActivity() {
}
back.setOnClickListener { setResultAndFinish() }
}

binding.draggableFrame.apply {
systemUiVisibility = (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION)
setOnApplyWindowInsetsListener { _, insets ->
updateLayoutParams<MarginLayoutParams> { topMargin = insets.systemWindowInsetTop }
binding.bodyScroll.updatePadding(bottom = insets.systemWindowInsetBottom)
insets
}
}
}

override fun onResume() {
Expand Down
6 changes: 5 additions & 1 deletion dribbble/src/main/res/layout/activity_dribbble_shot.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
android:transitionGroup="false"
app:layoutFullscreen="@{true}"
app:marginTopSystemWindowInsets="@{true}"
app:marginRightSystemWindowInsets="@{true}"
app:dragDismissDistance="@dimen/drag_dismiss_distance"
app:dragDismissScale="0.95"
tools:context=".ui.shot.ShotActivity">
Expand Down Expand Up @@ -116,7 +119,8 @@
android:clipToPadding="false"
android:orientation="vertical"
android:scrollbars="vertical"
android:scrollbarStyle="insideOverlay">
android:scrollbarStyle="insideOverlay"
app:paddingBottomSystemWindowInsets="@{true}">

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
Expand Down

0 comments on commit d21ef86

Please sign in to comment.