Skip to content

Commit

Permalink
fix: Properly clean up state when revealable is disposed (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
svenjacobs authored Jan 3, 2023
1 parent 7fb33ae commit 16afef0
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 8 deletions.
30 changes: 22 additions & 8 deletions reveal-core/src/main/kotlin/com/svenjacobs/reveal/RevealScope.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.svenjacobs.reveal

import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.Immutable
import androidx.compose.ui.Modifier
import androidx.compose.ui.composed
import androidx.compose.ui.layout.onGloballyPositioned
import androidx.compose.ui.unit.dp

Expand All @@ -19,6 +21,9 @@ public interface RevealScope {
* Internally [Modifier.onGloballyPositioned] is used. Hence elements are only registered after
* they have been laid out.
*
* If the element that this modifier is applied to leaves the composition while the reveal
* effect is shown for the element, the effect is finished.
*
* @param key Unique key to identify the revealable content. Also see documentation of [Key].
* @param padding Additional padding around the reveal area. Positive values increase area while
* negative values decrease it. Defaults to 8 dp on all sides.
Expand All @@ -40,13 +45,22 @@ internal class RevealScopeInstance(

override fun Modifier.revealable(key: Key, padding: PaddingValues, shape: RevealShape): Modifier =
this.then(
Modifier.onGloballyPositioned { layoutCoordinates ->
revealState.putRevealable(
key = key,
shape = shape,
padding = padding,
layoutCoordinates = layoutCoordinates,
)
},
Modifier
.onGloballyPositioned { layoutCoordinates ->
revealState.putRevealable(
key = key,
shape = shape,
padding = padding,
layoutCoordinates = layoutCoordinates,
)
}
.composed {
DisposableEffect(Unit) {
onDispose {
revealState.removeRevealable(key)
}
}
this
},
)
}
17 changes: 17 additions & 0 deletions reveal-core/src/main/kotlin/com/svenjacobs/reveal/RevealState.kt
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,23 @@ public class RevealState internal constructor(
}
}

/**
* Is called from [RevealScope.revealable] when the composable is disposed.
*/
internal fun removeRevealable(key: Key) {
revealables.remove(key)

// Hide effect if the current revealable left the composition.
// currentRevealable and previousRevealable are reset via onHideAnimationFinished().
if (currentRevealableKey == key) {
visible = false
}

if (previousRevealableKey == key) {
previousRevealable = null
}
}

internal companion object {

internal fun newSaver(keySaver: Saver<Key, Any>): Saver<RevealState, *> = listSaver(
Expand Down

0 comments on commit 16afef0

Please sign in to comment.