Skip to content

Commit

Permalink
refactor: removing the directly dispose of ModelStore and replace wit…
Browse files Browse the repository at this point in the history
…h Screen Lifecycle API (#231)
  • Loading branch information
DevSrSouza authored Oct 21, 2023
1 parent 6154083 commit 2105dcf
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
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 }
}
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

0 comments on commit 2105dcf

Please sign in to comment.