Skip to content

Commit

Permalink
Track the root cause of the composition looping
Browse files Browse the repository at this point in the history
Thanks to @adamp for catching this. We were not remembering any
LiveData transformations around insets, which meant that the
stream was being created on every call.

Fixed by remembering the transformed LiveData, and then
observing that. Meaning we can go back to `ambientOf`.
  • Loading branch information
chrisbanes committed Jun 26, 2020
1 parent 86ab0b2 commit 6548266
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
11 changes: 8 additions & 3 deletions common-ui-compose/src/main/java/app/tivi/common/compose/Debug.kt
Expand Up @@ -14,25 +14,30 @@
* limitations under the License.
*/

@file:Suppress("NOTHING_TO_INLINE")

package app.tivi.common.compose

import android.util.Log
import androidx.compose.Composable
import androidx.compose.onCommit
import androidx.compose.remember

private class Ref(var value: Int)
class Ref(var value: Int)

private const val EnableDebugCompositionLogs = false
const val EnableDebugCompositionLogs = false

/**
* An effect which longs the number compositions at the invoked point of the slot table.
* Thanks to [objcode](https://github.com/objcode) for this code.
*
* This is an inline function to act as like a C-style #include to the host composable function.
* That way we track it's compositions, not this function's compositions.
*
* @param tag Log tag used for [Log.d]
*/
@Composable
fun LogCompositions(tag: String) {
inline fun LogCompositions(tag: String) {
if (EnableDebugCompositionLogs && BuildConfig.DEBUG) {
val ref = remember { Ref(0) }
onCommit { ref.value++ }
Expand Down
Expand Up @@ -19,7 +19,8 @@ package app.tivi.common.compose
import android.view.View
import androidx.compose.Composable
import androidx.compose.Providers
import androidx.compose.staticAmbientOf
import androidx.compose.ambientOf
import androidx.compose.remember
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.doOnAttach
Expand Down Expand Up @@ -47,17 +48,17 @@ data class InsetsHolder(
val vertical get() = top + bottom
}

val InsetsAmbient = staticAmbientOf { InsetsHolder() }
val InsetsAmbient = ambientOf { InsetsHolder() }

@Composable
fun ProvideInsets(
liveData: LiveData<WindowInsetsCompat?>,
children: @Composable () -> Unit
) {
val currentInsets = liveData
.map { if (it != null) InsetsHolder(it) else InsetsHolder() }
.distinctUntilChanged()
.observeAsState(InsetsHolder())
val currentInsets = remember(liveData) {
liveData.map { if (it != null) InsetsHolder(it) else InsetsHolder() }
.distinctUntilChanged()
}.observeAsState(InsetsHolder())

Providers(InsetsAmbient provides currentInsets.value) {
children()
Expand Down

0 comments on commit 6548266

Please sign in to comment.