Skip to content
Permalink
Browse files

Move to using Insetter πŸŽ‰

  • Loading branch information...
chrisbanes committed Oct 11, 2019
1 parent 0e239ec commit 2e3fd07d415d3b0e4b3d82fb42be3a8e4413c8e0
@@ -208,6 +208,8 @@ dependencies {

implementation Libs.Google.material

implementation Libs.Insetter.ktx

implementation Libs.threeTenAbp

implementation Libs.timber
@@ -25,9 +25,9 @@ import androidx.core.view.updatePadding
import androidx.navigation.navArgs
import app.tivi.R
import app.tivi.TiviActivity
import app.tivi.extensions.doOnApplyWindowInsets
import app.tivi.showdetails.details.ShowDetailsFragment
import app.tivi.util.observeEvent
import dev.chrisbanes.insetter.doOnApplyWindowInsets

class ShowDetailsActivity : TiviActivity() {

@@ -46,6 +46,12 @@ object Libs {

const val inboxRecyclerView = "me.saket:inboxrecyclerview:2.0.0-beta3"

object Insetter {
private const val version = "0.0.1"
const val dbx = "dev.chrisbanes:insetter-dbx:$version"
const val ktx = "dev.chrisbanes:insetter-ktx:$version"
}

object Google {
const val material = "com.google.android.material:material:1.1.0-beta01"
const val firebaseCore = "com.google.firebase:firebase-core:17.2.0"
@@ -49,6 +49,8 @@ dependencies {
implementation project(":data")
implementation project(":common-ui")

api Libs.Insetter.dbx

implementation Libs.AndroidX.swiperefresh
implementation Libs.AndroidX.coreKtx
}
@@ -21,16 +21,13 @@ import android.graphics.Outline
import android.graphics.Typeface
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.view.ViewOutlineProvider
import android.widget.ImageView
import android.widget.TextView
import androidx.core.content.res.ResourcesCompat
import androidx.core.view.isGone
import androidx.core.view.isVisible
import androidx.core.view.updateLayoutParams
import androidx.databinding.BindingAdapter
import app.tivi.extensions.doOnApplyWindowInsets
import app.tivi.extensions.requestApplyInsetsWhenAttached
import app.tivi.extensions.resolveThemeReferenceResId
import app.tivi.ui.MaxLinesToggleClickListener
@@ -138,122 +135,3 @@ fun noopApplyWindowInsets(view: View, enabled: Boolean) {
view.setOnApplyWindowInsetsListener(null)
}
}

@BindingAdapter(
"paddingLeftSystemWindowInsets",
"paddingTopSystemWindowInsets",
"paddingRightSystemWindowInsets",
"paddingBottomSystemWindowInsets",
"paddingLeftGestureInsets",
"paddingTopGestureInsets",
"paddingRightGestureInsets",
"paddingBottomGestureInsets",
"marginLeftSystemWindowInsets",
"marginTopSystemWindowInsets",
"marginRightSystemWindowInsets",
"marginBottomSystemWindowInsets",
"marginLeftGestureInsets",
"marginTopGestureInsets",
"marginRightGestureInsets",
"marginBottomGestureInsets",
requireAll = false
)
fun applySystemWindows(
view: View,
padSystemWindowLeft: Boolean,
padSystemWindowTop: Boolean,
padSystemWindowRight: Boolean,
padSystemWindowBottom: Boolean,
padGestureLeft: Boolean,
padGestureTop: Boolean,
padGestureRight: Boolean,
padGestureBottom: Boolean,
marginSystemWindowLeft: Boolean,
marginSystemWindowTop: Boolean,
marginSystemWindowRight: Boolean,
marginSystemWindowBottom: Boolean,
marginGestureLeft: Boolean,
marginGestureTop: Boolean,
marginGestureRight: Boolean,
marginGestureBottom: Boolean
) {
require(((padSystemWindowLeft && padGestureLeft) ||
(padSystemWindowTop && padGestureTop) ||
(padSystemWindowRight && padGestureRight) ||
(padSystemWindowBottom && padGestureBottom) ||
(marginSystemWindowLeft && marginGestureLeft) ||
(marginSystemWindowTop && marginGestureTop) ||
(marginSystemWindowRight && marginGestureRight) ||
(marginSystemWindowBottom && marginGestureBottom)).not()) {
"Invalid parameters. Can not request system window and gesture inset handling" +
" for the same dimension"
}

view.doOnApplyWindowInsets { v, insets, initialPadding, initialMargin ->
// Padding handling
val paddingLeft = when {
padGestureLeft -> insets.systemGestureInsets.left
padSystemWindowLeft -> insets.systemWindowInsetLeft
else -> 0
}
val paddingTop = when {
padGestureTop -> insets.systemGestureInsets.top
padSystemWindowTop -> insets.systemWindowInsetTop
else -> 0
}
val paddingRight = when {
padGestureRight -> insets.systemGestureInsets.right
padSystemWindowRight -> insets.systemWindowInsetRight
else -> 0
}
val paddingBottom = when {
padGestureBottom -> insets.systemGestureInsets.bottom
padSystemWindowBottom -> insets.systemWindowInsetBottom
else -> 0
}
v.setPadding(
initialPadding.left + paddingLeft,
initialPadding.top + paddingTop,
initialPadding.right + paddingRight,
initialPadding.bottom + paddingBottom
)

// Margin handling
val marginInsetRequested = marginSystemWindowLeft || marginGestureLeft ||
marginSystemWindowTop || marginGestureTop || marginSystemWindowRight ||
marginGestureRight || marginSystemWindowBottom || marginGestureBottom
if (marginInsetRequested) {
require(v.layoutParams is ViewGroup.MarginLayoutParams) {
"Margin inset handling requested but view LayoutParams do not" +
" extend MarginLayoutParams"
}

val marginLeft = when {
marginGestureLeft -> insets.systemGestureInsets.left
marginSystemWindowLeft -> insets.systemWindowInsetLeft
else -> 0
}
val marginTop = when {
marginGestureTop -> insets.systemGestureInsets.top
marginSystemWindowTop -> insets.systemWindowInsetTop
else -> 0
}
val marginRight = when {
marginGestureRight -> insets.systemGestureInsets.right
marginSystemWindowRight -> insets.systemWindowInsetRight
else -> 0
}
val marginBottom = when {
marginGestureBottom -> insets.systemGestureInsets.bottom
marginSystemWindowBottom -> insets.systemWindowInsetBottom
else -> 0
}
v.updateLayoutParams<ViewGroup.MarginLayoutParams> {
leftMargin = initialMargin.left + marginLeft
topMargin = initialMargin.top + marginTop
rightMargin = initialMargin.right + marginRight
bottomMargin = initialMargin.bottom + marginBottom
}
}
}
}
@@ -19,8 +19,6 @@ package app.tivi.extensions
import android.graphics.Rect
import android.view.View
import android.view.ViewGroup
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.transition.AutoTransition
import androidx.transition.TransitionManager

@@ -32,24 +30,6 @@ fun View.getBounds(rect: Rect) {
rect.set(left, top, right, bottom)
}

fun View.doOnApplyWindowInsets(
f: (
View,
insets: WindowInsetsCompat,
initialPadding: ViewDimensions,
initialMargin: ViewDimensions
) -> Unit
) {
// Create a snapshot of the view's padding state
val initialPadding = createStateForViewPadding(this)
val initialMargin = createStateForViewMargin(this)
ViewCompat.setOnApplyWindowInsetsListener(this) { v, insets ->
f(v, insets, initialPadding, initialMargin)
insets
}
requestApplyInsetsWhenAttached()
}

/**
* Call [View.requestApplyInsets] in a safe away. If we're attached it calls it straight-away.
* If not it sets an [View.OnAttachStateChangeListener] and waits to be attached before calling
@@ -99,24 +79,3 @@ inline fun View.doOnLayouts(crossinline action: (view: View) -> Boolean) {
}
})
}

private fun createStateForViewPadding(view: View) = ViewDimensions(
view.paddingLeft, view.paddingTop, view.paddingRight, view.paddingBottom, view.paddingStart,
view.paddingEnd
)

private fun createStateForViewMargin(view: View): ViewDimensions {
return (view.layoutParams as? ViewGroup.MarginLayoutParams)?.let {
ViewDimensions(it.leftMargin, it.topMargin, it.rightMargin, it.bottomMargin,
it.marginStart, it.marginEnd)
} ?: ViewDimensions()
}

data class ViewDimensions(
val left: Int = 0,
val top: Int = 0,
val right: Int = 0,
val bottom: Int = 0,
val start: Int = 0,
val end: Int = 0
)
@@ -78,6 +78,8 @@ dependencies {
implementation Libs.AndroidX.Navigation.fragment
implementation Libs.AndroidX.Navigation.ui

implementation Libs.Insetter.ktx

implementation Libs.inboxRecyclerView

implementation Libs.Google.material
@@ -36,7 +36,6 @@ import app.tivi.data.entities.Episode
import app.tivi.data.entities.Season
import app.tivi.data.entities.TiviShow
import app.tivi.episodedetails.EpisodeDetailsFragment
import app.tivi.extensions.doOnApplyWindowInsets
import app.tivi.extensions.resolveThemeColor
import app.tivi.extensions.scheduleStartPostponedTransitions
import app.tivi.extensions.updateConstraintSets
@@ -46,6 +45,7 @@ import app.tivi.ui.recyclerview.TiviLinearSmoothScroller
import com.airbnb.mvrx.MvRx
import com.airbnb.mvrx.fragmentViewModel
import com.airbnb.mvrx.withState
import dev.chrisbanes.insetter.doOnApplyWindowInsets
import kotlinx.android.parcel.Parcelize
import me.saket.inboxrecyclerview.dimming.TintPainter
import me.saket.inboxrecyclerview.page.PageStateChangeCallbacks

0 comments on commit 2e3fd07

Please sign in to comment.
You can’t perform that action at this time.