-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replaced registry usage with RetainedStateProvider
- Loading branch information
1 parent
465f98f
commit 24d8547
Showing
5 changed files
with
98 additions
and
47 deletions.
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
36 changes: 36 additions & 0 deletions
36
.../src/commonMain/kotlin/com/slack/circuit/foundation/internal/WithRetainedStateProvider.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,36 @@ | ||
// Copyright (C) 2024 Slack Technologies, LLC | ||
// SPDX-License-Identifier: Apache-2.0 | ||
package com.slack.circuit.foundation.internal | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.DisposableEffect | ||
import com.slack.circuit.retained.CanRetainChecker | ||
import com.slack.circuit.retained.LocalCanRetainChecker | ||
import com.slack.circuit.retained.LocalRetainedStateRegistry | ||
import com.slack.circuit.retained.RetainedStateProvider | ||
import com.slack.circuit.retained.RetainedStateRegistry | ||
import com.slack.circuit.retained.rememberRetained | ||
|
||
/** Copy of [RetainedStateProvider] to return content value */ | ||
@Composable | ||
internal fun <T> withRetainedStateProvider(key: String, content: @Composable () -> T): T { | ||
val canRetainChecker = LocalCanRetainChecker.current ?: CanRetainChecker.Always | ||
val parentRegistry = LocalRetainedStateRegistry.current | ||
val registry = rememberRetained(key = key) { RetainedStateRegistry() } | ||
return withCompositionLocalProvider( | ||
LocalRetainedStateRegistry provides registry, | ||
LocalCanRetainChecker provides CanRetainChecker.Always, | ||
) { | ||
content() | ||
} | ||
.also { | ||
DisposableEffect(key, registry) { | ||
onDispose { | ||
registry.saveAll() | ||
if (canRetainChecker.canRetain(registry)) { | ||
parentRegistry.saveValue(key) | ||
} | ||
} | ||
} | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
circuit-retained/src/commonMain/kotlin/com/slack/circuit/retained/RetainedStateProvider.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 @@ | ||
// Copyright (C) 2024 Slack Technologies, LLC | ||
// SPDX-License-Identifier: Apache-2.0 | ||
package com.slack.circuit.retained | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.CompositionLocalProvider | ||
import androidx.compose.runtime.DisposableEffect | ||
import kotlin.uuid.ExperimentalUuidApi | ||
import kotlin.uuid.Uuid | ||
|
||
/** | ||
* Provides a [RetainedStateRegistry] for the child [content] based on the specified [key]. Before | ||
* the provided registry is disposed, it calls [RetainedStateRegistry.saveValue] on the parent | ||
* registry to save the current value, allowing it to be restored on the next visit with the same | ||
* key. | ||
*/ | ||
@Composable | ||
public fun <T> RetainedStateProvider(key: String? = null, content: @Composable () -> T) { | ||
@OptIn(ExperimentalUuidApi::class) | ||
val finalKey = key ?: rememberRetained { Uuid.random().toString() } | ||
val canRetainChecker = LocalCanRetainChecker.current ?: CanRetainChecker.Always | ||
val parentRegistry = LocalRetainedStateRegistry.current | ||
val registry = rememberRetained(key = finalKey) { RetainedStateRegistry() } | ||
CompositionLocalProvider( | ||
LocalRetainedStateRegistry provides registry, | ||
LocalCanRetainChecker provides CanRetainChecker.Always, | ||
) { | ||
content() | ||
} | ||
DisposableEffect(finalKey, registry) { | ||
onDispose { | ||
registry.saveAll() | ||
if (canRetainChecker.canRetain(registry)) { | ||
parentRegistry.saveValue(finalKey) | ||
} | ||
} | ||
} | ||
} |
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