Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: removing the directly dispose of ModelStore and replace with Screen Lifecycle API #231

Merged
merged 2 commits into from
Oct 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cafe.adriel.voyager.core.model
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisallowComposableCalls
import androidx.compose.runtime.remember
import cafe.adriel.voyager.core.lifecycle.ScreenLifecycleStore
import cafe.adriel.voyager.core.screen.Screen
import kotlinx.coroutines.CoroutineName
import kotlinx.coroutines.CoroutineScope
Expand Down Expand Up @@ -32,10 +33,14 @@ public val ScreenModel.screenModelScope: CoroutineScope
public inline fun <reified T : ScreenModel> Screen.rememberScreenModel(
tag: String? = null,
crossinline factory: @DisallowComposableCalls () -> T
): T =
remember(ScreenModelStore.getKey<T>(this, tag)) {
ScreenModelStore.getOrPut(this, tag, factory)
): T {
val screenModelStore = remember(this) {
ScreenLifecycleStore.register(this) { ScreenModelStore }
}
Comment on lines +37 to 39
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This garantes that the ScreenLifecycleStore is being registered for this screen

return remember(screenModelStore.getKey<T>(this, tag)) {
screenModelStore.getOrPut(this, tag, factory)
}
}

public interface ScreenModel {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cafe.adriel.voyager.core.model

import androidx.compose.runtime.DisallowComposableCalls
import cafe.adriel.voyager.core.concurrent.ThreadSafeMap
import cafe.adriel.voyager.core.lifecycle.ScreenDisposable
import cafe.adriel.voyager.core.platform.multiplatformName
import cafe.adriel.voyager.core.screen.Screen
import kotlinx.coroutines.flow.MutableStateFlow
Expand All @@ -13,7 +14,7 @@ private typealias DependencyInstance = Any
private typealias DependencyOnDispose = (Any) -> Unit
private typealias Dependency = Pair<DependencyInstance, DependencyOnDispose>

public object ScreenModelStore {
public object ScreenModelStore : ScreenDisposable {

@PublishedApi
internal val screenModels: MutableMap<ScreenModelKey, ScreenModel> = ThreadSafeMap()
Expand Down Expand Up @@ -66,7 +67,7 @@ public object ScreenModelStore {
.first as T
}

public fun remove(screen: Screen) {
override fun onDispose(screen: Screen) {
screenModels.onEach(screen) { key ->
screenModels[key]?.onDispose()
screenModels -= key
Expand All @@ -78,6 +79,14 @@ public object ScreenModelStore {
}
}

@Deprecated(
message = "Use 'onDispose' instead. Will be removed in 1.0.0.",
replaceWith = ReplaceWith("onDispose")
)
public fun remove(screen: Screen) {
onDispose(screen)
}

private fun Map<String, *>.onEach(screen: Screen, block: (String) -> Unit) =
asSequence()
.filter { it.key.startsWith(screen.key) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,6 @@ public class Navigator @InternalVoyagerApi constructor(
public fun dispose(
screen: Screen
) {
ScreenModelStore.remove(screen)
ScreenLifecycleStore.remove(screen)
stateKeys
.asSequence()
Expand Down