Skip to content

Commit

Permalink
Merge pull request #474 from arkivanov/unique-class-names-for-keys
Browse files Browse the repository at this point in the history
Use unique class names for configuration keys
  • Loading branch information
arkivanov authored Sep 8, 2023
2 parents 314a9f6 + b23cec4 commit 1f0f04a
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 13 deletions.
3 changes: 3 additions & 0 deletions decompose/api/android/decompose.api
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ public abstract interface annotation class com/arkivanov/decompose/FaultyDecompo
public abstract interface annotation class com/arkivanov/decompose/InternalDecomposeApi : java/lang/annotation/Annotation {
}

public final class com/arkivanov/decompose/UtilsKt {
}

public final class com/arkivanov/decompose/errorhandler/ErrorHandlersKt {
public static final fun getOnDecomposeError ()Lkotlin/jvm/functions/Function1;
public static final fun setOnDecomposeError (Lkotlin/jvm/functions/Function1;)V
Expand Down
3 changes: 3 additions & 0 deletions decompose/api/jvm/decompose.api
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ public abstract interface annotation class com/arkivanov/decompose/FaultyDecompo
public abstract interface annotation class com/arkivanov/decompose/InternalDecomposeApi : java/lang/annotation/Annotation {
}

public final class com/arkivanov/decompose/UtilsKt {
}

public final class com/arkivanov/decompose/errorhandler/ErrorHandlersKt {
public static final fun getOnDecomposeError ()Lkotlin/jvm/functions/Function1;
public static final fun setOnDecomposeError (Lkotlin/jvm/functions/Function1;)V
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
package com.arkivanov.decompose

import com.arkivanov.essenty.lifecycle.Lifecycle
import kotlin.reflect.KClass

@InternalDecomposeApi
fun Any.hashString(): String =
"${this::class.uniqueName ?: this::class.simpleName}_${hashCode().toString(radix = 36)}"

internal expect val KClass<*>.uniqueName: String?

internal expect fun Any.ensureNeverFrozen()

Expand Down
6 changes: 6 additions & 0 deletions decompose/src/jsMain/kotlin/com/arkivanov/decompose/Utils.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.arkivanov.decompose

import kotlin.reflect.KClass

internal actual val KClass<*>.uniqueName: String?
get() = js.name
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.arkivanov.decompose.router.stack.webhistory

import com.arkivanov.decompose.ExperimentalDecomposeApi
import com.arkivanov.decompose.hashString
import com.arkivanov.decompose.router.findFirstDifferentIndex
import com.arkivanov.decompose.router.stack.ChildStack
import com.arkivanov.decompose.router.stack.StackNavigator
Expand Down Expand Up @@ -125,7 +126,7 @@ class DefaultWebHistoryController internal constructor(
isStateObserverEnabled = false

navigator.navigate { stack ->
val indexInStack = stack.indexOfLast { it.hashCode() == newConfigurationKey }
val indexInStack = stack.indexOfLast { it.hashString() == newConfigurationKey }
if (indexInStack >= 0) {
// History popped, pop from the Router
stack.take(indexInStack + 1)
Expand All @@ -139,13 +140,13 @@ class DefaultWebHistoryController internal constructor(
}

private fun History.pushState(configuration: C) {
val nextItem = PageItem(configurationKey = configuration.hashCode(), path = getPath(configuration))
val nextItem = PageItem(configurationKey = configuration.hashString(), path = getPath(configuration))
val newData = getCurrentData() + nextItem
pushState(data = newData, url = nextItem.path)
}

private fun History.replaceState(configuration: C) {
val newItem = PageItem(configurationKey = configuration.hashCode(), path = getPath(configuration))
val newItem = PageItem(configurationKey = configuration.hashString(), path = getPath(configuration))
val newData = getCurrentData().dropLast(1).toTypedArray() + newItem
replaceState(data = newData, url = newItem.path)
}
Expand All @@ -156,7 +157,7 @@ class DefaultWebHistoryController internal constructor(
}

private data class PageItem(
val configurationKey: Int,
val configurationKey: String,
val path: String,
)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.arkivanov.decompose

import kotlin.reflect.KClass

internal actual val KClass<*>.uniqueName: String?
get() = qualifiedName
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import com.arkivanov.decompose.extensions.android.DefaultViewContext
import com.arkivanov.decompose.extensions.android.R
import com.arkivanov.decompose.extensions.android.ViewContext
import com.arkivanov.decompose.extensions.android.forEachChild
import com.arkivanov.decompose.hashString
import com.arkivanov.decompose.lifecycle.MergedLifecycle
import com.arkivanov.decompose.router.stack.ChildStack
import com.arkivanov.decompose.value.Value
Expand Down Expand Up @@ -125,7 +126,7 @@ class StackRouterView @JvmOverloads constructor(

val newChildView = findNewChildView()

val activeChildKey = activeChild.configuration.hashCode().toString()
val activeChildKey = activeChild.configuration.hashString()

newChildView.key = activeChildKey

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import com.arkivanov.decompose.Child
import com.arkivanov.decompose.extensions.compose.jetbrains.stack.animation.StackAnimation
import com.arkivanov.decompose.extensions.compose.jetbrains.stack.animation.emptyStackAnimation
import com.arkivanov.decompose.extensions.compose.jetbrains.subscribeAsState
import com.arkivanov.decompose.hashString
import com.arkivanov.decompose.router.stack.ChildStack
import com.arkivanov.decompose.value.Value

Expand All @@ -27,7 +28,7 @@ fun <C : Any, T : Any> Children(
val anim = animation ?: emptyStackAnimation()

anim(stack = stack, modifier = modifier) { child ->
holder.SaveableStateProvider(child.configuration.key()) {
holder.SaveableStateProvider(child.configuration.hashString()) {
content(child)
}
}
Expand All @@ -51,9 +52,7 @@ fun <C : Any, T : Any> Children(
}

private fun ChildStack<*, *>.getConfigurations(): Set<String> =
items.mapTo(HashSet()) { it.configuration.key() }

private fun Any.key(): String = "${this::class.simpleName}_${hashCode().toString(radix = 36)}"
items.mapTo(HashSet()) { it.configuration.hashString() }

@Composable
private fun SaveableStateHolder.retainStates(currentKeys: Set<Any>) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@ import androidx.compose.runtime.saveable.SaveableStateHolder
import androidx.compose.runtime.saveable.rememberSaveableStateHolder
import androidx.compose.ui.Modifier
import com.arkivanov.decompose.Child
import com.arkivanov.decompose.InternalDecomposeApi
import com.arkivanov.decompose.extensions.compose.jetpack.stack.animation.StackAnimation
import com.arkivanov.decompose.extensions.compose.jetpack.stack.animation.emptyStackAnimation
import com.arkivanov.decompose.extensions.compose.jetpack.subscribeAsState
import com.arkivanov.decompose.hashString
import com.arkivanov.decompose.router.stack.ChildStack
import com.arkivanov.decompose.value.Value

@OptIn(InternalDecomposeApi::class)
@Composable
fun <C : Any, T : Any> Children(
stack: ChildStack<C, T>,
Expand All @@ -28,7 +31,7 @@ fun <C : Any, T : Any> Children(
val anim = animation ?: emptyStackAnimation()

anim(stack = stack, modifier = modifier) { child ->
holder.SaveableStateProvider(child.configuration.key()) {
holder.SaveableStateProvider(child.configuration.hashString()) {
content(child)
}
}
Expand All @@ -51,10 +54,9 @@ fun <C : Any, T : Any> Children(
)
}

@OptIn(InternalDecomposeApi::class)
private fun ChildStack<*, *>.getConfigurations(): Set<String> =
items.mapTo(HashSet()) { it.configuration.key() }

private fun Any.key(): String = "${this::class.simpleName}_${hashCode().toString(radix = 36)}"
items.mapTo(HashSet()) { it.configuration.hashString() }

@SuppressLint("ComposableNaming")
@Composable
Expand Down

0 comments on commit 1f0f04a

Please sign in to comment.