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 Nov 14, 2020
1 parent 1ace73f commit 3c03408
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 32 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
78 changes: 47 additions & 31 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 All @@ -145,10 +145,13 @@ val AmbientWindowInsets = staticAmbientOf<WindowInsets> {
*
* @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
Expand Down Expand Up @@ -195,41 +198,15 @@ fun ProvideWindowInsets(
}
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()
}

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
}
}
)
}

onDispose {
view.removeOnAttachStateChangeListener(attachListener)
}
Expand All @@ -240,6 +217,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 3c03408

Please sign in to comment.