Skip to content

Commit

Permalink
Added function to retrieve instance from backstack
Browse files Browse the repository at this point in the history
  • Loading branch information
forhad013 committed Oct 26, 2023
1 parent 3803bd4 commit 416959a
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
1 change: 1 addition & 0 deletions decompose-router/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ kotlin {
implementation(libs.essenty.parcelable)
implementation(libs.decompose)
implementation(libs.decompose.compose.multiplatform)
implementation(libs.kotlin.reflect)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import androidx.compose.runtime.State
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.staticCompositionLocalOf
import com.arkivanov.decompose.Child
import com.arkivanov.decompose.router.stack.ChildStack
import com.arkivanov.decompose.router.stack.StackNavigation
import com.arkivanov.decompose.router.stack.childStack
Expand Down Expand Up @@ -119,3 +120,42 @@ fun <T : Instance> rememberOnRoute(

return instance
}

/***
* Retrieve a instance of [V] that is scoped to the current route
*
* @param type of [V] instance
* @param childClass class of stack child[T]
* @param keyBlock lambda to configure the key [which was used to save the instance [T]] by default [type].key
*/

private inline fun <C : Parcelable, reified T : Any, reified V : Instance> Router<C>.getInstanceFromBackStack(
type: KClass<V>,
childClass: KClass<T>,
keyBlock: ((childConfiguration: T) -> Any?) = { null }
): V {
val child: Child.Created<C, RouterContext>? = this.stack.value.backStack
.firstOrNull { it.configuration is T }

require(child != null) {
"Couldn't find the ${T::class} in the backstack"
}

val configuration: C = child.configuration
require(configuration is T) {
"Couldn't find the ${T::class} in the backstack"
}

val childConfiguration: T = configuration
val key: Any? = keyBlock(childConfiguration)
val keyInstance: String = if (key == null) "${type.key}.instance"
else "${key}.instance"

val viewModelInstance: Instance? = child.instance.instanceKeeper.get(keyInstance)

require(viewModelInstance is V) {
"Couldn't find the viewModel for ${T::class} in the backstack"
}

return viewModelInstance
}
1 change: 1 addition & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ decompose-compose-multiplatform = { module = "com.arkivanov.decompose:extensions
essenty-parcelable = { module = "com.arkivanov.essenty:parcelable", version.ref = "essenty" }
horologist-compose-layouts = { module = "com.google.android.horologist:horologist-compose-layout", version.ref = "horologist" }
kotlin-gradle-plugin = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin", version.ref = "kotlin" }
kotlin-reflect = { module = "org.jetbrains.kotlin:kotlin-reflect", version.ref = "kotlin" }
kotlinx-coroutines-swing = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-swing", version.ref = "kotlinx-coroutines" }
wear-compose-foundation = { module = "androidx.wear.compose:compose-foundation", version.ref = "wear-compose" }
wear-compose-material = { module = "androidx.wear.compose:compose-material", version.ref = "wear-compose" }
Expand Down

0 comments on commit 416959a

Please sign in to comment.