-
-
Notifications
You must be signed in to change notification settings - Fork 145
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Dispose Screens only when transition ends
- Loading branch information
1 parent
486497c
commit fa93d7c
Showing
3 changed files
with
82 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
...itions/src/commonMain/kotlin/cafe/adriel/voyager/transitions/internal/rememberPrevious.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package cafe.adriel.voyager.transitions.internal | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.MutableState | ||
import androidx.compose.runtime.SideEffect | ||
import androidx.compose.runtime.remember | ||
|
||
@Composable | ||
internal fun <T> rememberRef(): MutableState<T?> { | ||
// for some reason it always recreated the value with vararg keys, | ||
// leaving out the keys as a parameter for remember for now | ||
return remember() { | ||
object: MutableState<T?> { | ||
override var value: T? = null | ||
|
||
override fun component1(): T? = value | ||
|
||
override fun component2(): (T?) -> Unit = { value = it } | ||
} | ||
} | ||
} | ||
|
||
@Composable | ||
internal fun <T> rememberPrevious( | ||
current: T, | ||
shouldUpdate: (prev: T?, curr: T) -> Boolean = { a: T?, b: T -> a != b }, | ||
): T? { | ||
val ref = rememberRef<T>() | ||
|
||
// launched after render, so the current render will have the old value anyway | ||
SideEffect { | ||
if (shouldUpdate(ref.value, current)) { | ||
ref.value = current | ||
} | ||
} | ||
|
||
return ref.value | ||
} |