Skip to content

Commit

Permalink
Extract WindowInsetsAnimations impl to class
Browse files Browse the repository at this point in the history
Also added a enable/disable flag to ProvideWindowInsets
  • Loading branch information
chrisbanes committed Dec 1, 2020
1 parent 848000e commit 294a933
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 36 deletions.
2 changes: 1 addition & 1 deletion insets/api/insets.api
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
public final class dev/chrisbanes/accompanist/insets/ComposeInsets {
public static final fun ProvideWindowInsets (ZLkotlin/jvm/functions/Function2;Landroidx/compose/runtime/Composer;II)V
public static final fun ProvideWindowInsets (ZZLkotlin/jvm/functions/Function2;Landroidx/compose/runtime/Composer;II)V
public static final fun add-L4cKpv0 (Landroidx/compose/foundation/layout/PaddingValues;FFFF)Landroidx/compose/foundation/layout/PaddingValues;
public static final fun add-L4cKpv0$default (Landroidx/compose/foundation/layout/PaddingValues;FFFFILjava/lang/Object;)Landroidx/compose/foundation/layout/PaddingValues;
public static final fun getAmbientWindowInsets ()Landroidx/compose/runtime/ProvidableAmbient;
Expand Down
101 changes: 66 additions & 35 deletions insets/src/main/java/dev/chrisbanes/accompanist/insets/Insets.kt
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ class Insets {
}

val AmbientWindowInsets = staticAmbientOf<WindowInsets> {
error("AmbientInsets value not available. Are you using ProvideWindowInsets?")
error("AmbientWindowInsets value not available. Are you using ProvideWindowInsets?")
}

/**
Expand Down Expand Up @@ -197,13 +197,20 @@ class ViewWindowInsetObserver(private val view: View) {
* @param consumeWindowInsets Whether to consume any [WindowInsetsCompat]s which are
* dispatched to the host view. Defaults to `true`.
*/
fun start(consumeWindowInsets: Boolean = true): WindowInsets {
fun start(
consumeWindowInsets: Boolean = true,
windowInsetsAnimationsEnabled: Boolean = true
): WindowInsets {
return WindowInsets().apply {
observeInto(this, consumeWindowInsets)
observeInto(this, consumeWindowInsets, windowInsetsAnimationsEnabled)
}
}

internal fun observeInto(windowInsets: WindowInsets, consumeWindowInsets: Boolean) {
internal fun observeInto(
windowInsets: WindowInsets,
consumeWindowInsets: Boolean,
windowInsetsAnimationsEnabled: Boolean,
) {
require(!isObserving) {
"start() called, but this ViewWindowInsetObserver is already observing"
}
Expand Down Expand Up @@ -239,40 +246,18 @@ class ViewWindowInsetObserver(private val view: View) {
if (consumeWindowInsets) WindowInsetsCompat.CONSUMED else wic
}

if (Build.VERSION.SDK_INT >= 30) {
view.setWindowInsetsAnimationCallback(
object : WindowInsetsAnimation.Callback(DISPATCH_MODE_STOP) {
override fun onPrepare(animation: WindowInsetsAnimation) {
if (animation.typeMask and WindowInsetsPlatform.Type.ime() != 0) {
windowInsets.ime.ongoingAnimations++
}
// TODO add rest of types
}

override fun onProgress(
insets: android.view.WindowInsets,
runningAnimations: MutableList<WindowInsetsAnimation>
): android.view.WindowInsets {
windowInsets.ime.updateFrom(insets, WindowInsetsPlatform.Type.ime())
// TODO add rest of types

return insets
}

override fun onEnd(animation: WindowInsetsAnimation) {
if (animation.typeMask and WindowInsetsPlatform.Type.ime() != 0) {
windowInsets.ime.ongoingAnimations--
}
// TODO add rest of types
}
}
)
}

// Add an OnAttachStateChangeListener to request an inset pass each time we're attached
// to the window
val attachListener = object : View.OnAttachStateChangeListener {
override fun onViewAttachedToWindow(v: View) = v.requestApplyInsets()
override fun onViewDetachedFromWindow(v: View) = Unit
}
view.addOnAttachStateChangeListener(attachListener)

if (Build.VERSION.SDK_INT >= 30 && windowInsetsAnimationsEnabled) {
InnerWindowInsetsAnimationCallback.setup(view, windowInsets)
}

if (view.isAttachedToWindow) {
// If the view is already attached, we can request an inset pass now
view.requestApplyInsets()
Expand Down Expand Up @@ -320,18 +305,25 @@ class ViewWindowInsetObserver(private val view: View) {
*
* @param consumeWindowInsets Whether to consume any [WindowInsetsCompat]s which are dispatched to
* the host view. Defaults to `true`.
* @param windowInsetsAnimationsEnabled Whether to listen for [WindowInsetsAnimation]s, such as
* IME animations. Defaults to `true`.
*/
@Composable
fun ProvideWindowInsets(
consumeWindowInsets: Boolean = true,
windowInsetsAnimationsEnabled: Boolean = true,
content: @Composable () -> Unit
) {
val view = ViewAmbient.current
val windowInsets = remember { WindowInsets() }

DisposableEffect(view) {
val observer = ViewWindowInsetObserver(view)
observer.observeInto(windowInsets, consumeWindowInsets)
observer.observeInto(
windowInsets,
consumeWindowInsets,
windowInsetsAnimationsEnabled
)

onDispose {
observer.stop()
Expand All @@ -343,6 +335,45 @@ fun ProvideWindowInsets(
}
}

@RequiresApi(30)
private class InnerWindowInsetsAnimationCallback(
private val windowInsets: WindowInsets,
) : WindowInsetsAnimation.Callback(DISPATCH_MODE_STOP) {
override fun onPrepare(animation: WindowInsetsAnimation) {
if (animation.typeMask and WindowInsetsPlatform.Type.ime() != 0) {
windowInsets.ime.ongoingAnimations++
}
// TODO add rest of types
}

override fun onProgress(
insets: WindowInsetsPlatform,
runningAnimations: MutableList<WindowInsetsAnimation>
): WindowInsetsPlatform {
windowInsets.ime.updateFrom(insets, WindowInsetsPlatform.Type.ime())
// TODO add rest of types

return insets
}

override fun onEnd(animation: WindowInsetsAnimation) {
if (animation.typeMask and WindowInsetsPlatform.Type.ime() != 0) {
windowInsets.ime.ongoingAnimations--
}
// TODO add rest of types
}

companion object {
/**
* This function may look useless, but this keeps the API 30 method call in a
* separate class, which makes class loaders on older platforms happy.
*/
fun setup(view: View, windowInsets: WindowInsets) {
view.setWindowInsetsAnimationCallback(InnerWindowInsetsAnimationCallback(windowInsets))
}
}
}

/**
* Updates our mutable state backed [Insets] from an Android system insets.
*/
Expand Down

0 comments on commit 294a933

Please sign in to comment.